From b6a2ec1c5926254072e9fe9e74571ed0fba8e1ff Mon Sep 17 00:00:00 2001 From: kyz Date: Fri, 4 Aug 2023 17:26:06 +0800 Subject: [PATCH] =?utf8?q?=E4=BF=AE=E6=94=B9=E5=91=BC=E5=90=B8=E7=81=AF?= =?utf8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- app/inc/device.h | 6 ++++ app/src/device_config.c | 64 ++++++++++++++++++++++------------------ app/src/device_gesture.c | 4 ++- led/inc/led.h | 2 +- led/src/led.c | 34 ++++++++++++++------- led/test/light.c | 4 +-- 6 files changed, 70 insertions(+), 44 deletions(-) diff --git a/app/inc/device.h b/app/inc/device.h index 4e215c3..7aaae4b 100644 --- a/app/inc/device.h +++ b/app/inc/device.h @@ -179,8 +179,14 @@ typedef enum{ }light_mode_t; +typedef struct{ + uint32_t increase_ms; + uint32_t decrease_ms; + uint32_t apnea_ms; +} breath_cfg_t; typedef struct{ uint8_t br[101]; //0--100亮度值 + breath_cfg_t breath; }light_cfg_t; void light_mode_set( mg_dev_t *dev, light_mode_t mode ); diff --git a/app/src/device_config.c b/app/src/device_config.c index cd18107..abc8bdd 100644 --- a/app/src/device_config.c +++ b/app/src/device_config.c @@ -844,7 +844,7 @@ char *load_ble_adv_local_name( void ) } -light_cfg_t light_cfg_default = { +const light_cfg_t light_cfg_default = { .br = { //亮度0 到100, 对应到sysfs接口的0-255 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, @@ -858,6 +858,11 @@ light_cfg_t light_cfg_default = { 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 255, }, + .breath={ + .increase_ms = 1500, + .decrease_ms = 1500, + .apnea_ms = 0, + }, }; //light cfg @@ -867,28 +872,19 @@ int32_t load_light_cfg( light_cfg_t * cfg ) size_t n; cJSON *root = NULL; cJSON *br = NULL; + cJSON *breath = NULL; cJSON *item= NULL; light_cfg_t read_cfg; - int32_t ret = -1; + int32_t ret = 0; int i; - root =load_json_data_from_file( LIGHT_CFG_PATH ); - if( !root ){ - printf( "load light cfg json:%p\n", root ); - }else{ - - br = cJSON_GetObjectItem(root, "brightness"); + memcpy( &read_cfg, &light_cfg_default, sizeof( read_cfg ) ); - if( !br ){ - printf( "load light cfg item br :%p\n", br ); - }else{ - printf( "is_Array:%d\n", cJSON_IsArray(br) ); - - if( !cJSON_IsArray(br) || cJSON_GetArraySize( br )!= 101 ){ - MG_LOGD( "get array:fail\r\n" ); - }else{ - ret = 0; + root =load_json_data_from_file( LIGHT_CFG_PATH ); + if( root ){ + if((br = cJSON_GetObjectItem(root, "brightness"))){ + if( cJSON_IsArray(br) || cJSON_GetArraySize( br ) == 101 ){ for( i=0; i<101; i++ ){ item = cJSON_GetArrayItem( br, i ); if( cJSON_IsNumber( item ) && item->valueint <= 255 ){ @@ -901,21 +897,31 @@ int32_t load_light_cfg( light_cfg_t * cfg ) } } - cJSON_Delete( root ); } - } - if( ret == 0 ){ - //printf( "-----------------------------------------------\n" ); - //printf( "load light cfg:\n" ); - //for( i=0; i<101; i++ ){ - // printf( "%4d%s", read_cfg.br[i], (i%10)?" ":"\n" ); - //} - //printf( "-----------------------------------------------\n" ); - memcpy( cfg, &read_cfg, sizeof( *cfg ) ); - }else{ - memcpy( cfg, &light_cfg_default, sizeof( *cfg ) ); + if((breath = cJSON_GetObjectItem(root, "breath") ) ){ + + if((item = cJSON_GetObjectItem(breath, "increase")) && cJSON_IsNumber( item ) ) + read_cfg.breath.increase_ms = item->valueint; + + if((item = cJSON_GetObjectItem(breath, "decrease")) && cJSON_IsNumber( item ) ) + read_cfg.breath.decrease_ms = item->valueint; + + if((item = cJSON_GetObjectItem(breath, "apnea")) && cJSON_IsNumber( item ) ) + read_cfg.breath.apnea_ms = item->valueint; + + } + cJSON_Delete( root ); } + memcpy( cfg, &read_cfg, sizeof( *cfg ) ); + + //printf( "-----------------------------------------------\n" ); + //printf( "load light cfg:\n" ); + //for( i=0; i<101; i++ ){ + // printf( "%4d%s", read_cfg.br[i], (i%10)?" ":"\n" ); + //} + //printf( "breath:%d, %d, %d\n", read_cfg.breath.increase_ms, read_cfg.breath.decrease_ms, read_cfg.breath.apnea_ms ); + //printf( "-----------------------------------------------\n" ); return ret; diff --git a/app/src/device_gesture.c b/app/src/device_gesture.c index 5ccf2ea..147dae5 100644 --- a/app/src/device_gesture.c +++ b/app/src/device_gesture.c @@ -218,6 +218,7 @@ static void gr_ring_disable(light_ctrl_t led_ctrl) static void gr_ring_enable(light_ctrl_t led_ctrl,uint32_t index) { + light_cfg_t cfg; if(index) { ring_led_flash(led_ctrl, 1000, 5000); @@ -227,7 +228,8 @@ static void gr_ring_enable(light_ctrl_t led_ctrl,uint32_t index) } ring_led_flash(led_ctrl, 1000, 5000); sleep(5); - ring_led_breath(led_ctrl, 2000, 0); + load_light_cfg( &cfg ); + ring_led_breath(led_ctrl, cfg.breath.increase_ms, cfg.breath.decrease_ms, cfg.breath.apnea_ms, 0); } // Report data and turn off gesture mode diff --git a/led/inc/led.h b/led/inc/led.h index 5d17ef6..8175265 100644 --- a/led/inc/led.h +++ b/led/inc/led.h @@ -52,7 +52,7 @@ int32_t atmos_led_st( light_ctrl_t ctrl, uint32_t led_id, light_st_t *st ); int32_t ring_led_on( light_ctrl_t ctrl, uint32_t br_from, uint32_t br_to, uint32_t transition_time ); int32_t ring_led_off( light_ctrl_t ctrl, uint32_t br_from, uint32_t br_to, uint32_t transition_time ); int32_t ring_led_flash( light_ctrl_t ctrl, uint32_t period_ms, uint32_t duration_ms ); -int32_t ring_led_breath( light_ctrl_t ctrl, uint32_t period_ms, uint32_t duration_ms ); +int32_t ring_led_breath( light_ctrl_t ctrl, uint32_t increase_ms, uint32_t decrease_ms, uint32_t apnea_ms, uint32_t duration_ms ); int32_t ring_led_br_update( light_ctrl_t ctrl, uint32_t br ); int32_t ring_led_st( light_ctrl_t ctrl, light_st_t *st ); diff --git a/led/src/led.c b/led/src/led.c index 2142f82..c507a33 100644 --- a/led/src/led.c +++ b/led/src/led.c @@ -61,6 +61,10 @@ typedef struct { uint32_t duration_start_ms; uint32_t duration_stop_ms; + uint32_t increase_ms; + uint32_t decrease_ms; + uint32_t apnea_ms; + //---------------------------------- pthread_t led_thread; //LED控制线程 pthread_mutex_t lock; @@ -83,7 +87,8 @@ static void led_dev_unlock( led_dev_t *dev ); static int32_t mg_led_on( light_ctrl_t ctrl, uint32_t led_id, uint32_t br_from, uint32_t br_to, uint32_t transition_time ); static int32_t mg_led_off( light_ctrl_t ctrl, uint32_t led_id, uint32_t br_from, uint32_t br_to, uint32_t transition_time ); static int32_t mg_led_flash( light_ctrl_t ctrl, uint32_t led_id, uint32_t period_ms, uint32_t duration_ms ); -static int32_t mg_led_breath( light_ctrl_t ctrl, uint32_t led_id, uint32_t period_ms, uint32_t duration_ms ); +//static int32_t mg_led_breath( light_ctrl_t ctrl, uint32_t led_id, uint32_t period_ms ); +static int32_t mg_led_breath( light_ctrl_t ctrl, uint32_t led_id, uint32_t increas_ms, uint32_t decreas_ms, uint32_t apnea_ms, uint32_t duration_ms ); static int32_t mg_led_br_update( light_ctrl_t ctrl, uint32_t led_id, uint32_t br ); static int32_t mg_led_st( light_ctrl_t ctrl, uint32_t led_id, light_st_t *st ); //-------------------------------------------------------------------------------------------------------------------------------- @@ -188,9 +193,9 @@ int32_t ring_led_off( light_ctrl_t ctrl, uint32_t br_from, uint32_t br_to, uint3 return mg_led_off( ctrl, LED_RING, br_from, br_to, transition_time ); } -int32_t ring_led_breath( light_ctrl_t ctrl, uint32_t period_ms, uint32_t duration_ms ) +int32_t ring_led_breath( light_ctrl_t ctrl, uint32_t increase_ms, uint32_t decrease_ms, uint32_t apnea_ms, uint32_t duration_ms ) { - return mg_led_breath( ctrl, LED_RING, period_ms, duration_ms ); + return mg_led_breath( ctrl, LED_RING, increase_ms, decrease_ms, apnea_ms, duration_ms ); } int32_t ring_led_flash( light_ctrl_t ctrl, uint32_t period_ms, uint32_t duration_ms ) @@ -327,7 +332,7 @@ printf( "LED_%d flash \n", i+1 ); return retval; } -int32_t mg_led_breath( light_ctrl_t ctrl, uint32_t led_id, uint32_t period_ms, uint32_t duration_ms ) +int32_t mg_led_breath( light_ctrl_t ctrl, uint32_t led_id, uint32_t increase_ms, uint32_t decrease_ms, uint32_t apnea_ms, uint32_t duration_ms ) { int32_t retval = 0; led_ctrl_t *led_ctrl; @@ -343,12 +348,15 @@ int32_t mg_led_breath( light_ctrl_t ctrl, uint32_t led_id, uint32_t period_ms, u //lock dev = &led_ctrl->led[i]; dev->flash = 2; - dev->period_ms = period_ms; dev->duration_ms = duration_ms; if( duration_ms ){ dev->duration_start_ms = now_ms; dev->duration_stop_ms = now_ms + duration_ms; } + dev->apnea_ms = apnea_ms; + dev->increase_ms = increase_ms; + dev->decrease_ms = decrease_ms; + dev->period_ms = increase_ms+decrease_ms+apnea_ms; //led_dev_unlock( dev ); } } @@ -508,15 +516,19 @@ void* led_thread_fun( void *led ) }else if( dev->flash == 2 ){ //breath; //呼吸周期计算 - uint32_t ms = now_ms%dev->period_ms; - if( ms*2 < dev->period_ms ){ + uint32_t ms = now_ms%(dev->increase_ms + dev->decrease_ms + dev->apnea_ms); + + if( ms < dev->increase_ms ){ //up - br = 100 * ms * 2/dev->period_ms; - }else{ + br = 100 * ms /dev->increase_ms; + }else if( ms < dev->increase_ms + dev->decrease_ms ){ //down - br = 100 * (dev->period_ms - ms) * 2/dev->period_ms; + br = 100 * (dev->increase_ms + dev->decrease_ms -ms )/dev->decrease_ms; + }else{ + //apnea , led off + br = 0; } - if( br <1 ) br = 1; + //if( br <1 ) br = 1; if( br >100 ) br = 100; //curr_br = br_gamma[br]; curr_br = br; diff --git a/led/test/light.c b/led/test/light.c index 1a0de63..d9e5e97 100644 --- a/led/test/light.c +++ b/led/test/light.c @@ -147,8 +147,8 @@ printf( "open ir led ...\n" ); printf( "ring led flash period 1s ...\n" ); ring_led_flash( light, 1000, 5000 ); sleep(5); -printf( "ring led breath period 1s ...\n" ); - ring_led_breath( light, 5000, 0 ); +printf( "ring led breath period inc 2s, dec 3s, apnea 1s...\n" ); + ring_led_breath( light, 2000, 3000, 1000, 30000 ); while( 1 ){ key_led_off( light, LED_ID_KEY_ALL, 100, 0, 5000 ); -- 2.34.1