1 /*
2  * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdarg.h>
9 
10 #include "aos/cli.h"
11 #include "aos/kernel.h"
12 #include "sensor/sensor.h"
13 #include <vfsdev/i2c_dev.h>
14 
15 #define SENSOR_SAMPLE_TIME 1000 /* sensor sampling period is 1000 ms */
16 
sensor_local_test()17 void sensor_local_test()
18 {
19     int ret;
20     temperature_data_t temp;
21     humidity_data_t humi;
22 
23     /* Sensor Hal start */
24     ret = sensor_hal_init();
25     if (ret != 0) {
26         return;
27     }
28     // printf("\n enter %s : %d\n", __func__, __LINE__);
29     /* Open the acceleration sensor device */
30     ret = sensor_hal_open(TAG_DEV_TEMP, 0);
31     if (ret != 0) {
32         return;
33     }
34 
35     ret = sensor_hal_open(TAG_DEV_HUMI, 0);
36     if (ret != 0) {
37         return;
38     }
39 
40     // printf("\n enter %s : %d\n", __func__, __LINE__);
41     /* Set the sampling period for sensors */
42     //(void)sensor_hal_ioctl(TAG_DEV_TEMP, 0, SENSOR_IOCTL_ODR_SET, SENSOR_SAMPLE_TIME);
43 
44     while(1) {
45         // printf("\n v1.4 enter %s : %d\n", __func__, __LINE__);
46         /* Read the temperature sensor data */
47         ret = sensor_hal_read(TAG_DEV_TEMP, 0, &temp, sizeof(temp));
48         if(ret > 0){
49             printf("Temperature value : %.1f centidegree\r\n", ((float)temp.t)/10);
50         }
51 
52         ret = sensor_hal_read(TAG_DEV_HUMI, 0, &humi, sizeof(humi));
53         if(ret > 0){
54             printf("Humidity value : %.1f H\r\n", ((float)humi.h)/10);
55         } else {
56             printf("read Humidity fail , ret = %d\r\n", ret);
57         }
58 
59         // printf("\n enter %s : %d\n", __func__, __LINE__);
60         /* Task sleep */
61         aos_msleep(1000);
62     }
63 
64 }
65 
66 extern int32_t sensor_i2c_init(i2c_dev_t *i2c);
i2c_init(void)67 void i2c_init(void)
68 {
69     i2c_dev_t i2c_dev;
70     i2c_dev.port                 = 1;
71     i2c_dev.config.address_width = I2C_HAL_ADDRESS_WIDTH_7BIT;
72     i2c_dev.config.freq          = I2C_BUS_BIT_RATES_100K;
73     i2c_dev.config.mode          = I2C_MODE_MASTER;
74     //i2c_dev.config.dev_addr      = SI7006_I2C_ADDRESS;
75 
76     // hal_i2c_init(&i2c_dev);
77     sensor_i2c_init(&i2c_dev);
78 }
79 
80 
sensor_local_demo(int argc,char ** argv)81 static void sensor_local_demo(int argc, char **argv)
82 {
83     (void)argc;
84     (void)argv;
85     i2c_init();
86     /* Sensor local test sample */
87     sensor_local_test();
88 
89     return 0;
90 }
91 
92 #if AOS_COMP_CLI
93 /* reg args: fun, cmd, description*/
94 ALIOS_CLI_CMD_REGISTER(sensor_local_demo, sensor_local_test, sensor local base example)
95 #endif
96 
97 
98