Browse Source

improved scheduler #483

refactored NTP
generate bin.gz only for ESP8285
fix calcSunrise calculation
pull/498/head
lumapu 2 years ago
parent
commit
183b9b35f6
  1. 6
      scripts/getVersion.py
  2. 14
      src/app.cpp
  3. 10
      src/app.h
  4. 2
      src/defines.h
  5. 33
      src/utils/scheduler.h
  6. 16
      src/wifi/ahoywifi.cpp

6
scripts/getVersion.py

@ -56,19 +56,16 @@ def readVersion(path, infile):
src = path + ".pio/build/esp8266-release/firmware.bin" src = path + ".pio/build/esp8266-release/firmware.bin"
dst = path + "firmware/" + versionout dst = path + "firmware/" + versionout
os.rename(src, dst) os.rename(src, dst)
gzip_bin(dst, dst + ".gz")
versionout = version[:-1] + "_esp8266_nokia5110_" + sha + ".bin" versionout = version[:-1] + "_esp8266_nokia5110_" + sha + ".bin"
src = path + ".pio/build/esp8266-nokia5110/firmware.bin" src = path + ".pio/build/esp8266-nokia5110/firmware.bin"
dst = path + "firmware/" + versionout dst = path + "firmware/" + versionout
os.rename(src, dst) os.rename(src, dst)
gzip_bin(dst, dst + ".gz")
versionout = version[:-1] + "_esp8266_ssd1306_" + sha + ".bin" versionout = version[:-1] + "_esp8266_ssd1306_" + sha + ".bin"
src = path + ".pio/build/esp8266-ssd1306/firmware.bin" src = path + ".pio/build/esp8266-ssd1306/firmware.bin"
dst = path + "firmware/" + versionout dst = path + "firmware/" + versionout
os.rename(src, dst) os.rename(src, dst)
gzip_bin(dst, dst + ".gz")
versionout = version[:-1] + "_esp8285_" + sha + ".bin" versionout = version[:-1] + "_esp8285_" + sha + ".bin"
src = path + ".pio/build/esp8285-release/firmware.bin" src = path + ".pio/build/esp8285-release/firmware.bin"
@ -80,19 +77,16 @@ def readVersion(path, infile):
src = path + ".pio/build/esp32-wroom32-release/firmware.bin" src = path + ".pio/build/esp32-wroom32-release/firmware.bin"
dst = path + "firmware/" + versionout dst = path + "firmware/" + versionout
os.rename(src, dst) os.rename(src, dst)
gzip_bin(dst, dst + ".gz")
versionout = version[:-1] + "_esp32_nokia5110_" + sha + ".bin" versionout = version[:-1] + "_esp32_nokia5110_" + sha + ".bin"
src = path + ".pio/build/esp32-wroom32-nokia5110/firmware.bin" src = path + ".pio/build/esp32-wroom32-nokia5110/firmware.bin"
dst = path + "firmware/" + versionout dst = path + "firmware/" + versionout
os.rename(src, dst) os.rename(src, dst)
gzip_bin(dst, dst + ".gz")
versionout = version[:-1] + "_esp32_ssd1306_" + sha + ".bin" versionout = version[:-1] + "_esp32_ssd1306_" + sha + ".bin"
src = path + ".pio/build/esp32-wroom32-ssd1306/firmware.bin" src = path + ".pio/build/esp32-wroom32-ssd1306/firmware.bin"
dst = path + "firmware/" + versionout dst = path + "firmware/" + versionout
os.rename(src, dst) os.rename(src, dst)
gzip_bin(dst, dst + ".gz")
# other ESP32 bin files # other ESP32 bin files
src = path + ".pio/build/esp32-wroom32-release/" src = path + ".pio/build/esp32-wroom32-release/"

14
src/app.cpp

@ -33,10 +33,9 @@ void app::setup() {
everySec(std::bind(&app::tickSecond, this)); everySec(std::bind(&app::tickSecond, this));
everyMin(std::bind(&app::tickMinute, this));
every12h(std::bind(&app::tickNtpUpdate, this));
every(std::bind(&app::tickSend, this), mConfig->nrf.sendInterval); every(std::bind(&app::tickSend, this), mConfig->nrf.sendInterval);
#if !defined(AP_ONLY) #if !defined(AP_ONLY)
once(std::bind(&app::tickNtpUpdate), 2);
if((mConfig->sun.lat) && (mConfig->sun.lon)) { if((mConfig->sun.lat) && (mConfig->sun.lon)) {
mCalculatedTimezoneOffset = (int8_t)((mConfig->sun.lon >= 0 ? mConfig->sun.lon + 7.5 : mConfig->sun.lon - 7.5) / 15) * 3600; mCalculatedTimezoneOffset = (int8_t)((mConfig->sun.lon >= 0 ? mConfig->sun.lon + 7.5 : mConfig->sun.lon - 7.5) / 15) * 3600;
once(std::bind(&app::tickCalcSunrise, this), 5); once(std::bind(&app::tickCalcSunrise, this), 5);
@ -131,6 +130,15 @@ void app::loop(void) {
mMqtt.loop(); mMqtt.loop();
} }
//-----------------------------------------------------------------------------
void app::tickNtpUpdate(void) {
uint32_t nxtTrig = 5; // default: check again in 5 sec
if (mWifi.getNtpTime())
nxtTrig = 43200; // check again in 12 h
once(std::bind(&app::tickNtpUpdate, this), nxtTrig);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::tickCalcSunrise(void) { void app::tickCalcSunrise(void) {
if (0 == mTimestamp) { if (0 == mTimestamp) {
@ -139,7 +147,7 @@ void app::tickCalcSunrise(void) {
} }
ah::calculateSunriseSunset(mTimestamp, mCalculatedTimezoneOffset, mConfig->sun.lat, mConfig->sun.lon, &mSunrise, &mSunset); ah::calculateSunriseSunset(mTimestamp, mCalculatedTimezoneOffset, mConfig->sun.lat, mConfig->sun.lon, &mSunrise, &mSunset);
uint32_t nxtTrig = mTimestamp - (mTimestamp % 86400) + 86400; // next midnight uint32_t nxtTrig = mTimestamp - ((mTimestamp + 1000) % 86400) + 86400; // next midnight
onceAt(std::bind(&app::tickCalcSunrise, this), nxtTrig); onceAt(std::bind(&app::tickCalcSunrise, this), nxtTrig);
if (mConfig->mqtt.broker[0] > 0) { if (mConfig->mqtt.broker[0] > 0) {
once(std::bind(&PubMqttType::tickerSun, &mMqtt), 1); once(std::bind(&PubMqttType::tickerSun, &mMqtt), 1);

10
src/app.h

@ -143,15 +143,7 @@ class app : public ah::Scheduler {
} }
} }
void tickMinute(void) { void tickNtpUpdate(void);
if(0 == mTimestamp) {
mUpdateNtp = true;
}
}
void tickNtpUpdate(void) {
mUpdateNtp = true;
}
void tickCalcSunrise(void); void tickCalcSunrise(void);
void tickSend(void); void tickSend(void);

2
src/defines.h

@ -13,7 +13,7 @@
//------------------------------------- //-------------------------------------
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 5 #define VERSION_MINOR 5
#define VERSION_PATCH 50 #define VERSION_PATCH 51
//------------------------------------- //-------------------------------------
typedef struct { typedef struct {

33
src/utils/scheduler.h

@ -48,20 +48,28 @@ namespace ah {
void loop(void) { void loop(void) {
mMillis = millis(); mMillis = millis();
mDiff = mMillis - mPrevMillis; mDiff = mMillis - mPrevMillis;
if(mDiff >= 1000) { if (mDiff < 1000)
if(mMillis < mPrevMillis) { // overflow return;
mPrevMillis = mMillis;
return; mDiffSeconds = 1;
if (mDiff < 2000)
mPrevMillis += 1000;
else {
if (mMillis < mPrevMillis) { // overflow
mDiff = mMillis;
if (mDiff < 1000)
return;
} }
mDiffSeconds = mDiff / 1000; mDiffSeconds = mDiff / 1000;
mDiffFraq = mDiff % 1000; mPrevMillis += (mDiffSeconds * 1000);
mPrevMillis += (mDiffSeconds * 1000) - mDiffFraq;
checkEvery();
checkAt();
mUptime += mDiffSeconds;
if(0 != mTimestamp)
mTimestamp += mDiffSeconds;
} }
mUptime += mDiffSeconds;
if(0 != mTimestamp)
mTimestamp += mDiffSeconds;
checkEvery();
checkAt();
} }
void once(scdCb c, uint32_t timeout) { mStack.add(c, timeout, 0); } void once(scdCb c, uint32_t timeout) { mStack.add(c, timeout, 0); }
@ -105,6 +113,7 @@ namespace ah {
if(expired) { if(expired) {
(p->d.c)(); (p->d.c)();
yield();
if(0 == p->d.reload) if(0 == p->d.reload)
p = mStack.rem(p); p = mStack.rem(p);
else { else {
@ -122,6 +131,7 @@ namespace ah {
while(NULL != p) { while(NULL != p) {
if((p->d.timestamp) <= mTimestamp) { if((p->d.timestamp) <= mTimestamp) {
(p->d.c)(); (p->d.c)();
yield();
p = mStackAt.rem(p); p = mStackAt.rem(p);
} }
else else
@ -134,7 +144,6 @@ namespace ah {
uint32_t mMillis, mPrevMillis, mDiff; uint32_t mMillis, mPrevMillis, mDiff;
uint32_t mUptime; uint32_t mUptime;
uint8_t mDiffSeconds; uint8_t mDiffSeconds;
uint16_t mDiffFraq;
}; };
} }

16
src/wifi/ahoywifi.cpp

@ -115,9 +115,11 @@ void ahoywifi::setupStation(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void ahoywifi::getNtpTime(void) { bool ahoywifi::getNtpTime(void) {
//DPRINTLN(DBG_VERBOSE, F("wifi::getNtpTime")); //DPRINTLN(DBG_VERBOSE, F("wifi::getNtpTime"));
time_t date = 0; if(!mConnected)
return false;
IPAddress timeServer; IPAddress timeServer;
uint8_t buf[NTP_PACKET_SIZE]; uint8_t buf[NTP_PACKET_SIZE];
uint8_t retry = 0; uint8_t retry = 0;
@ -138,16 +140,16 @@ void ahoywifi::getNtpTime(void) {
secsSince1900 |= (buf[42] << 8); secsSince1900 |= (buf[42] << 8);
secsSince1900 |= (buf[43] ); secsSince1900 |= (buf[43] );
date = secsSince1900 - 2208988800UL; // UTC time *mUtcTimestamp = secsSince1900 - 2208988800UL; // UTC time
break; DPRINTLN(DBG_INFO, F("[NTP]: ") + ah::getDateTimeStr(*mUtcTimestamp) + F(" UTC"));
return true;
} else } else
delay(10); delay(10);
} }
} }
*mUtcTimestamp = date; DPRINTLN(DBG_INFO, F("[NTP]: getNtpTime failed"));
return false;
DPRINTLN(DBG_INFO, "[NTP]: " + ah::getDateTimeStr(*mUtcTimestamp) + " UTC");
} }

Loading…
Cancel
Save