metesp/metesp.ino

202 lines
5.2 KiB
Arduino
Raw Normal View History

2022-03-28 00:42:48 +02:00
/* Includes ------------------------------------------------------------------*/
#include "env.h"
#include <Arduino.h>
#include <Wire.h>
#include <stdlib.h>
#include <WiFi.h>
#include <WiFiMulti.h>
2022-03-28 00:42:48 +02:00
#include "src/epd/epd_cfg.h"
#include "src/epd/epd_driver.h"
#include "src/epd/epd_paint.h"
2022-04-26 23:49:18 +02:00
#include "src/ntp/ntp.h"
2022-03-28 00:42:48 +02:00
#include "src/owm/owm.h"
#include "src/sensor/sensor.h"
#include "src/touch/gt1151.h"
#include "src/influxdb/influx.h"
2022-04-26 23:49:18 +02:00
#include "src/display/display.h"
#include "src/timedfun/timedfun.h"
2022-03-28 00:42:48 +02:00
#define PIN_SDA 18
#define PIN_SCL 23
2022-05-03 13:13:37 +02:00
#define uS_TO_S_FACTOR 1000000 //Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP (60*5)
#define BUTTON_PIN_BITMASK 0x8000
2022-05-03 13:13:37 +02:00
2022-03-28 00:42:48 +02:00
2022-05-28 15:06:59 +02:00
void loop_DRAW(void * pvParameters );
void loop_TOUCH(void * pvParameters );
void loop_MEASURE(void * pvParameters );
/* Global Variables -----------------------------------------------------------*/
2022-03-28 00:42:48 +02:00
static WiFiMulti wifiMulti;
static TwoWire I2C = TwoWire(0);
2022-04-27 19:17:55 +02:00
TaskHandle_t task_measure;
TaskHandle_t task_draw;
2022-05-03 17:32:27 +02:00
TaskHandle_t task_touch;
2022-04-26 23:49:18 +02:00
static NTPtime* ntp = new NTPtime(NTP_URL);
2022-03-28 00:42:48 +02:00
static Sensor* sensor = new Sensor(&I2C);
static GT1151* touch = new GT1151(&I2C);
2022-04-08 20:46:49 +02:00
static OWM* owm[6] = {new OWM(LOCATION_0, OPENWEATHER_API),
new OWM(LOCATION_1, OPENWEATHER_API),
2022-04-26 23:49:18 +02:00
new OWM(LOCATION_2, OPENWEATHER_API),
NULL,NULL,NULL};
2022-05-28 15:54:18 +02:00
static Influx* iflx = new Influx(sensor, owm);//TODO: Add sensor
2022-04-27 19:17:55 +02:00
static TimedFun* tf = new TimedFun();
2022-03-28 00:42:48 +02:00
2022-05-03 17:32:27 +02:00
static bool time_update = true, view_update = true, view_refresh = false;
2022-04-26 23:49:18 +02:00
2022-04-27 19:17:55 +02:00
static const int8_t views_size = 6;
2022-04-26 23:49:18 +02:00
static DisplayWrapper* views[views_size] = {
new MainDisplay(),
new WeatherDisplay(owm[0]),
new WeatherDisplay(owm[1]),
new TempDisplay(sensor),
2022-04-27 19:17:55 +02:00
new AirDisplay(sensor),
new LightDisplay(sensor),
2022-04-26 23:49:18 +02:00
};
2022-03-28 00:42:48 +02:00
/* IRQ ------------------------------------------------------------------------*/
hw_timer_t * timer = NULL;
void IRAM_ATTR onSecondTimer() {
2022-04-26 23:49:18 +02:00
strDateTime* dt = &(ntp->dt);
dt->second += 1;
if (dt->second == 60) {
dt->minute += 1;
dt->second = 0;
if (dt->minute == 60) {
dt->hour += 1;
dt->minute = 0;
if (dt->hour == 24) {
dt->hour = 0;
dt->minute = 0;
dt->second = 0;
2022-03-28 00:42:48 +02:00
}
2022-04-26 23:49:18 +02:00
view_refresh =true;
2022-03-28 00:42:48 +02:00
}
view_update = true;
time_update = true;
2022-03-28 00:42:48 +02:00
}
2022-05-28 15:06:59 +02:00
tf->tick();
2022-03-28 00:42:48 +02:00
}
void IRAM_ATTR onTouch(int8_t contacts, GTPoint *points) {
if(touch->dev.holding) return;
2022-04-27 19:17:55 +02:00
if(points[0].y < 125) Display::nextView();
2022-04-26 23:49:18 +02:00
else Display::prevView();
2022-03-28 00:42:48 +02:00
view_update = true;
}
/* Setup Functions -----------------------------------------------------------*/
void setup_TOUCH() {
touch->setHandler(onTouch);
touch->begin();
}
void setup_WIFI(){
uint8_t tries = 3;
WIFI_REGISTER_AP(wifiMulti);
2022-04-08 20:46:49 +02:00
while(wifiMulti.run() != WL_CONNECTED && tries-- >0) delay(500);
2022-05-03 17:32:27 +02:00
if(wifiMulti.run() != WL_CONNECTED) Serial.println("WIFI: Not Connected");
else Serial.println("WIFI: Connected");
2022-03-28 00:42:48 +02:00
}
void setup_TIMER(){
2022-04-26 23:49:18 +02:00
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &onSecondTimer, true);
timerAlarmWrite(timer, 1000000, true);
timerAlarmEnable(timer);
2022-03-28 00:42:48 +02:00
}
2022-04-26 23:49:18 +02:00
#define REGISTER_TIMEDFUN(name,code) static void name (){code;}
#define REGISTER_TIMEDFUN_CALL(name,min) tf->registerFun(name,min)
2022-05-28 15:06:59 +02:00
REGISTER_TIMEDFUN(SENSOR_MEASURE_TF, do{sensor->measure();view_update = true;}while(0));
2022-04-27 02:31:29 +02:00
REGISTER_TIMEDFUN(OWM_MEASURE_TF, do{uint8_t i = 0;while(owm && owm[i]){owm[i]->update(); ++i;}}while(0));
2022-04-26 23:49:18 +02:00
REGISTER_TIMEDFUN(RECORD_LOCAL_TF, iflx->record_local());
REGISTER_TIMEDFUN(RECORD_WEATHER_TF, iflx->record_weather());
REGISTER_TIMEDFUN(NTP_TF, ntp->updateNTPtime());
void setup_TIMEDFUN() {
2022-05-28 15:06:59 +02:00
REGISTER_TIMEDFUN_CALL(NTP_TF, 60*60);
2022-05-28 15:54:18 +02:00
REGISTER_TIMEDFUN_CALL(SENSOR_MEASURE_TF,5); // 5s LOCAL SYNC
REGISTER_TIMEDFUN_CALL(OWM_MEASURE_TF,5*60); // 5m WEATHER SYNC
REGISTER_TIMEDFUN_CALL(RECORD_LOCAL_TF,1*60); // 1m LOCAL DB
// REGISTER_TIMEDFUN_CALL(RECORD_WEATHER_TF,5*60); // 5m WEATHER DB
2022-04-26 23:49:18 +02:00
}
2022-03-28 00:42:48 +02:00
2022-05-03 13:13:37 +02:00
void setup_POWER() {
2022-05-03 20:07:39 +02:00
2022-05-03 13:13:37 +02:00
}
2022-03-28 00:42:48 +02:00
/* Setup Functions --------------------------------------------------------------*/
void setup(){
Serial.begin(115200);
2022-05-03 13:13:37 +02:00
I2C.begin(PIN_SDA, PIN_SCL);
2022-05-03 17:32:27 +02:00
setup_POWER();
2022-05-03 13:13:37 +02:00
Display::setup();
2022-03-28 00:42:48 +02:00
setup_WIFI();
2022-05-03 20:07:39 +02:00
ntp->updateNTPtime();
2022-05-03 17:32:27 +02:00
setup_TOUCH();
2022-03-28 00:42:48 +02:00
setup_TIMER();
2022-04-26 23:49:18 +02:00
setup_TIMEDFUN();
2022-05-28 15:06:59 +02:00
sensor->init();
2022-03-28 00:42:48 +02:00
2022-05-03 17:32:27 +02:00
iflx->check();
2022-04-26 23:49:18 +02:00
Display::setViews(views,views_size);
tf->updateForce();
2022-05-28 15:06:59 +02:00
tf->setTick(ntp->dt.minute*60 + ntp->dt.second);
2022-05-03 17:32:27 +02:00
2022-04-26 23:49:18 +02:00
xTaskCreatePinnedToCore(
loop_MEASURE, "Task LMeasure",
10000, NULL, 1, &task_measure, 0);
xTaskCreatePinnedToCore(
loop_DRAW, "Task LDraw",
10000, NULL, 1, &task_draw, 1);
2022-05-03 17:32:27 +02:00
xTaskCreatePinnedToCore(
loop_TOUCH, "Task LTouch",
10000, NULL, 1, &task_touch, 1);
2022-05-03 13:13:37 +02:00
Serial.println("Completed Setup");
2022-03-28 00:42:48 +02:00
}
2022-04-26 23:49:18 +02:00
/* The main loop -------------------------------------------------------------*/
void loop(){
delay(5000);
2022-03-28 00:42:48 +02:00
}
2022-04-26 23:49:18 +02:00
void loop_DRAW(void * pvParameters ){
for(;;){
if(view_refresh) Display::refresh();
if(view_update) Display::update();
if(time_update) Display::drawTime(&(ntp->dt));
2022-04-08 20:46:49 +02:00
2022-04-26 23:49:18 +02:00
view_refresh = false;
view_update = false;
time_update = false;
2022-05-03 17:32:27 +02:00
vTaskDelay(100);
2022-03-28 00:42:48 +02:00
}
}
2022-05-03 17:32:27 +02:00
void loop_TOUCH(void * pvParameters ){
for(;;){
touch->update();
2022-05-03 17:32:27 +02:00
vTaskDelay(10);
}
}
2022-04-26 23:49:18 +02:00
void loop_MEASURE(void * pvParameters ){
for(;;){
tf->update();
2022-05-28 15:06:59 +02:00
vTaskDelay(500);
2022-03-28 00:42:48 +02:00
}
}