修改呼吸灯接口
authorkyz <kongyuzhong@gmail.com>
Fri, 4 Aug 2023 09:26:06 +0000 (17:26 +0800)
committerkyz <kongyuzhong@gmail.com>
Sat, 5 Aug 2023 13:47:33 +0000 (21:47 +0800)
app/inc/device.h
app/src/device_config.c
app/src/device_gesture.c
led/inc/led.h
led/src/led.c
led/test/light.c

index 4e215c344fac798d834673e527fbeb247a214c7c..7aaae4bdc904f0e6bcf0bbfc11a081b69827eed2 100644 (file)
@@ -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 );
index cd1810730f0562a3fd3ce12cf27114b7fa147900..abc8bddafd5760cdcf31d4773203477b8338a1e0 100644 (file)
@@ -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;
 
index 5ccf2ea127fb97a365370a16dfbcaaab2173a113..147dae53f3a9fc9c973086ad331eac183c59bd28 100644 (file)
@@ -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
index 5d17ef66685b8b77fbf08f69c8454022db627610..8175265c5a847b3764d6e30049bd09d5b988e4f2 100644 (file)
@@ -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 );
 
index 2142f8290cb3d25501167a0365adff9123672314..c507a338add0137e35d1c1fb6b6d9bd69bddac6e 100644 (file)
@@ -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;
index 1a0de6313e99bb7a4047aca90d372e8c141a5ccb..d9e5e97669e9043ba0bf7f65ce41bb74aaa21487 100644 (file)
@@ -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 );