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
20 changes: 10 additions & 10 deletions examples/companion_radio/ui-new/UITask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class SplashScreen : public UIScreen {
}

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

void refresh_sensors() {
if (millis() > next_sensors_refresh) {
if (millis_passed(next_sensors_refresh)) {
sensors_lpp.reset();
sensors_nb = 0;
sensors_lpp.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f);
Expand Down Expand Up @@ -655,8 +655,8 @@ void UITask::newMsg(uint8_t path_len, const char* from_name, const char* text, i

void UITask::userLedHandler() {
#ifdef PIN_STATUS_LED
int cur_time = millis();
if (cur_time > next_led_change) {
unsigned long cur_time = millis();
if (millis_passed(next_led_change)) {
if (led_state == 0) {
led_state = 1;
if (_msgcount > 0) {
Expand Down Expand Up @@ -767,7 +767,7 @@ void UITask::loop() {
}
#endif
#if defined(BACKLIGHT_BTN)
if (millis() > next_backlight_btn_check) {
if (millis_passed(next_backlight_btn_check)) {
bool touch_state = digitalRead(PIN_BUTTON2);
#if defined(DISP_BACKLIGHT)
digitalWrite(DISP_BACKLIGHT, !touch_state);
Expand All @@ -793,10 +793,10 @@ void UITask::loop() {
if (curr) curr->poll();

if (_display != NULL && _display->isOn()) {
if (millis() >= _next_refresh && curr) {
if (millis_passed(_next_refresh) && curr) {
_display->startFrame();
int delay_millis = curr->render(*_display);
if (millis() < _alert_expiry) { // render alert popup
if (!millis_passed(_alert_expiry)) { // render alert popup
_display->setTextSize(1);
int y = _display->height() / 3;
int p = _display->height() / 32;
Expand All @@ -812,7 +812,7 @@ void UITask::loop() {
_display->endFrame();
}
#if AUTO_OFF_MILLIS > 0
if (millis() > _auto_off) {
if (millis_passed(_auto_off)) {
_display->turnOff();
}
#endif
Expand All @@ -823,7 +823,7 @@ void UITask::loop() {
#endif

#ifdef AUTO_SHUTDOWN_MILLIVOLTS
if (millis() > next_batt_chck) {
if (millis_passed(next_batt_chck)) {
uint16_t milliVolts = getBattMilliVolts();
if (milliVolts > 0 && milliVolts < AUTO_SHUTDOWN_MILLIVOLTS) {

Expand Down
4 changes: 2 additions & 2 deletions examples/companion_radio/ui-new/UITask.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class UITask : public AbstractUITask {
unsigned long _alert_expiry;
int _msgcount;
unsigned long ui_started_at, next_batt_chck;
int next_backlight_btn_check = 0;
unsigned long next_backlight_btn_check = 0;
#ifdef PIN_STATUS_LED
int led_state = 0;
int next_led_change = 0;
unsigned long next_led_change = 0;
int last_led_increment = 0;
#endif

Expand Down
10 changes: 5 additions & 5 deletions examples/companion_radio/ui-orig/UITask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,11 @@ void UITask::renderCurrScreen() {
void UITask::userLedHandler() {
#ifdef PIN_STATUS_LED
static int state = 0;
static int next_change = 0;
static unsigned long next_change = 0;
static int last_increment = 0;

int cur_time = millis();
if (cur_time > next_change) {
unsigned long cur_time = millis();
if (millis_passed(next_change)) {
if (state == 0) {
state = 1;
if (_msgcount > 0) {
Expand Down Expand Up @@ -334,14 +334,14 @@ void UITask::loop() {
_need_refresh = true;
_firstBoot = false;
}
if (millis() >= _next_refresh && _need_refresh) {
if (millis_passed(_next_refresh) && _need_refresh) {
_display->startFrame();
renderCurrScreen();
_display->endFrame();

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

#define AUTO_OFF_MILLIS 20000 // 20 seconds
#define BOOT_SCREEN_MILLIS 4000 // 4 seconds
Expand Down Expand Up @@ -42,7 +43,7 @@ void UITask::begin(NodePrefs* node_prefs, const char* build_date, const char* fi

void UITask::renderCurrScreen() {
char tmp[80];
if (millis() < BOOT_SCREEN_MILLIS) { // boot screen
if (!millis_passed(BOOT_SCREEN_MILLIS)) { // boot screen
// meshcore logo
_display->setColor(DisplayDriver::BLUE);
int logoWidth = 128;
Expand Down Expand Up @@ -82,7 +83,7 @@ void UITask::renderCurrScreen() {

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

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

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

#define AUTO_OFF_MILLIS 20000 // 20 seconds
#define BOOT_SCREEN_MILLIS 4000 // 4 seconds
Expand Down Expand Up @@ -42,7 +43,7 @@ void UITask::begin(NodePrefs* node_prefs, const char* build_date, const char* fi

void UITask::renderCurrScreen() {
char tmp[80];
if (millis() < BOOT_SCREEN_MILLIS) { // boot screen
if (!millis_passed(BOOT_SCREEN_MILLIS)) { // boot screen
// meshcore logo
_display->setColor(DisplayDriver::BLUE);
int logoWidth = 128;
Expand Down Expand Up @@ -82,7 +83,7 @@ void UITask::renderCurrScreen() {

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

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

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

#define AUTO_OFF_MILLIS 20000 // 20 seconds
#define BOOT_SCREEN_MILLIS 4000 // 4 seconds
Expand Down Expand Up @@ -42,7 +43,7 @@ void UITask::begin(NodePrefs* node_prefs, const char* build_date, const char* fi

void UITask::renderCurrScreen() {
char tmp[80];
if (millis() < BOOT_SCREEN_MILLIS) { // boot screen
if (!millis_passed(BOOT_SCREEN_MILLIS)) { // boot screen
// meshcore logo
_display->setColor(DisplayDriver::BLUE);
int logoWidth = 128;
Expand Down Expand Up @@ -82,7 +83,7 @@ void UITask::renderCurrScreen() {

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

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

_next_refresh = millis() + 1000; // refresh every second
}
if (millis() > _auto_off) {
if (millis_passed(_auto_off)) {
_display->turnOff();
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/Dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ void Dispatcher::begin() {
n_sent_flood = n_sent_direct = 0;
n_recv_flood = n_recv_direct = 0;
_err_flags = 0;
radio_nonrx_start = _ms->getMillis();

unsigned long now = _ms->getMillis();
radio_nonrx_start = now;
// Initialize timers to "just passed" so millisHasNowPassed() returns true
// immediately. Using 0 breaks when millis is in the upper half of uint32
// range (near the 49-day wrap), because the signed comparison trick
// interprets 0 as a future timestamp.
next_tx_time = now;
next_floor_calib_time = now;
next_agc_reset_time = now;

duty_cycle_window_ms = getDutyCycleWindowMs();
float duty_cycle = 1.0f / (1.0f + getAirtimeBudgetFactor());
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/ArduinoHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ class ArduinoMillis : public mesh::MillisecondClock {
unsigned long getMillis() override { return millis(); }
};

/**
* \brief Wrap-safe millis deadline check, handling the 49-day millis() overflow.
* \param target The deadline timestamp obtained from millis() + delay.
* \returns true when the deadline has passed.
* \note Use this instead of \c millis()>=target which fails near the 32-bit wrap.
* Works via signed subtraction (2's complement).
*/
inline bool millis_passed(unsigned long target) {
return (long)(millis() - target) > 0;
}

class StdRNG : public mesh::RNG {
public:
void begin(long seed) { randomSeed(seed); }
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/NRF52Board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ void NRF52Board::sleep(uint32_t secs) {
float NRF52Board::getMCUTemperature() {
NRF_TEMP->TASKS_START = 1; // Start temperature measurement

long startTime = millis();
unsigned long startTime = millis();
while (NRF_TEMP->EVENTS_DATARDY == 0) { // Wait for completion. Should complete in 50us
if(millis() - startTime > 5) { // To wait 5ms just in case
NRF_TEMP->TASKS_STOP = 1;
Expand Down
7 changes: 4 additions & 3 deletions src/helpers/esp32/SerialBLEInterface.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "SerialBLEInterface.h"
#include "esp_mac.h"
#include <helpers/ArduinoHelpers.h>

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
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 !millis_passed(_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
&& millis_passed(_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 && millis_passed(adv_restart_time)) {
if (pServer->getConnectedCount() == 0) {
BLE_DEBUG_PRINTLN("SerialBLEInterface -> re-starting advertising");
pServer->getAdvertising()->start(); // re-Start advertising
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/radiolib/CustomSX1276.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <RadioLib.h>
#include <helpers/ArduinoHelpers.h>

#define RH_RF95_MODEM_STATUS_CLEAR 0x10
#define RH_RF95_MODEM_STATUS_HEADER_INFO_VALID 0x08
Expand Down Expand Up @@ -79,7 +80,7 @@ class CustomSX1276 : public SX1276 {

// wait for channel activity detected or timeout
unsigned long timeout = millis() + 16;
while(!this->mod->hal->digitalRead(this->mod->getIrq()) && millis() < timeout) {
while(!this->mod->hal->digitalRead(this->mod->getIrq()) && !millis_passed(timeout)) {
this->mod->hal->yield();
if(this->mod->hal->digitalRead(this->mod->getGpio())) {
return(RADIOLIB_PREAMBLE_DETECTED);
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/ArduinoHelpers.h>

#if ENV_PIN_SDA && ENV_PIN_SCL
#define TELEM_WIRE &Wire1 // Use Wire1 as the I2C bus for Environment Sensors
Expand Down Expand Up @@ -704,13 +705,13 @@ void EnvironmentSensorManager::stop_gps() {
}

void EnvironmentSensorManager::loop() {
static long next_gps_update = 0;
static unsigned long next_gps_update = 0;

#if ENV_INCLUDE_GPS
if (gps_active) {
_location->loop();
}
if (millis() > next_gps_update) {
if (millis_passed(next_gps_update)) {

if(gps_active){
#ifdef RAK_WISBLOCK_GPS
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/sensors/MicroNMEALocationProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <MicroNMEA.h>
#include <RTClib.h>
#include <helpers/RefCountedDigitalPin.h>
#include <helpers/ArduinoHelpers.h>

#ifndef GPS_EN
#ifdef PIN_GPS_EN
Expand Down Expand Up @@ -42,7 +43,7 @@ class MicroNMEALocationProvider : public LocationProvider {
int8_t _claims = 0;
int _pin_reset;
int _pin_en;
long next_check = 0;
unsigned long next_check = 0;
long time_valid = 0;
unsigned long _last_time_sync = 0;
static const unsigned long TIME_SYNC_INTERVAL = 1800000; // Re-sync every 30 minutes
Expand Down Expand Up @@ -143,7 +144,7 @@ public :

if (!isValid()) time_valid = 0;

if (millis() > next_check) {
if (millis_passed(next_check)) {
next_check = millis() + 1000;
// Re-enable time sync periodically when GPS has valid fix
if (!_time_sync_needed && _clock != NULL && (millis() - _last_time_sync) > TIME_SYNC_INTERVAL) {
Expand Down
Loading
Loading