diff --git a/lilygo-sensor-clock.ino b/lilygo-sensor-clock.ino index 59982f9..d2c6dd7 100644 --- a/lilygo-sensor-clock.ino +++ b/lilygo-sensor-clock.ino @@ -21,8 +21,8 @@ #define COMMAND "FDFCFBFA0800120000000200000004030203" //Normal mode command, see documentation waveshare sensor #define PRESENCE_DISTANCE 200 // in cm #define DELAY_STATUS_CHECK 1000 // delay (in ms) between two checks of the presence -#define SLEEP_DELAY 10000 // delay (in ms) until sleep mode -#define POWER_DELAY 30000 // delay (in ms) until turning off the screen +#define SLEEP_DELAY 30000 // delay (in ms) until sleep mode +#define POWER_DELAY 100000 // delay (in ms) until turning off the screen byte configCommand[] = { 0xFD, 0xFC, 0xFB, 0xFA, 0x08, 0x00, 0x12, 0x00, @@ -85,6 +85,7 @@ void loop() { flag_bl = 1; lv_delay_ms(500); ui_begin(); + lv_msg_send(MSG_NEW_VOLT, ""); } } } @@ -112,6 +113,8 @@ void initSerial() { // Start Serial2 for the HMMD Sensor Serial2.begin(BAUD, SERIAL_8N1, RX2_PIN, TX2_PIN); Serial.println("Serial2 Initialized on RX:" + String(RX2_PIN) + ", TX:" + String(TX2_PIN)); + Serial.println("Sending configuration to the sensor"); + delay(2000); Serial2.write(configCommand, sizeof(configCommand)); } @@ -228,17 +231,17 @@ void readAndProcessSensorLines() { if (currentStatus && state != 0) { state = 0; Serial.println("Motion detected, turning screen on."); - update_display_state(state); + lv_msg_send(MSG_NEW_VOLT, "0"); //restApiAction(0); } else if (!currentStatus && state == 1 && (millis() - lastMotionDetected >= POWER_DELAY)) { state = 2; - Serial.printf("No motion detected for %d seconds, turning screen off.\n", POWER_DELAY); - update_display_state(state); + Serial.printf("No motion detected for %d ms, turning screen off.\n", POWER_DELAY); + lv_msg_send(MSG_NEW_VOLT, "2"); //restApiAction(2); } else if (!currentStatus && state == 0 && (millis() - lastMotionDetected >= SLEEP_DELAY)) { state = 1; - Serial.printf("No motion detected for %d seconds, turning screen screensaver on.\n", SLEEP_DELAY); - update_display_state(state); + Serial.printf("No motion detected for %d ms, turning screen screensaver on.\n", SLEEP_DELAY); + lv_msg_send(MSG_NEW_VOLT, "1"); //restApiAction(1); } } diff --git a/ui-util.cpp b/ui-util.cpp index 973faaf..04ee163 100644 --- a/ui-util.cpp +++ b/ui-util.cpp @@ -39,6 +39,8 @@ static lv_obj_t *img5; static lv_obj_t *img6; static lv_obj_t *img7; +lv_obj_t *state_symbol; + // Transition animation static const void *trans_ainm_buf[] = { &gif_01, &gif_12, &gif_23, &gif_34, &gif_45, @@ -46,6 +48,7 @@ static const void *trans_ainm_buf[] = { }; static void update_text_subscriber_cb_demo1(void *s, lv_msg_t *msg); +static void update_display_state(void *s, lv_msg_t *msg); void set_flip_time_anim(int hour, int minute, int second); void ui_begin() { @@ -55,6 +58,12 @@ void ui_begin() { lv_obj_set_style_bg_color(dis, lv_color_black(), LV_PART_MAIN | LV_STATE_DEFAULT); lv_obj_t *tv1 = lv_tileview_add_tile(dis, 0, 0, LV_DIR_VER); + + state_symbol = lv_label_create(lv_scr_act()); + lv_obj_align(state_symbol, LV_ALIGN_TOP_LEFT, 20, 5); + lv_obj_set_width(state_symbol, LV_PCT(80)); + lv_label_set_long_mode(state_symbol, LV_LABEL_LONG_SCROLL); + lv_label_set_recolor(state_symbol, true); img1 = lv_gif_create(tv1); img2 = lv_gif_create(tv1); @@ -80,25 +89,23 @@ void ui_begin() { lv_msg_subsribe(MSG_NEW_HOUR, update_text_subscriber_cb_demo1, NULL); lv_msg_subsribe(MSG_NEW_MIN, update_text_subscriber_cb_demo1, NULL); lv_msg_subsribe(MSG_NEW_SEC, update_text_subscriber_cb_demo1, NULL); + lv_msg_subsribe(MSG_NEW_VOLT, update_display_state, NULL); } -void update_display_state(uint16_t state) { - lv_obj_t *state_symbol = lv_label_create(lv_scr_act()); - lv_obj_align(state_symbol, LV_ALIGN_TOP_LEFT, 20, 5); - lv_obj_set_width(state_symbol, LV_PCT(80)); - lv_label_set_long_mode(state_symbol, LV_LABEL_LONG_SCROLL); - lv_label_set_recolor(state_symbol, true); - - if (state == 0) { - lv_label_set_text(state_symbol, "#00FF00 " LV_SYMBOL_OK " ON #"); - } else if (state == 1) { - lv_label_set_text(state_symbol, "#FFA500 " LV_SYMBOL_PAUSE " IDLE #"); - } else { - lv_label_set_text(state_symbol, "#FFA500 " LV_SYMBOL_PAUSE " OFF #"); - } +static void update_display_state(void *s, lv_msg_t *msg) { + const char * payload = (const char *)lv_msg_get_payload(msg); - lv_delay_ms(5000); - lv_label_set_text(state_symbol, ""); + if (lv_msg_get_id(msg) == MSG_NEW_VOLT) { + if (payload == "0") { + lv_label_set_text(state_symbol, "#00FF00 " LV_SYMBOL_OK " ON #"); + } else if (payload == "1") { + lv_label_set_text(state_symbol, "#FFA500 " LV_SYMBOL_PAUSE " IDLE #"); + } else if (payload == "2") { + lv_label_set_text(state_symbol, "#FF0000 " LV_SYMBOL_POWER" OFF #"); + } else { + lv_label_set_text(state_symbol, ""); + } + } } static void update_text_subscriber_cb_demo1(void *s, lv_msg_t *msg) { diff --git a/ui-util.h b/ui-util.h index 2a29159..d9c197e 100644 --- a/ui-util.h +++ b/ui-util.h @@ -30,5 +30,4 @@ extern size_t lcd_PushColors_len; void ui_begin(); void lv_delay_ms(int x); -void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p); -void update_display_state(uint16_t state); \ No newline at end of file +void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p); \ No newline at end of file