[WIP] Sleep

This commit is contained in:
choelzl 2022-05-03 17:32:27 +02:00
parent a3f967e747
commit 97b0351761
Signed by: sora
GPG Key ID: A362EA0491E2EEA0
9 changed files with 111 additions and 88 deletions

View File

@ -32,12 +32,11 @@
/* Global Variables -----------------------------------------------------------*/
RTC_DATA_ATTR int bootCount = 0;
static WiFiMulti wifiMulti;
static TwoWire I2C = TwoWire(0);
TaskHandle_t task_measure;
TaskHandle_t task_draw;
TaskHandle_t task_touch;
static NTPtime* ntp = new NTPtime(NTP_URL);
@ -52,7 +51,7 @@ static Influx* iflx = new Influx(NULL, owm);//TODO: Add sensor
static TimedFun* tf = new TimedFun();
static bool time_update = false, view_update = true, view_refresh = true;
static bool time_update = true, view_update = true, view_refresh = false;
static bool shutdownt = false;
static int shutdownc = 0;
@ -97,15 +96,15 @@ void IRAM_ATTR onSecondTimer() {
void IRAM_ATTR onTouch(int8_t contacts, GTPoint *points) {
if(touch->dev.holding){
Serial.printf("Draging:\n");
Serial.printf("C0: #0 %d,%d \n", touch->dev.dx, touch->dev.dy);
// Serial.printf("Draging:\n");
// Serial.printf("C0: #0 %d,%d \n", touch->dev.dx, touch->dev.dy);
return;
}
Serial.printf("Contacts: %d\n", contacts);
for (uint8_t i = 0; i < contacts; i++) {
Serial.printf("C%d: #%d %d,%d s:%d\n", i, points[i].id, points[i].x, points[i].y, points[i].a);
yield();
}
// Serial.printf("Contacts: %d\n", contacts);
// for (uint8_t i = 0; i < contacts; i++) {
// Serial.printf("C%d: #%d %d,%d s:%d\n", i, points[i].id, points[i].x, points[i].y, points[i].a);
// yield();
// }
if(points[0].y < 125) Display::nextView();
else Display::prevView();
view_update = true;
@ -122,6 +121,8 @@ void setup_WIFI(){
uint8_t tries = 3;
WIFI_REGISTER_AP(wifiMulti);
while(wifiMulti.run() != WL_CONNECTED && tries-- >0) delay(500);
if(wifiMulti.run() != WL_CONNECTED) Serial.println("WIFI: Not Connected");
else Serial.println("WIFI: Connected");
}
void setup_TIMER(){
@ -141,36 +142,19 @@ REGISTER_TIMEDFUN(RECORD_WEATHER_TF, iflx->record_weather());
REGISTER_TIMEDFUN(NTP_TF, ntp->updateNTPtime());
void setup_TIMEDFUN() {
REGISTER_TIMEDFUN_CALL(NTP_TF, 60);
REGISTER_TIMEDFUN_CALL(SENSOR_MEASURE_TF,5);
REGISTER_TIMEDFUN_CALL(OWM_MEASURE_TF,5);
REGISTER_TIMEDFUN_CALL(RECORD_LOCAL_TF,5);
REGISTER_TIMEDFUN_CALL(RECORD_WEATHER_TF,5);
REGISTER_TIMEDFUN_CALL(NTP_TF, 60);
}
void setup_POWER() {
esp_sleep_enable_ext0_wakeup(GPIO_NUM_15, 1);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, 1);
esp_sleep_enable_gpio_wakeup();
gpio_wakeup_enable(GPIO_NUM_15,GPIO_INTR_HIGH_LEVEL);
// esp_sleep_enable_ext0_wakeup(GPIO_NUM_15, 1);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_sleep_pd_config(ESP_PD_DOMAIN_VDDSDIO,ESP_PD_OPTION_ON);
}
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}
@ -179,20 +163,17 @@ void setup(){
Serial.begin(115200);
I2C.begin(PIN_SDA, PIN_SCL);
I2C.setPins(PIN_SDA,PIN_SCL);
++bootCount;
Serial.println("Boot number: " + String(bootCount));
print_wakeup_reason();
setup_POWER();
Display::setup();
setup_TOUCH();
setup_WIFI();
setup_TOUCH();
setup_TIMER();
setup_TIMEDFUN();
iflx->check();
Display::setViews(views,views_size);
iflx->check();
tf->updateForce();
shutdownc = 0;
@ -204,25 +185,46 @@ void setup(){
loop_DRAW, "Task LDraw",
10000, NULL, 1, &task_draw, 1);
xTaskCreatePinnedToCore(
loop_TOUCH, "Task LTouch",
10000, NULL, 1, &task_touch, 1);
Serial.println("Completed Setup");
}
/* The main loop -------------------------------------------------------------*/
void wakeup(){
view_refresh = true;
view_update = true;
time_update = true;
setup_WIFI();
ntp->updateNTPtime();
vTaskDelay(200);
shutdownc = 0;
}
void loop(){
delay(5000);
delay(500);
if(shutdownc >= 3){
EPD_2IN13_V2_Sleep();
Serial.println("Shuting down");
strDateTime odt = (ntp->dt);
vTaskDelay(2000);
// touch->Reset();
touch->update();
vTaskDelay(500);
esp_light_sleep_start();
Serial.println("Wakey Wakey");
wakeup();
tf->minuteTick(ntp->dt.minute - odt.minute);
}
}
void loop_DRAW(void * pvParameters ){
for(;;){
touch->update();
if(shutdownc >= 10){
Serial.println("Shuting down");
Display::refresh();
EPD_2IN13_V2_Sleep();
esp_deep_sleep_start();
}
if(!touch->dev.holding) shutdownc = 0;
if(shutdownc >= 2){vTaskDelay(500);continue;}
if(view_refresh) Display::refresh();
if(view_update) Display::update();
@ -231,14 +233,23 @@ void loop_DRAW(void * pvParameters ){
view_refresh = false;
view_update = false;
time_update = false;
vTaskDelay(10);
vTaskDelay(100);
}
}
void loop_TOUCH(void * pvParameters ){
for(;;){
if(shutdownc >= 2){vTaskDelay(500);continue;}
touch->update();
if(!touch->dev.holding) shutdownc = 0;
vTaskDelay(10);
}
}
void loop_MEASURE(void * pvParameters ){
for(;;){
if(shutdownc >= 2){vTaskDelay(500);continue;}
tf->update();
vTaskDelay(10);
vTaskDelay(200);
}
}

View File

@ -8,28 +8,31 @@ static int8_t view_count = -1;
static int8_t view_curr = 0;
static DisplayWrapper** views = 0;
void DrawMain(bool v){
Paint_DrawIcon(89,36, ICON_LOGO, &FontIcon, BLACK, WHITE);
Paint_DrawString_EN(29, 0, "Helcel", &Font45, WHITE, BLACK);
if(v)
Paint_DrawString_EN(2,EPD_2IN13_V2_WIDTH - 2 - Font16.Height,"WESP-v0.1", &Font16, WHITE, BLACK);
}
void Display::setup(){
DEV_Module_Init();
EPD_2IN13_V2_Init(EPD_2IN13_V2_FULL);
EPD_2IN13_V2_Clear();
delay(100);
// EPD_2IN13_V2_Init(EPD_2IN13_V2_FULL);
// EPD_2IN13_V2_Clear();
uint16_t Imagesize = ((EPD_2IN13_V2_WIDTH % 8 == 0) ? (EPD_2IN13_V2_WIDTH / 8 ) : (EPD_2IN13_V2_WIDTH / 8 + 1)) * EPD_2IN13_V2_HEIGHT;
if ((BlackImage = (uint8_t *)malloc(Imagesize)) == NULL) exit(1);
Paint_NewImage(BlackImage, EPD_2IN13_V2_WIDTH, EPD_2IN13_V2_HEIGHT, 270, WHITE);
Paint_SelectImage(BlackImage);
Paint_SetMirroring(MIRROR_HORIZONTAL);
Paint_Clear(WHITE);
Paint_DrawIcon(89,36, ICON_LOGO, &FontIcon, BLACK, WHITE);
Paint_DrawString_EN(29, 0, "Helcel", &Font45, WHITE, BLACK);
Paint_SelectImage(BlackImage);
EPD_2IN13_V2_Display(BlackImage);
delay(100);
refresh();
DrawMain(false);
EPD_2IN13_V2_DisplayPart(BlackImage);
}
void Display::drawTime(strDateTime* dt){
Paint_ClearWindows(170, EPD_2IN13_V2_WIDTH - Font24.Height, 170 + Font24.Width * 4.5, EPD_2IN13_V2_WIDTH, WHITE);
Paint_ClearWindows(170, EPD_2IN13_V2_WIDTH - Font24.Height+2, 170 + Font24.Width * 4.5, EPD_2IN13_V2_WIDTH, WHITE);
if (!dt->valid) return;
Paint_DrawTime(170, 98, dt, &Font24, WHITE, BLACK);
EPD_2IN13_V2_DisplayPart(BlackImage);
@ -89,7 +92,7 @@ void WeatherDisplay::drawDisplay() {
JsonObject weather_sys = cowm->weather["sys"];
if(!weather_0 || !weather_main || !weather_sys || !weather_wind) return;
Paint_DrawIcon(0,8, getWeatherIcon(weather_0["id"]), &FontIcon, BLACK, WHITE);
Paint_DrawString_EN(2,EPD_2IN13_V2_WIDTH - 4 - Font16.Height,weather_0["description"], &Font16, WHITE, BLACK);
Paint_DrawString_EN(2,EPD_2IN13_V2_WIDTH - 2 - Font16.Height,weather_0["main"], &Font16, WHITE, BLACK);
Paint_DrawFltUnit(76,0,weather_main["temp"], "C", &Font24, BLACK, WHITE);
Paint_DrawFltUnit(186,6,weather_main["feels_like"], "C", &Font12, BLACK, WHITE);
Paint_DrawIntUnit(76,24,weather_main["pressure"], "hPa", &Font24, BLACK, WHITE);
@ -108,7 +111,7 @@ void TempDisplay::drawDisplay() {
Paint_DrawFltUnit(76,24,sensor->pres, "hPa", &Font24, BLACK, WHITE);
Paint_DrawFltUnit(76,48,sensor->hum, "%", &Font24, BLACK, WHITE);
Paint_DrawString_EN(2,EPD_2IN13_V2_WIDTH - 4 - Font16.Height,"nominal", &Font16, WHITE, BLACK);
Paint_DrawString_EN(2,EPD_2IN13_V2_WIDTH - 2 - Font16.Height,"Nominal", &Font16, WHITE, BLACK);
}
void AirDisplay::drawDisplay() {
@ -131,7 +134,7 @@ void AirDisplay::drawDisplay() {
// particles_50um, ///< 5.0um Particle Count
// particles_100um; ///< 10.0um Particle Count
Paint_DrawString_EN(2,EPD_2IN13_V2_WIDTH - 4 - Font16.Height,"nominal", &Font16, WHITE, BLACK);
Paint_DrawString_EN(2,EPD_2IN13_V2_WIDTH - 2 - Font16.Height,"Nominal", &Font16, WHITE, BLACK);
}
@ -144,14 +147,12 @@ void LightDisplay::drawDisplay() {
Paint_DrawFltUnit(76,24*2,sensor->light, "l", &Font24, BLACK, WHITE);
Paint_DrawIntUnit(76,24*3,sensor->light_vis, "vl", &Font24, BLACK, WHITE);
Paint_DrawString_EN(2,EPD_2IN13_V2_WIDTH - 4 - Font16.Height,"nominal", &Font16, WHITE, BLACK);
Paint_DrawString_EN(2,EPD_2IN13_V2_WIDTH - 2 - Font16.Height,"Nominal", &Font16, WHITE, BLACK);
}
void MainDisplay::drawDisplay() {
Paint_DrawIcon(89,36, ICON_LOGO, &FontIcon, BLACK, WHITE);
Paint_DrawString_EN(29, 0, "Helcel", &Font45, WHITE, BLACK);
Paint_DrawString_EN(2,EPD_2IN13_V2_WIDTH - 4 - Font16.Height,"WESP-v0.1", &Font16, WHITE, BLACK);
DrawMain(true);
}
void CustomDisplay::drawDisplay() {

View File

@ -3,7 +3,6 @@
#define DEVICE "ESP32"
#include <Arduino.h>
#include <WiFi.h>
#include <InfluxDbClient.h>
@ -61,7 +60,6 @@ class Influx {
}
void record(){
if(WiFi.status() != WL_CONNECTED) return;
uint8_t i = 0;
while(owm && owm[i] != NULL){
record_weather(owm[i]);
@ -71,7 +69,6 @@ class Influx {
}
void record(bool w, bool s){
if(WiFi.status() != WL_CONNECTED) return;
uint8_t i = 0;
while(w && owm && owm[i] != NULL){
record_weather(owm[i]);
@ -83,7 +80,7 @@ class Influx {
void record_local(){
if(!sensor) return;
if(WiFi.status() != WL_CONNECTED) return;
Serial.println("InfluxDB: Uploading Local Data");
dp.clearFields();
dp.clearTags();
@ -121,6 +118,7 @@ class Influx {
dp.addField("particles_100um", sensor->pmd.particles_100um);
client.writePoint(dp);
Serial.println("InfluxDB: OK");
}
void record_weather(){
@ -129,8 +127,11 @@ class Influx {
void record_weather(OWM* owm){
if(!owm) return;
if(!owm->valid_weather) return;
if(WiFi.status() != WL_CONNECTED) return;
Serial.println("InfluxDB: Uploading OWM Data");
if(!owm->valid_weather) {
Serial.println("InfluxDB: Error, not valid weather");
return;
}
JsonObject weather_0 = owm->weather["weather"][0];
JsonObject weather_main = owm->weather["main"];
@ -167,6 +168,7 @@ class Influx {
dp.addField("precipitation_snow_3h",weather_snow?float(weather_snow["3h"]):0.0);
client.writePoint(dp);
Serial.println("InfluxDB: OK");
}

View File

@ -226,7 +226,6 @@ strDateTime NTPtime::getNTPtime(float _timeZone, boolean _DayLightSaving) {
void NTPtime::updateNTPtime(){
if(WiFi.status() != WL_CONNECTED) return;
strDateTime ndt = getNTPtime(1.0f, true);
if(ndt.valid) dt = ndt;
};

View File

@ -2,7 +2,6 @@
#define NTPtime_h
#include <WiFiUdp.h>
#include <WiFi.h>
struct strDateTime {
byte hour;

View File

@ -1,9 +1,9 @@
#ifndef OWM_H
#define OWM_H
#include <Arduino.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <WiFi.h>
#define OPENWEATHER_URL(loc, api) ("https://api.openweathermap.org/data/2.5/weather?q=" + loc + "&appid=" + api + "&units=metric")
@ -18,20 +18,24 @@ class OWM{
bool valid_weather = false;
bool update(){
if(WiFi.status() != WL_CONNECTED) return false;
Serial.println("OWWM: Updating Data");
String response = httpGETRequest(OPENWEATHER_URL(location,key).c_str());
DeserializationError error = deserializeJson(weather, response);
if (error) {
Serial.println("OWWM: Error, request or response invalid");
valid_weather = false;
return false;
}
if(weather["weather"]==NULL ||weather["main"]==NULL ||
weather["wind"]==NULL || weather["sys"]==NULL ){
Serial.println("OWWM: Error, missing fields");
valid_weather = false;
return false;
}
Serial.println("OWWM: OK");
valid_weather = true;
return true;
}

View File

@ -3,7 +3,7 @@
#ifndef TG_TIMEDFUN_H
#define TG_TIMEDFUN_H
#include <map>
#include <vector>
#include <stdint.h>
#include <Arduino.h>
@ -14,22 +14,27 @@ class TimedFun {
TimedFun(){};
void registerFun(timedfun_ptr p, uint64_t s){
funmap[p] = s;
funmap.push_back(std::pair<timedfun_ptr,uint64_t>(p,s));
};
void registerFunCond(timedfun_ptr p, uint64_t s){
funmap[p] = s;
funmap.push_back(std::pair<timedfun_ptr,uint64_t>(p,s));
};
void minuteTick(){
++counter;
}
void minuteTick(int64_t m){
counter += m;
}
void update(){
uint64_t ccounter = old_counter;
old_counter = counter;
for (const auto &e : funmap){
if((counter % e.second == 0) ||
((counter-old_counter) >= e.second) ||
((counter%e.second) < (old_counter%e.second))){
if( ((counter % e.second == 0) && (counter != ccounter)) ||
((counter-ccounter) >= e.second) || ((counter%e.second) < (ccounter%e.second))){
e.first();
}
}
@ -37,15 +42,17 @@ class TimedFun {
};
void updateForce(){
Serial.println("TF: Force updating all");
for (const auto &e : funmap){
e.first();
}
Serial.println("TF: OK");
}
private:
uint64_t counter = 0;
uint64_t old_counter = 0;
std::map<timedfun_ptr,uint64_t> funmap;
std::vector<std::pair<timedfun_ptr,uint64_t>> funmap;
};

View File

@ -56,7 +56,7 @@ int16_t GT1151::readInput(GTPoint* points) {
void GT1151::Reset(void){
pinMode(GT1151_INT_PIN, OUTPUT);
pinMode(GT1151_RESET_PIN, OUTPUT);
pinMode(GT1151_INT_PIN, INPUT);
digitalWrite(GT1151_RESET_PIN, HIGH);
delay(100);

View File

@ -49,6 +49,7 @@ class GT1151{
void update(void);
void begin(void);
void Reset(void);
void Gesture(void);
void setHandler(void (*handler)(int8_t, GTPoint*));
@ -56,7 +57,6 @@ class GT1151{
void (*touchHandler)(int8_t, GTPoint*);
TwoWire* wire;
void Reset(void);
void armIRQ(void);
void onIRQ(void);