Skip to content

Instantly share code, notes, and snippets.

@benevpi
Created July 10, 2020 12:32
Show Gist options
  • Save benevpi/d0b910c24bccf8745315c7cc602f2592 to your computer and use it in GitHub Desktop.
Save benevpi/d0b910c24bccf8745315c7cc602f2592 to your computer and use it in GitHub Desktop.
A more-working smart watch pedometer
#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?
// no need to do processing (such as time updates) if the screen is off.
// could just get steps when the screen is turned on (maybe battery as well).
//try light sleep rather than delay? something like:
// let's not over complicate things. The battery lasts all day anyway. Can look at this if/when needed
// esp_sleep_enable_timer_wakeup(1000000); //1 seconds
// int ret = esp_light_sleep_start();
//next thing to do -- detect charging and do wifi (weather) stuff when plugged in.
//if that works, work on online step tracking
//need to work out what's going on with screen not coming on after charging
//need to add a load of serial output lines and see what interrupts / states are being called
//do I want things like detecting periods of inactivity? That might be nice.
// first version (no auto screen off and delay 200) had about 40% battery left at the end of the day
TTGOClass *ttgo;
//config opitions
int sleep_time = 3000; // turn screen off after 3 seconds
//loop delay
// wifi options
//weather option
char buf[128];
bool irq = false;
bool toggle_screen = true;
int last_on = 0;
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;
last_on = millis();
//update step counter only when the screen is refreshed
ttgo->tft->setTextColor(random(0xFFFF), TFT_BLACK);
snprintf(buf, sizeof(buf), "Steps: %u", ttgo->bma->getCounter());
ttgo->tft->drawString(buf, 22, 60, 4);
}
}
void setup()
{
ttgo = TTGOClass::getWatch();
ttgo->begin();
setCpuFrequencyMhz(10);
ttgo->openBL();
pinMode(BMA423_INT1, INPUT);
//do we need interrupts on BMA now? yes, step counter doesn't work unless we process all the interrupts
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);
last_on = millis();
}
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()) {
//don't think we need to update step counter here
// }
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);
if(toggle_screen & millis() > (last_on + sleep_time)) { low_energy(); } // turn screen off after set number of seconds
delay(300);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment