Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions examples/companion_radio/ui-new/UITask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class SplashScreen : public UIScreen {
}

void poll() override {
if (millis() >= dismiss_after) {
if (millisHasPassed(dismiss_after)) {
_task->gotoHomeScreen();
}
}
Expand Down Expand Up @@ -153,10 +153,10 @@ class HomeScreen : public UIScreen {
int sensors_nb = 0;
bool sensors_scroll = false;
int sensors_scroll_offset = 0;
int next_sensors_refresh = 0;
uint32_t next_sensors_refresh = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defining something like a timestamp_t or millis_t as an alias for uint32_t would make these even clearer!


void refresh_sensors() {
if (millis() > next_sensors_refresh) {
if (millisHasPassed(next_sensors_refresh)) {
sensors_lpp.reset();
sensors_nb = 0;
sensors_lpp.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f);
Expand Down Expand Up @@ -764,7 +764,7 @@ void UITask::loop() {
}
#endif
#if defined(BACKLIGHT_BTN)
if (millis() > next_backlight_btn_check) {
if (millisHasPassed(next_backlight_btn_check)) {
bool touch_state = digitalRead(PIN_BUTTON2);
#if defined(DISP_BACKLIGHT)
digitalWrite(DISP_BACKLIGHT, !touch_state);
Expand All @@ -790,7 +790,7 @@ void UITask::loop() {
if (curr) curr->poll();

if (_display != NULL && _display->isOn()) {
if (millis() >= _next_refresh && curr) {
if (millisHasPassed(_next_refresh) && curr) {
_display->startFrame();
int delay_millis = curr->render(*_display);
if (millis() < _alert_expiry) { // render alert popup
Expand Down Expand Up @@ -818,7 +818,7 @@ void UITask::loop() {
_auto_off = millis() + AUTO_OFF_MILLIS;
}
#endif
if (millis() > _auto_off) {
if (millisHasPassed(_auto_off)) {
_display->turnOff();
}
#endif
Expand All @@ -829,7 +829,7 @@ void UITask::loop() {
#endif

#ifdef AUTO_SHUTDOWN_MILLIVOLTS
if (millis() > next_batt_chck) {
if (millisHasPassed(next_batt_chck)) {
uint16_t milliVolts = getBattMilliVolts();
if (milliVolts > 0 && milliVolts < AUTO_SHUTDOWN_MILLIVOLTS) {
if(!board.isExternalPowered()) {
Expand Down
3 changes: 2 additions & 1 deletion examples/companion_radio/ui-new/UITask.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <helpers/SensorManager.h>
#include <helpers/BaseSerialInterface.h>
#include <Arduino.h>
#include <helpers/ArduinoTimer.h>
#include <helpers/sensors/LPPDataHelpers.h>

#ifndef LED_STATE_ON
Expand Down Expand Up @@ -37,7 +38,7 @@ class UITask : public AbstractUITask {
unsigned long _alert_expiry;
int _msgcount;
unsigned long ui_started_at, next_batt_chck;
int next_backlight_btn_check = 0;
uint32_t next_backlight_btn_check = 0;
#ifdef PIN_STATUS_LED
int led_state = 0;
int next_led_change = 0;
Expand Down
5 changes: 3 additions & 2 deletions examples/companion_radio/ui-orig/UITask.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "UITask.h"
#include <Arduino.h>
#include <helpers/ArduinoTimer.h>
#include <helpers/TxtDataHelpers.h>
#include "../MyMesh.h"

Expand Down Expand Up @@ -335,7 +336,7 @@ void UITask::loop() {
_need_refresh = true;
_firstBoot = false;
}
if (millis() >= _next_refresh && _need_refresh) {
if (millisHasPassed(_next_refresh) && _need_refresh) {
_display->startFrame();
renderCurrScreen();
_display->endFrame();
Expand All @@ -351,7 +352,7 @@ void UITask::loop() {
_auto_off = millis() + AUTO_OFF_MILLIS;
}
#endif
if (millis() > _auto_off) {
if (millisHasPassed(_auto_off)) {
_display->turnOff();
}
}
Expand Down
3 changes: 2 additions & 1 deletion examples/companion_radio/ui-tiny/ScrollingStatusBar.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <helpers/ui/DisplayDriver.h>
#include <helpers/ArduinoTimer.h>


#ifndef STATUS_BAR_SCROLL_MS
Expand Down Expand Up @@ -93,7 +94,7 @@ class ScrollingStatusBar {
// Returns true if the status bar needs a redraw this frame.
bool needsRedraw() {
if (_text_width <= _display_width) return _needs_redraw; // static, no scrolling
return millis() >= _next_scroll;
return millisHasPassed(_next_scroll);
}

// Render the status bar via DisplayDriver.
Expand Down
16 changes: 8 additions & 8 deletions examples/companion_radio/ui-tiny/UITask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class SplashScreen : public UIScreen {
}

void poll() override {
if (millis() >= dismiss_after) {
if (millisHasPassed(dismiss_after)) {
_task->gotoHomeScreen();
}
}
Expand Down Expand Up @@ -116,10 +116,10 @@ class HomeScreen : public UIScreen {
int sensors_nb = 0;
bool sensors_scroll = false;
int sensors_scroll_offset = 0;
int next_sensors_refresh = 0;
uint32_t next_sensors_refresh = 0;

void refresh_sensors() {
if (millis() > next_sensors_refresh) {
if (millisHasPassed(next_sensors_refresh)) {
sensors_lpp.reset();
sensors_nb = 0;
sensors_lpp.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f);
Expand Down Expand Up @@ -633,7 +633,7 @@ void UITask::loop() {
}
#endif
#if defined(BACKLIGHT_BTN)
if (millis() > next_backlight_btn_check) {
if (millisHasPassed(next_backlight_btn_check)) {
bool touch_state = digitalRead(PIN_BUTTON2);
#if defined(DISP_BACKLIGHT)
digitalWrite(DISP_BACKLIGHT, !touch_state);
Expand Down Expand Up @@ -676,7 +676,7 @@ void UITask::loop() {
isSerialEnabled());

bool status_dirty = _statusBar.needsRedraw();
bool content_dirty = (millis() >= _next_refresh && curr);
bool content_dirty = (millisHasPassed(_next_refresh) && curr);

if (status_dirty || content_dirty) {
_display->startFrame();
Expand Down Expand Up @@ -713,7 +713,7 @@ void UITask::loop() {
_auto_off = millis() + AUTO_OFF_MILLIS;
}
#endif
if (millis() > _auto_off) {
if (millisHasPassed(_auto_off)) {
_display->turnOff();
}
#endif
Expand All @@ -724,7 +724,7 @@ void UITask::loop() {
#endif

#ifdef AUTO_SHUTDOWN_MILLIVOLTS
if (millis() > next_batt_chck) {
if (millisHasPassed(next_batt_chck)) {
_cached_batt_mv = getBattMilliVolts();
if (_cached_batt_mv > 0 && _cached_batt_mv < AUTO_SHUTDOWN_MILLIVOLTS) {
if(!board.isExternalPowered()) {
Expand All @@ -743,7 +743,7 @@ void UITask::loop() {
next_batt_chck = millis() + 8000;
}
#else
if (_display != NULL && _display->isOn() && millis() >= next_batt_chck) {
if (_display != NULL && _display->isOn() && millisHasPassed(next_batt_chck)) {
_cached_batt_mv = getBattMilliVolts();
next_batt_chck = millis() + 8000;
}
Expand Down
3 changes: 2 additions & 1 deletion examples/companion_radio/ui-tiny/UITask.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <helpers/SensorManager.h>
#include <helpers/BaseSerialInterface.h>
#include <Arduino.h>
#include <helpers/ArduinoTimer.h>
#include <helpers/sensors/LPPDataHelpers.h>

#include "ScrollingStatusBar.h"
Expand Down Expand Up @@ -41,7 +42,7 @@ class UITask : public AbstractUITask {
unsigned long _alert_expiry;
int _msgcount;
unsigned long ui_started_at, next_batt_chck;
int next_backlight_btn_check = 0;
uint32_t next_backlight_btn_check = 0;
uint16_t _cached_batt_mv;
#ifdef PIN_STATUS_LED
int led_state = 0;
Expand Down
7 changes: 4 additions & 3 deletions examples/simple_repeater/UITask.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "UITask.h"
#include <Arduino.h>
#include <helpers/ArduinoTimer.h>
#include <helpers/CommonCLI.h>

#ifndef USER_BTN_PRESSED
Expand Down Expand Up @@ -95,7 +96,7 @@ void UITask::renderCurrScreen() {

void UITask::loop() {
#ifdef PIN_USER_BTN
if (millis() >= _next_read) {
if (millisHasPassed(_next_read)) {
int btnState = digitalRead(PIN_USER_BTN);
if (btnState != _prevBtnState) {
if (btnState == USER_BTN_PRESSED) { // pressed?
Expand All @@ -113,14 +114,14 @@ void UITask::loop() {
#endif

if (_display->isOn()) {
if (millis() >= _next_refresh) {
if (millisHasPassed(_next_refresh)) {
_display->startFrame();
renderCurrScreen();
_display->endFrame();

_next_refresh = millis() + 1000; // refresh every second
}
if (millis() > _auto_off) {
if (millisHasPassed(_auto_off)) {
_display->turnOff();
}
}
Expand Down
7 changes: 4 additions & 3 deletions examples/simple_room_server/UITask.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "UITask.h"
#include <Arduino.h>
#include <helpers/ArduinoTimer.h>
#include <helpers/CommonCLI.h>

#ifndef USER_BTN_PRESSED
Expand Down Expand Up @@ -95,7 +96,7 @@ void UITask::renderCurrScreen() {

void UITask::loop() {
#ifdef PIN_USER_BTN
if (millis() >= _next_read) {
if (millisHasPassed(_next_read)) {
int btnState = digitalRead(PIN_USER_BTN);
if (btnState != _prevBtnState) {
if (btnState == USER_BTN_PRESSED) { // pressed?
Expand All @@ -113,14 +114,14 @@ void UITask::loop() {
#endif

if (_display->isOn()) {
if (millis() >= _next_refresh) {
if (millisHasPassed(_next_refresh)) {
_display->startFrame();
renderCurrScreen();
_display->endFrame();

_next_refresh = millis() + 1000; // refresh every second
}
if (millis() > _auto_off) {
if (millisHasPassed(_auto_off)) {
_display->turnOff();
}
}
Expand Down
7 changes: 4 additions & 3 deletions examples/simple_sensor/UITask.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "UITask.h"
#include <Arduino.h>
#include <helpers/ArduinoTimer.h>
#include <helpers/CommonCLI.h>

#ifndef USER_BTN_PRESSED
Expand Down Expand Up @@ -95,7 +96,7 @@ void UITask::renderCurrScreen() {

void UITask::loop() {
#ifdef PIN_USER_BTN
if (millis() >= _next_read) {
if (millisHasPassed(_next_read)) {
int btnState = digitalRead(PIN_USER_BTN);
if (btnState != _prevBtnState) {
if (btnState == USER_BTN_PRESSED) { // pressed?
Expand All @@ -113,14 +114,14 @@ void UITask::loop() {
#endif

if (_display->isOn()) {
if (millis() >= _next_refresh) {
if (millisHasPassed(_next_refresh)) {
_display->startFrame();
renderCurrScreen();
_display->endFrame();

_next_refresh = millis() + 1000; // refresh every second
}
if (millis() > _auto_off) {
if (millisHasPassed(_auto_off)) {
_display->turnOff();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/helpers/ArduinoHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <Mesh.h>
#include <Arduino.h>
#include <helpers/ArduinoTimer.h>

class VolatileRTCClock : public mesh::RTCClock {
uint32_t base_time;
Expand Down
17 changes: 17 additions & 0 deletions src/helpers/ArduinoTimer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <stdint.h>

// Wrap-safe check: has `now` reached or passed timestamp `t`?
// Handles uint32_t rollover correctly for intervals up to ~24 days.
inline bool millisHasPassed(uint32_t now, uint32_t t) {
return (int32_t)(now - t) >= 0;
}

#ifdef ARDUINO
#include <Arduino.h>
// Convenience wrapper using Arduino's millis().
inline bool millisHasPassed(uint32_t t) {
return millisHasPassed((uint32_t)millis(), t);
}
#endif
7 changes: 4 additions & 3 deletions src/helpers/esp32/SerialBLEInterface.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "SerialBLEInterface.h"
#include <helpers/ArduinoTimer.h>
#include "esp_mac.h"

// See the following for generating UUIDs:
Expand Down Expand Up @@ -183,12 +184,12 @@ size_t SerialBLEInterface::writeFrame(const uint8_t src[], size_t len) {
#define BLE_WRITE_MIN_INTERVAL 60

bool SerialBLEInterface::isWriteBusy() const {
return millis() < _last_write + BLE_WRITE_MIN_INTERVAL; // still too soon to start another write?
return !millisHasPassed(_last_write + BLE_WRITE_MIN_INTERVAL); // still too soon to start another write?
}

size_t SerialBLEInterface::checkRecvFrame(uint8_t dest[]) {
if (send_queue_len > 0 // first, check send queue
&& millis() >= _last_write + BLE_WRITE_MIN_INTERVAL // space the writes apart
&& millisHasPassed(_last_write + BLE_WRITE_MIN_INTERVAL) // space the writes apart
) {
_last_write = millis();
pTxCharacteristic->setValue(send_queue[0].buf, send_queue[0].len);
Expand Down Expand Up @@ -238,7 +239,7 @@ size_t SerialBLEInterface::checkRecvFrame(uint8_t dest[]) {
oldDeviceConnected = deviceConnected;
}

if (adv_restart_time && millis() >= adv_restart_time) {
if (adv_restart_time && millisHasPassed(adv_restart_time)) {
if (pServer->getConnectedCount() == 0) {
BLE_DEBUG_PRINTLN("SerialBLEInterface -> re-starting advertising");
pServer->getAdvertising()->start(); // re-Start advertising
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/sensors/EnvironmentSensorManager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "EnvironmentSensorManager.h"
#include <helpers/ArduinoTimer.h>

#include <Wire.h>

Expand Down Expand Up @@ -889,11 +890,11 @@ void EnvironmentSensorManager::stop_gps() {
void EnvironmentSensorManager::loop() {

#if ENV_INCLUDE_GPS
static long next_gps_update = 0;
static uint32_t next_gps_update = 0;
if (gps_active) {
_location->loop();
}
if (millis() > next_gps_update) {
if (millisHasPassed(next_gps_update)) {

if(gps_active){
#ifdef RAK_WISBLOCK_GPS
Expand Down
Loading