1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <aos/errno.h>
7 #include <aos/kernel.h>
8 #include "aos/init.h"
9 #include "board.h"
10 #include <k_api.h>
11 #include "aos/cli.h"
12 #include "ulog/ulog.h"
13 
14 #include "led.h"
15 #include "key.h"
16 
17 #include "hal_oled.h"
18 
19 #include "drv_temp_humi_si_si7006.h"
20 #include "drv_temp_humi_sensylink_cht8305.h"
21 
22 #include "drv_acc_gyro_inv_mpu6050.h"
23 #include "drv_acc_gyro_qst_qmi8610.h"
24 
25 #include "drv_baro_goertek_spl06.h"
26 #include "drv_baro_qst_qmp6988.h"
27 
28 #include "drv_mag_qst_qmc5883l.h"
29 #include "drv_mag_qst_qmc6310.h"
30 
31 #include "drv_als_ps_ir_liteon_ap3216c.h"
32 
33 typedef int (*sensortest_cb)(int turn);
34 static int sensors_test_inited = 0;
35 static int humiture_test(uint8_t turn);
36 static int gyro_test(uint8_t turn);
37 static int mag_test(uint8_t turn);
38 static int baro_test(uint8_t turn);
39 static int ap3216c_test(uint8_t turn);
40 
41 static int g_haasboard_is_k1c = 0;
42 
43 sensortest_cb sensor_test_funs[] = {
44     humiture_test,
45     gyro_test,
46     mag_test,
47     baro_test,
48     ap3216c_test,
49 };
50 extern int usb_class_detect;
51 
humiture_test(uint8_t turn)52 static int humiture_test(uint8_t turn)
53 {
54     printf("\r\n\r\n");
55     printf("***************************************************************\r\n");
56     printf("******************* Sensor HUMITURE Test **********************\r\n");
57     printf("** How to test: ***********************************************\r\n");
58     printf("***************************************************************\r\n");
59     printf("***************************************************************\r\n");
60     printf("\r\n === test start====\r\n");
61     uint8_t id_buf[8] = {0};
62     float Temp = 0.0, Humidity = 0.0;
63     LOGI("SENSOR_HUMI_TEST", "test start, turn %d\n", turn);
64     if (g_haasboard_is_k1c) {
65         cht8305_init();
66     } else {
67         si7006_init();
68     }
69     aos_msleep(200);
70 
71     while (turn--) {
72         if (g_haasboard_is_k1c) {
73             cht8305_getTempHumidity(&Humidity, &Temp);
74             LOGI("humiture_test", "Temp :%f Humidity : %f\n", Temp, Humidity);
75             if ((Humidity > 300.0 || Humidity < -100.0) || (Temp > 200.0 || Temp < -50.0)) {
76                 cht8305_deinit();
77                 goto _failed;
78             }
79             printf("===Result : SENSOR HUMI TEST PASS !!! ===\r\n");
80             cht8305_deinit();
81             return 0;
82         } else {
83             // get data
84             si7006_getTempHumidity(&Humidity, &Temp);
85             LOGI("humiture_test", "Temp :%f Humidity : %f\n", Temp, Humidity);
86             if ((Humidity > 300.0 || Humidity < -100.0) || (Temp > 200.0 || Temp < -50.0)) {
87                 si7006_deinit();
88                 goto _failed;
89             }
90             printf("===Result : SENSOR HUMI TEST PASS !!! ===\r\n");
91             si7006_deinit();
92             return 0;
93         }
94     }
95 
96 _failed:
97     printf("===Result: SENSOR HUMI TEST FAIL !!! ===\r\n");
98     return -1;
99 }
100 
gyro_test(uint8_t turn)101 static int gyro_test(uint8_t turn)
102 {
103     printf("\r\n\r\n");
104     printf("***************************************************************\r\n");
105     printf("************************ GYRO Test ****************************\r\n");
106     printf("** How to test: ***********************************************\r\n");
107     printf("***************************************************************\r\n");
108     printf("***************************************************************\r\n");
109     printf("\r\n ===GYRO test start====\r\n");
110     short gx = 0, gy = 0, gz = 0;
111     short ax = 0, ay = 0, az = 0;
112     float acc[3];
113     LOGI("GYRO_TEST", "test start, turn %d\n", turn);
114     if (g_haasboard_is_k1c) {
115         FisImu_init();
116         LOGI("GYRO_TEST", "FisImu_init done\n");
117     } else {
118         MPU_Init();
119         LOGI("GYRO_TEST", "MPU_Init done\n");
120     }
121     aos_msleep(200);
122 
123     while (turn--) {
124         if (g_haasboard_is_k1c) {
125             FisImu_read_acc_xyz(acc);
126 
127             if (acc[0] > 4000.0 || acc[0] < -4000.0) {
128                 printf("===Result: GYRO test FAIL !!! ===\r\n");
129                 FisImu_deinit();
130                 return -1;
131             }
132             FisImu_deinit();
133         } else {
134             float temp = MPU_Get_Temperature();
135             MPU_Deinit();
136             LOGI("GYRO_TEST", "temp %0.1f\n", temp);
137             if (temp / 100 > 200.0 || temp / 100 < -50.0) {
138                 printf("===Result: GYRO test FAIL !!! ===\r\n");
139                 return -1;
140             }
141         }
142     }
143     printf("===Result : GYRO test PASS !!! ===\r\n");
144     return 0;
145 }
146 
baro_test(uint8_t turn)147 static int baro_test(uint8_t turn)
148 {
149     printf("\r\n\r\n");
150     printf("***************************************************************\r\n");
151     printf("*********************** BARO Test ****************************\r\n");
152     printf("** How to test: ***********************************************\r\n");
153     printf("***************************************************************\r\n");
154     printf("***************************************************************\r\n");
155     printf("\r\n ===BARAO test start====\r\n");
156     spl06_data_t spl06_data = {0};
157     qmp6988_data p_qmp6988_data = {0};
158 
159     LOGI("BARO_TEST", "test start, turn %d\n", turn);
160     if (g_haasboard_is_k1c) {
161         p_qmp6988_data.slave = QMP6988_SLAVE_ADDRESS_H;
162         qmp6988_init(&p_qmp6988_data);
163         LOGI("BARO_TEST", "qmp6988_init done\n");
164     } else {
165         spl06_init();
166         LOGI("BARO_TEST", "spl06_init done\n");
167     }
168     aos_msleep(200);
169     while (turn--) {
170         if (g_haasboard_is_k1c) {
171             qmp6988_calc_pressure(&p_qmp6988_data);
172             printf("Temperature: C %0.1f\n", p_qmp6988_data.temperature);
173             printf("Temperature: F %0.1f\n", ((p_qmp6988_data.temperature * 9 / 5) + 32));
174             printf("Measured Air Pressure: : %0.2f mb\n", p_qmp6988_data.pressure);
175             printf("altitude: %0.2f m\n", p_qmp6988_data.altitude);
176             if (p_qmp6988_data.pressure / 100.0 > 3000.0 || p_qmp6988_data.pressure / 100.0 < 500.0) {
177                 printf("===Result: BARO_TEST test FAIL !!! ===\r\n");
178                 qmp6988_deinit();
179                 return -1;
180             }
181             qmp6988_deinit();
182         } else {
183             spl06_getdata(&spl06_data);
184             printf("Temperature: C %0.1f\n", spl06_data.Ctemp);
185             printf("Temperature: F %0.1f\n", spl06_data.Ftemp);
186             printf("Measured Air Pressure: : %0.2f mb\n", spl06_data.pressure);
187             printf("altitude: %0.2f m\n", spl06_data.altitude);
188             // printf("altitude: %0.2f ft\n", spl06_data.altitude * 3.281);
189             if (spl06_data.pressure > 3000.0 || spl06_data.pressure < 500.0) {
190                 printf("===Result: BARO_TEST test FAIL !!! ===\r\n");
191                 spl06_deinit();
192                 return -1;
193             }
194             spl06_deinit();
195         }
196     }
197     printf("===Result : BARO_TEST test PASS !!! ===\r\n");
198     return 0;
199 }
200 
mag_test(uint8_t turn)201 static int mag_test(uint8_t turn)
202 {
203     printf("\r\n\r\n");
204     printf("***************************************************************\r\n");
205     printf("*********************** MAG Test **************************\r\n");
206     printf("** How to test: ***********************************************\r\n");
207     printf("***************************************************************\r\n");
208     printf("***************************************************************\r\n");
209     printf("\r\n ===MAG test start====\r\n");
210     LOGI("MAG_TEST", "test start, turn %d\n", turn);
211     int heading = 0;
212     int times = 50;
213 
214     if (g_haasboard_is_k1c) {
215         qmc6310_init();
216         LOGI("MAG_TEST", "qmc6310_init done\n");
217     } else {
218         qmc5883l_init();
219         LOGI("MAG_TEST", "qmc5883l_init done\n");
220     }
221 
222     while (turn--) {
223         if (g_haasboard_is_k1c) {
224             while(times --){
225                 heading = qmc6310_readHeading();
226             }
227             heading /= 50;
228             qmc6310_deinit();
229             LOGD("MAG_TEST", "heading %d\n", heading);
230         } else {
231             while(times --){
232                 heading += qmc5883l_readHeading();
233             }
234             heading /= 50;
235             qmc5883l_deinit();
236             LOGD("MAG_TEST", "heading %d\n", heading);
237         }
238         if (heading == 0) {
239             printf("===Result : MAG test FAILs !!! ===\r\n");
240             return -1;
241         }
242     }
243     printf("===Result: MAG test PASS !!! ===\r\n");
244     return 0;
245 }
246 
ap3216c_test(uint8_t turn)247 static int ap3216c_test(uint8_t turn)
248 {
249     printf("\r\n\r\n");
250     printf("***************************************************************\r\n");
251     printf("*********************** AP3216C Test ***************************\r\n");
252     printf("** How to test: ***********************************************\r\n");
253     printf("***************************************************************\r\n");
254     printf("***************************************************************\r\n");
255     printf("\r\n ===ap3216c test start====\r\n");
256     LOGI("AP3216C_TEST", "test start, turn %d\n", turn);
257 
258     uint16_t ALS;
259     uint16_t PS = 0, IR = 0;
260     ap3216c_init();
261 
262     while (turn--) {
263         ALS = ap3216c_read_ambient_light();
264         PS = ap3216c_read_ps_data();
265         IR = ap3216c_read_ir_data();
266         ap3216c_deinit();
267         if ((PS >> 15) & 1)
268             printf("物体接近\n");
269         else
270             printf("物体远离\n");
271         LOGI("APP", "\n光照强度是:%d\n红外强度是:%d\n", ALS, IR);
272 
273         if (ALS <= 0) {
274             printf("===Result : ap3216c test FAIL !!! ===\r\n");
275             return -1;
276         }
277     }
278     printf("===Result : ap3216c test PASS !!! ===\r\n");
279     return 0;
280 }
281 
handle_sensortest_cmd(char * pwbuf,int blen,int argc,char ** argv)282 static void handle_sensortest_cmd(char *pwbuf, int blen, int argc, char **argv)
283 {
284     if (argv != 2)
285         return;
286 
287     sensortest_cb fun = sensor_test_funs[atoi(argv[0])];
288     (fun)(atoi(argv[0]));
289 }
290 
291 static struct cli_command sensortest_cmd = {
292     .name     = "sensortest",
293     .help     = "sensortest",
294     .function = handle_sensortest_cmd
295 };
296 
sensors_test(int index)297 int sensors_test(int index)
298 {
299     if (!sensors_test_inited) {
300         g_haasboard_is_k1c = haasedu_is_k1c();
301         aos_cli_register_command(&sensortest_cmd);
302         sensors_test_inited = 1;
303     }
304 
305     if ((index < 0) || (index > 4)) {
306         printf("invalid index %d\n", index);
307         return -1;
308     }
309     return (sensor_test_funs[index])(1);
310 }
311