add tof cfg
authorkyz <kongyuzhong@gmail.com>
Mon, 10 Jul 2023 06:11:25 +0000 (14:11 +0800)
committerkyz <kongyuzhong@gmail.com>
Mon, 10 Jul 2023 06:11:25 +0000 (14:11 +0800)
app/inc/device.h
app/src/device_config.c
app/src/device_gesture.c
tof/src/tof.c

index 5cdb3405583faf368a7db413a1c13ad69cf8bfdb..03cf5bbbfb6df8da8e636fc9663be2d7eba239e7 100644 (file)
 #define MG_ACTIVE_PATH     "mg.active"
 #define READ_UUID_CMD      "cat cpuinfo | grep Serial"
 #define ALS_CFG_PATH       "als.cfg"
+#define TOF_CFG_PATH       "tof.cfg"
 
 #else
 
 #define IOT_KEY_PATH       "/customer/tuya_key"
 #define DEV_CFG_PATH       "/appconfigs/dev.cfg"
 #define ALS_CFG_PATH       "/appconfigs/als.cfg"
+#define TOF_CFG_PATH       "/appconfigs/tof.cfg"
 
 #define TUYA_AUTH_PATH     "/customer/tuya_auth"
 #define TUYA_BINDCODE_PATH "/customer/tuya_bindcode"
@@ -51,6 +53,9 @@
 #define ALS_THRESHOLD_HIGH_DEFAULT  1000
 #define ALS_THRESHOLD_LOW_DEFAULT    400
 
+//人感检测无人判断定时器周期,单位秒
+#define TOF_NOBODY_TIMER_SECS_DEFAULT 1 //默认1秒
+
 
 #define KEY_LED_MAX_DEF            100 //(0-100%)
 #define ATMOS_LED_MAX_DEF          100 //(0-100%)
@@ -283,6 +288,8 @@ mg_dev_t * mg_dev_get( void );
 //
 int32_t load_als_threshold( uint32_t *ts_high, uint32_t *ts_low);
 
+uint32_t load_tof_nobody_timer_secs(void);
+
 void btn_light_ctrl_init( mg_dev_t *dev );
 void btn_light_ctrl_start(  mg_dev_t *dev );
 
index 153b35f2f39d927e172b6d85ea42126e848ada5d..49ac5e970569adbe67f18ff022556fc24b025887 100644 (file)
@@ -565,6 +565,41 @@ int32_t cfg_mg_tof_sync_write( mg_dev_t *dev, int32_t sync )
        return 0;
 }
 
+cJSON * load_json_data_from_file( char *path )
+{
+       FILE *fd;
+       char *json_data;
+       cJSON *root = NULL;
+       size_t file_size;
+
+       fd = fopen( path, "r" );
+       if( fd ){
+               fseek(fd,0,SEEK_END);
+               file_size = ftell(fd);          
+               fseek(fd,0,SEEK_SET);
+               json_data = (char *)malloc( file_size+1 );
+               if( json_data ){
+                       if(fread( json_data, file_size, 1, fd ) ){
+                               json_data[ file_size ] = 0;
+                               root = cJSON_Parse((const char *)json_data);
+                               if( !root ){
+                                       printf( "parser json data fail!\r\n" );
+                               }
+                       }else{
+                               MG_LOGE( "read file[%s] fail!\n", path );
+                       }
+                       free( json_data );
+               }else{
+                       MG_LOGE( "out of memory!\n");
+               }
+               fclose( fd );
+       }else{
+               MG_LOGE( "open file [%s] fail\n", path );
+       }
+
+       return root;
+}
+
 int32_t load_als_threshold( uint32_t *ts_high, uint32_t *ts_low)
 {
        FILE *fd;
@@ -573,57 +608,34 @@ int32_t load_als_threshold( uint32_t *ts_high, uint32_t *ts_low)
        cJSON *root = NULL;
        cJSON *high = NULL;
        cJSON *low = NULL;
-       
-       //printf( "load als thresold...\r\n" ); 
-       //get als.cfg : { "high":1000, "low":400 }
-       //1. open file
-       fd = fopen( ALS_CFG_PATH, "r" );
-       if( !fd ){
-               MG_LOGD( "open als_cfg  file[%s] fail!\r\n", ALS_CFG_PATH );
-               goto err_out;
-       }
-       //2. read json data
-       n = fread( json_data, 1, sizeof(json_data)-1, fd );
-       if( n == 0 ){
-               MG_LOGD( "read als config file fail!\r\n" );
-               goto err_read;
-       }
-       json_data[n] = 0;
-       //( "get json data[%ld]:%s\r\n", n, json_data );
 
-       //3. parse json
-       root = cJSON_Parse((const char *)json_data);
-       if( !root ){
-               MG_LOGD( "parser json data fail!\r\n" );
-               goto err_json;
-       }
+       root =load_json_data_from_file( ALS_CFG_PATH );
 
-       high =cJSON_GetObjectItem(root, "high");
-       low  =cJSON_GetObjectItem(root, "low");
+       if( root ){
 
-       if( !cJSON_IsNumber(high) || !cJSON_IsNumber(low) ){
-               MG_LOGD( "get threshold high:fail\r\n" );
-               goto err_json;
-       }
+               high =cJSON_GetObjectItem(root, "high");
+               low  =cJSON_GetObjectItem(root, "low");
 
-       if( high )
-               *ts_high = high->valueint;
-       if( ts_low )
-               *ts_low = low->valueint;
+               if( !cJSON_IsNumber(high) || !cJSON_IsNumber(low) ){
+                       MG_LOGD( "get threshold high:fail\r\n" );
+                       goto err_out;
+               }
 
+               if( high )
+                       *ts_high = high->valueint;
+               if( ts_low )
+                       *ts_low = low->valueint;
 
-       cJSON_Delete( root );
-       return 0;
+
+               cJSON_Delete( root );
+               return 0;
+       }
        
-err_json:
+err_out:
        if( root ){
                cJSON_Delete( root );
        }
 
-err_read:
-       if( fd )
-               fclose(fd);
-err_out:
        if( ts_high )
                *ts_high = ALS_THRESHOLD_HIGH_DEFAULT;
        if( ts_low )
@@ -631,40 +643,36 @@ err_out:
        return -1;
 }
 
-
-cJSON * load_json_data_from_file( char *path )
+//有人到无人判断时间
+uint32_t load_tof_nobody_timer_secs( void )
 {
        FILE *fd;
-       char *json_data;
+       char json_data[200];
+       size_t n;
        cJSON *root = NULL;
-       size_t file_size;
+       cJSON *nobody_timer = NULL;
+       uint32_t secs;
 
-       fd = fopen( path, "r" );
-       if( fd ){
-               fseek(fd,0,SEEK_END);
-               file_size = ftell(fd);          
-               fseek(fd,0,SEEK_SET);
-               json_data = (char *)malloc( file_size+1 );
-               if( json_data ){
-                       if(fread( json_data, file_size, 1, fd ) ){
-                               json_data[ file_size ] = 0;
-                               root = cJSON_Parse((const char *)json_data);
-                               if( !root ){
-                                       printf( "parser json data fail!\r\n" );
-                               }
-                       }else{
-                               MG_LOGE( "read file[%s] fail!\n", path );
-                       }
-                       free( json_data );
+       root =load_json_data_from_file( TOF_CFG_PATH );
+
+       if( root ){
+
+               nobody_timer = cJSON_GetObjectItem(root, "nobody_timer");
+
+               if( !cJSON_IsNumber(nobody_timer ) ){
+                       MG_LOGD( "get tof nobody timer :fail\r\n" );
+                       secs = TOF_NOBODY_TIMER_SECS_DEFAULT;
                }else{
-                       MG_LOGE( "out of memory!\n");
+                       secs = nobody_timer->valueint;
                }
-               fclose( fd );
+
+               cJSON_Delete( root );
        }else{
-               MG_LOGE( "open file [%s] fail\n", path );
+               secs = TOF_NOBODY_TIMER_SECS_DEFAULT;
        }
+       
+       return secs;
 
-       return root;
 }
 
 int32_t wifi_config_store( char *ssid, char *passwd )
index 06402b21303ad3f56ce2e74aa7c289773a20aed9..ffecee177a1cabd441940926cdc42587e6d84101 100644 (file)
@@ -960,6 +960,7 @@ int32_t gesture_recognition_start(mg_dev_t *dev)
     gr_ctrl.light_ctrl = dev->light;
     gr_ctrl.dev = dev;
     load_als_threshold(&als_th_high, &als_th_low);
+    printf( "load als threshold: %d, %d\n", als_th_high, als_th_low );
 
     usleep(100 * 1000);
 
index 6bd7237f22cbaf09ea36661f854276cbd7bf324e..a879dd2f1680be117c45e7bc0036a0bd674d9271 100644 (file)
@@ -2210,6 +2210,12 @@ static void mg_tof_timer_init()
        return; 
 }
 
+uint32_t __attribute__((weak)) load_tof_nobody_timer_secs( void ) 
+{
+       //定义weak函数,避免编译tof_test报错
+       return 1;
+}
+
 /*
 线程服务函数
 参数定义:
@@ -2226,8 +2232,13 @@ void* tof_task(void* data)
        struct itimerspec it;  
        struct itimerspec fix_it; 
 
+       uint32_t nobody_timer_secs = 0;
+
+       nobody_timer_secs = load_tof_nobody_timer_secs();
+       printf( "load tof nobody timer secs:%d\n", nobody_timer_secs );
+
        //等待配置时间
-       it.it_interval.tv_sec = 1;
+       it.it_interval.tv_sec = nobody_timer_secs;
        it.it_interval.tv_nsec = 0;
        it.it_value.tv_sec = 1;
        it.it_value.tv_nsec = 0;