From 2c78e510c2fc4e1414dbb7c10fab414ac7f0bbdb Mon Sep 17 00:00:00 2001 From: kyz Date: Mon, 5 Jun 2023 10:44:53 +0800 Subject: [PATCH] load als.cfg --- CMakeLists.txt | 2 +- app/inc/device.h | 10 ++++++ app/src/device_config.c | 66 ++++++++++++++++++++++++++++++++++ {485 => eia485}/CMakeLists.txt | 0 {485 => eia485}/inc/485.h | 0 {485 => eia485}/src/485.c | 0 6 files changed, 77 insertions(+), 1 deletion(-) rename {485 => eia485}/CMakeLists.txt (100%) rename {485 => eia485}/inc/485.h (100%) rename {485 => eia485}/src/485.c (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 49b7463..f0720a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,5 +10,5 @@ add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/log ) add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/tof ) add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/als ) add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/buzzer ) -#add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/485 ) +add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/eia485 ) add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/tuya-iot-core-sdk ) diff --git a/app/inc/device.h b/app/inc/device.h index d78cc65..06e5ae5 100644 --- a/app/inc/device.h +++ b/app/inc/device.h @@ -10,6 +10,7 @@ #include "led.h" #include "button.h" #include "cQueue.h" +#include "cJSON.h" #ifdef __x86_64__ //#warning "PC Version Debug" @@ -17,15 +18,22 @@ #define TUYA_AUTH_PATH "tuya_auth" #define MG_ACTIVE_PATH "mg.active" #define READ_UUID_CMD "cat cpuinfo | grep Serial" +#define ALS_CFG_PATH "als.cfg" #else #define DEV_CFG_PATH "/appconfigs/dev.cfg" +#define ALS_CFG_PATH "/appconfigs/als.cfg" #define TUYA_AUTH_PATH "/customer/tuya_auth" #define MG_ACTIVE_PATH "/customer/mg.active" #define READ_UUID_CMD "cat /proc/cpuinfo | grep Serial" #endif +//环境光传感器默认阈值 +#define ALS_THRESHOLD_HIGH_DEFAULT 1000 +#define ALS_THRESHOLD_LOW_DEFAULT 400 + + #define KEY_LED_MAX_DEF 100 //(0-100%) #define ATMOS_LED_MAX_DEF 100 //(0-100%) //#define RECOGNITION_MODE_DEF 0 //enum 0:"light_mode", 1:"dark_mode" @@ -232,5 +240,7 @@ int32_t dp_key_report( char *key_value ); void mg_dev_lock( mg_dev_t *dev ); void mg_dev_unlock( mg_dev_t *dev ); +// +int32_t load_als_threshold( uint32_t *ts_high, uint32_t *ts_low); #endif //__MG_DEVICE_H__ diff --git a/app/src/device_config.c b/app/src/device_config.c index e64db27..1f07c1c 100644 --- a/app/src/device_config.c +++ b/app/src/device_config.c @@ -547,3 +547,69 @@ int32_t cfg_mg_tof_sync_write( mg_dev_t *dev, int32_t sync ) return 0; } +int32_t load_als_threshold( uint32_t *ts_high, uint32_t *ts_low) +{ + FILE *fd; + char json_data[200]; + size_t n; + 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; + } + + high =cJSON_GetObjectItem(root, "high"); + low =cJSON_GetObjectItem(root, "low"); + + if( !cJSON_IsNumber(high) || !cJSON_IsNumber(low) ){ + MG_LOGD( "get threshold high:fail\r\n" ); + goto err_json; + } + + if( high ) + *ts_high = high->valueint; + if( ts_low ) + *ts_low = low->valueint; + + + cJSON_Delete( root ); + return 0; + +err_json: + 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 ) + *ts_low = ALS_THRESHOLD_LOW_DEFAULT; + return -1; +} + diff --git a/485/CMakeLists.txt b/eia485/CMakeLists.txt similarity index 100% rename from 485/CMakeLists.txt rename to eia485/CMakeLists.txt diff --git a/485/inc/485.h b/eia485/inc/485.h similarity index 100% rename from 485/inc/485.h rename to eia485/inc/485.h diff --git a/485/src/485.c b/eia485/src/485.c similarity index 100% rename from 485/src/485.c rename to eia485/src/485.c -- 2.34.1