From 9e6325e6d8a8a197b118cc7a18a4e20cee9f2650 Mon Sep 17 00:00:00 2001 From: SQFMI Date: Sat, 6 Jul 2024 01:38:48 -0400 Subject: [PATCH] Add USB detection --- examples/WatchFaces/7_SEG/Watchy_7_SEG.cpp | 25 +++++++++++++--------- examples/WatchFaces/7_SEG/icons.h | 8 +++++++ library.json | 2 +- library.properties | 2 +- src/Display.cpp | 2 -- src/Watchy.cpp | 24 +++++++++++++++++++-- src/Watchy.h | 3 +++ 7 files changed, 50 insertions(+), 16 deletions(-) diff --git a/examples/WatchFaces/7_SEG/Watchy_7_SEG.cpp b/examples/WatchFaces/7_SEG/Watchy_7_SEG.cpp index a0c628c..9ff254b 100644 --- a/examples/WatchFaces/7_SEG/Watchy_7_SEG.cpp +++ b/examples/WatchFaces/7_SEG/Watchy_7_SEG.cpp @@ -16,10 +16,15 @@ void Watchy7SEG::drawWatchFace(){ drawSteps(); drawWeather(); drawBattery(); - display.drawBitmap(120, 77, WIFI_CONFIGURED ? wifi : wifioff, 26, 18, DARKMODE ? GxEPD_WHITE : GxEPD_BLACK); + display.drawBitmap(116, 75, WIFI_CONFIGURED ? wifi : wifioff, 26, 18, DARKMODE ? GxEPD_WHITE : GxEPD_BLACK); if(BLE_CONFIGURED){ - display.drawBitmap(100, 75, bluetooth, 13, 21, DARKMODE ? GxEPD_WHITE : GxEPD_BLACK); + display.drawBitmap(100, 73, bluetooth, 13, 21, DARKMODE ? GxEPD_WHITE : GxEPD_BLACK); } + #ifdef ARDUINO_ESP32S3_DEV + if(USB_PLUGGED_IN){ + display.drawBitmap(140, 75, charge, 16, 18, DARKMODE ? GxEPD_WHITE : GxEPD_BLACK); + } + #endif } void Watchy7SEG::drawTime(){ @@ -81,25 +86,25 @@ void Watchy7SEG::drawSteps(){ display.println(stepCount); } void Watchy7SEG::drawBattery(){ - display.drawBitmap(154, 73, battery, 37, 21, DARKMODE ? GxEPD_WHITE : GxEPD_BLACK); - display.fillRect(159, 78, 27, BATTERY_SEGMENT_HEIGHT, DARKMODE ? GxEPD_BLACK : GxEPD_WHITE);//clear battery segments + display.drawBitmap(158, 73, battery, 37, 21, DARKMODE ? GxEPD_WHITE : GxEPD_BLACK); + display.fillRect(163, 78, 27, BATTERY_SEGMENT_HEIGHT, DARKMODE ? GxEPD_BLACK : GxEPD_WHITE);//clear battery segments int8_t batteryLevel = 0; float VBAT = getBatteryVoltage(); - if(VBAT > 4.1){ + if(VBAT > 4.0){ batteryLevel = 3; } - else if(VBAT > 3.95 && VBAT <= 4.1){ + else if(VBAT > 3.6 && VBAT <= 4.0){ batteryLevel = 2; } - else if(VBAT > 3.80 && VBAT <= 3.95){ + else if(VBAT > 3.20 && VBAT <= 3.6){ batteryLevel = 1; } - else if(VBAT <= 3.80){ + else if(VBAT <= 3.20){ batteryLevel = 0; } for(int8_t batterySegments = 0; batterySegments < batteryLevel; batterySegments++){ - display.fillRect(159 + (batterySegments * BATTERY_SEGMENT_SPACING), 78, BATTERY_SEGMENT_WIDTH, BATTERY_SEGMENT_HEIGHT, DARKMODE ? GxEPD_WHITE : GxEPD_BLACK); + display.fillRect(163 + (batterySegments * BATTERY_SEGMENT_SPACING), 78, BATTERY_SEGMENT_WIDTH, BATTERY_SEGMENT_HEIGHT, DARKMODE ? GxEPD_WHITE : GxEPD_BLACK); } } @@ -150,4 +155,4 @@ void Watchy7SEG::drawWeather(){ } display.drawBitmap(145, 158, weatherIcon, WEATHER_ICON_WIDTH, WEATHER_ICON_HEIGHT, DARKMODE ? GxEPD_WHITE : GxEPD_BLACK); -} +} \ No newline at end of file diff --git a/examples/WatchFaces/7_SEG/icons.h b/examples/WatchFaces/7_SEG/icons.h index 49ab826..9d67f76 100644 --- a/examples/WatchFaces/7_SEG/icons.h +++ b/examples/WatchFaces/7_SEG/icons.h @@ -189,6 +189,14 @@ const unsigned char wifioff [] PROGMEM = { 0x01, 0xff, 0xc0, 0x00, 0x07, 0xe1, 0xc0, 0x00, 0x0f, 0xc0, 0x80, 0x00, 0x1f, 0x0c, 0x00, 0x00, 0x3c, 0x1e, 0x00, 0x00, 0xf8, 0x0c, 0x00, 0x00 }; + +// 'charge', 16x18px +const unsigned char charge [] PROGMEM = { + 0x00, 0x10, 0x00, 0x70, 0x00, 0xe0, 0x01, 0xe0, 0x03, 0xe0, 0x07, 0xc0, 0x0f, 0xc0, 0x1f, 0xff, + 0x3f, 0xfe, 0x7f, 0xfc, 0x7f, 0xf8, 0x03, 0xf0, 0x03, 0xe0, 0x07, 0xc0, 0x07, 0x80, 0x07, 0x00, + 0x0e, 0x00, 0x0c, 0x00 +}; + // 'chip', 48x32px const unsigned char chip [] PROGMEM = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, diff --git a/library.json b/library.json index 24c321f..ae59ef6 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "Watchy", - "version": "1.4.13", + "version": "1.4.14", "description": "Watchy - An Open Source E-Paper Watch by SQFMI", "authors": [ { diff --git a/library.properties b/library.properties index 18abeac..8cc9f86 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Watchy -version=1.4.13 +version=1.4.14 author=SQFMI maintainer=SQFMI sentence=Watchy - An Open Source E-Paper Watch by SQFMI diff --git a/src/Display.cpp b/src/Display.cpp index ecaac83..94cf86d 100644 --- a/src/Display.cpp +++ b/src/Display.cpp @@ -19,11 +19,9 @@ RTC_DATA_ATTR bool displayFullInit = true; void WatchyDisplay::busyCallback(const void *) { - #ifndef ARDUINO_ESP32S3_DEV gpio_wakeup_enable((gpio_num_t)DISPLAY_BUSY, GPIO_INTR_LOW_LEVEL); esp_sleep_enable_gpio_wakeup(); esp_light_sleep_start(); - #endif } WatchyDisplay::WatchyDisplay() : diff --git a/src/Watchy.cpp b/src/Watchy.cpp index 893d459..8003aef 100644 --- a/src/Watchy.cpp +++ b/src/Watchy.cpp @@ -19,6 +19,7 @@ RTC_DATA_ATTR weatherData currentWeather; RTC_DATA_ATTR int weatherIntervalCounter = -1; RTC_DATA_ATTR long gmtOffset = 0; RTC_DATA_ATTR bool alreadyInMenu = true; +RTC_DATA_ATTR bool USB_PLUGGED_IN = false; RTC_DATA_ATTR tmElements_t bootTime; RTC_DATA_ATTR uint32_t lastIPAddress; RTC_DATA_ATTR char lastSSID[30]; @@ -32,7 +33,6 @@ void Watchy::init(String datetime) { Wire.begin(SDA, SCL); // init i2c #endif RTC.init(); - // Init the display since is almost sure we will use it display.epd2.initWatchy(); @@ -67,9 +67,23 @@ void Watchy::init(String datetime) { case ESP_SLEEP_WAKEUP_EXT1: // button Press handleButtonPress(); break; + #ifdef ARDUINO_ESP32S3_DEV + case ESP_SLEEP_WAKEUP_EXT0: // USB plug in + pinMode(USB_DET_PIN, INPUT); + USB_PLUGGED_IN = (digitalRead(USB_DET_PIN) == 1); + if(guiState == WATCHFACE_STATE){ + RTC.read(currentTime); + showWatchFace(true); + } + break; + #endif default: // reset RTC.config(datetime); _bmaConfig(); + #ifdef ARDUINO_ESP32S3_DEV + pinMode(USB_DET_PIN, INPUT); + USB_PLUGGED_IN = (digitalRead(USB_DET_PIN) == 1); + #endif gmtOffset = settings.gmtOffset; RTC.read(currentTime); RTC.read(bootTime); @@ -85,9 +99,16 @@ void Watchy::deepSleep() { display.hibernate(); RTC.clearAlarm(); // resets the alarm flag in the RTC #ifdef ARDUINO_ESP32S3_DEV + esp_sleep_enable_ext0_wakeup((gpio_num_t)USB_DET_PIN, USB_PLUGGED_IN ? LOW : HIGH); //// enable deep sleep wake on USB plug in/out + rtc_gpio_set_direction((gpio_num_t)USB_DET_PIN, RTC_GPIO_MODE_INPUT_ONLY); + rtc_gpio_pullup_en((gpio_num_t)USB_DET_PIN); + esp_sleep_enable_ext1_wakeup( BTN_PIN_MASK, ESP_EXT1_WAKEUP_ANY_LOW); // enable deep sleep wake on button press + rtc_gpio_set_direction((gpio_num_t)UP_BTN_PIN, RTC_GPIO_MODE_INPUT_ONLY); + rtc_gpio_pullup_en((gpio_num_t)UP_BTN_PIN); + rtc_clk_32k_enable(true); //rtc_clk_slow_freq_set(RTC_SLOW_FREQ_32K_XTAL); struct tm timeinfo; @@ -108,7 +129,6 @@ void Watchy::deepSleep() { BTN_PIN_MASK, ESP_EXT1_WAKEUP_ANY_HIGH); // enable deep sleep wake on button press #endif - gpio_deep_sleep_hold_dis(); esp_deep_sleep_start(); } diff --git a/src/Watchy.h b/src/Watchy.h index b7c7a89..680a1e7 100644 --- a/src/Watchy.h +++ b/src/Watchy.h @@ -27,6 +27,8 @@ #include "soc/rtc_cntl_reg.h" #include "time.h" #include "esp_sntp.h" + #include "hal/rtc_io_types.h" + #include "driver/rtc_io.h" #define uS_TO_S_FACTOR 1000000ULL //Conversion factor for micro seconds to seconds #define ADC_VOLTAGE_DIVIDER ((360.0f+100.0f)/360.0f) //Voltage divider at battery ADC #else @@ -116,5 +118,6 @@ extern RTC_DATA_ATTR int menuIndex; extern RTC_DATA_ATTR BMA423 sensor; extern RTC_DATA_ATTR bool WIFI_CONFIGURED; extern RTC_DATA_ATTR bool BLE_CONFIGURED; +extern RTC_DATA_ATTR bool USB_PLUGGED_IN; #endif