Version 0.9
This commit is contained in:
219
src/main.cpp
219
src/main.cpp
@@ -1,4 +1,3 @@
|
||||
// change next line to use with another board/shield
|
||||
#include <WiFi.h>
|
||||
#include <SPI.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
@@ -9,19 +8,22 @@
|
||||
|
||||
#include <HTTPClient.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <DateTime.h>
|
||||
#include <IotWebConf.h>
|
||||
#include <NTP.h>
|
||||
#include <SPIFFS.h>
|
||||
#include <time.h>
|
||||
#include <DateTime.h>
|
||||
|
||||
#include "ESP32TimerInterrupt.h"
|
||||
#include "Scroller.h"
|
||||
#include "calendar.h"
|
||||
#include "TimeZone.h"
|
||||
#ifndef PSTR
|
||||
#define PSTR // Make Arduino Due happy
|
||||
#endif
|
||||
|
||||
#define COMMAND_PARAMETER_LENGTH 30
|
||||
|
||||
#define MAX_TIMES_SCROLLED 5
|
||||
#define PIN 15
|
||||
|
||||
#include <WiFiUdp.h>
|
||||
@@ -49,7 +51,7 @@ enum Lines_names
|
||||
String Lines[LAST_LINE];
|
||||
|
||||
Adafruit_NeoMatrix *matrix;
|
||||
const uint16_t colors[] = {
|
||||
uint16_t colors[] = {
|
||||
Adafruit_NeoMatrix::Color(255, 0, 0), Adafruit_NeoMatrix::Color(0, 255, 0), Adafruit_NeoMatrix::Color(0, 0, 255)};
|
||||
|
||||
WiFiUDP ntpUDP;
|
||||
@@ -74,10 +76,53 @@ struct timer_def {
|
||||
int when; int h; int m; int d; int mh; int y;
|
||||
};
|
||||
|
||||
int ntimers;
|
||||
int ntimers,nextTimer = -1;
|
||||
int nts;
|
||||
|
||||
DateTimeClass nextAlarmDateTime;
|
||||
#define MAX_TIMERS 10
|
||||
timer_def Timers[MAX_TIMERS];
|
||||
|
||||
Scroller scroller(MAX_TIMES_SCROLLED);
|
||||
|
||||
unsigned long last_millis;
|
||||
bool showText = false;
|
||||
int scrolledTicker = 0;
|
||||
bool updateNtpTime = false;
|
||||
|
||||
ESP32Timer ITimer0(0);
|
||||
ESP32Timer ITimer1(1);
|
||||
ESP32Timer ITimer2(2);
|
||||
|
||||
#define TIMER0_INTERVAL_MS 1000
|
||||
#define TIMER1_INTERVAL_MS 100
|
||||
#define TIMER2_INTERVAL_MS 60000
|
||||
|
||||
void IRAM_ATTR TimerCheckTime0(void)
|
||||
{
|
||||
unsigned long m = millis(), delta;
|
||||
|
||||
delta = (m - last_millis)/1000;
|
||||
|
||||
DateTime += delta;
|
||||
|
||||
if (nextAlarmDateTime <= DateTime && nextTimer != -1) {
|
||||
showText = true;
|
||||
scroller.TurnOn();
|
||||
}
|
||||
|
||||
last_millis = m;
|
||||
}
|
||||
|
||||
void IRAM_ATTR TimerScrollTime1(void)
|
||||
{
|
||||
scrolledTicker++;
|
||||
}
|
||||
|
||||
void IRAM_ATTR TimerUpdateNTP2(void)
|
||||
{
|
||||
updateNtpTime = true;
|
||||
}
|
||||
|
||||
String getValue(String data, char separator, int index)
|
||||
{
|
||||
@@ -212,7 +257,7 @@ void read_config()
|
||||
{
|
||||
s += (char)file.read();
|
||||
}
|
||||
Serial.print(s);
|
||||
Serial.println(s);
|
||||
|
||||
file.close();
|
||||
|
||||
@@ -258,7 +303,16 @@ void read_config()
|
||||
void calculate_timers()
|
||||
{
|
||||
timer_def *t;
|
||||
int timer = -1;
|
||||
struct tm tm;
|
||||
DateTimeClass AlarmTime,NextAlarm;
|
||||
time_t alarm;
|
||||
TimeZone TZ;
|
||||
|
||||
Serial.println("---CALC TIMERS---");
|
||||
TZ.ruleDST("CEST", Last, Sun, Mar, 2, 120); // last sunday in march 2:00, timetone +120min (+1 GMT + 1h summertime offset)
|
||||
TZ.ruleSTD("CET", Last, Sun, Oct, 3, 60); // last sunday in october 3:00, timezone +60min (+1 GMT)
|
||||
|
||||
for (int i=0; i<ntimers ; i++)
|
||||
{
|
||||
t = &Timers[i];
|
||||
@@ -269,8 +323,8 @@ void calculate_timers()
|
||||
|
||||
if (t->d !=0) {
|
||||
tm.tm_mday = t->d;
|
||||
tm.tm_mon = t->mh;
|
||||
tm.tm_year = t->y;
|
||||
tm.tm_mon = t->mh - 1;
|
||||
tm.tm_year = t->y - 1900;
|
||||
} else {
|
||||
tm.tm_mday = 0;
|
||||
tm.tm_mon = 0;
|
||||
@@ -279,15 +333,67 @@ void calculate_timers()
|
||||
|
||||
if (t->d == 0) {
|
||||
if (t->when == WORK_DAYS) {
|
||||
int offset=24*3600;
|
||||
int wday;
|
||||
|
||||
tm.tm_mday = DateTime.getParts().getMonthDay();
|
||||
tm.tm_mon = DateTime.getParts().getMonth();
|
||||
tm.tm_year = DateTime.getParts().getYear() - 1900;
|
||||
wday = DateTime.getParts().getWeekDay();
|
||||
|
||||
if (wday == 0) offset = 3600*24;
|
||||
if (wday == 6) offset = 2*3600*24;
|
||||
|
||||
alarm = mktime(&tm);
|
||||
TZ.setLocalTime(alarm);
|
||||
|
||||
AlarmTime.setTime(TZ.getUTCTime());
|
||||
|
||||
if (AlarmTime > DateTime && wday == 5 ) offset = 3*3600*24;
|
||||
|
||||
TZ.setLocalTime(alarm + offset);
|
||||
AlarmTime.setTime(TZ.getUTCTime());
|
||||
|
||||
} else if (t->when == WEEKENDS) {
|
||||
int offset=24*3600;
|
||||
int wday;
|
||||
|
||||
} else if (t->when == TOMOROW ) {
|
||||
tm.tm_mday = DateTime.getParts().getMonthDay();
|
||||
tm.tm_mon = DateTime.getParts().getMonth();
|
||||
tm.tm_year = DateTime.getParts().getYear() - 1900;
|
||||
wday = DateTime.getParts().getWeekDay();
|
||||
|
||||
if (wday >= 1 && wday <= 5) offset = (7-wday) *3600*24;
|
||||
|
||||
alarm = mktime(&tm);
|
||||
TZ.setLocalTime(alarm);
|
||||
AlarmTime.setTime(TZ.getUTCTime());
|
||||
|
||||
if (AlarmTime > DateTime && wday == 5 ) offset = 3*3600*24;
|
||||
|
||||
TZ.setLocalTime(alarm + offset);
|
||||
AlarmTime.setTime(TZ.getUTCTime());
|
||||
|
||||
} else if (t->when == ONCE ) {
|
||||
alarm = mktime(&tm);
|
||||
TZ.setLocalTime(alarm);
|
||||
AlarmTime.setTime(TZ.getUTCTime());
|
||||
}
|
||||
|
||||
if (AlarmTime > DateTime && (AlarmTime < NextAlarm || timer == -1)) {
|
||||
timer = i;
|
||||
NextAlarm = AlarmTime;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (timer != -1) {
|
||||
nextTimer = timer;
|
||||
nextAlarmDateTime = NextAlarm;
|
||||
Serial.println(DateTime.toString());
|
||||
Serial.println(nextAlarmDateTime.toString());
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
@@ -304,6 +410,22 @@ void setup()
|
||||
return;
|
||||
}
|
||||
|
||||
if (ITimer0.attachInterruptInterval(TIMER0_INTERVAL_MS * 1000, TimerCheckTime0))
|
||||
Serial.println("Starting ITimer0 OK, millis() = " + String(millis()));
|
||||
else
|
||||
Serial.println("Can't set ITimer0. Select another freq. or timer");
|
||||
|
||||
// Interval in microsecs
|
||||
if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS * 1000, TimerScrollTime1))
|
||||
Serial.println("Starting ITimer1 OK, millis() = " + String(millis()));
|
||||
else
|
||||
Serial.println("Can't set ITimer1. Select another freq. or timer");
|
||||
|
||||
if (ITimer2.attachInterruptInterval(TIMER2_INTERVAL_MS * 1000, TimerUpdateNTP2))
|
||||
Serial.println("Starting ITimer2 OK, millis() = " + String(millis()));
|
||||
else
|
||||
Serial.println("Can't set ITimer2. Select another freq. or timer");
|
||||
|
||||
|
||||
// -- Set up required URL handlers on the web server.
|
||||
server.on("/", handleRoot);
|
||||
@@ -313,42 +435,11 @@ void setup()
|
||||
|
||||
setup_matrix();
|
||||
read_config();
|
||||
|
||||
scroller.SetColors(colors);
|
||||
scroller.SetMatrix(matrix);
|
||||
}
|
||||
|
||||
void scroll_line(String line)
|
||||
{
|
||||
int x = 0, mx = 32;
|
||||
int y = 0;
|
||||
|
||||
if (line.length() * 6 > mx)
|
||||
{
|
||||
for (x = 0; x < line.length() * 6 - mx; x++)
|
||||
{
|
||||
matrix->fillScreen(0);
|
||||
matrix->setCursor(-x, y);
|
||||
matrix->setTextColor(colors[1]);
|
||||
matrix->print(line);
|
||||
matrix->show();
|
||||
if (x == 0)
|
||||
delay(300);
|
||||
else
|
||||
delay(100);
|
||||
}
|
||||
delay(1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
matrix->fillScreen(0);
|
||||
matrix->setCursor(x, y);
|
||||
matrix->setTextColor(colors[1]);
|
||||
matrix->print(line);
|
||||
matrix->show();
|
||||
delay(3000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void handleRoot()
|
||||
{
|
||||
// -- Let IotWebConf test and handle captive portal requests.
|
||||
@@ -385,32 +476,48 @@ void loop()
|
||||
unsigned long n = 0;
|
||||
|
||||
// -- doLoop should be called as frequently as possible.
|
||||
iotWebConf.doLoop();
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
{
|
||||
if (initialized == false)
|
||||
{
|
||||
Serial.println("---INIT---");
|
||||
ntp.ruleDST("CEST", Last, Sun, Mar, 2, 120); // last sunday in march 2:00, timetone +120min (+1 GMT + 1h summertime offset)
|
||||
ntp.ruleSTD("CET", Last, Sun, Oct, 3, 60); // last sunday in october 3:00, timezone +60min (+1 GMT)
|
||||
ntp.begin();
|
||||
sec = ntp.epoch();
|
||||
DateTime.setTime(sec);
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
sec = ntp.epoch();
|
||||
DateTime.setTime(sec);
|
||||
|
||||
if (n++ % 25 == 0)
|
||||
Lines[WEATHER_LINE] = get_weather_line();
|
||||
if (updateNtpTime) {
|
||||
Serial.println("---NTP UPDATE---");
|
||||
ntp.update();
|
||||
sec = ntp.epoch();
|
||||
DateTime.setTime(sec);
|
||||
calculate_timers();
|
||||
updateNtpTime = false;
|
||||
|
||||
|
||||
Lines[TIME_LINE] = get_time_line();
|
||||
Lines[DATE_LINE] = get_date_line();
|
||||
Lines[NAMES_LINE] = get_names_line();
|
||||
if (n++ % 10 == 0)
|
||||
Lines[WEATHER_LINE] = get_weather_line();
|
||||
|
||||
for (int i = 0; i < LAST_LINE; i++)
|
||||
{
|
||||
Serial.println(Lines[i]);
|
||||
scroll_line(Lines[i]);
|
||||
iotWebConf.doLoop();
|
||||
Lines[TIME_LINE] = get_time_line();
|
||||
Lines[DATE_LINE] = get_date_line();
|
||||
Lines[NAMES_LINE] = get_names_line();
|
||||
|
||||
scroller.AddLines(Lines,LAST_LINE);
|
||||
|
||||
}
|
||||
|
||||
if (showText) {
|
||||
Serial.println("---SHOW TEXT----");
|
||||
scroller.loop(scrolledTicker);
|
||||
|
||||
if (scroller.TimesScrolled() > MAX_TIMES_SCROLLED) showText = false;
|
||||
}
|
||||
}
|
||||
|
||||
iotWebConf.doLoop();
|
||||
}
|
||||
Reference in New Issue
Block a user