From: kyz Date: Mon, 29 May 2023 02:33:28 +0000 (+0800) Subject: 修改灯光接口,增加呼吸灯效果 X-Git-Url: http://kyz.pub/gitweb/?a=commitdiff_plain;h=b78ed329ae980c300e98076f3214d9b6579e465d;p=yn%2Falarmmonitor.git 修改灯光接口,增加呼吸灯效果 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index ec9c922..876914a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.8) +project(mg_app) + # Add the posix targets add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/app ) add_subdirectory( ${CMAKE_CURRENT_LIST_DIR}/button ) diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 7f18519..a91f08b 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -37,8 +37,8 @@ target_link_libraries( middleware_implementation platform_port mg_button - mg_led mg_tof + mg_led mg_log log4c rt diff --git a/app/inc/device.h b/app/inc/device.h index 8ce35d8..02d5770 100644 --- a/app/inc/device.h +++ b/app/inc/device.h @@ -3,8 +3,13 @@ #include #include + +#include "tof.h" +#include "iot.h" +#include "mg_log.h" #include "led.h" #include "button.h" +#include "cQueue.h" #ifdef __x86_64__ #warning "PC Version Debug" @@ -141,7 +146,7 @@ typedef enum{ void light_mode_set( light_mode_t mode ); -void light_brightness_set( led_id_t id, uint32_t br ); +//void light_brightness_set( led_id_t id, uint32_t br ); void device_wifi_init( void ); void device_wiwi_start( void ); diff --git a/app/src/device_button.c b/app/src/device_button.c index 2e8943c..c2d850c 100644 --- a/app/src/device_button.c +++ b/app/src/device_button.c @@ -5,11 +5,11 @@ #include #include #include "button.h" -#include "led.h" -#include "iot.h" -#include "mg_log.h" -#include "cQueue.h" +//#include "iot.h" +//#include "mg_log.h" +//#include "cQueue.h" +#include "device.h" static mg_button_t *btn_ctrl; static pthread_mutex_t btn_lock; diff --git a/app/src/device_config.c b/app/src/device_config.c index 35151d4..d895dba 100644 --- a/app/src/device_config.c +++ b/app/src/device_config.c @@ -5,12 +5,11 @@ #include #include #include +#include -#include "mg_log.h" +//#include "mg_log.h" #include "device.h" -#include "zlib.h" - static const dev_cfg_t dev_cfg_default={ //dp info diff --git a/app/src/device_ctrl.c b/app/src/device_ctrl.c index 7e90aae..78e9ce3 100644 --- a/app/src/device_ctrl.c +++ b/app/src/device_ctrl.c @@ -5,9 +5,9 @@ #include #include #include "button.h" -#include "led.h" -#include "iot.h" -#include "mg_log.h" +//#include "led.h" +//#include "iot.h" +//#include "mg_log.h" #include "cQueue.h" @@ -41,6 +41,9 @@ extern int32_t is_btn_device_clean( btn_msg_t *msg ); extern int32_t is_btn_wifi_config( btn_msg_t *msg ); extern void device_light_init( void ); +extern void light_set_key_br( uint32_t id, uint32_t br ); +extern void light_set_atmos_br( uint32_t id, uint32_t br ); + extern void device_button_init( void ); extern int32_t btn_msg_rx( btn_msg_t *msg ); extern int32_t is_btn_wifi_config( btn_msg_t *msg ); @@ -187,10 +190,8 @@ void * btn_light_ctrl_task( void* unused ) //手动模式 light_mode_set( LIGHT_MODE_ACTIVE ); - light_brightness_set( LED_ID_KEY_ALL, key_led_br ); - light_brightness_set( LED_ID_FUN_ALL, fun_led_br ); - - + light_set_key_br( LED_ID_KEY_ALL, key_led_br ); + light_set_atmos_br( LED_ID_ATMOS_ALL, fun_led_br ); }else{ static uint32_t tof_dist = 1000; diff --git a/app/src/device_light.c b/app/src/device_light.c index 4a8326c..9a3abd9 100644 --- a/app/src/device_light.c +++ b/app/src/device_light.c @@ -1,7 +1,7 @@ //led manage.c -#include "led.h" -#include "mg_log.h" -#include "button.h" +//#include "led.h" +//#include "mg_log.h" +//#include "button.h" #include "device.h" //--------------------------------------------------// /* led ctrl api */ @@ -10,7 +10,8 @@ // g_led_ctrl = mg_led_open(); // mg_led_brightness_set( g_mg_led_ctrl, LED_ID_KEY_ALL, br ); //--------------------------------------------------// -static mg_led_ctrl_t *g_led_ctrl; + +static light_ctrl_t g_led_ctrl; typedef struct{ uint32_t key_led_max; @@ -28,13 +29,13 @@ static lightinfo_t g_lightinfo; //保存灯状态 void device_light_init( void ) { - led_cfg_t cfg; MG_LOGI( "device light init" ); g_lightinfo.mode = LIGHT_MODE_INIT; g_lightinfo.key_led_max = 100; g_lightinfo.fun_led_max = 100; g_led_ctrl = mg_led_open(); + //MG_LOGI( "device light init, g_led_ctrl:%p", g_led_ctrl ); if( g_led_ctrl ){ light_mode_set( LIGHT_MODE_INIT ); @@ -58,109 +59,71 @@ char *light_mode_name[]={ void light_mode_set( light_mode_t mode ) { - led_cfg_t cfg; if( g_lightinfo.mode == mode ) return; - memset( &cfg, 0, sizeof( cfg )); - MG_LOGI( "light mode %s -> %s", light_mode_name[g_lightinfo.mode], light_mode_name[mode] ); g_lightinfo.mode = mode; switch( g_lightinfo.mode ){ case LIGHT_MODE_INIT: - cfg.mode = LED_MODE_OFF; - cfg.next_mode = LED_MODE_OFF; - mg_led_mode_set( g_led_ctrl, LED_ID_KEY_ALL|LED_ID_FUN_ALL, &cfg ); + key_led_off( g_led_ctrl, LED_ID_KEY_ALL, 0, 0, 0); + atmos_led_off( g_led_ctrl, LED_ID_ATMOS_ALL, 0, 0, 0); + ring_led_off( g_led_ctrl, 0, 0, 0); break; - case LIGHT_MODE_CLEAN: //clean mode en //进入清洁模式,所有 LED 连续慢闪; - cfg.mode = LED_MODE_ON; - cfg.next_mode = LED_MODE_ON; - cfg.max_brightness = g_lightinfo.key_led_max; - mg_led_mode_set( g_led_ctrl, LED_ID_KEY_ALL, &cfg ); - cfg.max_brightness = g_lightinfo.fun_led_max; - mg_led_mode_set( g_led_ctrl, LED_ID_FUN_ALL, &cfg ); - usleep(50*1000); - cfg.mode = LED_MODE_FLASH_SLOW_1; - cfg.next_mode = LED_MODE_FLASH_SLOW_1; - cfg.max_brightness = g_lightinfo.key_led_max; - mg_led_mode_set( g_led_ctrl, LED_ID_KEY_ALL, &cfg ); - cfg.max_brightness = g_lightinfo.fun_led_max; - mg_led_mode_set( g_led_ctrl, LED_ID_FUN_ALL, &cfg ); + key_led_flash( g_led_ctrl, LED_ID_KEY_ALL, 1000, 0); + atmos_led_flash( g_led_ctrl, LED_ID_ATMOS_ALL, 1000, 0); break; case LIGHT_MODE_AUTH_WIFICONFIGING : //wifi config + key_led_off( g_led_ctrl, LED_ID_KEY_ALL&~(LED_ID_KEY_7|LED_ID_KEY_8), 0, 0, 0); + key_led_flash( g_led_ctrl, LED_ID_KEY_7|LED_ID_KEY_8, 1000, 0); + atmos_led_off( g_led_ctrl, LED_ID_ATMOS_ALL, 0, 0, 0); //进入配网模式,所有 LED7 LED8 连续慢闪; - cfg.mode = LED_MODE_OFF; - cfg.next_mode = LED_MODE_OFF; - mg_led_mode_set( g_led_ctrl, LED_ID_KEY_ALL|LED_ID_FUN_ALL, &cfg ); - usleep(50*1000); - cfg.mode = LED_MODE_FLASH_SLOW_1; - cfg.next_mode = LED_MODE_FLASH_SLOW_1; - cfg.max_brightness = g_lightinfo.key_led_max; - mg_led_mode_set( g_led_ctrl, LED_ID_KEY_7|LED_ID_KEY_8, &cfg ); break; case LIGHT_MODE_AUTH_WIFICONFIG_NO_ACTIVE: // - cfg.mode = LED_MODE_OFF; - cfg.next_mode = LED_MODE_OFF; - mg_led_mode_set( g_led_ctrl, LED_ID_KEY_ALL|LED_ID_FUN_ALL, &cfg ); + key_led_off( g_led_ctrl, LED_ID_KEY_ALL, 0, 0, 0); + atmos_led_off( g_led_ctrl, LED_ID_ATMOS_ALL, 0, 0, 0); + ring_led_off( g_led_ctrl, 0, 0, 0); break; + case LIGHT_MODE_ACTIVE: // - cfg.mode = LED_MODE_ON; - cfg.next_mode = LED_MODE_ON; - cfg.max_brightness = g_lightinfo.key_led_max; - mg_led_mode_set( g_led_ctrl, LED_ID_KEY_ALL, &cfg ); - cfg.max_brightness = g_lightinfo.fun_led_max; - mg_led_mode_set( g_led_ctrl, LED_ID_FUN_ALL, &cfg ); + key_led_on( g_led_ctrl, LED_ID_KEY_ALL, 0, 100, 0); + atmos_led_on( g_led_ctrl, LED_ID_ATMOS_ALL, 0, 100, 0); break; case LIGHT_MODE_TOF_ACTIVE: // - cfg.mode = LED_MODE_GRADUAL_ON; //渐变到最大亮度后常亮 - cfg.max_brightness = 100; - mg_led_mode_set( g_led_ctrl, LED_ID_KEY_ALL, &cfg ); - - cfg.mode = LED_MODE_GRADUAL_OFF, //渐变到关闭 - mg_led_mode_set( g_led_ctrl, LED_ID_FUN_ALL, &cfg ); + key_led_on( g_led_ctrl, LED_ID_KEY_ALL, 0, 100, 3*1000 ); + atmos_led_off( g_led_ctrl, LED_ID_ATMOS_ALL, 100, 0, 3*1000 ); + ring_led_breath( g_led_ctrl, 2000, 10*1000 ); break; + case LIGHT_MODE_TOF_DEACTIVE: // - cfg.mode = LED_MODE_GRADUAL_OFF, //渐变到关闭 - mg_led_mode_set( g_led_ctrl, LED_ID_KEY_ALL, &cfg ); - cfg.mode = LED_MODE_GRADUAL_ON; //渐变到最大亮度后常亮 - cfg.max_brightness = 100; - mg_led_mode_set( g_led_ctrl, LED_ID_FUN_ALL, &cfg ); + key_led_off( g_led_ctrl, LED_ID_KEY_ALL, 100, 0, 10*1000 ); + //atmos_led_on( g_led_ctrl, LED_ID_ATMOS_ALL, 0, 100, 3*1000 ); + atmos_led_on( g_led_ctrl, LED_ID_ATMOS_ALL, 0, 100, 3*1000 ); + ring_led_breath( g_led_ctrl, 2000, 0 ); break; + } } -void light_brightness_set( led_id_t id, uint32_t br ) +void light_set_atmos_br( uint32_t id, uint32_t br ) { - if( (id&LED_ID_KEY_ALL )==LED_ID_KEY_ALL ){ - g_lightinfo.key_led_max = br; - mg_led_brightness_set( g_led_ctrl, LED_ID_KEY_ALL, br ); - } - if( (id&LED_ID_FUN_ALL )==LED_ID_FUN_ALL ){ - g_lightinfo.fun_led_max = br; - mg_led_brightness_set( g_led_ctrl, LED_ID_FUN_ALL, br ); - } - if( (id&LED_ID_IR )==LED_ID_IR ){ - g_lightinfo.ir_led_max = br; - mg_led_brightness_set( g_led_ctrl, LED_ID_IR, br ); - } - if( (id&LED_ID_RING )==LED_ID_RING ){ - g_lightinfo.ring_led_max = br; - mg_led_brightness_set( g_led_ctrl, LED_ID_RING, br ); - } - //MG_LOGI( "light [%04X] -> %d", id, br ); + atmos_led_br_update( g_led_ctrl, id, br ); +} +void light_set_key_br( uint32_t id, uint32_t br ) +{ + key_led_br_update( g_led_ctrl, id, br ); } void led_flash_for_btn_single_click( button_code_t btn_code ) { - led_id_t led_id = 0; - led_cfg_t cfg; + uint32_t led_id = 0; if( btn_code & BUTTON_1 ) led_id = LED_ID_KEY_1; else if( btn_code & BUTTON_2 ) led_id = LED_ID_KEY_2; @@ -172,12 +135,7 @@ void led_flash_for_btn_single_click( button_code_t btn_code ) else if( btn_code & BUTTON_8 ) led_id = LED_ID_KEY_8; if( led_id ){ - cfg.mode = LED_MODE_FLASH_FAST; - cfg.next_mode = LED_MODE_NOCHANGE; - cfg.next_time_ms = 1*1000; - cfg.breath_period_ms = 0; - cfg.max_brightness = g_lightinfo.key_led_max; - mg_led_mode_set( g_led_ctrl, led_id, &cfg ); + key_led_flash( g_led_ctrl, led_id, 125, 1*1000 ); } } diff --git a/app/src/device_wifi.c b/app/src/device_wifi.c index 0cb7e8e..ba0378a 100644 --- a/app/src/device_wifi.c +++ b/app/src/device_wifi.c @@ -4,10 +4,9 @@ #include #include #include -#include "button.h" -#include "led.h" -#include "iot.h" -#include "mg_log.h" +//#include "button.h" +//#include "iot.h" +//#include "mg_log.h" #include "cQueue.h" #include "device.h" diff --git a/app/src/main.c b/app/src/main.c index 803e337..dae6778 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -5,10 +5,10 @@ #include #include -#include "button.h" -#include "led.h" -#include "iot.h" -#include "mg_log.h" +//#include "button.h" +//#include "led.h" +//#include "iot.h" +//#include "mg_log.h" #include "device.h" @@ -17,6 +17,7 @@ void iot_start( void ); extern void btn_light_ctrl_init( void ); extern void btn_light_ctrl_start( void ); +extern int32_t dp_property_report_bool( char *dp_name, int32_t dp_value ); extern int32_t dp_report_all( mg_dev_t *dev ); mg_dev_t g_mg_dev; @@ -40,7 +41,8 @@ dev_st_t mg_device_st_set(dev_st_t st ) return old_st; } -int32_t is_iot_connected( void ); +extern int32_t is_iot_connected( void ); +extern void device_wifi_start( void ); int main( void ) { diff --git a/led/CMakeLists.txt b/led/CMakeLists.txt index 903bd56..efcfaa3 100644 --- a/led/CMakeLists.txt +++ b/led/CMakeLists.txt @@ -25,8 +25,8 @@ add_library( ${PROJECT_NAME} STATIC ${SRCS} ) link_directories( ${PROJECT_SOURCE_DIR}/. ) -aux_source_directory(./test/ TEST_SRCS) -add_executable( led_test ${TEST_SRCS} ) -target_link_libraries( led_test PUBLIC rt pthread mg_led mg_log log4c rt pthread ) +#aux_source_directory(./test/ TEST_SRCS) +#add_executable( led_test ${TEST_SRCS} ) +#target_link_libraries( led_test PUBLIC rt pthread mg_led mg_log log4c rt pthread ) diff --git a/led/inc/led.h b/led/inc/led.h index 5dbff97..60efefd 100644 --- a/led/inc/led.h +++ b/led/inc/led.h @@ -1,96 +1,72 @@ -#ifndef __LED__H__ -#define __LED__H__ +#ifndef __MG_LIGHT_H__ +#define __MG_LIGHT_H__ #include -typedef enum{ - LED_KEY_1 = 0, - LED_KEY_2, - LED_KEY_3, - LED_KEY_4, - LED_KEY_5, - LED_KEY_6, - LED_KEY_7, - LED_KEY_8, - LED_FUN_1, - LED_FUN_2, - LED_FUN_3, - LED_FUN_4, - LED_FUN_5, - LED_RING, - LED_IR, - LED_NR, -}led_index_t; - -typedef enum{ - LED_ID_KEY_1 = (1<<0), - LED_ID_KEY_2 = (1<<1), - LED_ID_KEY_3 = (1<<2), - LED_ID_KEY_4 = (1<<3), - LED_ID_KEY_5 = (1<<4), - LED_ID_KEY_6 = (1<<5), - LED_ID_KEY_7 = (1<<6), - LED_ID_KEY_8 = (1<<7), - LED_ID_FUN_1 = (1<<8), - LED_ID_FUN_2 = (1<<9), - LED_ID_FUN_3 = (1<<10), - LED_ID_FUN_4 = (1<<11), - LED_ID_FUN_5 = (1<<12), - LED_ID_RING = (1<<13), - LED_ID_IR = (1<<14), - LED_ID_KEY_ALL = 0x00ff, - LED_ID_FUN_ALL = 0x1f00, - LED_ID_ALL = 0x7fff, -}led_id_t; - -typedef enum{ - LED_MODE_OFF=0, //关闭 - LED_MODE_ON=1, //常亮 - LED_MODE_FLASH_1HZ, - LED_MODE_FLASH_2HZ, - LED_MODE_FLASH_8HZ, - LED_MODE_FLASH_XHZ, - - LED_MODE_GRADUAL_ON, //渐变到DP设定亮度后常亮 - //LED_MODE_GRADUAL_MAX_ON, //渐变到最大亮度后常亮 - LED_MODE_GRADUAL_OFF, //渐变到关闭 - - LED_MODE_FLASH_FAST = LED_MODE_FLASH_8HZ, //快闪 8HZ - LED_MODE_FLASH_SLOW_1 = LED_MODE_FLASH_1HZ, //慢闪 1HZ - LED_MODE_FLASH_SLOW_2 = LED_MODE_FLASH_2HZ, //慢闪 2HZ - LED_MODE_BREATH_XHZ, //呼吸指示模式 - LED_MODE_NOCHANGE, -}led_mode_t; - -struct mg_led_ctrl; -typedef struct mg_led_ctrl mg_led_ctrl_t; - -//int32_t mg_led_set( mg_led_ctrl_t *led_ctrl, led_id_t id, led_mode_t mode, led_mode_t next_mode, uint32_t keep_ms, uint32_32_t breath_period ); -//int32_t mg_led_get( mg_led_ctrl_t *led_ctrl, led_id_t id, led_mode_t * mode, led_mode_t *nest_mode, uint32_t *breath_period); +#define BR_MIN 0 +#define BR_MAX 100 +#define BR_CURR ((uint32_t)-1) + +#define LED_ID_KEY_1 (1<<0) +#define LED_ID_KEY_2 (1<<1) +#define LED_ID_KEY_3 (1<<2) +#define LED_ID_KEY_4 (1<<3) +#define LED_ID_KEY_5 (1<<4) +#define LED_ID_KEY_6 (1<<5) +#define LED_ID_KEY_7 (1<<6) +#define LED_ID_KEY_8 (1<<7) +#define LED_ID_KEY_ALL (0xff) + +#define LED_ID_ATMOS_1 (1<<0) +#define LED_ID_ATMOS_2 (1<<1) +#define LED_ID_ATMOS_3 (1<<2) +#define LED_ID_ATMOS_4 (1<<3) +#define LED_ID_ATMOS_5 (1<<4) +#define LED_ID_ATMOS_ALL ( 0x1f ) + +#ifdef __x86_64__ +typedef uint64_t light_ctrl_t; +#else + +typedef uint32_t light_ctrl_t; +#endif typedef struct{ - led_mode_t mode; //当前模式 - led_mode_t next_mode; //下一个模式 - uint32_t next_time_ms; //切换到下一个模式的时间 - uint32_t breath_period_ms; //呼吸模式的周期时间 - uint32_t max_brightness; //最大亮度 - uint32_t br_chg_start; - uint32_t br_chg_stop; - uint32_t br_chg_time; -}led_cfg_t; - - -//---------------------------------------------------------------/ -//api -//---------------------------------------------------------------/ -mg_led_ctrl_t * mg_led_open( void ); -void mg_led_close( mg_led_ctrl_t * led_ctrl ); -int32_t mg_led_mode_set( mg_led_ctrl_t *led_ctrl, led_id_t id, led_cfg_t *cfg ); -int32_t mg_led_mode_get( mg_led_ctrl_t *led_ctrl, led_id_t id, led_cfg_t *cfg ); -int32_t mg_led_brightness_set( mg_led_ctrl_t *led_ctrl, led_id_t id, uint32_t max_br); -int32_t mg_led_brightness_get( mg_led_ctrl_t *led_ctrl, led_id_t id, uint32_t *out_max_br ); - -//---------------------------------------------------------------/ - -#endif //__LED__H__ + uint32_t on; //0:off, 1:on, 2:flash, 3:breath + uint32_t period; //flash or breath period + uint32_t br_max; //brightness +} light_st_t; + + + +//key led api +int32_t key_led_on( light_ctrl_t ctrl, uint32_t led_id, uint32_t br_from, uint32_t br_to, uint32_t transition_time ); +int32_t key_led_off( light_ctrl_t ctrl, uint32_t led_id, uint32_t br_from, uint32_t br_to, uint32_t transition_time ); +int32_t key_led_flash( light_ctrl_t ctrl, uint32_t led_id, uint32_t period_ms, uint32_t duration_ms ); +int32_t key_led_br_update( light_ctrl_t ctrl, uint32_t led_id, uint32_t br ); +int32_t key_led_st( light_ctrl_t ctrl, uint32_t led_id, light_st_t *st ); + +//atmosphere led +int32_t atmos_led_on( light_ctrl_t ctrl, uint32_t led_id, uint32_t br_from, uint32_t br_to, uint32_t transition_time ); +int32_t atmos_led_off( light_ctrl_t ctrl, uint32_t led_id, uint32_t br_from, uint32_t br_to, uint32_t transition_time ); +int32_t atmos_led_flash( light_ctrl_t ctrl, uint32_t led_id, uint32_t period_ms, uint32_t duration_ms ); +int32_t atmos_led_br_update( light_ctrl_t ctrl, uint32_t led_id, uint32_t br ); +int32_t atmos_led_st( light_ctrl_t ctrl, uint32_t led_id, light_st_t *st ); + +//ring led +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_breath( light_ctrl_t ctrl, uint32_t period_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 ); + +//ir led +int32_t ir_led_on( light_ctrl_t ctrl, uint32_t br ); +int32_t ir_led_off( light_ctrl_t ctrl ); +int32_t ir_led_br_update( light_ctrl_t ctrl, uint32_t br ); +int32_t ir_led_st( light_ctrl_t ctrl, light_st_t *st ); + + +light_ctrl_t mg_led_open( void ); +#endif //__MG_LIGHT_H__ diff --git a/led/src/led.c b/led/src/led.c index be07b62..3c6aca2 100644 --- a/led/src/led.c +++ b/led/src/led.c @@ -1,539 +1,608 @@ -#include #include +#include #include #include -#include -#include -#include "led.h" -#include "mg_log.h" -#include "pthread.h" -#include "semaphore.h" - +#include #include #include #include +#include -#if XXXXX - -void led_on( led_x_t *led, uint32_t brightness ) -{ - char system_cmd[100]; - - brightness = brightness*255/100; -#if 0 - sprintf( system_cmd, "echo none > %s/trigger", led->sys_path ); - MG_LOGD( "[%s] system cmd [%s]\r\n", led->name, system_cmd ); - if( system( system_cmd ) ){ - } - - sprintf( system_cmd, "echo %d > %s/brightness", brightness, led->sys_path ); - MG_LOGD( "[%s] system cmd [%s]\r\n", led->name, system_cmd ); - if( system( system_cmd ) ){ - } -#else - led_device_write_str( led->sys_path, "trigger", "none" ); - led_device_write_int( led->sys_path, "brightness", brightness ); -#endif -} - -void led_flash( led_x_t *led, uint32_t brightness, uint32_t on_ms, uint32_t off_ms ) -{ - char system_cmd[200]; - - brightness = brightness*255/100; -#if 0 - sprintf( system_cmd, "echo timer > %s/trigger", led->sys_path ); - MG_LOGD( "[%s] system cmd [%s]\r\n", led->name, system_cmd ); - if( system( system_cmd )){ - } - - sprintf( system_cmd, "echo %d > %s/delay_on", on_ms, led->sys_path); - //MG_LOGD( "[%s] system cmd [%s]\r\n", led->name, system_cmd ); - if( system( system_cmd )){ - } - - sprintf( system_cmd, "echo %d > %s/delay_off", off_ms, led->sys_path); - //MG_LOGD( "[%s] system cmd [%s]\r\n", led->name, system_cmd ); - if( system( system_cmd )){ - } +#include "led.h" +#include "mg_log.h" - sprintf( system_cmd, "echo %d > %s/brightness", brightness, led->sys_path ); - //MG_LOGD( "[%s] system cmd [%s]\r\n", led->name, system_cmd ); - if( system( system_cmd )){ - } -#else - led_device_write_str( led->sys_path, "trigger", "timer" ); - led_device_write_int( led->sys_path, "brightness", brightness ); - led_device_write_int( led->sys_path, "delay_on", on_ms ); - led_device_write_int( led->sys_path, "delay_off", off_ms ); -#endif -} +//------------------------------------------------------------------------------------------------------------- +#define LED_KEY_1 (1<<0) +#define LED_KEY_2 (1<<1) +#define LED_KEY_3 (1<<2) +#define LED_KEY_4 (1<<3) +#define LED_KEY_5 (1<<4) +#define LED_KEY_6 (1<<5) +#define LED_KEY_7 (1<<6) +#define LED_KEY_8 (1<<7) +#define LED_ATMOS_1 (1<<8) +#define LED_ATMOS_2 (1<<9) +#define LED_ATMOS_3 (1<<10) +#define LED_ATMOS_4 (1<<11) +#define LED_ATMOS_5 (1<<12) +#define LED_RING (1<<13) +#define LED_IR (1<<14) +#define LED_NR 15 -//---------------------------------------------------------------------------------------- -#endif +struct led_path{ + uint32_t id; + const char * name; + const char * path; +}; struct led_ctrl; - +typedef struct led_ctrl led_ctrl_t; typedef struct { - led_id_t id; + //---------------------------------- + uint32_t id; const char *name; const char *sys_path; - led_cfg_t cfg; - + int32_t br_fd; //->sys/class/leds/xxxx/brightness + + //---------------------------------- + uint32_t on; + uint32_t br_max; + uint32_t br_from; + uint32_t br_to; + uint32_t br_curr; + uint32_t trans_ms; + uint32_t trans_start_ms; + uint32_t trans_stop_ms; + + uint32_t flash; //0:off, 1:flash, 2:breath + uint32_t period_ms; + uint32_t duration_ms; + uint32_t duration_start_ms; + uint32_t duration_stop_ms; + + //---------------------------------- pthread_t led_thread; //LED控制线程 pthread_mutex_t lock; - sem_t sem; - mg_led_ctrl_t *ctrl; //指向控制器 + //sem_t sem; + led_ctrl_t *ctrl; //指向控制器 uint32_t run; + //---------------------------------- }led_dev_t; -struct led_path{ - led_id_t id; - const char * name; - const char * path; -}; - -struct mg_led_ctrl{ +struct led_ctrl{ led_dev_t led[LED_NR]; }; -static mg_led_ctrl_t g_mg_led; +static void led_dev_lock( led_dev_t *dev ); +static void led_dev_unlock( led_dev_t *dev ); -static void led_dev_lock( led_dev_t *led ); -static void led_dev_unlock( led_dev_t *led ); -static void led_dev_wait( led_dev_t *led ); -static void led_dev_wake( led_dev_t *led ); -static void * led_thread_fun( void *led ); -static void led_init( led_dev_t *led, char* path, char*name ); -static void led_ctrl_init( mg_led_ctrl_t *ctrl ); +//-------------------------------------------------------------------------------------------------------------------------------- -const struct led_path led_path_info[LED_NR] = { - { LED_ID_KEY_1, "KEY_LED_1", "/sys/class/leds/key_led1" }, - { LED_ID_KEY_2, "KEY_LED_2", "/sys/class/leds/key_led2" }, - { LED_ID_KEY_3, "KEY_LED_3", "/sys/class/leds/key_led3" }, - { LED_ID_KEY_4, "KEY_LED_4", "/sys/class/leds/key_led4" }, - { LED_ID_KEY_5, "KEY_LED_5", "/sys/class/leds/key_led5" }, - { LED_ID_KEY_6, "KEY_LED_6", "/sys/class/leds/key_led6" }, - { LED_ID_KEY_7, "KEY_LED_7", "/sys/class/leds/key_led7" }, - { LED_ID_KEY_8, "KEY_LED_8", "/sys/class/leds/key_led8" }, - { LED_ID_FUN_1, "FUN_LED_1", "/sys/class/leds/fun_led1" }, - { LED_ID_FUN_2, "FUN_LED_2", "/sys/class/leds/fun_led2" }, - { LED_ID_FUN_3, "FUN_LED_3", "/sys/class/leds/fun_led3" }, - { LED_ID_FUN_4, "FUN_LED_4", "/sys/class/leds/fun_led4" }, - { LED_ID_FUN_5, "FUN_LED_5", "/sys/class/leds/fun_led5" }, - { LED_ID_RING, "RING_LED", "/sys/class/leds/ring_led" }, - { LED_ID_IR, "IR_LED", "/sys/class/leds/ir_led" }, -}; +//-------------------------------------------------------------------------------------------------------------------------------- +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_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 ); +//-------------------------------------------------------------------------------------------------------------------------------- -//hal_led_brightness( led, birghtness ) -//hal_led_flash( led, on_ms, off_ms ); +uint32_t now_msec( void ) +{ + struct timeval tm; + gettimeofday( &tm, NULL ); + return (uint32_t)(tm.tv_sec*1000+tm.tv_usec/1000 ); +} -static void led_device_write_str( const char *path, const char *name, char * value ) +static uint32_t get_key_led_id( uint32_t key_led ) { - char led_path[100]; - char led_value[50]; - sprintf( led_path, "%s/%s", path, name ); - sprintf( led_value, "%s", value ); - int fd; - if( (fd = open( led_path, O_WRONLY )) >= 0){ - if( write( fd, (void*)led_value, strlen( led_value ) ) <= 0 ){ - MG_LOGE( "write file[%s] fail!", led_path ); - } - close(fd); - }else{ - MG_LOGE( "open %s -- fail!", led_path ); + if( key_led &~0xff ){ + //MG_LOGE( ); + return 0; } + return key_led; } -static void led_device_write_int( const char *path, const char *name, int32_t value ) +static uint32_t get_atmos_led_id( uint32_t atmos_led ) { - char led_path[100]; - char led_value[50]; - sprintf( led_path, "%s/%s", path, name ); - sprintf( led_value, "%d\n", value ); - - int fd; - if( (fd = open( led_path, O_WRONLY )) >= 0){ - if( fd < 2 ){ - MG_LOGW( "open %s -- ret:%d!", led_path, fd ); - } - if( write( fd, (void*)led_value, strlen( led_value ) ) <= 0 ){ - MG_LOGE( "write file[%s] fail!", led_path ); - } - if( fd > 2) - close(fd); - }else{ - MG_LOGE( "open %s -- fail!", led_path ); + if( atmos_led &~0x1f ){ + //MG_LOGE( ); + return 0; } + return (atmos_led<<8); } -static void hal_led_on( led_dev_t *led, uint32_t br ) +//key led api +int32_t key_led_on( light_ctrl_t ctrl, uint32_t led_id, uint32_t br_from, uint32_t br_to, uint32_t transition_time ) { - led_device_write_str( led->sys_path, "trigger", "none" ); - led_device_write_int( led->sys_path, "brightness", br*255/100 ); +//printf( "key led on, id:%x, from:%d, to:%d, time:%d\n", led_id, br_from, br_to, transition_time ); + return mg_led_on( ctrl, get_key_led_id( led_id ), br_from, br_to, transition_time ); } -static void hal_led_flash( led_dev_t *led, uint32_t br, uint32_t on_ms, uint32_t off_ms ) +int32_t key_led_off( light_ctrl_t ctrl, uint32_t led_id, uint32_t br_from, uint32_t br_to, uint32_t transition_time ) { -//printf( "led [%s] flash br:%d, on: %dms, off:%d ms\n", led->name, br, on_ms, off_ms ); - led_device_write_str( led->sys_path, "trigger", "timer" ); - led_device_write_int( led->sys_path, "brightness", br*255/100 ); - usleep( 10000 ); - led_device_write_int( led->sys_path, "delay_on", on_ms ); - led_device_write_int( led->sys_path, "delay_off", off_ms ); +//printf( "key led off, id:%x, from:%d, to:%d, time:%d\n", led_id, br_from, br_to, transition_time ); + return mg_led_off( ctrl, get_key_led_id( led_id ), br_from, br_to, transition_time ); } -static void hal_led_brightness( led_dev_t *led, uint32_t br ) +int32_t key_led_flash( light_ctrl_t ctrl, uint32_t led_id, uint32_t period_ms, uint32_t duration_ms ) { - if( led->cfg.mode != LED_MODE_OFF ){ - led_device_write_int( led->sys_path, "brightness", br*255/100 ); - } +//printf( "key led flash, id:%x, period:%d, dur:%d\n", led_id, period_ms, duration_ms ); + return mg_led_flash( ctrl, get_key_led_id( led_id ), period_ms, duration_ms ); } -void* led_thread_fun( void *led ) +int32_t key_led_br_update( light_ctrl_t ctrl, uint32_t led_id, uint32_t br ) { - led_cfg_t curr_cfg; - - uint32_t mode_changed; - uint32_t br_changed; - - uint32_t to_next_mode_tick; - struct timeval to_next_mode_tm; - struct timeval add_tm; - struct timeval curr_tm; - uint32_t breath_br; - uint32_t curr_br; +//printf( "key led br updete, id:%x, br:%d \n", led_id, br ); + return mg_led_br_update( ctrl, get_key_led_id( led_id ), br ); +} - led_dev_t *p_led; - p_led = ( led_dev_t*) led; +int32_t key_led_st( light_ctrl_t ctrl, uint32_t led_id, light_st_t *st ) +{ + return mg_led_st( ctrl, get_key_led_id( led_id ), st ); +} - memset( &curr_cfg, 0, sizeof( curr_cfg ) ); - curr_br = 100; - while( p_led->run ){ +//atmos led api +int32_t atmos_led_on( light_ctrl_t ctrl, uint32_t led_id, uint32_t br_from, uint32_t br_to, uint32_t transition_time ) +{ +//printf( "atmos led on, id:%x, from:%d, to:%d, time:%d\n", led_id, br_from, br_to, transition_time ); + return mg_led_on( ctrl, get_atmos_led_id( led_id ), br_from, br_to, transition_time ); +} - if( p_led->id != LED_ID_KEY_1 || p_led->id!= LED_ID_FUN_1 ){ - //sleep(10); - //continue; - } +int32_t atmos_led_off( light_ctrl_t ctrl, uint32_t led_id, uint32_t br_from, uint32_t br_to, uint32_t transition_time ) +{ +//printf( "atmos led off, id:%x, from:%d, to:%d, time:%d\n", led_id, br_from, br_to, transition_time ); + return mg_led_off( ctrl, get_atmos_led_id( led_id ), br_from, br_to, transition_time ); +} - led_dev_lock( p_led ); - if( curr_cfg.mode != p_led->cfg.mode || curr_cfg.next_time_ms != p_led->cfg.next_time_ms ) - mode_changed = 1; - if( curr_cfg.max_brightness != p_led->cfg.max_brightness ) - br_changed = 1; - memcpy( &curr_cfg, &p_led->cfg, sizeof( curr_cfg ) ); - led_dev_unlock( p_led ); - - if( mode_changed ){ - MG_LOGD( "led [%s] mode -> %d, time_to_next:%d ms", p_led->name, curr_cfg.mode, curr_cfg.next_time_ms ); - switch( curr_cfg.mode ){ - case LED_MODE_OFF: - curr_br = 0; - hal_led_on( p_led, curr_br ); - break; - case LED_MODE_ON: - curr_br = curr_cfg.max_brightness; - hal_led_on( p_led, curr_br ); - break; - case LED_MODE_FLASH_1HZ: - curr_br = curr_cfg.max_brightness; - hal_led_flash( p_led, curr_br, 500, 500 ); - break; - case LED_MODE_FLASH_2HZ: - curr_br = curr_cfg.max_brightness; - hal_led_flash( p_led, curr_br, 250, 250 ); - break; - case LED_MODE_FLASH_8HZ: - curr_br = curr_cfg.max_brightness; - hal_led_flash( p_led, curr_br, 62, 63 ); - break; - - case LED_MODE_GRADUAL_ON: //渐变到设定亮度后常亮 - //if( curr_br == curr_cfg.max_brightness ) - // curr_br = 0; - //curr_br = 0; - MG_LOGD( "led mode gradual -> on: br from %d to %d", curr_br, curr_cfg.max_brightness ); - break; - case LED_MODE_GRADUAL_OFF: //渐变到关闭 - //curr_br = curr_cfg.max_brightness; - //curr_br = 100; - MG_LOGD( "led mode gradual -> off: br from %d to %d", curr_br, curr_cfg.max_brightness ); - break; - } - mode_changed = 0; - br_changed = 0; - }else if( br_changed ){ - switch( curr_cfg.mode ){ - case LED_MODE_ON: - case LED_MODE_FLASH_1HZ: - case LED_MODE_FLASH_2HZ: - case LED_MODE_FLASH_8HZ: - curr_br = curr_cfg.max_brightness; - hal_led_brightness( p_led, curr_br ); - break; - } - br_changed = 0; - } +int32_t atmos_led_flash( light_ctrl_t ctrl, uint32_t led_id, uint32_t period_ms, uint32_t duration_ms ) +{ +//printf( "atmos led flash, id:%x, period:%d, dur:%d\n", led_id, period_ms, duration_ms ); + return mg_led_flash( ctrl, get_atmos_led_id( led_id ), period_ms, duration_ms ); +} - if( curr_cfg.next_time_ms || curr_cfg.mode == LED_MODE_GRADUAL_ON - || curr_cfg.mode == LED_MODE_GRADUAL_OFF ){ - - if( curr_cfg.next_time_ms ){ - gettimeofday(&curr_tm, NULL); - add_tm.tv_sec = 0; - add_tm.tv_usec = curr_cfg.next_time_ms *1000; - timeradd( &curr_tm, &add_tm, &to_next_mode_tm ); - } +int32_t atmos_led_br_update( light_ctrl_t ctrl, uint32_t led_id, uint32_t br ) +{ +//printf( "atmos led br updete, id:%x, br:%d \n", led_id, br ); + return mg_led_br_update( ctrl, get_atmos_led_id( led_id ), br ); +} - //printf( " to next: curr:%d, +%d = %d \n", clock(), curr_cfg.next_time_ms, to_next_mode_tick ); - - while( 1){ - usleep( 100*1000 ); - gettimeofday(&curr_tm, NULL); - - if( curr_cfg.mode == LED_MODE_GRADUAL_ON ){ - if( curr_br < curr_cfg.max_brightness ) - curr_br += 1; //10s->100% - //printf( "[%s]br up -> %d\n",p_led->name, curr_br ); - hal_led_on( p_led, curr_br ); - if( curr_br >= curr_cfg.max_brightness ){ - led_dev_lock( p_led ); - p_led->cfg.mode = p_led->cfg.next_mode = LED_MODE_ON; - p_led->cfg.next_time_ms = 0; - led_dev_unlock( p_led ); - printf( "brightness arrived next max\n" ); - break; - } - }else if( curr_cfg.mode == LED_MODE_GRADUAL_OFF ){ - if( curr_br > 0 ) - curr_br -= 1; - //printf( "[%s]br down -> %d\n",p_led->name, curr_br ); - hal_led_on( p_led, curr_br ); - if( curr_br == 0 ){ - led_dev_lock( p_led ); - p_led->cfg.mode = p_led->cfg.next_mode = LED_MODE_OFF; - p_led->cfg.next_time_ms = 0; - led_dev_unlock( p_led ); - printf( "brightness arrived 0\n" ); - break; - } - }else if( timercmp( &curr_tm, &to_next_mode_tm, > ) ){ - led_dev_lock( p_led ); - p_led->cfg.mode = p_led->cfg.next_mode; - p_led->cfg.next_time_ms = 0; - led_dev_unlock( p_led ); - printf( "[%s]arrived next mode time\n", p_led->name ); - break; - } +int32_t atmos_led_st( light_ctrl_t ctrl, uint32_t led_id, light_st_t *st ) +{ + return mg_led_st( ctrl, get_atmos_led_id( led_id ), st ); +} +//ring led +int32_t ring_led_on( light_ctrl_t ctrl, uint32_t br_from, uint32_t br_to, uint32_t transition_time ) +{ + return mg_led_on( ctrl, LED_RING, br_from, br_to, transition_time ); +} - led_dev_lock( p_led ); - if( memcmp( &curr_cfg, &p_led->cfg, sizeof( curr_cfg )) != 0 ){ - led_dev_unlock( p_led ); - break; - } - led_dev_unlock( p_led ); +int32_t ring_led_off( light_ctrl_t ctrl, uint32_t br_from, uint32_t br_to, uint32_t transition_time ) +{ + 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 ) +{ + return mg_led_breath( ctrl, LED_RING, period_ms, duration_ms ); +} - } - led_dev_lock( p_led ); - if( memcmp( &curr_cfg, &p_led->cfg, sizeof( curr_cfg )) == 0 ){ - led_dev_unlock( p_led ); +int32_t ring_led_br_update( light_ctrl_t ctrl, uint32_t br ) +{ + return mg_led_br_update( ctrl, LED_RING, br ); +} - led_dev_wait( led ); - }else{ - led_dev_unlock( p_led ); - } - } - return NULL; +int32_t ring_led_st( light_ctrl_t ctrl, light_st_t *st ) +{ + return mg_led_st( ctrl, LED_RING, st ); } -//api -//------------------------------------------------------------------------------ +//ir led +int32_t ir_led_on( light_ctrl_t ctrl, uint32_t br ) +{ + return mg_led_on( ctrl, LED_IR, br, br, 0 ); +} -void led_dev_lock( led_dev_t *led ) +int32_t ir_led_off( light_ctrl_t ctrl ) { - pthread_mutex_lock( &led->lock ); + return mg_led_on( ctrl, LED_IR, 0, 0, 0 ); } -void led_dev_unlock( led_dev_t *led ) +int32_t ir_led_br_update( light_ctrl_t ctrl, uint32_t br ) { - pthread_mutex_unlock( &led->lock ); + return mg_led_br_update( ctrl, LED_IR, br ); } -void led_dev_wake( led_dev_t *led ) +int32_t ir_led_st( light_ctrl_t ctrl, light_st_t *st ) { - sem_post( &led->sem ); + return mg_led_st( ctrl, LED_IR, st ); } -void led_dev_wait( led_dev_t *led ) +//------------------------------------------------------------------------------------------------------------- +//mg led api + +void led_dev_lock( led_dev_t *dev ) { - sem_wait( &led->sem ); + pthread_mutex_lock( &dev->lock ); } -static void led_dev_init( led_dev_t *led, const struct led_path *path_info ) +void led_dev_unlock( led_dev_t *dev ) { - struct sigevent evp; + pthread_mutex_unlock( &dev->lock ); +} - if( !led ){ - MG_LOGE( "led x init fail, path:%s\n", path_info->name ); - return; - } - pthread_mutex_init( &led->lock, NULL ); - sem_init( &led->sem, 0, 0 ); +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 ) +{ + int32_t retval = 0; + led_ctrl_t *led_ctrl; + led_dev_t *dev; + uint32_t now_ms; - led->id = path_info->id; - led->name = path_info->name; - led->sys_path = path_info->path; + now_ms = now_msec(); - led->cfg.mode = LED_MODE_OFF; - led->cfg.next_mode = LED_MODE_OFF; - led->cfg.next_time_ms = 0; - led->cfg.breath_period_ms = 0; - led->cfg.max_brightness = 100; + led_ctrl = (led_ctrl_t*)ctrl; - pthread_create( &led->led_thread, NULL, led_thread_fun, led ); - led->ctrl = &g_mg_led; - led->run = 1; - return; + for( int i=0; iled[i]; + led_dev_lock( dev ); + if( br_to ){ + //on + dev->on = 1; + dev->br_max = br_to; + }else{ + //off + dev->on = 0; + //dev->br_max = br_to; + } + dev->br_from = br_from; + if( br_from < 100 ) + dev->br_curr = br_from; + dev->br_to = br_to; + dev->trans_ms = transition_time; + + if( dev->trans_ms ){ + dev->trans_start_ms = now_ms; + dev->trans_stop_ms = now_ms+dev->trans_ms; + } + dev->flash = 0; + dev->period_ms = 0; + dev->duration_ms = 0; + dev->duration_start_ms = 0; + dev->duration_stop_ms = 0; + led_dev_unlock( dev ); + } + } + return retval; } -static void led_dev_deinit( led_dev_t *led ) +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 ) { - led->run = 0; - pthread_cancel( led->led_thread ); - pthread_mutex_destroy( &led->lock ); - sem_destroy( &led->sem ); - return; + //直接调用 led on, 通过 br_to是0 则是OFF, 否则为 on; + return mg_led_on( ctrl, led_id, br_from, br_to, transition_time ); } -static void led_ctrl_init( mg_led_ctrl_t *ctrl ) +int32_t mg_led_flash( light_ctrl_t ctrl, uint32_t led_id, uint32_t period_ms, uint32_t duration_ms ) { - for( int i=0; iled[i], &led_path_info[i] ); + int32_t retval = 0; + led_ctrl_t *led_ctrl; + led_dev_t *dev; + uint32_t now_ms; + + now_ms = now_msec(); + + led_ctrl = (led_ctrl_t*)ctrl; + + for( int i=0; iled[i]; + +printf( "LED_%d flash \n", i+1 ); + led_dev_lock( dev ); + dev->flash = 1; + 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; + } + led_dev_unlock( dev ); + } } + + return retval; } -static void led_ctrl_deinit( mg_led_ctrl_t *ctrl ) +int32_t mg_led_breath( light_ctrl_t ctrl, uint32_t led_id, uint32_t period_ms, uint32_t duration_ms ) { - for( int i=0; iled[i] ); + int32_t retval = 0; + led_ctrl_t *led_ctrl; + led_dev_t *dev; + uint32_t now_ms; + + now_ms = now_msec(); + + led_ctrl = (led_ctrl_t*)ctrl; + + for( int i=0; iled[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; + } + //led_dev_unlock( dev ); + } } + + return retval; } -int32_t mg_led_brightness_set( mg_led_ctrl_t *led_ctrl, led_id_t id, uint32_t max_br ) +int32_t mg_led_br_update( light_ctrl_t ctrl, uint32_t led_id, uint32_t br ) { + int32_t retval = 0; + led_ctrl_t *led_ctrl; + led_dev_t *dev; + + led_ctrl = (led_ctrl_t*)ctrl; + + for( int i=0; iled[i]; + led_dev_lock( dev ); + dev->br_max = br; + led_dev_unlock( dev ); + } + } - int32_t i; - led_dev_t *led; + return retval; +} - for( i=0; iled[i]; - led_dev_lock( led ); - if( led->cfg.max_brightness != max_br){ - MG_LOGI( "led[%s] br %d -> %d", led->name, led->cfg.max_brightness, max_br ); - led->cfg.max_brightness = max_br; - } - led_dev_unlock( led ); - led_dev_wake( led ); +int32_t mg_led_st( light_ctrl_t ctrl, uint32_t led_id, light_st_t *st ) +{ + int32_t retval = -1; + led_ctrl_t *led_ctrl; + led_dev_t *dev; + + led_ctrl = (led_ctrl_t*)ctrl; + + for( int i=0; iled[i]; + led_dev_lock( dev ); + if( dev->flash == 2 ) + st->on = 3; //breath; + else if( dev->flash == 1 ) + st->on = 3; //flash + else if( dev->on == 1 ) + st->on = 1; //on + else + st->on = 0; //off + st->br_max = dev->br_max; + st->period = dev->period_ms; + led_dev_unlock( dev ); + retval = 0; + break; } } - return 0; + return retval; } -int32_t mg_led_brightness_get( mg_led_ctrl_t *led_ctrl, led_id_t id, uint32_t *out_max_br ) +static led_ctrl_t g_light; + + +const struct led_path led_path_info[LED_NR] = { + { LED_KEY_1, "KEY_LED_1", "/sys/class/leds/key_led1" }, + { LED_KEY_2, "KEY_LED_2", "/sys/class/leds/key_led2" }, + { LED_KEY_3, "KEY_LED_3", "/sys/class/leds/key_led3" }, + { LED_KEY_4, "KEY_LED_4", "/sys/class/leds/key_led4" }, + { LED_KEY_5, "KEY_LED_5", "/sys/class/leds/key_led5" }, + { LED_KEY_6, "KEY_LED_6", "/sys/class/leds/key_led6" }, + { LED_KEY_7, "KEY_LED_7", "/sys/class/leds/key_led7" }, + { LED_KEY_8, "KEY_LED_8", "/sys/class/leds/key_led8" }, + { LED_ATMOS_1, "FUN_LED_1", "/sys/class/leds/fun_led1" }, + { LED_ATMOS_2, "FUN_LED_2", "/sys/class/leds/fun_led2" }, + { LED_ATMOS_3, "FUN_LED_3", "/sys/class/leds/fun_led3" }, + { LED_ATMOS_4, "FUN_LED_4", "/sys/class/leds/fun_led4" }, + //{ LED_ATMOS_5, "FUN_LED_5", "/sys/class/leds/fun_led5" }, + //{ LED_RING, "RING_LED", "/sys/class/leds/ring_led" }, + { LED_ATMOS_5, "FUN_LED_5", "/sys/class/leds/ring_led" }, + { LED_RING, "RING_LED", "/sys/class/leds/fun_led5" }, + { LED_IR, "IR_LED", "/sys/class/leds/ir_led" }, +}; + +void hw_led_brignt( led_dev_t * dev, uint32_t br ) { -#if 0 - int32_t i; - led_dev_t *led; - - for( i=0; iled_x[i]; - led_lock( led_ctrl ); - if( out_max_brightness ) - *out_max_brightness = led->max_brightness; - if( out_dp_brightness ) - *out_dp_brightness = led->dp_brightness; - led_lock( led_ctrl ); - break; + char br_str[10]; + + if( br<= 100 ){ + sprintf( br_str, "%d\n", br*255/100 ); + if( write(dev->br_fd, br_str, strlen( br_str ) ) < 0 ){ + MG_LOGE( "write %s br:%d fail!", dev->name, br ); } } -#endif - return 0; } -int32_t mg_led_mode_set( mg_led_ctrl_t *led_ctrl, led_id_t id, led_cfg_t *cfg ) +void* led_thread_fun( void *led ) { + uint32_t curr_br = 0; + led_dev_t *dev; + uint32_t now_ms; + + dev = (led_dev_t *)led; + + while(1){ + usleep( 50*1000 ); + + now_ms = now_msec(); + + led_dev_lock( dev ); - int32_t i; - led_dev_t *led; - MG_LOGD( "mg_led_mode_set: led:%04X, cfg:{ mode:%d, next_mode:%d, time:%d", id, cfg->mode, cfg->next_mode, cfg->next_time_ms ); + curr_br = dev->br_curr; + if( dev->duration_ms && now_ms > dev->duration_stop_ms){ + //时间到,关闭闪烁或呼吸 + dev->flash = 0; + dev->duration_ms = 0; + curr_br = dev->br_max; + + } - for( i=0; iled[i]; - MG_LOGD( "led[%s] mode set %d", led->name ); - led_dev_lock( led ); - if( led->cfg.mode != cfg->mode ){ - MG_LOGI( "led[%s] mode: %d -> %d", led->name, led->cfg.mode, cfg->mode); - led->cfg.mode = cfg->mode; + if( dev->flash == 1 ){ + static uint32_t flash_br = 0; + //flash + if( (now_ms % dev->period_ms) *2 > dev->period_ms ) + curr_br = dev->br_max; + else + curr_br = 0; + if( curr_br != flash_br ){ + //printf( "[%d] %s thread, flash br %d -> %d\n", now_ms, dev->name, flash_br, curr_br ); + flash_br = curr_br; } - led->cfg.next_time_ms = cfg->next_time_ms; - led->cfg.breath_period_ms = cfg->breath_period_ms; - if( cfg->next_mode != LED_MODE_NOCHANGE ){ - led->cfg.next_mode = cfg->next_mode; + + }else if( dev->flash == 2 ){ + //breath; + //呼吸周期计算 + uint32_t ms = now_ms%dev->period_ms; + if( ms*2 < dev->period_ms ){ + //up + curr_br = 100 * ms * 2/dev->period_ms; + }else{ + //down + curr_br = 100 * (dev->period_ms - ms) * 2/dev->period_ms; + } + //printf( "[%d] breath br:%d\n", now_ms%100000, curr_br ); + + }else if( dev->flash == 0 ){ + //on /off + if( dev->trans_ms ){ + //渐变计算 + if( now_ms > dev->trans_stop_ms + || ( dev->on && curr_br >= dev->br_to ) + || ( !dev->on&& curr_br <= dev->br_to )){ + dev->trans_ms = 0; + curr_br = dev->br_to; + }else{ + if( dev->on ){ + //trans_ms 是 0-到100 的变化时间, + //curr_br = dev->br_from + (now_ms - dev->trans_start_ms)*(dev->br_to - dev->br_from)/dev->trans_ms; + curr_br = dev->br_from + (now_ms - dev->trans_start_ms)*(100 - 0)/dev->trans_ms; + }else{ + curr_br = dev->br_from - (now_ms - dev->trans_start_ms)*(100 - 0)/dev->trans_ms; + } + //printf( "[%d] LED-%s br trans [ %d -> %d ], now br:%d\n", now_ms%100000, dev->name, dev->br_from, dev->br_to, curr_br ); + } + }else if( dev->on){ + //ON + curr_br = dev->br_max; + }else{ + //OFF + curr_br = 0; } - led->cfg.max_brightness = cfg->max_brightness; - led_dev_unlock( led ); - led_dev_wake( led ); + //write curr_mx -> br_fd; } + dev->br_curr = curr_br; + led_dev_unlock( dev ); + + hw_led_brignt( dev, curr_br ); + } - return 0; + return NULL; } -int32_t mg_led_mode_get( mg_led_ctrl_t *led_ctrl, led_id_t id, led_cfg_t *cfg ) +static void led_dev_init( led_dev_t *led, const struct led_path *path_info ) { -#if 0 - int32_t i; - led_dev_t *led; - - for( i=0; iled_x[i]; - led_lock( led_ctrl ); - led->mode = cfg->mode; - led->next_time_ms = cfg->next_time_ms; - led->breath_period_ms = cfg->breath_period_ms; - if( cfg->next_mode != LED_MODE_KEEP ){ - led->next_mode = cfg->next_mode; - } - led_unlock( led_ctrl ); - led_x_update( led ); - } + char led_path[100]; + + if( !led ){ + MG_LOGE( "led x init fail, path:%s\n", path_info->name ); + return; } -#endif - return 0; + + memset( led, 0, sizeof( *led ) ); + + pthread_mutex_init( &led->lock, NULL ); + + led->id = path_info->id; + led->name = path_info->name; + led->sys_path = path_info->path; + + sprintf( led_path, "%s/brightness", led->sys_path ); + led->br_fd = open( led_path, O_WRONLY ); + if( led->br_fd < 0 ){ + MG_LOGE( "[%s]open brightness file[%s/brightness] fail!", led->name, led->sys_path ); + return; + } + + led->br_max = 100; + + led->ctrl = &g_light; + led->run = 1; + pthread_create( &led->led_thread, NULL, led_thread_fun, led ); + + return; } -mg_led_ctrl_t * mg_led_open( void ) +static void led_ctrl_init( led_ctrl_t *ctrl ) +{ + for( int i=0; iled[i], &led_path_info[i] ); + } + +} + +light_ctrl_t mg_led_open( void ) { MG_LOGI( "mg_led_open..." ); - led_ctrl_init( &g_mg_led); - return &g_mg_led; + + led_ctrl_init( &g_light ); + + return ( light_ctrl_t)&g_light; } -void mg_led_close( mg_led_ctrl_t *led_ctrl ) +void mg_led_close( void ) { MG_LOGI( "mg_led_close..." ); - if( led_ctrl ){ - led_ctrl_deinit( led_ctrl); - } - return; + //led_ctrl_deinit( &g_light ); } +#if 0 +int main( void ) +{ + mg_log_init(); + + light_ctrl_t ctrl; + ctrl = mg_led_open(); + if( ctrl == 0 ){ + printf( "open led fail!\n" ); + exit(0); + } + + printf( "led 1 on from 0 to 100 , in 5s ... \n" ); + key_led_flash( ctrl, 1, 1000, 5000 ); + + sleep( 2 ); + key_led_br_update( ctrl, 1, 40 ); + sleep( 4 ); + exit(0); + + printf( "led 1 off from 100 to 0 , in 3s ... \n" ); + key_led_off( ctrl, 1, 100, 0, 3000 ); + sleep( 4 ); + + ring_led_breath( ctrl, 1000, 5000 ); + + sleep(25); + return 0; +} +#endif