eliminate usage of millis() in whole code
This commit is contained in:
parent
8341a2115e
commit
e5d13be42e
@ -62,6 +62,8 @@
|
||||
|
||||
// emulate millis to avoid rollovers
|
||||
#define _millis() esp_timer_get_time() / 1000
|
||||
#define _micros() esp_timer_get_time()
|
||||
#define _seconds() _millis() / 1000.0
|
||||
|
||||
enum sendprio_t { prio_low, prio_normal, prio_high };
|
||||
enum timesource_t { _gps, _rtc, _lora, _unsynced };
|
||||
|
@ -228,7 +228,7 @@ void updateState(void) {
|
||||
} else {
|
||||
|
||||
/* Update every STATE_SAVE_PERIOD minutes */
|
||||
if ((stateUpdateCounter * STATE_SAVE_PERIOD) < millis()) {
|
||||
if ((stateUpdateCounter * STATE_SAVE_PERIOD) < _millis()) {
|
||||
update = true;
|
||||
stateUpdateCounter++;
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ bool cwa_init(void) {
|
||||
}
|
||||
|
||||
void cwa_mac_add(uint16_t hashedmac) {
|
||||
cwaSeenNotifiers[hashedmac] = millis(); // hash last seen at ....
|
||||
cwaSeenNotifiers[hashedmac] = _millis(); // hash last seen at ....
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -94,7 +94,7 @@ void dp_init(bool verbose) {
|
||||
#if (HAS_DISPLAY) == 1 // i2c
|
||||
// block i2c bus access
|
||||
if (!I2C_MUTEX_LOCK())
|
||||
ESP_LOGV(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0);
|
||||
ESP_LOGV(TAG, "[%0.3f] i2c mutex lock failed", _seconds());
|
||||
else {
|
||||
#endif
|
||||
|
||||
@ -190,7 +190,7 @@ void dp_refresh(bool nextPage) {
|
||||
|
||||
// block i2c bus access
|
||||
if (!I2C_MUTEX_LOCK())
|
||||
ESP_LOGV(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0);
|
||||
ESP_LOGV(TAG, "[%0.3f] i2c mutex lock failed", _seconds());
|
||||
else {
|
||||
// set display on/off according to current device configuration
|
||||
if (DisplayIsOn != cfg.screenon) {
|
||||
@ -600,7 +600,7 @@ void dp_shutdown(void) {
|
||||
#if (HAS_DISPLAY) == 1
|
||||
// block i2c bus access
|
||||
if (!I2C_MUTEX_LOCK())
|
||||
ESP_LOGV(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0);
|
||||
ESP_LOGV(TAG, "[%0.3f] i2c mutex lock failed", _seconds());
|
||||
else {
|
||||
cfg.screenon = 0;
|
||||
obdPower(&ssoled, false);
|
||||
|
@ -109,7 +109,7 @@ uint8_t i2c_readBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len) {
|
||||
I2C_MUTEX_UNLOCK(); // release i2c bus access
|
||||
return ret;
|
||||
} else {
|
||||
ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0);
|
||||
ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", _seconds());
|
||||
return 0xFF;
|
||||
}
|
||||
}
|
||||
@ -128,7 +128,7 @@ uint8_t i2c_writeBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len) {
|
||||
I2C_MUTEX_UNLOCK(); // release i2c bus access
|
||||
return ret ? ret : 0xFF;
|
||||
} else {
|
||||
ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0);
|
||||
ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", _seconds());
|
||||
return 0xFF;
|
||||
}
|
||||
}
|
||||
|
14
src/led.cpp
14
src/led.cpp
@ -9,7 +9,7 @@ led_states previousLEDState =
|
||||
TaskHandle_t ledLoopTask;
|
||||
|
||||
uint16_t LEDColor = COLOR_NONE, LEDBlinkDuration = 0; // state machine variables
|
||||
unsigned long LEDBlinkStarted = 0; // When (in millis() led blink started)
|
||||
unsigned long LEDBlinkStarted = 0; // When (in _millis() led blink started)
|
||||
|
||||
#ifdef HAS_RGB_LED
|
||||
|
||||
@ -133,7 +133,7 @@ void blink_LED(uint16_t set_color, uint16_t set_blinkduration) {
|
||||
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
|
||||
LEDColor = set_color; // set color for RGB LED
|
||||
LEDBlinkDuration = set_blinkduration; // duration
|
||||
LEDBlinkStarted = millis(); // Time Start here
|
||||
LEDBlinkStarted = _millis(); // Time Start here
|
||||
LEDState = LED_ON; // Let main set LED on
|
||||
#endif
|
||||
}
|
||||
@ -145,8 +145,8 @@ void ledLoop(void *parameter) {
|
||||
// Custom blink running always have priority other LoRaWAN led
|
||||
// management
|
||||
if (LEDBlinkStarted && LEDBlinkDuration) {
|
||||
// Custom blink is finished, let this order, avoid millis() overflow
|
||||
if ((millis() - LEDBlinkStarted) >= LEDBlinkDuration) {
|
||||
// Custom blink is finished, let this order, avoid _millis() overflow
|
||||
if ((_millis() - LEDBlinkStarted) >= LEDBlinkDuration) {
|
||||
// Led becomes off, and stop blink
|
||||
LEDState = LED_OFF;
|
||||
LEDBlinkStarted = 0;
|
||||
@ -165,7 +165,7 @@ void ledLoop(void *parameter) {
|
||||
LEDColor = COLOR_YELLOW;
|
||||
// quick blink 20ms on each 1/5 second
|
||||
LEDState =
|
||||
((millis() % 200) < 20) ? LED_ON : LED_OFF; // TX data pending
|
||||
((_millis() % 200) < 20) ? LED_ON : LED_OFF; // TX data pending
|
||||
} else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) {
|
||||
// select color to blink by message port
|
||||
switch (LMIC.pendTxPort) {
|
||||
@ -180,13 +180,13 @@ void ledLoop(void *parameter) {
|
||||
break;
|
||||
}
|
||||
// small blink 10ms on each 1/2sec (not when joining)
|
||||
LEDState = ((millis() % 500) < 10) ? LED_ON : LED_OFF;
|
||||
LEDState = ((_millis() % 500) < 10) ? LED_ON : LED_OFF;
|
||||
// This should not happen so indicate a problem
|
||||
} else if (LMIC.opmode &
|
||||
((OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0)) {
|
||||
LEDColor = COLOR_RED;
|
||||
// heartbeat long blink 200ms on each 2 seconds
|
||||
LEDState = ((millis() % 2000) < 200) ? LED_ON : LED_OFF;
|
||||
LEDState = ((_millis() % 2000) < 200) ? LED_ON : LED_OFF;
|
||||
} else
|
||||
#endif // HAS_LORA
|
||||
{
|
||||
|
@ -178,9 +178,9 @@ int do_ota_update() {
|
||||
client.print("Cache-Control: no-cache\r\n");
|
||||
client.print("Connection: close\r\n\r\n");
|
||||
|
||||
unsigned long timeout = millis();
|
||||
unsigned long timeout = _millis();
|
||||
while (client.available() == 0) {
|
||||
if ((millis() - timeout) > (RESPONSE_TIMEOUT_MS)) {
|
||||
if ((_millis() - timeout) > (RESPONSE_TIMEOUT_MS)) {
|
||||
ESP_LOGI(TAG, "Client timeout");
|
||||
ota_display(3, " E", "client timeout");
|
||||
goto abort;
|
||||
|
@ -26,7 +26,7 @@ Ticker timesyncer;
|
||||
void setTimeSyncIRQ() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); }
|
||||
|
||||
void calibrateTime(void) {
|
||||
ESP_LOGD(TAG, "[%0.3f] calibrateTime, timeSource == %d", millis() / 1000.0,
|
||||
ESP_LOGD(TAG, "[%0.3f] calibrateTime, timeSource == %d", _millis() / 1000.0,
|
||||
timeSource);
|
||||
time_t t = 0;
|
||||
uint16_t t_msec = 0;
|
||||
@ -85,7 +85,7 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec,
|
||||
vTaskDelay(pdMS_TO_TICKS(1000 - t_msec % 1000));
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "[%0.3f] UTC time: %d.%03d sec", millis() / 1000.0,
|
||||
ESP_LOGI(TAG, "[%0.3f] UTC time: %d.%03d sec", _seconds(),
|
||||
time_to_set, t_msec % 1000);
|
||||
|
||||
// if we have got an external timesource, set RTC time and shift RTC_INT pulse
|
||||
@ -106,11 +106,11 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec,
|
||||
timeSource = mytimesource; // set global variable
|
||||
timesyncer.attach(TIME_SYNC_INTERVAL * 60, setTimeSyncIRQ);
|
||||
ESP_LOGD(TAG, "[%0.3f] Timesync finished, time was set | source: %c",
|
||||
millis() / 1000.0, timeSetSymbols[mytimesource]);
|
||||
_seconds(), timeSetSymbols[mytimesource]);
|
||||
} else {
|
||||
timesyncer.attach(TIME_SYNC_INTERVAL_RETRY * 60, setTimeSyncIRQ);
|
||||
ESP_LOGD(TAG, "[%0.3f] Timesync failed, invalid time fetched | source: %c",
|
||||
millis() / 1000.0, timeSetSymbols[mytimesource]);
|
||||
_seconds(), timeSetSymbols[mytimesource]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,7 +47,7 @@ void timesync_request(void) {
|
||||
// start timesync handshake
|
||||
else {
|
||||
ESP_LOGI(TAG, "[%0.3f] Timeserver sync request started, seqNo#%d",
|
||||
millis() / 1000.0, time_sync_seqNo);
|
||||
_seconds(), time_sync_seqNo);
|
||||
xTaskNotifyGive(timeSyncProcTask); // unblock timesync task
|
||||
}
|
||||
}
|
||||
@ -98,14 +98,14 @@ void IRAM_ATTR timesync_processReq(void *taskparameter) {
|
||||
if (xTaskNotifyWait(0x00, ULONG_MAX, &rcv_seqNo,
|
||||
pdMS_TO_TICKS(TIME_SYNC_TIMEOUT * 1000)) == pdFALSE) {
|
||||
ESP_LOGW(TAG, "[d%0.3f] Timesync aborted: timed out",
|
||||
millis() / 1000.0);
|
||||
_seconds());
|
||||
goto Fail; // no timestamp received before timeout
|
||||
}
|
||||
|
||||
// check if we are in handshake with server
|
||||
if (rcv_seqNo != time_sync_seqNo) {
|
||||
ESP_LOGW(TAG, "[%0.3f] Timesync aborted: handshake out of sync",
|
||||
millis() / 1000.0);
|
||||
_seconds());
|
||||
goto Fail;
|
||||
}
|
||||
|
||||
@ -166,7 +166,7 @@ void IRAM_ATTR timesync_processReq(void *taskparameter) {
|
||||
|
||||
// store incoming timestamps
|
||||
void timesync_store(uint32_t timestamp, timesync_t timestamp_type) {
|
||||
ESP_LOGD(TAG, "[%0.3f] seq#%d[%d]: t%d=%d", millis() / 1000.0,
|
||||
ESP_LOGD(TAG, "[%0.3f] seq#%d[%d]: t%d=%d", _seconds(),
|
||||
time_sync_seqNo, sample_idx, timestamp_type, timestamp);
|
||||
timesync_timestamp[sample_idx][timestamp_type] = timestamp;
|
||||
}
|
||||
@ -214,10 +214,10 @@ void IRAM_ATTR timesync_serverAnswer(void *pUserData, int flag) {
|
||||
if (flag != TIME_SYNC_FRAME_LENGTH) {
|
||||
if (rcv_seqNo == TIME_SYNC_END_FLAG)
|
||||
ESP_LOGI(TAG, "[%0.3f] Timeserver error: no confident time available",
|
||||
millis() / 1000.0);
|
||||
_seconds());
|
||||
else
|
||||
ESP_LOGW(TAG, "[%0.3f] Timeserver error: spurious data received",
|
||||
millis() / 1000.0);
|
||||
_seconds());
|
||||
goto Exit; // failure
|
||||
}
|
||||
|
||||
@ -233,13 +233,13 @@ void IRAM_ATTR timesync_serverAnswer(void *pUserData, int flag) {
|
||||
|
||||
if (flag != 1) {
|
||||
ESP_LOGW(TAG, "[%0.3f] Network did not answer time request",
|
||||
millis() / 1000.0);
|
||||
_seconds());
|
||||
goto Exit;
|
||||
}
|
||||
|
||||
// Populate lmic_time_reference
|
||||
if ((LMIC_getNetworkTimeReference(&lmicTime)) != 1) {
|
||||
ESP_LOGW(TAG, "[%0.3f] Network time request failed", millis() / 1000.0);
|
||||
ESP_LOGW(TAG, "[%0.3f] Network time request failed", _seconds());
|
||||
goto Exit;
|
||||
}
|
||||
|
||||
@ -267,7 +267,7 @@ Finish:
|
||||
rc = 1;
|
||||
} else {
|
||||
ESP_LOGW(TAG, "[%0.3f] Timeserver error: outdated time received",
|
||||
millis() / 1000.0);
|
||||
_seconds());
|
||||
}
|
||||
|
||||
Exit:
|
||||
|
Loading…
Reference in New Issue
Block a user