void miio_app_thread(void* arg) { static bool has_sync_time = false; g_mum = mum_create(); //*****************************button init*******************************/ os_timer_create(&g_reset_prov_timer, "reset-prov-timer", os_msec_to_ticks(4000), reset_prov, NULL, OS_TIMER_ONE_SHOT, OS_TIMER_NO_ACTIVATE); button_frame_work_init(); register_button_callback(0, button_1_press_handle); if (WM_SUCCESS != os_semaphore_create(&btn_pressed_sem, "btn_pressed_sem")) { LOG_ERROR("btn_pressed_sem creation failed\r\n"); } else if (WM_SUCCESS != os_semaphore_get(&btn_pressed_sem, OS_WAIT_FOREVER)) { LOG_ERROR("first get btn_pressed_sem failed.\r\n"); } //******************************button init end*******************************************/ local_timer_init();//add by [email protected] miio_led_on(); LOG_INFO("miio_app_thread while loop start.\r\n"); #ifdef MIIO_COMMANDS_DEBUG while(!ot_api_is_online())api_os_tick_sleep(100); uart_wifi_debug_init(); #endif while (1) { if (WM_SUCCESS == os_semaphore_get(&btn_pressed_sem, OS_NO_WAIT)) { mum_set_property(g_mum,"button_pressed","\"wifi_rst\""); } if(has_sync_time)//only after sync time, we start check timer check_schedule_and_set_timer(); else if(otn_is_online()) has_sync_time = true; os_thread_sleep(os_msec_to_ticks(PERIOD_CHECK_BUTTON)); } local_timer_deinit();//add by [email protected] //*****************************button deinit*******************************/ mum_destroy(&g_mum); os_timer_delete(&g_reset_prov_timer); os_semaphore_delete(&btn_pressed_sem); //******************************button deinit end*******************************************/ }
/* This task publishes messages to the Evrythng cloud when button is pressed. */ static void button_task(os_thread_arg_t arg) { int led_state = 0; int button = (int)arg; int presses = 0; char json_str[256]; char led_action_json_fmt[] = "{\"type\":\"%s\",\"customFields\":{\"status\":\"%d\"}}"; char button_json_fmt[] = "[{\"value\": \"%d\"}]"; char* button_property = button == button_1 ? "button_1" : "button_2"; char* led_action = button == button_1 ? "_led1" : "_led2"; os_semaphore_t sem = button == button_1 ? button1_sem : button2_sem; while(1) { if (os_semaphore_get(&sem, OS_WAIT_FOREVER) == WM_SUCCESS) { sprintf(json_str, button_json_fmt, ++presses); EvrythngPubThngProperty(evt_handle, thng_id, button_property, json_str); led_state = !led_state; sprintf(json_str, led_action_json_fmt, led_action, led_state); EvrythngPubThngAction(evt_handle, thng_id, led_action, json_str); } } }