void iot_start(void);
+uint32_t dp_key_led_br_read( void );
+uint32_t dp_fun_led_br_read( void );
+uint32_t dp_bl_mode_read( void );
+uint32_t dp_tof_threshold_read( void );
+
#endif //__MG_IOT_H__
static cQueue_t *btn_msg_q;
static void button_single_clicked_cb( button_code_t btn_code, button_event_t btn_event, void* value );
+static void button_comb_cb( button_code_t btn_code, button_event_t btn_event, void* value );
typedef struct{
uint32_t code;
return -1;
}
-void btn_init( void )
+void device_button_init( void )
{
+ MG_LOGE( "btn init" );
pthread_mutex_init( &btn_lock, NULL );
btn_msg_q = cQcreate( 100 );
- btn_ctrl = mg_button_open("/dev/input/by-path/platform-gpio_keys-event" );
+ btn_ctrl = mg_button_open(NULL); //"/dev/input/by-path/platform-gpio_keys-event" );
if( btn_ctrl ){
- MG_LOGD( "register btn event cb\n" );
- button_single_clicked_register( btn_ctrl, button_single_clicked_cb );
+ mg_button_event_register( btn_ctrl, 0xFF, EVENT_SINGLE_CLICK|EVENT_DOUBLE_CLICK|EVENT_LONG_PRESS|EVENT_LONG_PRESS_UP, button_single_clicked_cb );
+ mg_button_event_combonation_register( btn_ctrl, BUTTON_7|BUTTON_8, EVENT_COMB_PRESS_5S, 0, button_comb_cb );
+ mg_button_event_combonation_register( btn_ctrl, BUTTON_1|BUTTON_8, EVENT_COMB_PRESS_2S, 0, button_comb_cb );
+ mg_button_event_combonation_register( btn_ctrl, BUTTON_2|BUTTON_7, EVENT_COMB_PRESS_2S, 0, button_comb_cb );
#if 0
//for debug
- mg_button_event_register( btn_ctrl, 0x0f, 0x0f, (button_event_cb_t *)0x11111111 );
- printf( "register -- 1F -- 0F -- 11111111\n" );
- mg_button_event_register( btn_ctrl, 0x20, 0x0f, (button_event_cb_t *)0x22222222 );
- printf( "register -- 20 -- 0F -- 22222222\n" );
- mg_button_event_register( btn_ctrl, 0x10, 0x0f, (button_event_cb_t *)0x33333333 );
- printf( "register -- 10 -- 0F -- 33333333\n" );
- mg_button_event_combonation_register( btn_ctrl, 0x03, 0x10, 0, (button_event_cb_t *)0x88888888 );
- printf( "register combo -- 03 -- 10 -- 88888888\n" );
+ mg_button_event_register( btn_ctrl, 0xff, 0x0f, button_single_clicked_cb);
+ mg_button_event_combonation_register( btn_ctrl, 0x03, EVENT_COMB_PRESS, 0, button_comb_cb );
- printf( "find 0x01 01 cb:%p\n", find_single_cb( btn_ctrl, 1, 1 ) );
- printf( "find 0x02 03 cb:%p\n", find_single_cb( btn_ctrl, 2, 3 ) );
printf( "find 0x80 0f cb:%p\n", find_single_cb( btn_ctrl, 0x80, 0xf ) );
- printf( "find combo 0x03 10 cb:%p\n", find_comb_cb( btn_ctrl, 3, 0x10 ) );
- printf( "find combo 0x03 00 cb:%p\n", find_comb_cb( btn_ctrl, 3, 0x00 ) );
+ printf( "find combo 0x03 10 cb:%p\n", find_comb_cb( btn_ctrl, 0x03, 0 ) );
+ printf( "find combo 0xc0 10 cb:%p\n", find_comb_cb( btn_ctrl, 0xc0, 0 ) );
+ printf( "find combo 0x30 10 cb:%p\n", find_comb_cb( btn_ctrl, 0x30, 0 ) );
#endif
//
+ }else{
+ MG_LOGE( "mg_button_open fail\n" );
}
}
extern int32_t dp_button_event_push( button_code_t btn_code, button_event_t btn_event );
+
static void button_single_clicked_cb( button_code_t btn_code, button_event_t btn_event, void* value )
{
- uint32_t dp_btn_code = 0;
btn_msg_t msg;
-// led_id_t led_id = LED_NR;
msg.code = btn_code;
msg.ev = btn_event;
msg.value = (uint32_t)value;
btn_msg_tx( &msg );
-#if 0
- switch( btn_code ){
- case BUTTON_1:
- dp_btn_code = DP_BTN_1;
-// led_id = KEY_LED_1;
- break;
- case BUTTON_8:
- dp_btn_code = DP_BTN_8;
-// led_id = FUN_LED_2;
- break;
- case BUTTON_7|BUTTON_8:
- break;
- }
- 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 );
- //if( led_id < LED_NR ){
- //KEYLED fast flash 1s.
-// mg_led_set( led, led_id, KEYLED_FLASH_FAST, 1000, NULL );
- //}
- }else if( btn_event == EVENT_DOUBLE_CLICK ){
-
- }
-#endif
- return;
}
-int32_t is_btn_wifi_config( btn_msg_t *msg )
+static void button_comb_cb( button_code_t btn_code, button_event_t btn_event, void* value )
{
- if( msg->ev == EVENT_SINGLE_CLICK && msg->code == BUTTON_3 )
- return 1; //for debug
+ btn_msg_t msg;
+ msg.code = btn_code;
+ msg.ev = btn_event;
+ msg.value = (uint32_t)value;
+ btn_msg_tx( &msg );
+ printf( "COMB BTN cb--code:%x, event:%x, value:%p\n", btn_code, btn_event, value );
+}
- if( msg->ev == EVENT_COMB_PRESS && msg->code == BUTTON_7|BUTTON_8 )
+int32_t is_btn_wifi_config( btn_msg_t *msg )
+{
+ if( msg->ev == EVENT_COMB_PRESS_5S && msg->code == BUTTON_7|BUTTON_8 )
return 1;
return 0;
}
int32_t is_btn_device_clean( btn_msg_t *msg )
{
- if( msg->ev == EVENT_SINGLE_CLICK && msg->code == BUTTON_7 )
- return 1; //for debug
-
- if( msg->ev == EVENT_COMB_PRESS && msg->code == BUTTON_1|BUTTON_8 )
+ if( msg->ev == EVENT_COMB_PRESS_2S && msg->code == BUTTON_1|BUTTON_8 )
return 1;
return 0;
}
int32_t is_btn_device_clean_exit( btn_msg_t *msg )
{
- if( msg->ev == EVENT_SINGLE_CLICK && msg->code == BUTTON_8 )
- return 1; //for debug
-
- if( msg->ev == EVENT_COMB_PRESS && msg->code == BUTTON_2|BUTTON_7 )
+ if( msg->ev == EVENT_COMB_PRESS_2S && msg->code == BUTTON_2|BUTTON_7 )
return 1;
return 0;
}
#include "cQueue.h"
#include "device.h"
+#include "tof.h"
static mg_dev_t g_mg_dev;
+mg_tof_t *g_tof;
#if 0
extern int32_t is_btn_wifi_config( btn_msg_t *msg );
extern void device_light_init( void );
-extern void btn_init( void );
+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 );
extern int32_t is_btn_device_clean( btn_msg_t *msg );
extern int32_t is_btn_device_clean_exit( btn_msg_t *msg );
-extern uint32_t dp_bl_mode_read( void );
uint32_t device_clean_mode = 0;
uint32_t device_wifi_config = 0;
//timeout = 100ms;
MG_LOGI( "btn light ctrl task run..." );
+
while(1){
usleep(100*1000);
//读取按键信息
g_mg_dev.clean_device = 1;
g_mg_dev.clean_timeout = time(NULL)+30;
- }else if( !g_mg_dev.clean_device && to_clean_mode_exit ){
+ }else if( g_mg_dev.clean_device && to_clean_mode_exit ){
MG_LOGI( "exit clean mode ..." );
g_mg_dev.clean_device = 0;
g_mg_dev.clean_timeout = 0;
//ligth_mode_set( LIGHT_MODE_ACTIVE ); //TODO
+ }else if( !g_mg_dev.clean_device && !g_mg_dev.wifi_config && to_wifi_config ){
+ MG_LOGI( "enter wifi config mode ..." );
+ g_mg_dev.wifi_config = 1;
+ g_mg_dev.wifi_config_timeout = time(NULL)+2*60; //2minutes timeout
}
- if( !g_mg_dev.clean_device && !g_mg_dev.wifi_config && to_wifi_config ){
- g_mg_dev.wifi_config = 1;
- g_mg_dev.wifi_config_timeout = time(NULL)+2*60; //2minutes timeout
- }
-
- if( !g_mg_dev.clean_device ){
+ if( !g_mg_dev.clean_device && !g_mg_dev.wifi_config ){
//获取单击按键的键值
if((btn_code = get_single_click_code( &btn_msg ))){
MG_LOGD( "key led flash fast, btn_code:%04X\n", btn_code );
led_flash_for_btn_single_click( btn_code );
dp_report_button_event( btn_code, btn_msg.ev );
- if( btn_code & BUTTON_6 ){
- dbg_tof_active = !dbg_tof_active;
- MG_LOGD( "btn_6 -> tof statu: %d", dbg_tof_active );
- }
}else if((btn_code = get_double_click_code( &btn_msg ))){
dp_report_button_event( btn_code, btn_msg.ev );
}else if((btn_code = get_long_press_code( &btn_msg ))){
dp_report_button_event( btn_code, btn_msg.ev );
}else if((btn_code = get_long_press_up_code( &btn_msg ))){
dp_report_button_event( btn_code, btn_msg.ev );
+ if( btn_code & BUTTON_6 ){
+ dbg_tof_active = !dbg_tof_active;
+ MG_LOGD( "btn_6 -> tof statu: %d", dbg_tof_active );
+ }
}
}
}
if( time(NULL) > g_mg_dev.clean_timeout ){
g_mg_dev.clean_device = 0;
g_mg_dev.clean_timeout = 0;
- //light_enter_clean_mode();
+ g_mg_dev.wifi_config= 0;
+ g_mg_dev.wifi_config_timeout = 0;
}
}else if( g_mg_dev.wifi_config){
if( time(NULL) > g_mg_dev.wifi_config_timeout ){
g_mg_dev.wifi_config= 0;
g_mg_dev.wifi_config_timeout = 0;
- //light_enter_clean_mode();
}
}
g_mg_dev.bl_manual = bl_manual;
if( device_bl_mode_manual ){
//TOF_Disable...
+ MG_LOGI( "disable tof ..." );
+ mg_tof_disable(g_tof);
}else{
//TOF_Enable...
+ MG_LOGI( "enable tof ..." );
+ mg_tof_enable(g_tof );
}
}
// led_key_update brightness
}else{
//自动模式
- //read tof_status();
- //if( st change ){
- // dp_report();
- //
- //}
}
}else if( g_mg_dev.mode == DEV_MODE_ACTIVE ){
if( g_mg_dev.bl_manual ){
- light_mode_set( LIGHT_MODE_ACTIVE );
+ uint32_t key_led_br;
+ uint32_t fun_led_br;
+
+ key_led_br = dp_key_led_br_read();
+ fun_led_br = dp_fun_led_br_read();
+
//手动模式
- //get key led brightness
- //get atmos led brightness
- //if( key_led_brightness change )
- // key_led_update brightness
- //if( atoms_led_brightness change )
- // atoms_led_update brightness
- // led_key_update brightness
+ 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 );
+
+
+
}else{
- //自动模式
- //read tof_status();
- //if( st change ){
- // dp_report();
- //
- //}
- if( dbg_tof_active ){
- light_mode_set( LIGHT_MODE_TOF_ACTIVE );
- }else{
- light_mode_set( LIGHT_MODE_TOF_DEACTIVE );
+ static uint32_t tof_dist = 1000;
+ if( tof_dist != dp_tof_threshold_read() ){
+ MG_LOGI( "tof dist change %d -> %d", tof_dist, dp_tof_threshold_read() );
+ tof_dist = dp_tof_threshold_read();
+ Distance_parameter_correction(g_tof, tof_dist);
}
- }
- }
+ //自动模式
+ static time_t tof_check_time =0;
+ if( tof_check_time < time(NULL ) ){
+ tof_check_time = time(NULL) + 5; //暂时按5秒检查一次
+ tof_info_t info;
+ memset( &info, 0, sizeof( info ) );
+
+ if( g_tof && mg_tof_read( g_tof, &info )==0 ){
+ MG_LOGD( "read tof info, enable:%d, status:%d, dist:%d", info.enable, info.status, info.distance );
+ }else{
+ MG_LOGW( "read tof info fail, g_tof:%p", g_tof );
+ }
-#if 0
- //背光模式检查
- bg_auto = get_bg_mode()
-
- if( device.bg_auto != bg_auto ){
- //背光模式发生改变,
- MG_LOGI( "bg mode %d --> %d\n", device.bg_auto, bg_auto );
- device.bg_auto = bg_auto;
- if( bg_auto = 0 ){
- bg_init( "auto" );
- tof_enable();
- }else{
- bg_init( "manual" );
- tof_close();
- }
- }
-
- if( bg_auto == 1 ){
- //自动模式处理
- tof_st = get_tof_st();
- if( tof_change ){
- dp_report()
- if( tof_inactive ){
- key_led_off;
- fun_led_on;
- }else{
- key_led_on;
- fun_led_off;
+ if( info.enable && info.status == TOF_ANYBODY ){
+ light_mode_set( LIGHT_MODE_TOF_ACTIVE );
+ //TODO:dp_report tof mode;
+ }else{
+ light_mode_set( LIGHT_MODE_TOF_DEACTIVE );
+ }
}
}
- }else{
- //手动模式处理
}
-#endif
-
}
return 0;
}
static pthread_t th;
+static void tof_cb( tof_info_t *info)
+{
+ MG_LOGI( "tof ec cb - tofinfo[ enable:%d, status:%d, dist:%d]", info->enable, info->status, info->distance );
+}
+
void btn_light_ctrl_init( void )
{
g_mg_dev.mode = DEV_MODE_ACTIVE;
g_mg_dev.clean_device = 0;
g_mg_dev.clean_timeout = 0;
- btn_init();
+ device_button_init();
device_light_init();
+ g_tof = mg_tof_open( 1000 ); //1000mm
+ if( g_tof ){
+ mg_tof_event_register( g_tof, tof_cb );
+ }else{
+ MG_LOGE( "open tof fail!" );
+ }
}
void btn_light_ctrl_start( void )
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 );
+ //MG_LOGI( "device light init, g_led_ctrl:%p", g_led_ctrl );
if( g_led_ctrl ){
- light_mode_set( LIGHT_MODE_CLEAN );
+ light_mode_set( LIGHT_MODE_INIT );
+ }else{
+ MG_LOGE( "mg_led_open fail!" );
}
}
+char *light_mode_name[]={
+ "led_off",
+ "led_on",
+ "led_flash_1hz",
+ "led_flash_2hz",
+ "led_flash_8hz",
+ "led_flash",
+ "led_gradual_on",
+ "led_gradual_off",
+ "led_breath",
+ "led_nochange",
+};
+
void light_mode_set( light_mode_t mode )
{
led_cfg_t cfg;
memset( &cfg, 0, sizeof( cfg ));
- MG_LOGI( "light mode %d -> %d", g_lightinfo.mode, mode );
+ 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: //
+ 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 );
break;
case LIGHT_MODE_CLEAN: //clean mode en
mg_led_mode_set( g_led_ctrl, LED_ID_KEY_ALL|LED_ID_FUN_ALL, &cfg );
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 );
break;
case LIGHT_MODE_TOF_ACTIVE: //
g_lightinfo.ring_led_max = br;
mg_led_brightness_set( g_led_ctrl, LED_ID_RING, br );
}
- MG_LOGI( "light [%04X] -> %d", id, br );
+ //MG_LOGI( "light [%04X] -> %d", id, br );
}
void led_flash_for_btn_single_click( button_code_t btn_code )
g_iot_dp.bl_mode = value;
//mg_bl_change_notify();
break;
+ case DP_MG_TOF_THRESHOLD:
+ g_iot_dp.mg_tof_threshold = value;
+ //mg_bl_change_notify();
+ break;
}
return -1;
}
case DP_BL_MODE:
*out_value = g_iot_dp.bl_mode;
break;
+ case DP_MG_TOF_THRESHOLD:
+ *out_value = g_iot_dp.mg_tof_threshold;
+ break;
}
return 0;
}
}
if(( item =cJSON_GetObjectItem(root, "mg_tof_threshold"))){
//人感检测距离
- printf( "mg_tof_threshold-->%d\r\n", item->valueint);
+ iot_dp_int_write( DP_MG_TOF_THRESHOLD, item->valueint*10 );
+ printf( "mg_tof_threshold-->%d cm\r\n", item->valueint);
}
if(( item =cJSON_GetObjectItem(root, "mg_tof_interval"))){
//人感检测周期
return g_iot_dp.bl_mode;
}
+uint32_t dp_key_led_br_read( void )
+{
+ return g_iot_dp.key_led_max;
+}
+
+uint32_t dp_fun_led_br_read( void )
+{
+ return g_iot_dp.atmos_led_max;
+}
+
+uint32_t dp_tof_threshold_read( void )
+{
+ return g_iot_dp.mg_tof_threshold;
+}
+
#include "cQueue.h"
#include "iot.h"
-#define AUTH_KEY_PATH "/customer/tuya_auth"
-//#define AUTH_KEY_PATH "./customer/tuya_auth"
+#define IOT_KEY_PATH "/customer/tuya_key"
+#define AUTH_KEY_PATH "./customer/tuya_auth"
static const char def_productID[] = "3xeyrwyvbuyajz4a";
static const char def_deviceID[] = "26dfc49b45e25a93dcwgzg";
cJSON *ds;
printf( "iot load key...\r\n" );
//1. open file
- fd = fopen( AUTH_KEY_PATH, "r" );
+ fd = fopen( IOT_KEY_PATH, "r" );
if( !fd ){
printf( "open auty key file[%s] fail!\r\n", AUTH_KEY_PATH );
goto err_out;
btn_light_ctrl_start();
MG_LOGD( "iot start..");
- //iot_start();
+ iot_start();
while(1){
sleep(1000);
${PROJECT_SOURCE_DIR}/./
${PROJECT_SOURCE_DIR}/src/
${PROJECT_SOURCE_DIR}/inc/
+ ${PROJECT_SOURCE_DIR}/../log/inc/
${CMAKE_SYSROOT}/usr/include/
)
EVENT_SINGLE_CLICK = (1<<0),
EVENT_DOUBLE_CLICK = (1<<1),
EVENT_LONG_PRESS = (1<<2),
- EVENT_LONG_PRESS_UP = (1<<3),
- EVENT_COMB_PRESS = (1<<4),
+ EVENT_LONG_PRESS_2S = (1<<3),
+ EVENT_LONG_PRESS_5S = (1<<4),
+ EVENT_LONG_PRESS_UP = (1<<5),
+ EVENT_COMB_PRESS = (1<<6),
+ EVENT_COMB_PRESS_2S = (1<<7),
+ EVENT_COMB_PRESS_5S = (1<<8),
}button_event_t;
struct mg_button;
mg_button_t * mg_button_open( char * button_device_path );
void mg_button_close( mg_button_t * btn_ctrl );
-int32_t mg_button_event_register( mg_button_t *btn_ctrl, button_code_t code, button_event_t event, button_event_cb_t *cb );
-int32_t mg_button_event_combonation_register( mg_button_t *btn_ctrl, button_code_t code, button_event_t event, uint32_t time_ms, button_event_cb_t *cb );
+int32_t mg_button_event_register( mg_button_t *btn_ctrl, button_code_t code, button_event_t event, button_event_cb_t cb );
+int32_t mg_button_event_combonation_register( mg_button_t *btn_ctrl, button_code_t code, button_event_t event, uint32_t time_ms, button_event_cb_t cb );
-button_event_cb_t* find_single_cb( mg_button_t *btn_ctrl, uint32_t code, uint32_t event );
-button_event_cb_t* find_comb_cb( mg_button_t *btn_ctrl, uint32_t code, uint32_t event );
+button_event_cb_t find_single_cb( mg_button_t *btn_ctrl, uint32_t code, uint32_t event );
+button_event_cb_t find_comb_cb( mg_button_t *btn_ctrl, uint32_t code, uint32_t event );
//--------------------------------------------
//for test
void button_single_clicked_register( mg_button_t *btn, button_event_cb_t cb );
//#include <linux/input.h>
#include <pthread.h>
#include "button.h"
+#include "mg_log.h"
#define SINGLE_BTN_CB_MAX 50
#define COMB_BTN_CB_MAX 50
typedef struct{
uint32_t code;
uint32_t event;
- button_event_cb_t *cb;
+ button_event_cb_t cb;
} cb_item_t;
typedef struct{
mg_button_t * mg_button_open( char * button_device_path );
void mg_button_close( mg_button_t * btn_ctrl );
-
+extern void btn_scan( mg_button_t *btn_ctrl, uint32_t btn_pressed_code );
static button_event_cb_t g_test_cb = NULL;
-volatile int32_t stop = 0;
static char * button_device_def ="/dev/input/event2";
return 0;
}
+
void* button_task( void* param )
{
mg_button_t *p_button;
int i, rd;
fd_set rdfs;
int32_t fd;
+ int32_t retval;
+
+ struct timeval timeout;
+
+ uint32_t pressed_code = 0;
p_button = ( mg_button_t*)param;
if( !p_button ){
}
fd = p_button->fd;
- FD_ZERO(&rdfs);
- FD_SET(fd, &rdfs);
- while (!stop) {
- select(fd + 1, &rdfs, NULL, NULL, NULL);
- if (stop)
- break;
+ while (1) {
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 10*1000;
+ FD_ZERO(&rdfs);
+ FD_SET(fd, &rdfs);
+ retval = select(fd + 1, &rdfs, NULL, NULL, &timeout);
+ if( retval == 0 ){
+ //timeout call btn_scan
+ btn_scan( p_button, pressed_code );
+ continue;
+ }
+
rd = read(fd, ev, sizeof(ev));
if (rd < (int) sizeof(struct input_event)) {
}
for (i = 0; i < rd / sizeof(struct input_event); i++) {
- unsigned int type, code;
+ unsigned int type, code, value;
type = ev[i].type;
code = ev[i].code;
+ value = ev[i].value;
//printf("++++Event: time %ld.%06ld, type:%d", ev[i].input_event_sec, ev[i].input_event_usec, type );
- printf("++++Event: ype:%d", type );
+ ///printf("++++Event: ype:%d", type );
if( type==EV_KEY ){
+ if( value == 0 ){
+ pressed_code &= ~( 1<< (code - KEY_1 ) );
+ printf( "btn up...code:%x\n", pressed_code );
+ }else if( value == 1 ){
+ printf( "btn down...code:%x\n", pressed_code );
+ pressed_code |= ( 1<< (code - KEY_1 ) );
+ }
if(g_test_cb){
if( ev[i].value==0){
g_test_cb( button_code(code), EVENT_SINGLE_CLICK, 0 );
}
}
- printf( "EV_KEY : KEY_%d, value:%d\n", code - KEY_1 +1, ev[i].value );
- continue;
+ //printf( "EV_KEY : KEY_%d, value:%d\n", code - KEY_1 +1, ev[i].value );
+ continue;
+ }
}
- }
}
ioctl(fd, EVIOCGRAB, (void*)0);
printf ( "tw8013 button close!\n" );
}
-int32_t mg_button_event_register( mg_button_t *btn_ctrl, button_code_t code, button_event_t event, button_event_cb_t *cb )
+int32_t mg_button_event_register( mg_button_t *btn_ctrl, button_code_t code, button_event_t event, button_event_cb_t cb )
{
uint32_t found = 0;
uint32_t i;
btn_cb_list_t *p_list = &btn_ctrl->cb_list;
+ MG_LOGI( "regiser single btn, code:%x, event:%x" );
+
pthread_mutex_lock( &p_list->lock );
for( i=0; i < p_list->single_nr; i++ ){
if( (p_list->single_cb[i].code & code )==code
return 0;
}
-int32_t mg_button_event_combonation_register( mg_button_t *btn_ctrl, button_code_t code, button_event_t event, uint32_t time_ms, button_event_cb_t *cb )
+int32_t mg_button_event_combonation_register( mg_button_t *btn_ctrl, button_code_t code, button_event_t event, uint32_t time_ms, button_event_cb_t cb )
{
uint32_t found = 0;
uint32_t i;
int32_t retval = 0;
btn_cb_list_t *p_list = &btn_ctrl->cb_list;
+ MG_LOGI( "regiser comb btn, code:%x, event:%x" );
+
pthread_mutex_lock( &p_list->lock );
for( i=0; i < p_list->comb_nr; i++ ){
if( p_list->comb_cb[i].code ==code
return retval;
}
-button_event_cb_t* find_single_cb( mg_button_t *btn_ctrl, uint32_t code, uint32_t event )
+button_event_cb_t find_single_cb( mg_button_t *btn_ctrl, uint32_t code, uint32_t event )
{
int32_t found = 0;
uint32_t i;
- button_event_cb_t *cb = NULL;
+ button_event_cb_t cb = NULL;
btn_cb_list_t *p_list = &btn_ctrl->cb_list;
return cb;
}
-button_event_cb_t* find_comb_cb( mg_button_t *btn_ctrl, uint32_t code, uint32_t event )
+button_event_cb_t find_comb_cb( mg_button_t *btn_ctrl, uint32_t code, uint32_t event )
{
int32_t found = 0;
uint32_t i;
- button_event_cb_t *cb = NULL;
+ button_event_cb_t cb = NULL;
btn_cb_list_t *p_list = &btn_ctrl->cb_list;
//临时测试用,单击按钮放开时调用
void button_single_clicked_register( mg_button_t *btn, button_event_cb_t cb )
{
- g_test_cb = cb;
- printf( "button regist cb: %p\n", g_test_cb );
+ //g_test_cb = cb;
+ //printf( "button regist cb: %p\n", g_test_cb );
}
--- /dev/null
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <time.h>
+#include <sys/time.h>
+#include <unistd.h>
+
+#include "button.h"
+
+//static uint32_t btn_pressed_code = 0x0; //bit0..bit7: 1:key pressed, 0:key released
+
+typedef struct {
+ uint32_t st; //0:up idle, 1: pressed, 2:up wait 250ms for next pressed. 3: double_pressed 8:wait all key released
+ uint32_t code; //btn_code
+ uint32_t pressed_tick;
+ uint32_t up_tick;
+ uint32_t single_click;
+ uint32_t double_click;
+ uint32_t long_press_1s;
+ uint32_t long_press_2s;
+ uint32_t long_press_5s;
+ uint32_t long_press_up;
+}single_btn_st_t;
+
+typedef struct{
+ uint32_t st; //0 idle, 1:combo pressed, 2:wait all released
+ uint32_t code;
+ uint32_t pressed_tick;
+ uint32_t long_press_1s;
+ uint32_t long_press_2s;
+ uint32_t long_press_5s;
+}comb_btn_st_t;
+
+extern button_event_cb_t find_single_cb( mg_button_t *btn_ctrl, uint32_t code, uint32_t event );
+extern button_event_cb_t find_comb_cb( mg_button_t *btn_ctrl, uint32_t code, uint32_t event );
+
+static single_btn_st_t single_st;
+static comb_btn_st_t comb_st;
+
+int32_t is_comb_code( mg_button_t *btn_ctrl, uint32_t code )
+{
+ if( find_comb_cb( btn_ctrl, code, 0 ) )
+ return 1;
+ else
+ return 0;
+}
+
+static uint32_t get_systime_ms( void )
+{
+ struct timeval tm;
+ gettimeofday( &tm, NULL );
+ return (uint32_t)(tm.tv_sec*1000+tm.tv_usec/1000);
+}
+
+void single_btn_report( mg_button_t *btn_ctrl, uint32_t code, button_event_t event )
+{
+ button_event_cb_t cb;
+
+ cb = find_single_cb( btn_ctrl, code, event );
+ if( cb )
+ cb( code, event, NULL );
+}
+
+void comb_btn_report( mg_button_t *btn_ctrl, uint32_t code, button_event_t event )
+{
+ button_event_cb_t cb;
+
+ cb = find_comb_cb( btn_ctrl, code, event );
+ if( cb )
+ cb( code, event, NULL );
+}
+
+void comb_btn_scan( mg_button_t *btn_ctrl, comb_btn_st_t *comb, uint32_t pressed_code )
+{
+ uint32_t curr_time_ms = get_systime_ms();
+ switch( comb->st ){
+ case 0: //idle
+ if( is_comb_code( btn_ctrl, pressed_code ) ){
+ comb->st = 1;
+ comb->code = pressed_code;
+ comb->pressed_tick = get_systime_ms();
+ comb->long_press_1s = 0;
+ comb->long_press_2s = 0;
+ comb->long_press_5s = 0;
+ }
+ break;
+
+ case 1:
+ if( pressed_code != comb->code ){
+ comb->st = 2; //wait all btn released;
+ }else{
+ if( !comb->long_press_1s && curr_time_ms - comb->pressed_tick > 1000 ){
+ printf( "---------------comb long press 1s ...%x, time:[%d - %d]\n", comb->code, comb->pressed_tick );
+ comb->long_press_1s = 1;
+ comb_btn_report( btn_ctrl, comb->code, EVENT_COMB_PRESS );
+ }else if( !comb->long_press_2s && curr_time_ms - comb->pressed_tick > 2000 ){
+ printf( "---------------comb long press 2s ...%x\n", comb->code );
+ comb->long_press_2s = 1;
+ comb_btn_report( btn_ctrl, comb->code, EVENT_COMB_PRESS_2S );
+ }else if( !comb->long_press_5s && curr_time_ms - comb->pressed_tick > 5000 ){
+ printf( "---------------comb long press 5s ...%x\n", comb->code );
+ comb->long_press_5s = 1;
+ comb->st = 2;
+ comb_btn_report( btn_ctrl, comb->code, EVENT_COMB_PRESS_5S );
+ }
+ }
+ break;
+ case 2:
+ if( pressed_code == 0 ){
+ comb->st = 0;
+ printf( "comb st -> idle\n" );
+ }
+ break;
+ }
+}
+
+void single_btn_scan( mg_button_t *btn_ctrl, single_btn_st_t * btn, uint32_t pressed_code )
+{
+ uint32_t curr_time_ms;
+ uint32_t i;
+
+ curr_time_ms = get_systime_ms();
+static uint32_t code =0;
+if( code != pressed_code ){
+ printf( "scan pressed code: %02x -> %02x\n", code, pressed_code );
+ code = pressed_code;
+}
+ switch( btn->st ){
+ case 0: //idle
+ if( pressed_code&0xff ){
+ for( i=0; i<8; i++ ){
+ if( pressed_code & (1<<i ) ){
+ //记录第一个按键, 其它按键忽略
+ btn->code = 1<<i;
+ break;
+ }
+ }
+ btn->st = 1;
+ btn->single_click = 0;
+ btn->double_click = 0;
+ btn->long_press_1s = 0;
+ btn->long_press_2s = 0;
+ btn->long_press_5s = 0;
+ btn->long_press_up = 0;
+
+ btn->pressed_tick = curr_time_ms;
+ btn->up_tick = curr_time_ms;
+ }
+ break;
+ case 1: //first pressed
+ if( btn->code & pressed_code ){
+ //check long pressed
+ if( !btn->long_press_1s && curr_time_ms - btn->pressed_tick > 1000 ){
+ btn->long_press_1s = 1;
+ printf( "------------ long press event ...%x\n", btn->code );
+ single_btn_report( btn_ctrl, btn->code, EVENT_LONG_PRESS );
+ }else if( !btn->long_press_2s && curr_time_ms - btn->pressed_tick > 2000 ){
+ btn->long_press_2s = 1;
+ printf( "------------ long press 2s event ...%x\n", btn->code );
+ single_btn_report( btn_ctrl, btn->code, EVENT_LONG_PRESS_2S );
+ }else if( !btn->long_press_5s && curr_time_ms - btn->pressed_tick > 5000 ){
+ btn->long_press_5s = 1;
+ printf( "------------ long press 5s event ...%x\n", btn->code );
+ single_btn_report( btn_ctrl, btn->code, EVENT_LONG_PRESS_5S );
+ }
+
+ }else{
+ btn->up_tick = curr_time_ms;
+ if( btn->long_press_1s ){
+ btn->long_press_up = 1;
+ printf( "------------ long press up event ...%x\n", btn->code );
+ btn->st = 8; //等待全部按键放开
+ single_btn_report( btn_ctrl, btn->code, EVENT_LONG_PRESS_UP );
+ }else{
+ btn->st = 2; //wait 250ms
+ }
+ }
+ break;
+ case 2: //wait 250ms
+ if( btn->code & pressed_code ){
+ btn->pressed_tick = curr_time_ms;
+ btn->st = 3;
+ }else{
+ if( curr_time_ms - btn->up_tick > 250 ){
+ btn->single_click = 1;
+ printf( "------------------sing click event ...%x\n", btn->code );
+ btn->st = 8; //wait all btn released
+ single_btn_report( btn_ctrl, btn->code, EVENT_SINGLE_CLICK );
+ }
+ }
+ break;
+ case 3: //
+ if( btn->code & pressed_code ){
+
+ }else{
+ btn->up_tick = curr_time_ms;
+ btn->double_click = 1;
+ printf( "------------------double click event ...%x\n", btn->code );
+ btn->st = 8;
+ single_btn_report( btn_ctrl, btn->code, EVENT_DOUBLE_CLICK );
+ }
+ break;
+ case 8:
+ if( !pressed_code ){
+ btn->code = 0;
+ btn->st = 0; //idle
+ printf( "single btn st -> idle\n" );
+ }
+ break;
+ }
+}
+
+void single_btn_init( void )
+{
+ memset( &single_st, 0, sizeof( single_st ) );
+}
+
+void comb_btn_init( void )
+{
+ memset( &comb_st, 0, sizeof( comb_st ) );
+}
+
+void btn_scan_init( void )
+{
+ single_btn_init();
+ comb_btn_init();
+}
+
+void btn_scan( mg_button_t *btn_ctrl, uint32_t btn_pressed_code )
+{
+ uint32_t pressed_code = btn_pressed_code; //get_btn_code();
+
+ if( single_st.st != 0 && single_st.long_press_1s != 0 ){
+ //已经产生了长按事件,不检测组合按键
+ comb_st.st = 0;
+ }else{
+ comb_btn_scan( btn_ctrl, &comb_st, pressed_code );
+ }
+
+ if( comb_st.st == 0 ){ //0:idle, 1:pressing 2: wait all btn releasd
+ //非组合按键,扫描单按键
+ single_btn_scan( btn_ctrl, &single_st, pressed_code );
+ }else{
+ //组合按键扫描,不扫描单按键
+ single_st.st = 0;
+ }
+}
+
project(mg_led)
+SET(CMAKE_BUILD_TYPE "Debug")
+SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
+SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
+
include_directories(
${PROJECT_SOURCE_DIR}/./
${PROJECT_SOURCE_DIR}/../log/inc/
#include "pthread.h"
#include "semaphore.h"
-//typedef void (*timer_handle_t)( int32_t );
-
-
-
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
}
-void led_update_brightness( led_x_t *led )
-{
- //亮度调整
- char system_cmd[100];
- uint32_t brightness;
-
- if( led->mode != LED_MODE_OFF ){
-
- brightness = led->dp_brightness*255/100;
-
- 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 ) ){
- }
-
- }
-}
-
-void led_timer_cb( union sigval v )
-{
- printf( "[%ld]timer cb sig v:%d\r\n", time( NULL ), v.sival_int );
- led_x_t *led;
- led = ( led_x_t *)v.sival_ptr;
- if( !led ){
- MG_LOGE( "led_timer_cb: invailed led\n" );
- }
- MG_LOGI( "[%s] timer cb, mode :%d -> %d\n", led->name, led->mode, led->next_mode );
- led_lock( &g_led );
- led->mode = led->next_mode;
- led->next_time_ms = 0;
- led_unlock( &g_led );
- led_x_update(led);
-
-}
//----------------------------------------------------------------------------------------
#endif
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 );
+ //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 );
}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 );
+ //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 );
if( memcmp( &curr_cfg, &p_led->cfg, sizeof( curr_cfg )) == 0 ){
led_dev_unlock( p_led );
- MG_LOGD( "led[%s] task wait sem...", p_led->name );
led_dev_wait( led );
- MG_LOGD( "led[%s] task get sem...", p_led->name );
}else{
led_dev_unlock( p_led );
- MG_LOGD( "led task scan...", p_led->name );
}
}
return NULL;
int32_t mg_led_brightness_set( mg_led_ctrl_t *led_ctrl, led_id_t id, uint32_t max_br )
{
-#if 0
+
int32_t i;
led_dev_t *led;
for( i=0; i<LED_NR; i++ ){
if( id&(1<<i) ){
- led = &led_ctrl->led_x[i];
- led_lock( led_ctrl );
- led->max_brightness = max_brightness;
- led->dp_brightness = dp_brightness;
- led_unlock( led_ctrl );
- if( led->mode != LED_MODE_OFF ){
- led_update_brightness( led );
+ led = &led_ctrl->led[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 );
}
}
-#endif
+
return 0;
}
for( i=0; i<LED_NR; i++ ){
if( id&(1<<i) ){
led = &led_ctrl->led[i];
- MG_LOGD( "led[%s] mode set", led->name );
+ MG_LOGD( "led[%s] mode set %d", led->name );
led_dev_lock( led );
- led->cfg.mode = cfg->mode;
+ 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;
+ }
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 ){
mg_led_ctrl_t * mg_led_open( void )
{
+ MG_LOGI( "mg_led_open..." );
led_ctrl_init( &g_mg_led);
return &g_mg_led;
}
void mg_led_close( mg_led_ctrl_t *led_ctrl )
{
- MG_LOGI( "mg_led_close...\n" );
+ MG_LOGI( "mg_led_close..." );
if( led_ctrl ){
led_ctrl_deinit( led_ctrl);
}
#install( TARGETS ${PROJECT_NAME} RUNTIME DESTINATION /app/lib )
#install( TARGETS ${PROJECT_NAME} LIBRARY DESTINATION /app/lib )
#install( TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION /app/lib )
+
+install( FILES log4crc DESTINATION /app )