Skip to content

Commit 54eeb81

Browse files
k3ldarCopilotCopilot
authored
Ensure correct and consistent use of millis64() (#170)
* Ensure correct and consistent use of millis64() * Update Docs/ScheduleEvents.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Apply reviewer feedback: fix time source mixing, typos, doc comments, vcxproj filters, and strtoull Agent-Logs-Url: https://github.com/SmartFuseBox/SmartFuseBox/sessions/9cc3b3e7-916a-4845-b9af-fabadb1c3d42 Co-authored-by: k3ldar <5587949+k3ldar@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: k3ldar <5587949+k3ldar@users.noreply.github.com>
1 parent c49ba7e commit 54eeb81

66 files changed

Lines changed: 389 additions & 351 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Docs/BluetoothBle.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Abstract base class. All services inherit from it and implement:
8585
| `begin()` | Create characteristics, initialise values, register with `BLE` |
8686
| `getServiceUUID()` | Return the service UUID string |
8787
| `getServiceName()` | Return a human-readable name for logging |
88-
| `loop(unsigned long)` | Periodic work — send notifications, update cached values |
88+
| `loop(uint64_t)` | Periodic work — send notifications, update cached values |
8989
| `getCharacteristicCount()` | Return number of characteristics (for diagnostics) |
9090
| `getBLEService()` | Return `void*` to the underlying `BLEService` (used by manager for advertising) |
9191

@@ -258,7 +258,7 @@ public:
258258
return true;
259259
}
260260
261-
void loop(unsigned long currentMillis) override
261+
void loop(uint64_t currentMillis) override
262262
{
263263
if (currentMillis - _lastUpdate >= 2000)
264264
{
@@ -275,7 +275,7 @@ public:
275275
private:
276276
BLEService* _service = nullptr;
277277
BLEIntCharacteristic* _charValue = nullptr;
278-
unsigned long _lastUpdate = 0;
278+
uint64_t _lastUpdate = 0;
279279
};
280280
```
281281

Docs/Local.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ On **Arduino Uno R4 WiFi**, the ANNA-B112 (BLE) and ESP32-S3 (WiFi) modules shar
117117
## 4. Serial Initialisation Timeout
118118

119119
```cpp
120-
constexpr unsigned long serialInitTimeoutMs = 300;
120+
constexpr uint64_t serialInitTimeoutMs = 300;
121121
```
122122

123123
If `waitForConnection` is `true` when initialising serial, the firmware waits up to this many milliseconds for the USB serial connection before continuing with setup. Useful when debugging startup log messages that would otherwise be missed because the serial monitor opens after the board has already booted.

Docs/ScheduleEvents.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Config (EEPROM) Persists ScheduledEvent array via ConfigManager
2424
│ read by
2525
2626
ScheduleController Runtime engine — called every loop()
27-
├── isTriggerDue() Evaluates trigger against current time / millis()
27+
├── isTriggerDue() Evaluates trigger against current time / millis64()
2828
├── isConditionMet() Evaluates guard condition
2929
├── executeAction() Drives RelayController
3030
└── updatePulses() Turns off relays when pulse duration expires
@@ -215,7 +215,7 @@ The scheduler overview is also included in the main status response at `GET /`.
215215
_scheduleController.begin(); // subscribes to MessageBus events
216216

217217
// loop()
218-
_scheduleController.update(millis());
218+
_scheduleController.update(SystemFunctions::millis64());
219219
```
220220

221221
### Evaluation Cycle
@@ -259,9 +259,9 @@ State is held in stack-allocated arrays indexed by event slot. It is **not persi
259259
|---|---|---|
260260
| `_lastFiredDay` | `uint32_t[20]` | Packed YYYYMMDD of last trigger evaluation |
261261
| `_lastFiredMinute` | `uint16_t[20]` | Minute-of-day (0–1439) of last trigger evaluation |
262-
| `_lastIntervalFireMs` | `unsigned long[20]` | `millis()` timestamp of last interval fire |
263-
| `_pulseStartMs` | `unsigned long[20]` | `millis()` when `RelayPulse` began |
264-
| `_pulseDurMs` | `unsigned long[20]` | `RelayPulse` duration in milliseconds |
262+
| `_lastIntervalFireMs` | `uint64_t[20]` | `SystemFunctions::millis64()` timestamp of last interval fire |
263+
| `_pulseStartMs` | `uint64_t[20]` | `SystemFunctions::millis64()` when `RelayPulse` began |
264+
| `_pulseDurMs` | `uint64_t[20]` | `RelayPulse` duration in milliseconds |
265265
| `_pulseRelayIdx` | `uint8_t[20]` | Relay to turn off when pulse expires |
266266
| `_pulseActive` | `bool[20]` | Whether a pulse is currently running for the slot |
267267

Docs/Sensors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ protected:
345345
pinMode(_sensorPin, INPUT);
346346
}
347347

348-
unsigned long update() override
348+
uint64_t update() override
349349
{
350350
_lastValue = analogRead(_sensorPin);
351351

Docs/Warnings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ uint32_t local = warningManager.getLocalWarningsMask();
9494
uint32_t remote = warningManager.getRemoteWarningsMask();
9595

9696
// Called from the main loop — drives heartbeat and connection state
97-
warningManager.update(millis());
97+
warningManager.update(SystemFunctions::millis64());
9898

9999
// Called from AckCommandHandler when ACK:F0=ok arrives
100100
warningManager.notifyHeartbeatAck();

SmartFuseBox/BaseSensor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class BaseSensor : public BaseSensorHandler, public JsonVisitor
114114

115115
virtual void getMqttValue(uint8_t channelIndex, char* buffer, size_t size) const = 0;
116116

117-
virtual unsigned long getMqttPublishIntervalMs(uint8_t channelIndex) const
117+
virtual uint64_t getMqttPublishIntervalMs(uint8_t channelIndex) const
118118
{
119119
(void)channelIndex;
120120
return 1000; // Default: publish every 1 second

SmartFuseBox/BinaryPresenceSensor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ void BinaryPresenceSensor::initialize()
108108
_lastState = digitalRead(_sensorPin);
109109
}
110110

111-
unsigned long BinaryPresenceSensor::update()
111+
uint64_t BinaryPresenceSensor::update()
112112
{
113113
if (_directPulseActive && _relayController)
114114
{
115-
if (SystemFunctions::hasElapsed(millis(), _directPulseStartMs, _directPulseDurMs))
115+
if (SystemFunctions::hasElapsed(_directPulseStartMs, _directPulseDurMs))
116116
{
117117
_relayController->setRelayState(_directPulseRelayIdx, false);
118118
_directPulseActive = false;
@@ -176,8 +176,8 @@ void BinaryPresenceSensor::executeAction(ExecutionActionType actionType, const u
176176

177177
uint8_t relayIdx = payload[0];
178178
_relayController->setRelayState(relayIdx, true);
179-
_directPulseStartMs = millis();
180-
_directPulseDurMs = static_cast<unsigned long>(durationSec) * MillisecondsPerSecond;
179+
_directPulseStartMs = SystemFunctions::millis64();
180+
_directPulseDurMs = static_cast<uint64_t>(durationSec) * MillisecondsPerSecond;
181181
_directPulseRelayIdx = relayIdx;
182182
_directPulseActive = true;
183183
break;

SmartFuseBox/BinaryPresenceSensor.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "RelayController.h"
2727
#include "SensorCommandHandler.h"
2828

29-
constexpr unsigned long BinaryPresenceCheckMs = 300;
29+
constexpr uint64_t BinaryPresenceCheckMs = 300;
3030

3131
/**
3232
* @brief Sensor handler for binary presence monitoring.
@@ -44,18 +44,18 @@ class BinaryPresenceSensor : public BaseSensor, public BroadcastLoggerSupport
4444
const uint8_t _sensorPin;
4545
int _activeState; // HIGH or LOW depending on sensor type
4646
int _lastState;
47-
unsigned long _lastChangeMs;
47+
uint64_t _lastChangeMs;
4848

4949
ExecutionActionType _onDetectedAction;
5050
uint8_t _onDetectedPayload[ConfigSchedulerPayloadSize];
5151
ExecutionActionType _onClearAction;
5252
uint8_t _onClearPayload[ConfigSchedulerPayloadSize];
5353

5454
// Pulse state for sensor-triggered direct actions (single dedicated slot)
55-
bool _directPulseActive;
56-
unsigned long _directPulseStartMs;
57-
unsigned long _directPulseDurMs;
58-
uint8_t _directPulseRelayIdx;
55+
bool _directPulseActive;
56+
uint64_t _directPulseStartMs;
57+
uint64_t _directPulseDurMs;
58+
uint8_t _directPulseRelayIdx;
5959

6060

6161
#if defined(MQTT_SUPPORT)
@@ -67,7 +67,7 @@ class BinaryPresenceSensor : public BaseSensor, public BroadcastLoggerSupport
6767

6868
protected:
6969
void initialize() override;
70-
unsigned long update() override;
70+
uint64_t update() override;
7171

7272
public:
7373
BinaryPresenceSensor(MessageBus* messageBus, BroadcastManager* broadcastManager, SensorCommandHandler* sensorCommandHandler,

SmartFuseBox/BluetoothManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ bool BluetoothManager::initializeServices()
130130

131131
void BluetoothManager::loop()
132132
{
133-
unsigned long currentMillis = millis();
133+
uint64_t currentMillis = SystemFunctions::millis64();
134134

135135
// Poll for BLE events
136136
BLE.poll();

SmartFuseBox/BluetoothManager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "BluetoothServiceBase.h"
2727
#include "WarningManager.h"
2828
#include "LoggingSupport.h"
29+
#include "SystemFunctions.h"
2930

3031
/**
3132
* @brief Central coordinator for Bluetooth BLE communication.
@@ -146,7 +147,7 @@ class BluetoothManager : public SingleLoggerSupport
146147
void* _server; // BLEServer* (void* to avoid exposing BLE library headers)
147148
bool _isAdvertising;
148149
bool _deviceConnected;
149-
unsigned long _lastLoopTime;
150+
uint64_t _lastLoopTime;
150151
const char* _deviceName;
151152

152153
/**

0 commit comments

Comments
 (0)