From 9c3f9d0652172b45c46a2a3bc4233c09f5f6a1a4 Mon Sep 17 00:00:00 2001 From: kyz Date: Thu, 11 May 2023 10:21:15 +0800 Subject: [PATCH] add led ctrl --- ...45\345\217\243\345\256\232\344\271\211.md" | 2 +- app/CMakeLists.txt | 1 + app/inc/dp.h | 16 +- app/inc/iot_devinfo.h | 12 + app/src/dp.c | 127 ++++++-- app/src/iot_devinfo.c | 73 +++++ app/src/iot_key.c | 113 +++++++ app/src/main.c | 32 +- led/inc/led.h | 48 ++- led/src/led.c | 304 +++++++++++++++++- 10 files changed, 673 insertions(+), 55 deletions(-) create mode 100644 app/inc/iot_devinfo.h create mode 100644 app/src/iot_devinfo.c create mode 100644 app/src/iot_key.c diff --git "a/TW8013\344\270\255\351\227\264\344\273\266\346\216\245\345\217\243\345\256\232\344\271\211.md" "b/TW8013\344\270\255\351\227\264\344\273\266\346\216\245\345\217\243\345\256\232\344\271\211.md" index f476b79..3528606 100755 --- "a/TW8013\344\270\255\351\227\264\344\273\266\346\216\245\345\217\243\345\256\232\344\271\211.md" +++ "b/TW8013\344\270\255\351\227\264\344\273\266\346\216\245\345\217\243\345\256\232\344\271\211.md" @@ -152,7 +152,7 @@ 成功,返回LED控制器指针。 #### 关闭控制器 - void mg_button_close( mg_led_t * btn_ctrl ); + void mg_led_close( mg_led_t * btn_ctrl ); 参数定义: btn_ctrl: 按键控制器 返回值: 无 diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index d9c3569..85c6a68 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -32,6 +32,7 @@ target_link_libraries( platform_port mg_button mg_led + rt pthread ) install( TARGETS ${PROJECT_NAME} RUNTIME DESTINATION /app ) diff --git a/app/inc/dp.h b/app/inc/dp.h index f5e1b10..2735d8a 100644 --- a/app/inc/dp.h +++ b/app/inc/dp.h @@ -2,14 +2,14 @@ #define __MG_DP_H__ typedef enum { - DP_BTN_1 = 100, - DP_BTN_2 = 101, - DP_BTN_3 = 102, - DP_BTN_4 = 103, - DP_BTN_5 = 104, - DP_BTN_6 = 105, - DP_BTN_7 = 106, - DP_BTN_8 = 107, + DP_BTN_1 = 0, + DP_BTN_2 = 1, + DP_BTN_3 = 2, + DP_BTN_4 = 3, + DP_BTN_5 = 4, + DP_BTN_6 = 5, + DP_BTN_7 = 6, + DP_BTN_8 = 7, DP_BTN_MAX, }dp_button_code_t; diff --git a/app/inc/iot_devinfo.h b/app/inc/iot_devinfo.h new file mode 100644 index 0000000..89a5ba6 --- /dev/null +++ b/app/inc/iot_devinfo.h @@ -0,0 +1,12 @@ +#ifndef __IOT_DEVINFO_H__ +#define __IOT_DEVINFO_H__ + +#include + +enum{ + DP_KEY_LED_MAX, + DP_ATMOS_LED_MAX, +}; + +#endif //__IOT_DEVINFO_H__ + diff --git a/app/src/dp.c b/app/src/dp.c index d82c9a6..d64accb 100644 --- a/app/src/dp.c +++ b/app/src/dp.c @@ -14,31 +14,19 @@ #include "cQueue.h" #include "dp.h" -#if 0 -const char productId[] = "gmabzdoevsvl****"; -const char deviceId[] = "6cc87b39279b6fb754****"; -const char deviceSecret[] = "ffad8eb31ae8****"; -#else -//ProductID:nyitvn2sp9sccl40 -//DeviceID:2667ab8ddf0e29a4aevack -//DeviceSecret:dTCB1BfAfAsBSZ1p - -//const char productId[] = "nyitvn2sp9sccl40"; -//const char deviceId[] = "2667ab8ddf0e29a4aevack"; -//const char deviceSecret[] = "dTCB1BfAfAsBSZ1p"; - -//ProductID:3xeyrwyvbuyajz4a -//DeviceID:26dfc49b45e25a93dcwgzg -//DeviceSecret:IAS0hHa3mmgrKn9L - -const char productId[] = "3xeyrwyvbuyajz4a"; -const char deviceId[] = "26dfc49b45e25a93dcwgzg"; -const char deviceSecret[] = "IAS0hHa3mmgrKn9L"; +extern int32_t iot_key_load( char ** out_productID, char ** out_deviceID, char ** out_deviceSecret ); -#endif +//const char productId[] = "3xeyrwyvbuyajz4a"; +//const char deviceId[] = "26dfc49b45e25a93dcwgzg"; +//const char deviceSecret[] = "IAS0hHa3mmgrKn9L"; +char *productId; +char *deviceId; +char *deviceSecret; tuya_mqtt_context_t client_instance; +void handle_iot_msg(tuya_mqtt_context_t* context, char *data_string ); + void on_connected(tuya_mqtt_context_t* context, void* user_data) { TY_LOGI("on connected"); @@ -71,10 +59,9 @@ void on_messages(tuya_mqtt_context_t* context, void* user_data, const tuyalink_m case THING_TYPE_PROPERTY_SET: TY_LOGI("property set:%s", msg->data_string); - tuyalink_thing_property_report(context, NULL, "{\"key_led_max\":{\"value\":35}}"); - //tuyalink_thing_event_trigger(context, NULL, "{\"eventCode\":\"button_1\", \"outputParams\": {\ - \"button_1\":\"long_press\", \"btn_100\":\"click\" } }" ); - tuyalink_thing_event_trigger(context, NULL, "{\"eventCode\":\"button_3\", \"outputParams\": {\ + handle_iot_msg( context, msg->data_string ); + //tuyalink_thing_property_report(context, NULL, "{\"key_led_max\":{\"value\":35}}"); + //tuyalink_thing_event_trigger(context, NULL, "{\"eventCode\":\"button_3\", \"outputParams\": {\ \"button_3\":\"single_click\" } }" ); break; @@ -87,11 +74,14 @@ void on_messages(tuya_mqtt_context_t* context, void* user_data, const tuyalink_m } printf("\r\n"); } - +#if 0 int dp_demo( void ) { int ret = OPRT_OK; + iot_key_load( &productId, &deviceId, &deviceSecret ); +printf( " DP Demo, KEY load: %s,%s\r\n", deviceId, deviceSecret ); + tuya_mqtt_context_t* client = &client_instance; ret = tuya_mqtt_init(client, &(const tuya_mqtt_config_t) { @@ -119,6 +109,7 @@ int dp_demo( void ) return ret; } +#endif cQueue_t *g_dp_event_q; @@ -151,7 +142,7 @@ char * button_event_name[]={ int32_t dp_button_event_push( dp_button_code_t btn_code, dp_button_event_t btn_event ) { char msg_buf[100]; - sprintf( msg_buf, btn_event_fmt, button_code_name[ btn_code ], button_code_name[ btn_code ], button_event_name[btn_event] ); + sprintf( msg_buf, btn_event_fmt, button_code_name[ btn_code-DP_BTN_1 ], button_code_name[ btn_code-DP_BTN_1 ], button_event_name[btn_event] ); void *msg_str = strdup( msg_buf ); cQPost( g_dp_event_q, ( void *)msg_str ); @@ -169,6 +160,86 @@ char* dp_event_pop( void ) return msg; } + +#include "iot_devinfo.h" +extern int32_t iot_dp_int_write( uint32_t dp_id, uint32_t value ); + +//111 手势开关状态 属性 recognition_running 布尔型(Bool) 只上报(ro) +void handle_iot_msg(tuya_mqtt_context_t* context, char *data_string ) +{ + cJSON *root; + cJSON *item; + char buf[300]; + + root = cJSON_Parse((const char *)data_string); + if( root ){ + if(( item =cJSON_GetObjectItem(root, "key_led_max"))){ + //按键背光最大亮度 + printf( "key_led_max-->%d\r\n", item->valueint ); + sprintf( buf, "{\"key_led_max\":{\"value\":%d}}", item->valueint ); + tuyalink_thing_property_report(context, NULL, buf ); + iot_dp_int_write( DP_KEY_LED_MAX, item->valueint ); + + } + if(( item =cJSON_GetObjectItem(root, "atmos_led_max"))){ + //氛围灯最大亮度 + printf( "atmos_led_max-->%d\r\n", item->valueint ); + sprintf( buf, "{\"atmos_led_max\":{\"value\":%d}}", item->valueint ); + tuyalink_thing_property_report(context, NULL, buf ); + iot_dp_int_write( DP_ATMOS_LED_MAX, item->valueint ); + } + if(( item =cJSON_GetObjectItem(root, "recognition_mode"))){ + // + printf( "recognition_mode-->%s\r\n", item->valuestring ); + } + if(( item =cJSON_GetObjectItem(root, "recognition_level"))){ + // + printf( "recognition_level-->%d\r\n", item->valueint); + } + if(( item =cJSON_GetObjectItem(root, "recognition_duration"))){ + // + printf( "recognition_duration-->%d\r\n", item->valueint); + } + if(( item =cJSON_GetObjectItem(root, "key"))){ + // + printf( "key-->%d\r\n", item->valuestring); + } + if(( item =cJSON_GetObjectItem(root, "led_atmos_on_duration"))){ + // + printf( "led_atmos_on_duration-->%d\r\n", item->valueint); + } + if(( item =cJSON_GetObjectItem(root, "mg_tof_threshold"))){ + // + printf( "mg_tof_threshold-->%d\r\n", item->valueint); + } + if(( item =cJSON_GetObjectItem(root, "mg_tof_interval"))){ + // + printf( "mg_tof_interval-->%d\r\n", item->valueint); + } + if(( item =cJSON_GetObjectItem(root, "mg_disalbe_row"))){ + // + printf( "mg_disalbe_row-->%d\r\n", item->valueint); //bool + } +#if 0 //TODO + if(( item =cJSON_GetObjectItem(root, ""))){ + printf( "-->%d\r\n", item->valueint); + } + if(( item =cJSON_GetObjectItem(root, ""))){ + printf( "-->%d\r\n", item->valueint); + } + if(( item =cJSON_GetObjectItem(root, ""))){ + printf( "-->%d\r\n", item->valueint); + } + if(( item =cJSON_GetObjectItem(root, ""))){ + printf( "-->%d\r\n", item->valueint); + } +#endif + cJSON_Delete( root ); + }else{ + printf( "parser json data fail!\r\n" ); + } +} + void* tuya_task( void * unused ) { char *msg; @@ -177,6 +248,8 @@ void* tuya_task( void * unused ) tuya_mqtt_context_t* client = &client_instance; + iot_key_load( &productId, &deviceId, &deviceSecret ); + ret = tuya_mqtt_init(client, &(const tuya_mqtt_config_t) { .host = "m1.tuyacn.com", .port = 8883, diff --git a/app/src/iot_devinfo.c b/app/src/iot_devinfo.c new file mode 100644 index 0000000..f8e12e3 --- /dev/null +++ b/app/src/iot_devinfo.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include + +#include "dp.h" +#include "iot_devinfo.h" + + +//enum{ +// DP_KEY_LED_MAX, +// DP_ATMOS_LED_MAX, +//}; + +typedef struct{ + uint32_t key_led_max; + uint32_t atmos_led_max; +} iot_dp_t; + +static iot_dp_t g_iot_dp = { 100, 100, }; + +extern void mg_led_brigtness_change_notify( void ); +int32_t iot_dp_int_read( uint32_t dp_id, uint32_t *out_value ); +int32_t iot_dp_int_write( uint32_t dp_id, uint32_t value ); + +int32_t iot_dp_int_write( uint32_t dp_id, uint32_t value ) +{ + printf( "dp write int , id:%d, value:%d\r\n", dp_id, value ); + if( dp_id == DP_KEY_LED_MAX ){ + g_iot_dp.key_led_max = value; + //notify led + mg_led_brigtness_change_notify(); + return 0; + }else if( dp_id == DP_ATMOS_LED_MAX ){ + g_iot_dp.atmos_led_max = value; + //notify led + mg_led_brigtness_change_notify(); + return 0; + } + return -1; +} + +int32_t iot_dp_int_read( uint32_t dp_id, uint32_t *out_value ) +{ + if( dp_id == DP_KEY_LED_MAX ){ + *out_value = g_iot_dp.key_led_max; + }else if( dp_id == DP_ATMOS_LED_MAX ){ + *out_value = g_iot_dp.atmos_led_max; + } + return 0; +} + +extern void mg_key_led_max_set( uint32_t brightness ); +extern void mg_atmos_led_max_set( uint32_t brightness ); + +void mg_led_brigtness_change_notify( void ) +{ + //send msg + uint32_t key_led_max; + uint32_t atmos_led_max; + + if( iot_dp_int_read( DP_KEY_LED_MAX, &key_led_max ) == 0 ){ + mg_key_led_max_set( key_led_max ); + } + if( iot_dp_int_read( DP_ATMOS_LED_MAX, &atmos_led_max ) == 0 ){ + mg_atmos_led_max_set( atmos_led_max ); + } + + + return; +} + diff --git a/app/src/iot_key.c b/app/src/iot_key.c new file mode 100644 index 0000000..4c3f39c --- /dev/null +++ b/app/src/iot_key.c @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include + +#include "cJSON.h" +//#include "tuya_cacert.h" +#include "tuya_log.h" +#include "tuya_error_code.h" +#include "system_interface.h" +#include "mqtt_client_interface.h" +#include "tuyalink_core.h" + +#include "cQueue.h" +#include "dp.h" + +#define AUTH_KEY_PATH "/customer/tuya_auth" +//#define AUTH_KEY_PATH "./customer/tuya_auth" + +static const char def_productID[] = "3xeyrwyvbuyajz4a"; +static const char def_deviceID[] = "26dfc49b45e25a93dcwgzg"; +static const char def_deviceSecret[] = "IAS0hHa3mmgrKn9L"; + +static char productID[50]; +static char deviceID[50]; +static char deviceSecret[50]; + + +int32_t iot_key_load( char ** out_productID, char ** out_deviceID, char ** out_deviceSecret ) +{ + FILE *fd; + char json_data[500]; + size_t n; + cJSON *root; + cJSON *di; + cJSON *pi; + cJSON *ds; + printf( "iot load key...\r\n" ); + //1. open file + fd = fopen( AUTH_KEY_PATH, "r" ); + if( !fd ){ + printf( "open auty key file[%s] fail!\r\n", AUTH_KEY_PATH ); + goto err_out; + } + //2. read json data + n = fread( json_data, 1, sizeof(json_data)-1, fd ); + if( n == 0 ){ + printf( "read auty key file fail!\r\n" ); + goto err_read; + } + json_data[n] = 0; + printf( "get json data[%d]:%s\r\n", n, json_data ); + + //3. parse json + root = cJSON_Parse((const char *)json_data); + if( !root ){ + printf( "parser json data fail!\r\n" ); + goto err_json; + } + + di =cJSON_GetObjectItem(root, "deviceID"); + pi =cJSON_GetObjectItem(root, "productID"); + ds =cJSON_GetObjectItem(root, "deviceSecret"); + if( !di){ + printf( "get devieID:fail\r\n" ); + goto err_json; + } + printf( "get devieID:%s\r\n", di->valuestring ); + if( !pi){ + printf( "get productID:fail\r\n" ); + goto err_json; + } + printf( "get productID:%s\r\n", pi->valuestring ); + if( !ds){ + printf( "get device secret:fail\r\n" ); + goto err_json; + } + printf( "get deviceSecret:%s\r\n", ds->valuestring ); + + strcpy( deviceID, di->valuestring ); + strcpy( productID, pi->valuestring ); + strcpy( deviceSecret, ds->valuestring ); + + if( out_productID ) + *out_productID = (char*)productID; + if( out_deviceID ) + *out_deviceID = (char*)deviceID; + if( out_deviceSecret ) + *out_deviceSecret = (char*)deviceSecret; + + cJSON_Delete( root ); + return 0; + +err_json: + if( root ){ + cJSON_Delete( root ); + } + +err_read: + if( fd ) + fclose(fd); +err_out: + if( out_productID ) + *out_productID = (char*)def_productID; + if( out_deviceID ) + *out_deviceID = (char*)def_deviceID; + if( out_deviceSecret ) + *out_deviceSecret = (char*)def_deviceSecret; + return -1; + +} + diff --git a/app/src/main.c b/app/src/main.c index d98816c..444907d 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -9,6 +9,9 @@ #include "led.h" #include "dp.h" +static mg_button_t *button; +static mg_led_t *led; + extern int dp_demo( void ); @@ -17,30 +20,40 @@ extern int32_t dp_button_event_push( button_code_t btn_code, button_event_t btn_ void button_single_clicked_cb( button_code_t btn_code, button_event_t btn_event, void* value ) { uint32_t dp_btn_code = 0; + led_id_t led_id = LED_NR; + switch( btn_code ){ case BUTTON_1: dp_btn_code = DP_BTN_1; + led_id = KEY_LED_1; break; case BUTTON_2: dp_btn_code = DP_BTN_2; + led_id = KEY_LED_2; break; case BUTTON_3: dp_btn_code = DP_BTN_3; + led_id = KEY_LED_3; break; case BUTTON_4: dp_btn_code = DP_BTN_4; + led_id = KEY_LED_4; break; case BUTTON_5: dp_btn_code = DP_BTN_5; + led_id = KEY_LED_5; break; case BUTTON_6: dp_btn_code = DP_BTN_6; + led_id = KEY_LED_6; break; case BUTTON_7: dp_btn_code = DP_BTN_7; + led_id = FUN_LED_1; break; case BUTTON_8: dp_btn_code = DP_BTN_8; + led_id = FUN_LED_2; break; case BUTTON_7|BUTTON_8: break; @@ -48,7 +61,10 @@ void button_single_clicked_cb( button_code_t btn_code, button_event_t btn_event, if( btn_event == EVENT_SINGLE_CLICK ){ printf( "key KEY_%d Released!\n", dp_btn_code-DP_BTN_1+1 ); dp_button_event_push( dp_btn_code, DP_SINGLE_CLICK ); - //TODO: add led ctrl + if( led_id < LED_NR ){ + //KEYLED fast flash 1s. + mg_led_set( led, led_id, KEYLED_FLASH_FAST, 1000, NULL ); + } } return; } @@ -57,20 +73,22 @@ void tuya_start( void ); int main( void ) { - mg_button_t *button; - printf( "\n-----------------------------------------\n"); button = mg_button_open("/dev/input/by-path/platform-gpio_keys-event" ); if( button ){ printf( "register cb, button:%p, cb:%p\n", button, button_single_clicked_cb ); button_single_clicked_register( button, button_single_clicked_cb ); } - printf( "\n-----------------------------------------\n"); - led_open(); - printf( "\n-----------------------------------------\n"); + + led = mg_led_open(); + if( led ){ + mg_led_set( led, FUN_LED_1, KEYLED_FLASH_FAST, 3000, NULL ); + mg_led_set( led, FUN_LED_2, KEYLED_FLASH_SLOW_1, 3000, NULL ); + mg_led_set( led, FUN_LED_3, KEYLED_FLASH_SLOW_2, 3000, NULL ); + } + printf( "\ntuya sdk start...\n" ); tuya_start(); - printf( "\n-----------------------------------------\n"); while(1){ sleep(1000); } diff --git a/led/inc/led.h b/led/inc/led.h index 2aaabdd..f68c959 100644 --- a/led/inc/led.h +++ b/led/inc/led.h @@ -1,9 +1,51 @@ #ifndef __LED__H__ #define __LED__H__ +typedef enum{ + KEY_LED_1 = 0, + KEY_LED_2, + KEY_LED_3, + KEY_LED_4, + KEY_LED_5, + KEY_LED_6, + KEY_LED_7, + KEY_LED_8, + FUN_LED_1, + FUN_LED_2, + FUN_LED_3, + FUN_LED_4, + FUN_LED_5, + RING_LED, + IR_LED, + LED_NR +}led_id_t; + +typedef enum{ + LED_OFF=0, //关闭 + LED_ON=1, //常亮 + LED_FLASH_1HZ, + LED_FLASH_2HZ, + LED_FLASH_8HZ, + LED_FLASH_XHZ, + + LED_GRAUAL_ON, //渐变到DP设定亮度后常亮 + LED_GRAUAL_MAX_ON, //渐变到最大亮度后常亮 + LED_GRADUAL_OFF, //渐变到关闭 + + KEYLED_FLASH_FAST = LED_FLASH_8HZ, //快闪 8HZ + KEYLED_FLASH_SLOW_1 = LED_FLASH_1HZ, //慢闪 1HZ + KEYLED_FLASH_SLOW_2 = LED_FLASH_2HZ, //慢闪 2HZ + LED_BREATH_XHZ //呼吸指示模式 +}led_mode_t; + +struct mg_led; +typedef struct mg_led mg_led_t; + +mg_led_t * mg_led_open( void ); +void mg_led_close( mg_led_t * btn_ctrl ); +int32_t mg_led_set( mg_led_t *led_ctrl, led_id_t id, led_mode_t mode, uint32_t keep_ms, void *breath ); +int32_t mg_led_get( mg_led_t *led_ctrl, led_id_t id, led_mode_t * mode, uint32_t *brightness ); -int32_t led_open( void ); -int32_t led_close( uint32_t led ); -int32_t led_set( uint32_t led, uint32_t mode ); #endif //__LED__H__ + diff --git a/led/src/led.c b/led/src/led.c index a101d39..ac0b9f0 100644 --- a/led/src/led.c +++ b/led/src/led.c @@ -2,24 +2,310 @@ #include #include #include - +#include +#include #include "led.h" -int32_t led_open( void ) + +typedef void (*timer_handle_t)( int32_t ); + +typedef struct { + led_id_t id; + const char* sys_path; + led_mode_t mode; + uint32_t to_stop_ms; + timer_t timerid; +}led_x_t; + +struct mg_led{ + uint32_t init; + + uint32_t dp_key_led_max; + uint32_t max_key_led_max; + uint32_t dp_atmos_led_max; + uint32_t max_atmos_led_max; + + led_x_t led_x[LED_NR]; +}; + +static void led_timer_cb( union sigval v ); + +static mg_led_t g_led={ + .init = 0, + .dp_key_led_max = 100, + .max_key_led_max = 100, + .dp_atmos_led_max = 100, + .max_atmos_led_max = 100, + + .led_x = { + { KEY_LED_1, "/sys/class/leds/key_led1", LED_OFF, }, + { KEY_LED_2, "/sys/class/leds/key_led2", LED_OFF, }, + { KEY_LED_3, "/sys/class/leds/key_led3", LED_OFF, }, + { KEY_LED_4, "/sys/class/leds/key_led4", LED_OFF, }, + { KEY_LED_5, "/sys/class/leds/key_led5", LED_OFF, }, + { KEY_LED_6, "/sys/class/leds/key_led6", LED_OFF, }, + { KEY_LED_7, "/sys/class/leds/key_led7", LED_OFF, }, + { KEY_LED_8, "/sys/class/leds/key_led8", LED_OFF, }, + { FUN_LED_1, "/sys/class/leds/fun_led1", LED_OFF, }, + { FUN_LED_2, "/sys/class/leds/fun_led2", LED_OFF, }, + { FUN_LED_3, "/sys/class/leds/fun_led3", LED_OFF, }, + { FUN_LED_4, "/sys/class/leds/fun_led4", LED_OFF, }, + { FUN_LED_5, "/sys/class/leds/fun_led5", LED_OFF, }, + { RING_LED, "/sys/class/leds/ring_led", LED_OFF, }, + { IR_LED, "/sys/class/leds/ir_led", LED_OFF, }, + }, +}; + +#if 0 +void led_timer_start( timer_t *timer ) { - printf ( "tw8013 led open!\n" ); - return 0; - //todo: start kbd task; + timer_t timerid; + struct sigevent evp; + struct sigaction act; + + memset(&act, 0, sizeof(act)); + act.sa_handler = led_timer_cb; + act.sa_flags = 0; + + sigemptyset(&act.sa_mask); + + sigaction(SIGUSR1, &act, NULL); + + memset(&evp, 0, sizeof(struct sigevent)); //清零初始化 + evp.sigev_value.sival_int = 111; //标识定时器的 + evp.sigev_signo = SIGUSR1; + evp.sigev_notify = SIGEV_SIGNAL; //产生信号 + + if (timer_create(CLOCK_REALTIME, &evp, &led->timerid) == -1) + { + perror("fail to timer_create"); + return; + } + + struct itimerspec it; + it.it_interval.tv_sec = ms/1000; //间隔s + it.it_interval.tv_nsec = (ms%1000)*1000*1000; //间隔ns + it.it_value.tv_sec = ms/1000; + it.it_value.tv_nsec = (ms%1000)*1000*1000; + if (timer_settime(led->timerid, 0, &it, NULL) == -1) + { + perror("fail to timer_settime"); + return; + } +} +#endif + +void led_timer_cb( union sigval v ) +{ + printf( "[%ld]timer cb sig v:%d\r\n", time( NULL ), v.sival_int ); + mg_led_set( &g_led, v.sival_int, LED_OFF, 0, 0 ); +} + +static void mg_led_x_init( mg_led_t *led, led_id_t id ) +{ + led_x_t *ledx; + struct sigevent evp; + + ledx = &led->led_x[id]; + //timer_create + + memset(&evp, 0, sizeof(struct sigevent)); //清零初始化 + evp.sigev_value.sival_int = id; //标识定时器的 + evp.sigev_notify = SIGEV_THREAD; + evp.sigev_notify_function = led_timer_cb; + if (timer_create(CLOCK_REALTIME, &evp, &ledx->timerid) == -1){ + printf( " led[%d] create timer fail!\r\n", id ); + }else{ + printf( " led[%d] create timer OK, id:%d!\r\n", id, ledx->timerid ); + } + + return; +} + +static void mg_led_init( mg_led_t *led ) +{ + for( int i=0; iinit = 1; +} + +mg_led_t * mg_led_open( void ) +{ + printf ( "tw8013 led open...\n" ); + if( !g_led.init ) + mg_led_init( &g_led ); + + return &g_led; } -int32_t led_close( uint32_t kbd ) +void led_close( mg_led_t *led ) { printf ( "tw8013 led close!\n" ); - return 0; + return; } -int32_t led_set( uint32_t led, uint32_t mode ) +void led_on( led_x_t *ledx, uint32_t brightness ) { - printf ( "tw8013 led set!\n" ); + char system_cmd[100]; + + brightness = brightness*255/100; + + sprintf( system_cmd, "echo none > %s/trigger", ledx->sys_path ); + printf( "system cmd [%s]\r\n", system_cmd ); + if( system( system_cmd ) ){ + } + + sprintf( system_cmd, "echo %d > %s/brightness", brightness, ledx->sys_path ); + printf( "system cmd [%s]\r\n", system_cmd ); + if( system( system_cmd ) ){ + } +} + +void led_flash( led_x_t *ledx, uint32_t brightness, uint32_t on_ms, uint32_t off_ms ) +{ + char system_cmd[200]; + + brightness = brightness*255/100; + + sprintf( system_cmd, "echo timer > %s/trigger", ledx->sys_path ); + printf( "system cmd [%s]\r\n", system_cmd ); + if( system( system_cmd )){ + } + + sprintf( system_cmd, "echo %d > %s/delay_on", on_ms, ledx->sys_path); + printf( "system cmd [%s]\r\n", system_cmd ); + if( system( system_cmd )){ + } + + sprintf( system_cmd, "echo %d > %s/delay_off", off_ms, ledx->sys_path); + printf( "system cmd [%s]\r\n", system_cmd ); + if( system( system_cmd )){ + } + + sprintf( system_cmd, "echo %d > %s/brightness", brightness, ledx->sys_path ); + printf( "system cmd [%s]\r\n", system_cmd ); + if( system( system_cmd )){ + } +} + +void mg_led_x_update( led_x_t *ledx ) +{ + uint32_t brightness; + if( ledx->id >= KEY_LED_1 && ledx->id <= KEY_LED_8 ){ + brightness = g_led.dp_key_led_max; + }else if( ledx->id >= FUN_LED_1 && ledx->id <= FUN_LED_5 ){ + brightness = g_led.dp_atmos_led_max; + }else{ + brightness = 100; + } + + switch( ledx->mode ){ + case LED_OFF: + led_on( ledx, 0 ); + break; + case LED_ON: + led_on( ledx, brightness ); + break; + case LED_FLASH_1HZ: + led_flash( ledx, brightness, 500, 500 ); + break; + case LED_FLASH_2HZ: + led_flash( ledx, brightness, 1000, 1000 ); + break; + case LED_FLASH_8HZ: + led_flash( ledx, brightness, 62, 63 ); + break; + } + + return; +} + +void mg_led_x_update_brightness( led_x_t *ledx ) +{ + char system_cmd[100]; + uint32_t brightness; + + if( ledx->id >= KEY_LED_1 && ledx->id <= KEY_LED_8 ){ + brightness = g_led.dp_key_led_max; + }else if( ledx->id >= FUN_LED_1 && ledx->id <= FUN_LED_5 ){ + brightness = g_led.dp_atmos_led_max; + }else{ + brightness = 100; + } + + brightness = brightness*255/100; + + sprintf( system_cmd, "echo %d > %s/brightness", brightness, ledx->sys_path ); + printf( "system cmd [%s]\r\n", system_cmd ); + if( system( system_cmd ) ){ + } + + return; +} + +int32_t mg_led_set( mg_led_t *led_ctrl, led_id_t id, led_mode_t mode, uint32_t keep_ms, void *breath ) +{ + led_x_t *led; + + led = &led_ctrl->led_x[id]; + + led->mode = mode; + + if( keep_ms ){ + //start timer... + struct itimerspec it; + + printf( " LED[%d] start timer, timerid:[%d] -- [%d]ms\r\n", led->timerid, id, keep_ms ); + + it.it_interval.tv_sec = 0; + it.it_interval.tv_nsec = 0; + it.it_value.tv_sec = keep_ms/1000; + it.it_value.tv_nsec = 1000*1000 *(keep_ms%1000); + + //led_create_timer( &led->timerid, + if (timer_settime( led->timerid, 0, &it, NULL) == -1){ + printf( "led[%d] start timer fail!\r\n", id ); + }else{ + printf( "led[%d] timer start\r\n", id ); + } + } + mg_led_x_update( led ); + return 0; + } + +int32_t mg_led_get( mg_led_t *led_ctrl, led_id_t id, led_mode_t * mode, uint32_t *brightness ) +{ + printf ( "tw8013 led get!\n" ); + return 0; +} + +void mg_key_led_max_set( uint32_t brightness ) +{ + if( g_led.dp_key_led_max != brightness ){ + g_led.dp_key_led_max = brightness; + + for( int i=KEY_LED_1; i<=KEY_LED_8; i++ ){ + if( g_led.led_x[i].mode != LED_OFF ){ + mg_led_x_update_brightness( &g_led.led_x[i] ); + } + } + + } } + +void mg_atmos_led_max_set( uint32_t brightness ) +{ + if( g_led.dp_atmos_led_max != brightness ){ + g_led.dp_atmos_led_max = brightness; + + for( int i=FUN_LED_1; i<=FUN_LED_5; i++ ){ + if( g_led.led_x[i].mode != LED_OFF ){ + mg_led_x_update_brightness( &g_led.led_x[i] ); + } + } + + } +} + -- 2.34.1