Created
July 7, 2020 09:45
-
-
Save benevpi/0b1d70b841aa16585033c2112baf821b to your computer and use it in GitHub Desktop.
A simple step counter watch for lilygo2020 v1. Note - also needs a config.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "config.h" | |
// ToDO | |
// There's a power bug that stops the screen going to sleep if the USB's been unpowered | |
// This also doesn't update the steps if the screen's been off UNLESS there's one more step (interrrupt) | |
// Get weather from wifi IF it's being charged | |
// Make it last a day comfortably of full use | |
// Save steps per day between power downs | |
// upload steps somewhere? Google Sheets? | |
TTGOClass *ttgo; | |
char buf[128]; | |
bool irq = false; | |
bool toggle_screen = true; | |
void low_energy() | |
{ | |
if (toggle_screen) { | |
ttgo->closeBL(); | |
ttgo->bma->enableStepCountInterrupt(false); | |
ttgo->displaySleep(); | |
toggle_screen = false; | |
} else { | |
ttgo->displayWakeup(); | |
ttgo->openBL(); | |
ttgo->bma->enableStepCountInterrupt(); | |
toggle_screen=true; | |
} | |
} | |
void setup() | |
{ | |
ttgo = TTGOClass::getWatch(); | |
ttgo->begin(); | |
setCpuFrequencyMhz(20); | |
ttgo->openBL(); | |
pinMode(BMA423_INT1, INPUT); | |
attachInterrupt(BMA423_INT1, [] { | |
irq = 1; | |
}, RISING); | |
ttgo->bma->begin(); | |
ttgo->bma->attachInterrupt(); | |
pinMode(AXP202_INT, INPUT_PULLUP); | |
attachInterrupt(AXP202_INT, [] { | |
irq = true; | |
}, FALLING); | |
//!Clear IRQ unprocessed first | |
ttgo->power->enableIRQ(AXP202_PEK_SHORTPRESS_IRQ | AXP202_VBUS_REMOVED_IRQ | AXP202_VBUS_CONNECT_IRQ | AXP202_CHARGING_IRQ, true); | |
ttgo->power->clearIRQ(); | |
ttgo->power->adc1Enable(AXP202_VBUS_VOL_ADC1 | AXP202_VBUS_CUR_ADC1 | AXP202_BATT_CUR_ADC1 | AXP202_BATT_VOL_ADC1, true); | |
ttgo->tft->fillScreen(TFT_BLACK); | |
// a really clumsy way of setting time -- uncoment this and put in the correct time, then comment it and re-flash | |
//ttgo->rtc->setDateTime(2020, 7, 7, 10, 01, 50); | |
} | |
void loop() | |
{ | |
int per = ttgo->power->getBattPercentage(); | |
if (irq) { | |
irq = 0; | |
bool rlst; | |
do { | |
rlst = ttgo->bma->readInterrupt(); | |
} while (!rlst); | |
ttgo->power->readIRQ(); | |
if (ttgo->bma->isStepCounter()) { | |
Serial.println(ttgo->bma->getCounter()); | |
ttgo->tft->setTextColor(random(0xFFFF), TFT_BLACK); | |
snprintf(buf, sizeof(buf), "Steps: %u", ttgo->bma->getCounter()); | |
ttgo->tft->drawString(buf, 22, 60, 4); | |
} | |
if (ttgo->power->isPEKShortPressIRQ()) { | |
low_energy(); | |
ttgo->power->clearIRQ(); | |
} | |
} | |
//ttgo->tft->setCursor(20, 190); | |
//ttgo->tft->print("Battery: "); ttgo->tft->print(per); ttgo->tft->println(" %"); | |
snprintf(buf, sizeof(buf), "Battery: %u", ttgo->power->getBattPercentage()); | |
ttgo->tft->drawString(buf, 22, 80, 4); | |
snprintf(buf, sizeof(buf), "%s", ttgo->rtc->formatDateTime()); | |
ttgo->tft->drawString(buf, 1, 10, 7); | |
delay(200); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment