2019-11-17 15:51:25 -05:00
|
|
|
/*
|
|
|
|
* Watchy - Button Test Example
|
|
|
|
* Watchy Basic + testing all 4 buttons
|
|
|
|
*
|
|
|
|
* ==============
|
|
|
|
* |--------------|
|
2019-11-26 17:26:01 -05:00
|
|
|
* (IO34) BTN 3 | | BTN 1 (IO39)
|
2019-11-17 15:51:25 -05:00
|
|
|
* | |
|
|
|
|
* | |
|
|
|
|
* | |
|
|
|
|
* (IO35) BTN 4 | | BTN 2 (IO36)
|
|
|
|
* |--------------|
|
|
|
|
* ==============
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <DS3232RTC.h>
|
|
|
|
#include <GxEPD.h>
|
|
|
|
#include <GxIO/GxIO_SPI/GxIO_SPI.h>
|
|
|
|
#include <GxIO/GxIO.h>
|
|
|
|
#include "GxGDEH0154D67.h"
|
|
|
|
#include "DSEG7_Classic_Bold_48.h"
|
|
|
|
#include "DSEG14_Classic_Bold_18.h"
|
|
|
|
|
|
|
|
#define VIB_MOTOR_PIN 27
|
|
|
|
#define RTC_PIN GPIO_NUM_33
|
|
|
|
#define BTN_1 GPIO_SEL_39
|
|
|
|
#define BTN_2 GPIO_SEL_36
|
2019-11-26 00:34:47 -05:00
|
|
|
#define BTN_3 GPIO_SEL_34
|
|
|
|
#define BTN_4 GPIO_SEL_35
|
2019-11-17 15:51:25 -05:00
|
|
|
#define BTN_PIN_MASK BTN_1|BTN_2|BTN_3|BTN_4
|
|
|
|
|
|
|
|
DS3232RTC RTC(false);
|
|
|
|
GxIO_Class io(SPI, /*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16); // arbitrary selection of 17, 16
|
|
|
|
GxEPD_Class display(io, /*RST=*/ 16, /*BUSY=*/ 4); // arbitrary selection of (16), 4
|
|
|
|
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
detect_wakeup_reason();
|
|
|
|
delay(100);
|
|
|
|
esp_sleep_enable_ext0_wakeup(RTC_PIN, 0); //enable deep sleep wake on RTC interrupt
|
|
|
|
esp_sleep_enable_ext1_wakeup(BTN_PIN_MASK, ESP_EXT1_WAKEUP_ANY_HIGH); //enable deep sleep wake on button press
|
|
|
|
esp_deep_sleep_start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop(){}
|
|
|
|
|
|
|
|
void handleButton()
|
|
|
|
{
|
|
|
|
uint64_t wakeupBit = esp_sleep_get_ext1_wakeup_status();
|
|
|
|
|
|
|
|
pinMode(VIB_MOTOR_PIN, OUTPUT);
|
|
|
|
digitalWrite(VIB_MOTOR_PIN, HIGH);
|
|
|
|
delay(100);
|
|
|
|
digitalWrite(VIB_MOTOR_PIN, LOW);
|
|
|
|
|
|
|
|
display.init();
|
|
|
|
display.fillScreen(GxEPD_WHITE);
|
|
|
|
display.setTextColor(GxEPD_BLACK);
|
|
|
|
display.setFont(&DSEG14_Classic_Bold_18);
|
|
|
|
|
|
|
|
display.setCursor(15, 100);
|
|
|
|
display.println("You pressed");
|
|
|
|
display.setCursor(65, 140);
|
|
|
|
if (wakeupBit & BTN_4) {
|
|
|
|
display.println("BTN 4");
|
|
|
|
}
|
|
|
|
else if (wakeupBit & BTN_3) {
|
|
|
|
display.println("BTN 3");
|
|
|
|
}
|
|
|
|
else if (wakeupBit & BTN_2) {
|
|
|
|
display.println("BTN 2");
|
|
|
|
}
|
|
|
|
else if (wakeupBit & BTN_1) {
|
|
|
|
display.println("BTN 1");
|
|
|
|
}
|
|
|
|
display.update();
|
|
|
|
display.deepSleep();
|
|
|
|
}
|
|
|
|
|
2019-11-21 20:18:04 -05:00
|
|
|
void updateTime()
|
2019-11-17 15:51:25 -05:00
|
|
|
{
|
|
|
|
RTC.begin();
|
|
|
|
if(RTC.oscStopped(false)){ //check if RTC has been stopped
|
|
|
|
RTC.squareWave(SQWAVE_NONE); //disable square wave output
|
|
|
|
RTC.set(compileTime()); //set RTC time to compile time
|
|
|
|
RTC.setAlarm(ALM2_EVERY_MINUTE, 0, 0, 0, 1); //set alarm to every minute
|
|
|
|
RTC.alarmInterrupt(ALARM_2, true); //enable alarm interrupt
|
|
|
|
}
|
|
|
|
RTC.alarm(ALARM_2); //resets the alarm flag in the RTC
|
|
|
|
tmElements_t currentTime;
|
|
|
|
RTC.read(currentTime);
|
|
|
|
|
|
|
|
display.init();
|
|
|
|
display.fillScreen(GxEPD_BLACK);
|
|
|
|
display.setTextColor(GxEPD_WHITE);
|
|
|
|
display.setFont(&DSEG7_Classic_Bold_48);
|
|
|
|
display.setCursor(15, 120);
|
|
|
|
|
|
|
|
if(currentTime.Hour < 10){
|
|
|
|
display.print('0');
|
|
|
|
}
|
|
|
|
display.print(currentTime.Hour);
|
|
|
|
display.print(':');
|
|
|
|
if(currentTime.Minute < 10){
|
|
|
|
display.print('0');
|
|
|
|
}
|
|
|
|
display.print(currentTime.Minute);
|
2019-11-21 20:18:04 -05:00
|
|
|
display.update();
|
2019-11-17 15:51:25 -05:00
|
|
|
display.deepSleep();
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t compileTime()
|
|
|
|
{
|
|
|
|
const time_t FUDGE(10); //fudge factor to allow for upload time, etc. (seconds, YMMV)
|
|
|
|
const char *compDate = __DATE__, *compTime = __TIME__, *months = "JanFebMarAprMayJunJulAugSepOctNovDec";
|
|
|
|
char compMon[3], *m;
|
|
|
|
|
|
|
|
strncpy(compMon, compDate, 3);
|
|
|
|
compMon[3] = '\0';
|
|
|
|
m = strstr(months, compMon);
|
|
|
|
|
|
|
|
tmElements_t tm;
|
|
|
|
tm.Month = ((m - months) / 3 + 1);
|
|
|
|
tm.Day = atoi(compDate + 4);
|
|
|
|
tm.Year = atoi(compDate + 7) - 1970;
|
|
|
|
tm.Hour = atoi(compTime);
|
|
|
|
tm.Minute = atoi(compTime + 3);
|
|
|
|
tm.Second = atoi(compTime + 6);
|
|
|
|
|
|
|
|
time_t t = makeTime(tm);
|
|
|
|
return t + FUDGE; //add fudge factor to allow for compile time
|
|
|
|
}
|
|
|
|
|
|
|
|
void detect_wakeup_reason(){
|
|
|
|
esp_sleep_wakeup_cause_t wakeup_reason;
|
|
|
|
|
|
|
|
wakeup_reason = esp_sleep_get_wakeup_cause();
|
|
|
|
|
|
|
|
switch(wakeup_reason)
|
|
|
|
{
|
2019-11-21 20:18:04 -05:00
|
|
|
case ESP_SLEEP_WAKEUP_EXT0: updateTime(); break; //RTC Alarm
|
2019-11-17 15:51:25 -05:00
|
|
|
case ESP_SLEEP_WAKEUP_EXT1: handleButton(); break; //Button Press
|
2019-11-21 20:18:04 -05:00
|
|
|
default: updateTime(); //Reset
|
2019-11-17 15:51:25 -05:00
|
|
|
}
|
2019-11-21 20:11:32 -05:00
|
|
|
}
|