Skip to content

Commit

Permalink
Merge branch 'master' into principale
Browse files Browse the repository at this point in the history
  • Loading branch information
ponzano authored Jan 27, 2025
2 parents 8cc07a5 + 4747e73 commit 54dd4ed
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 16 deletions.
10 changes: 10 additions & 0 deletions src/mesh/MeshModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
std::vector<MeshModule *> *MeshModule::modules;

const meshtastic_MeshPacket *MeshModule::currentRequest;
uint8_t MeshModule::numPeriodicModules = 0;

/**
* If any of the current chain of modules has already sent a reply, it will be here. This is useful to allow
Expand All @@ -35,6 +36,15 @@ MeshModule::~MeshModule()
modules->erase(it);
}

// ⚠️ **Only call once** to set the initial delay before a module starts broadcasting periodically
int32_t MeshModule::setStartDelay()
{
int32_t startDelay = MESHMODULE_MIN_BROADCAST_DELAY_MS + numPeriodicModules * MESHMODULE_BROADCAST_SPACING_MS;
numPeriodicModules++;

return startDelay;
}

meshtastic_MeshPacket *MeshModule::allocAckNak(meshtastic_Routing_Error err, NodeNum to, PacketId idFrom, ChannelIndex chIndex,
uint8_t hopLimit)
{
Expand Down
9 changes: 9 additions & 0 deletions src/mesh/MeshModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include <OLEDDisplayUi.h>
#endif

#define MESHMODULE_MIN_BROADCAST_DELAY_MS 30 * 1000 // Min. delay after boot before sending first broadcast by any module
#define MESHMODULE_BROADCAST_SPACING_MS 15 * 1000 // Initial spacing between broadcasts of different modules

/** handleReceived return enumeration
*
* Use ProcessMessage::CONTINUE to allows other modules to process a message.
Expand Down Expand Up @@ -119,6 +122,12 @@ class MeshModule
*/
static const meshtastic_MeshPacket *currentRequest;

// We keep track of the number of modules that send a periodic broadcast to schedule them spaced out over time
static uint8_t numPeriodicModules;

// Set the start delay for module that broadcasts periodically
int32_t setStartDelay();

/**
* If your handler wants to send a response, simply set currentReply and it will be sent at the end of response handling.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/modules/DetectionSensorModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ int32_t DetectionSensorModule::runOnce()
}
LOG_INFO("Detection Sensor Module: init");

return DELAYED_INTERVAL;
return setStartDelay();
}

// LOG_DEBUG("Detection Sensor Module: Current pin state: %i", digitalRead(moduleConfig.detection_sensor.monitor_pin));
Expand Down Expand Up @@ -161,4 +161,4 @@ bool DetectionSensorModule::hasDetectionEvent()
bool currentState = digitalRead(moduleConfig.detection_sensor.monitor_pin);
// LOG_DEBUG("Detection Sensor Module: Current state: %i", currentState);
return (moduleConfig.detection_sensor.detection_trigger_type & 1) ? currentState : !currentState;
}
}
7 changes: 4 additions & 3 deletions src/modules/NodeInfoModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ NodeInfoModule::NodeInfoModule()
: ProtobufModule("nodeinfo", meshtastic_PortNum_NODEINFO_APP, &meshtastic_User_msg), concurrency::OSThread("NodeInfo")
{
isPromiscuous = true; // We always want to update our nodedb, even if we are sniffing on others
setIntervalFromNow(30 *
1000); // Send our initial owner announcement 30 seconds after we start (to give network time to setup)

setIntervalFromNow(setStartDelay()); // Send our initial owner announcement 30 seconds
// after we start (to give network time to setup)
}

int32_t NodeInfoModule::runOnce()
Expand All @@ -112,4 +113,4 @@ int32_t NodeInfoModule::runOnce()
sendOurNodeInfo(NODENUM_BROADCAST, requestReplies); // Send our info (don't request replies)
}
return Default::getConfiguredOrDefaultMs(config.device.node_info_broadcast_secs, default_node_info_broadcast_secs);
}
}
5 changes: 3 additions & 2 deletions src/modules/PositionModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ PositionModule::PositionModule()
nodeStatusObserver.observe(&nodeStatus->onNewStatus);

if (config.device.role != meshtastic_Config_DeviceConfig_Role_TRACKER &&
config.device.role != meshtastic_Config_DeviceConfig_Role_TAK_TRACKER)
setIntervalFromNow(60 * 1000);
config.device.role != meshtastic_Config_DeviceConfig_Role_TAK_TRACKER) {
setIntervalFromNow(setStartDelay());
}

// Power saving trackers should clear their position on startup to avoid waking up and sending a stale position
if ((config.device.role == meshtastic_Config_DeviceConfig_Role_TRACKER ||
Expand Down
4 changes: 2 additions & 2 deletions src/modules/Telemetry/AirQualityTelemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ int32_t AirQualityTelemetryModule::runOnce()
nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_PMSA003I].first = found.address.address;
nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_PMSA003I].second =
i2cScanner->fetchI2CBus(found.address);
return 1000;
return setStartDelay();
}
#endif
return disable();
}
return 1000;
return setStartDelay();
}
return disable();
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/Telemetry/DeviceTelemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DeviceTelemetryModule : private concurrency::OSThread, public ProtobufModu
uptimeWrapCount = 0;
uptimeLastMs = millis();
nodeStatusObserver.observe(&nodeStatus->onNewStatus);
setIntervalFromNow(45 * 1000); // Wait until NodeInfo is sent
setIntervalFromNow(setStartDelay()); // Wait until NodeInfo is sent
}
virtual bool wantUIFrame() { return false; }

Expand Down Expand Up @@ -62,4 +62,4 @@ class DeviceTelemetryModule : private concurrency::OSThread, public ProtobufModu

uint32_t uptimeWrapCount;
uint32_t uptimeLastMs;
};
};
6 changes: 3 additions & 3 deletions src/modules/Telemetry/EnvironmentTelemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ int32_t EnvironmentTelemetryModule::runOnce()

if (moduleConfig.telemetry.environment_measurement_enabled) {
LOG_INFO("Environment Telemetry: init");
// it's possible to have this module enabled, only for displaying values on the screen.
// therefore, we should only enable the sensor loop if measurement is also enabled
#ifdef SENSECAP_INDICATOR
result = indicatorSensor.runOnce();
#endif
Expand Down Expand Up @@ -171,7 +169,9 @@ int32_t EnvironmentTelemetryModule::runOnce()
#endif
#endif
}
return result;
// it's possible to have this module enabled, only for displaying values on the screen.
// therefore, we should only enable the sensor loop if measurement is also enabled
return result == UINT32_MAX ? disable() : setStartDelay();
} else {
// if we somehow got to a second run of this module with measurement disabled, then just wait forever
if (!moduleConfig.telemetry.environment_measurement_enabled) {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/HealthTelemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int32_t HealthTelemetryModule::runOnce()
if (max30102Sensor.hasSensor())
result = max30102Sensor.runOnce();
}
return result;
return result == UINT32_MAX ? disable() : setStartDelay();
} else {
// if we somehow got to a second run of this module with measurement disabled, then just wait forever
if (!moduleConfig.telemetry.health_measurement_enabled) {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/PowerTelemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ int32_t PowerTelemetryModule::runOnce()
if (max17048Sensor.hasSensor() && !max17048Sensor.isInitialized())
result = max17048Sensor.runOnce();
}
return result;
return result == UINT32_MAX ? disable() : setStartDelay();
#else
return disable();
#endif
Expand Down

0 comments on commit 54dd4ed

Please sign in to comment.