mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-06 03:50:13 +02:00
Merge pull request #1347 from jonny5532/feature/use-millis64-to-avoid-overflow
Add a 64-bit millis64() function to avoid rollover issues
This commit is contained in:
commit
43bc738352
9 changed files with 43 additions and 70 deletions
|
@ -253,8 +253,6 @@ typedef struct {
|
|||
} DATALAYER_SYSTEM_INFO_TYPE;
|
||||
|
||||
typedef struct {
|
||||
/** Millis rollover count. Increments every 49.7 days. Used for keeping track on events */
|
||||
uint8_t millisrolloverCount = 0;
|
||||
#ifdef FUNCTION_TIME_MEASUREMENT
|
||||
/** Core task measurement variable */
|
||||
int64_t core_task_max_us = 0;
|
||||
|
|
|
@ -397,8 +397,6 @@ EVENTS_LEVEL_TYPE get_event_level(void) {
|
|||
return events.level;
|
||||
}
|
||||
|
||||
uint64_t get_timestamp(unsigned long currentMillis);
|
||||
|
||||
/* Local functions */
|
||||
|
||||
static void set_event(EVENTS_ENUM_TYPE event, uint8_t data, bool latched) {
|
||||
|
@ -417,7 +415,7 @@ static void set_event(EVENTS_ENUM_TYPE event, uint8_t data, bool latched) {
|
|||
}
|
||||
|
||||
// We should set the event, update event info
|
||||
events.entries[event].timestamp = get_timestamp(millis());
|
||||
events.entries[event].timestamp = millis64();
|
||||
events.entries[event].data = data;
|
||||
// Check if the event is latching
|
||||
events.entries[event].state = latched ? EVENT_STATE_ACTIVE_LATCHED : EVENT_STATE_ACTIVE;
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define __EVENTS_H__
|
||||
|
||||
#include <WString.h>
|
||||
#include <src/devboard/utils/millis64.h>
|
||||
#include <src/devboard/utils/types.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
|
12
Software/src/devboard/utils/millis64.cpp
Normal file
12
Software/src/devboard/utils/millis64.cpp
Normal file
|
@ -0,0 +1,12 @@
|
|||
#include <Arduino.h>
|
||||
#include "esp_timer.h"
|
||||
|
||||
uint64_t ARDUINO_ISR_ATTR millis64() {
|
||||
// ESP32's esp_timer_get_time() returns time in microseconds, we convert to
|
||||
// milliseconds by dividing by 1000.
|
||||
|
||||
// This is almost identical to the existing Arduino millis() function, except
|
||||
// we return a 64-bit value which won't roll over for 600k years.
|
||||
|
||||
return esp_timer_get_time() / 1000ULL;
|
||||
}
|
11
Software/src/devboard/utils/millis64.h
Normal file
11
Software/src/devboard/utils/millis64.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* @brief Return ESP32's high-resolution timer in milliseconds, as a 64 bit value.
|
||||
*
|
||||
* @param[in] void
|
||||
*
|
||||
* @return uint64_t Timestamp in milliseconds
|
||||
*
|
||||
*/
|
||||
extern uint64_t millis64(void);
|
|
@ -2,6 +2,7 @@
|
|||
#include <limits>
|
||||
#include "../../datalayer/datalayer.h"
|
||||
#include "../../devboard/utils/logging.h"
|
||||
#include "../../devboard/utils/millis64.h"
|
||||
|
||||
const char EVENTS_HTML_START[] = R"=====(
|
||||
<style>body{background-color:#000;color:#fff}.event-log{display:flex;flex-direction:column}.event{display:flex;flex-wrap:wrap;border:1px solid #fff;padding:10px}.event>div{flex:1;min-width:100px;max-width:90%;word-break:break-word}</style><div style="background-color:#303e47;padding:10px;margin-bottom:10px;border-radius:25px"><div class="event-log"><div class="event" style="background-color:#1e2c33;font-weight:700"><div>Event Type</div><div>Severity</div><div>Last Event</div><div>Count</div><div>Data</div><div>Message</div></div>
|
||||
|
@ -17,8 +18,6 @@ button:hover { background-color: #3A4A52; }</style>
|
|||
</script>
|
||||
)=====";
|
||||
|
||||
uint64_t get_timestamp(unsigned long currentMillis);
|
||||
|
||||
static std::vector<EventData> order_events;
|
||||
|
||||
String events_processor(const String& var) {
|
||||
|
@ -40,7 +39,7 @@ String events_processor(const String& var) {
|
|||
}
|
||||
// Sort events by timestamp
|
||||
std::sort(order_events.begin(), order_events.end(), compareEventsByTimestampDesc);
|
||||
uint64_t current_timestamp = get_timestamp(millis());
|
||||
uint64_t current_timestamp = millis64();
|
||||
|
||||
// Generate HTML and debug output
|
||||
for (const auto& event : order_events) {
|
||||
|
|
|
@ -36,25 +36,16 @@
|
|||
* https://github.com/YiannisBourkelis/
|
||||
*/
|
||||
|
||||
#include <Arduino.h> //for millis()
|
||||
#include "../../../devboard/utils/millis64.h"
|
||||
#include "uptime.h"
|
||||
|
||||
//private variabes for converting milliseconds to total seconds,minutes,hours and days
|
||||
//after each call to millis()
|
||||
unsigned long uptime::m_milliseconds;
|
||||
unsigned long uptime::m_seconds;
|
||||
unsigned long uptime::m_minutes;
|
||||
unsigned long uptime::m_hours;
|
||||
unsigned long uptime::m_days;
|
||||
|
||||
//in case of millis() overflow, we store in these private variables
|
||||
//the existing time passed until the moment of the overflow
|
||||
//so that we can add them on the next call to compute the time passed
|
||||
unsigned long uptime::m_last_milliseconds = 0;
|
||||
unsigned long uptime::m_remaining_seconds = 0;
|
||||
unsigned long uptime::m_remaining_minutes = 0;
|
||||
unsigned long uptime::m_remaining_hours = 0;
|
||||
unsigned long uptime::m_remaining_days = 0;
|
||||
uint64_t uptime::m_milliseconds;
|
||||
uint64_t uptime::m_seconds;
|
||||
uint64_t uptime::m_minutes;
|
||||
uint64_t uptime::m_hours;
|
||||
uint64_t uptime::m_days;
|
||||
|
||||
//private variables that in combination hold the actual time passed
|
||||
//Use the coresponding uptime::get_.... to read these private variables
|
||||
|
@ -90,26 +81,13 @@ unsigned long uptime::getDays()
|
|||
//and store them in their static variables
|
||||
void uptime::calculateUptime()
|
||||
{
|
||||
uptime::m_milliseconds = millis();
|
||||
|
||||
if (uptime::m_last_milliseconds > uptime::m_milliseconds){
|
||||
//in case of millis() overflow, store existing passed seconds,minutes,hours and days
|
||||
uptime::m_remaining_seconds = uptime::m_mod_seconds;
|
||||
uptime::m_remaining_minutes = uptime::m_mod_minutes;
|
||||
uptime::m_remaining_hours = uptime::m_mod_hours;
|
||||
uptime::m_remaining_days = uptime::m_days;
|
||||
}
|
||||
//store last millis(), so that we can detect on the next call
|
||||
//if there is a millis() overflow ( millis() returns 0 )
|
||||
uptime::m_last_milliseconds = uptime::m_milliseconds;
|
||||
uptime::m_milliseconds = millis64();
|
||||
|
||||
//convert passed millis to total seconds, minutes, hours and days.
|
||||
//In case of overflow, the uptime::m_remaining_... variables contain the remaining time before the overflow.
|
||||
//We add the remaining time, so that we can continue measuring the time passed from the last boot of the device.
|
||||
uptime::m_seconds = (uptime::m_milliseconds / 1000) + uptime::m_remaining_seconds;
|
||||
uptime::m_minutes = (uptime::m_seconds / 60) + uptime::m_remaining_minutes;
|
||||
uptime::m_hours = (uptime::m_minutes / 60) + uptime::m_remaining_hours;
|
||||
uptime::m_days = (uptime::m_hours / 24) + uptime::m_remaining_days;
|
||||
uptime::m_seconds = (uptime::m_milliseconds / 1000);
|
||||
uptime::m_minutes = (uptime::m_seconds / 60);
|
||||
uptime::m_hours = (uptime::m_minutes / 60);
|
||||
uptime::m_days = (uptime::m_hours / 24);
|
||||
|
||||
//calculate the actual time passed, using modulus, in milliseconds, seconds and hours.
|
||||
//The days are calculated allready in the previous step.
|
||||
|
|
|
@ -53,21 +53,15 @@ class uptime
|
|||
static unsigned long getDays();
|
||||
|
||||
private:
|
||||
static unsigned long m_milliseconds;
|
||||
static unsigned long m_seconds;
|
||||
static unsigned long m_minutes;
|
||||
static unsigned long m_hours;
|
||||
static unsigned long m_days;
|
||||
static uint64_t m_milliseconds;
|
||||
static uint64_t m_seconds;
|
||||
static uint64_t m_minutes;
|
||||
static uint64_t m_hours;
|
||||
static uint64_t m_days;
|
||||
|
||||
static unsigned long m_mod_milliseconds;
|
||||
static uint8_t m_mod_seconds;
|
||||
static uint8_t m_mod_minutes;
|
||||
static uint8_t m_mod_hours;
|
||||
|
||||
static unsigned long m_last_milliseconds;
|
||||
static unsigned long m_remaining_seconds;
|
||||
static unsigned long m_remaining_minutes;
|
||||
static unsigned long m_remaining_hours;
|
||||
static unsigned long m_remaining_days;
|
||||
};
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue