ESP32-PaxCounter/src/cyclic.cpp

102 lines
2.9 KiB
C++
Raw Normal View History

2018-07-22 20:27:58 +02:00
/* This routine is called by interrupt in regular intervals */
/* Interval can be set in paxcounter.conf (HOMECYCLE) */
// Basic config
#include "globals.h"
// Local logging tag
static const char TAG[] = "main";
2018-09-30 22:06:10 +02:00
portMUX_TYPE mutexHomeCycle = portMUX_INITIALIZER_UNLOCKED;
2018-07-22 20:27:58 +02:00
// do all housekeeping
2018-09-21 18:23:34 +02:00
void doHousekeeping() {
portENTER_CRITICAL(&mutexHomeCycle);
2018-09-21 18:23:34 +02:00
HomeCycleIRQ = 0;
portEXIT_CRITICAL(&mutexHomeCycle);
2018-07-25 00:17:31 +02:00
// update uptime counter
uptime();
2018-07-24 23:25:41 +02:00
2018-09-15 18:59:20 +02:00
// check if update mode trigger switch was set
if (cfg.runmode == 1)
ESP.restart();
// task storage debugging //
ESP_LOGD(TAG, "Wifiloop %d bytes left",
uxTaskGetStackHighWaterMark(wifiSwitchTask));
2018-10-03 16:24:45 +02:00
ESP_LOGD(TAG, "Stateloop %d bytes left",
uxTaskGetStackHighWaterMark(stateMachineTask));
#ifdef HAS_GPS
ESP_LOGD(TAG, "Gpsloop %d bytes left", uxTaskGetStackHighWaterMark(GpsTask));
#endif
2018-10-03 16:24:45 +02:00
#ifdef HAS_SPI
ESP_LOGD(TAG, "Spiloop %d bytes left", uxTaskGetStackHighWaterMark(SpiTask));
#endif
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
ESP_LOGD(TAG, "LEDloop %d bytes left", uxTaskGetStackHighWaterMark(ledLoopTask));
#endif
2018-07-22 20:27:58 +02:00
// read battery voltage into global variable
#ifdef HAS_BATTERY_PROBE
batt_voltage = read_voltage();
ESP_LOGI(TAG, "Measured Voltage: %dmV", batt_voltage);
#endif
// sync time & date if we have valid gps time
#ifdef HAS_GPS
if (gps.time.isValid()) {
setTime(gps.time.hour(), gps.time.minute(), gps.time.second(),
gps.date.day(), gps.date.month(), gps.date.year());
ESP_LOGI(TAG, "Time synced to %02d:%02d:%02d", hour(), minute(), second());
} else {
ESP_LOGI(TAG, "No valid GPS time");
}
#endif
// check free memory
if (esp_get_minimum_free_heap_size() <= MEM_LOW) {
2018-08-04 14:37:41 +02:00
ESP_LOGI(TAG,
2018-07-22 20:27:58 +02:00
"Memory full, counter cleared (heap low water mark = %d Bytes / "
"free heap = %d bytes)",
esp_get_minimum_free_heap_size(), ESP.getFreeHeap());
2018-08-04 15:27:58 +02:00
SendData(COUNTERPORT); // send data before clearing counters
2018-07-22 20:27:58 +02:00
reset_counters(); // clear macs container and reset all counters
get_salt(); // get new salt for salting hashes
2018-08-04 18:09:25 +02:00
if (esp_get_minimum_free_heap_size() <= MEM_LOW) // check again
esp_restart(); // memory leak, reset device
2018-07-22 20:27:58 +02:00
}
2018-09-21 18:23:34 +02:00
} // doHousekeeping()
2018-07-23 08:25:23 +02:00
void IRAM_ATTR homeCycleIRQ() {
portENTER_CRITICAL(&mutexHomeCycle);
2018-07-23 08:25:23 +02:00
HomeCycleIRQ++;
portEXIT_CRITICAL(&mutexHomeCycle);
2018-07-23 13:20:06 +02:00
}
// uptime counter 64bit to prevent millis() rollover after 49 days
uint64_t uptime() {
static uint32_t low32, high32;
uint32_t new_low32 = millis();
if (new_low32 < low32)
high32++;
low32 = new_low32;
return (uint64_t)high32 << 32 | low32;
}
void reset_counters() {
macs.clear(); // clear all macs container
macs_total = 0; // reset all counters
macs_wifi = 0;
macs_ble = 0;
}
#ifndef VERBOSE
int redirect_log(const char *fmt, va_list args) {
// do nothing
return 0;
}
#endif