diff --git a/lilygo-sensor-clock.ino b/lilygo-sensor-clock.ino index d2c6dd7..c6e86d6 100644 --- a/lilygo-sensor-clock.ino +++ b/lilygo-sensor-clock.ino @@ -9,6 +9,11 @@ #include #include #include "freertos/semphr.h" +#include "DFRobot_mmWave_Radar.h" +#include + +#include "Thread.h" +#include "ThreadController.h" // Serial #define BAUD 115200 @@ -18,25 +23,20 @@ #define TX2_PIN TOUCH_IICSDA // Sensor configuration -#define COMMAND "FDFCFBFA0800120000000200000004030203" //Normal mode command, see documentation waveshare sensor -#define PRESENCE_DISTANCE 200 // in cm +#define PRESENCE_DISTANCE 2 // in m #define DELAY_STATUS_CHECK 1000 // delay (in ms) between two checks of the presence #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, - 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x03, - 0x02, 0x03 -}; +HardwareSerial mySerial(1); +DFRobot_mmWave_Radar sensor(&mySerial); SemaphoreHandle_t xSemaphore = NULL; PowersSY6970 PMU; // Sensor variables uint16_t state = 1; // 0 = presence detected, 1 = no presence detected for the past SLEEP_DELAY ms, 2 = no presence detected for the past POWER_DELAY ms -unsigned long lastMotionCheckTime = millis(); -unsigned long lastMotionDetected = 0; +unsigned long lastMotionDetected = 0; // time when the last motion was detected // Time variables static uint32_t last_tick; @@ -52,6 +52,10 @@ static lv_color_t *buf1; static String BASE_URL_HA = "http://192.168.1.2:8123/api/services"; static String HA_TOKEN = "abc"; +ThreadController controller; +Thread thread1 = Thread(); +Thread thread2 = Thread(); + void setup() { xSemaphore = xSemaphoreCreateBinary(); xSemaphoreGive(xSemaphore); @@ -60,10 +64,12 @@ void setup() { initializeWifi(); initializeTime(); initializeUI(); + initializeThreading(); } void loop() { - delay(1); + controller.run(); + if (transfer_num <= 0 && lcd_PushColors_len <= 0) lv_timer_handler(); @@ -74,9 +80,6 @@ void loop() { static int flag_bl = 0; static unsigned long cnt = 0; - printLocalTime(); - readAndProcessSensorLines(); - cnt++; if (cnt >= 100) { if (flag_bl == 0) { @@ -110,12 +113,12 @@ void initSerial() { digitalWrite(TOUCH_RES, HIGH); delay(2); - // 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)); + // Start HMMD Sensor + mySerial.begin(BAUD, SERIAL_8N1, RX2_PIN, TX2_PIN); //RX,TX + Serial.println("Sensor Initialized on RX:" + String(RX2_PIN) + ", TX:" + String(TX2_PIN)); + sensor.factoryReset(); //Restore to the factory settings + sensor.DetRangeCfg(0, PRESENCE_DISTANCE); //The detection range is as far as 9m + sensor.OutputLatency(0, 0); } void initializeWifi() { @@ -188,6 +191,17 @@ void initializeUI() { Serial.println("UI initialized"); } +void initializeThreading() { + thread1.onRun(printLocalTime); + thread1.setInterval(100); + + thread2.onRun(readAndProcessSensorLines); + thread2.setInterval(1000); + + controller.add(&thread1); + controller.add(&thread2); +} + void printLocalTime() { if (millis() - last_tick > 100) { struct tm timeInfo; @@ -203,49 +217,29 @@ void printLocalTime() { } void readAndProcessSensorLines() { - if (millis() - lastMotionCheckTime >= DELAY_STATUS_CHECK) { - lastMotionCheckTime = millis(); - // Check if data is available on Serial2 - while (Serial2.available() > 0) { - // Read a line of text until a newline character (\n) is received - // The timeout helps prevent blocking forever if a line ending is missed - String line = Serial2.readStringUntil('\n'); - - // Clean up the line: remove potential carriage return (\r) and leading/trailing whitespace - line.trim(); - - // Check if the line contains the "Range" information - if (line.startsWith("Range ")) { - // Extract the substring after "Range " - String distanceStr = line.substring(6); - int distance = distanceStr.toInt(); - bool currentStatus = distance <= PRESENCE_DISTANCE; + int currentStatus = sensor.readPresenceDetection(); - if (currentStatus) { - lastMotionDetected = millis(); - } + if (currentStatus == 1) { + lastMotionDetected = millis(); + } - //Serial.print("Radar: "); - //Serial.println(distance); + //Serial.printf("Sensor: %d\n", currentStatus); - if (currentStatus && state != 0) { - state = 0; - Serial.println("Motion detected, turning screen on."); - 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 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 ms, turning screen screensaver on.\n", SLEEP_DELAY); - lv_msg_send(MSG_NEW_VOLT, "1"); - //restApiAction(1); - } - } - } + if (currentStatus == 1 && state != 0) { + state = 0; + Serial.println("Motion detected, turning screen on."); + lv_msg_send(MSG_NEW_VOLT, "0"); + //restApiAction(0); + } else if (currentStatus == 0 && state == 1 && (millis() - lastMotionDetected >= POWER_DELAY)) { + state = 2; + 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 == 0 && state == 0 && (millis() - lastMotionDetected >= SLEEP_DELAY)) { + state = 1; + Serial.printf("No motion detected for %d ms, turning screen screensaver on.\n", SLEEP_DELAY); + lv_msg_send(MSG_NEW_VOLT, "1"); + //restApiAction(1); } }