a lot of thread housekeeping. Switch them off when not needed / disabled.

This commit is contained in:
Thomas Göttgens 2022-12-29 15:45:49 +01:00
parent 1a949b7ca6
commit 41a1dfec79
15 changed files with 102 additions and 59 deletions

View File

@ -54,7 +54,7 @@ int32_t RotaryEncoderInterruptBase::runOnce()
this->action = ROTARY_ACTION_NONE; this->action = ROTARY_ACTION_NONE;
return 30000; // TODO: technically this can be MAX_INT return INT32_MAX;
} }
void RotaryEncoderInterruptBase::intPressHandler() void RotaryEncoderInterruptBase::intPressHandler()

View File

@ -7,7 +7,7 @@ enum RotaryEncoderInterruptBaseStateType { ROTARY_EVENT_OCCURRED, ROTARY_EVENT_C
enum RotaryEncoderInterruptBaseActionType { ROTARY_ACTION_NONE, ROTARY_ACTION_PRESSED, ROTARY_ACTION_CW, ROTARY_ACTION_CCW }; enum RotaryEncoderInterruptBaseActionType { ROTARY_ACTION_NONE, ROTARY_ACTION_PRESSED, ROTARY_ACTION_CW, ROTARY_ACTION_CCW };
class RotaryEncoderInterruptBase : public Observable<const InputEvent *>, private concurrency::OSThread class RotaryEncoderInterruptBase : public Observable<const InputEvent *>, public concurrency::OSThread
{ {
public: public:
explicit RotaryEncoderInterruptBase(const char *name); explicit RotaryEncoderInterruptBase(const char *name);

View File

@ -9,6 +9,8 @@ void RotaryEncoderInterruptImpl1::init()
{ {
if (!moduleConfig.canned_message.rotary1_enabled) { if (!moduleConfig.canned_message.rotary1_enabled) {
// Input device is disabled. // Input device is disabled.
setInterval(INT32_MAX);
enabled = false;
return; return;
} }

View File

@ -13,6 +13,8 @@ void CardKbI2cImpl::init()
if (cardkb_found != CARDKB_ADDR) if (cardkb_found != CARDKB_ADDR)
{ {
// Input device is not detected. // Input device is not detected.
setInterval(INT32_MAX);
enabled = false;
return; return;
} }

View File

@ -5,7 +5,7 @@
class KbI2cBase : class KbI2cBase :
public Observable<const InputEvent *>, public Observable<const InputEvent *>,
private concurrency::OSThread public concurrency::OSThread
{ {
public: public:
explicit KbI2cBase(const char *name); explicit KbI2cBase(const char *name);

View File

@ -165,11 +165,20 @@ void createSSLCert()
WebServerThread *webServerThread; WebServerThread *webServerThread;
WebServerThread::WebServerThread() : concurrency::OSThread("WebServerThread") {} WebServerThread::WebServerThread() : concurrency::OSThread("WebServerThread") {
if(!config.network.wifi_enabled) {
setInterval(INT32_MAX);
enabled = false;
}
}
int32_t WebServerThread::runOnce() int32_t WebServerThread::runOnce()
{ {
// DEBUG_MSG("WebServerThread::runOnce()\n"); if(!config.network.wifi_enabled) {
setInterval(INT32_MAX);
enabled = false;
}
handleWebResponse(); handleWebResponse();
if (requestRestart && (millis() / 1000) > requestRestart) { if (requestRestart && (millis() / 1000) > requestRestart) {

View File

@ -55,10 +55,18 @@ CannedMessageModule::CannedMessageModule()
if ((this->splitConfiguredMessages() <= 0) && (cardkb_found != CARDKB_ADDR)) { if ((this->splitConfiguredMessages() <= 0) && (cardkb_found != CARDKB_ADDR)) {
DEBUG_MSG("CannedMessageModule: No messages are configured. Module is disabled\n"); DEBUG_MSG("CannedMessageModule: No messages are configured. Module is disabled\n");
this->runState = CANNED_MESSAGE_RUN_STATE_DISABLED; this->runState = CANNED_MESSAGE_RUN_STATE_DISABLED;
setInterval(INT32_MAX);
enabled = false;
} else { } else {
DEBUG_MSG("CannedMessageModule is enabled\n"); DEBUG_MSG("CannedMessageModule is enabled\n");
this->inputObserver.observe(inputBroker); this->inputObserver.observe(inputBroker);
} }
setInterval(INT32_MAX);
enabled = false;
} else {
this->runState = CANNED_MESSAGE_RUN_STATE_DISABLED;
setInterval(INT32_MAX);
enabled = false;
} }
} }

View File

@ -199,6 +199,7 @@ ExternalNotificationModule::ExternalNotificationModule()
} }
} else { } else {
DEBUG_MSG("External Notification Module Disabled\n"); DEBUG_MSG("External Notification Module Disabled\n");
setInterval(INT32_MAX);
enabled = false; enabled = false;
} }
} }

View File

@ -48,64 +48,66 @@ static uint64_t digitalReads(uint64_t mask)
RemoteHardwareModule::RemoteHardwareModule() RemoteHardwareModule::RemoteHardwareModule()
: ProtobufModule("remotehardware", PortNum_REMOTE_HARDWARE_APP, &HardwareMessage_msg), concurrency::OSThread( : ProtobufModule("remotehardware", PortNum_REMOTE_HARDWARE_APP, &HardwareMessage_msg), concurrency::OSThread(
"remotehardware") "RemoteHardwareModule")
{ {
} }
bool RemoteHardwareModule::handleReceivedProtobuf(const MeshPacket &req, HardwareMessage *pptr) bool RemoteHardwareModule::handleReceivedProtobuf(const MeshPacket &req, HardwareMessage *pptr)
{ {
auto p = *pptr; if (moduleConfig.remote_hardware.enabled) {
DEBUG_MSG("Received RemoteHardware typ=%d\n", p.type); auto p = *pptr;
DEBUG_MSG("Received RemoteHardware typ=%d\n", p.type);
switch (p.type) { switch (p.type) {
case HardwareMessage_Type_WRITE_GPIOS: case HardwareMessage_Type_WRITE_GPIOS:
// Print notification to LCD screen // Print notification to LCD screen
screen->print("Write GPIOs\n"); screen->print("Write GPIOs\n");
for (uint8_t i = 0; i < NUM_GPIOS; i++) { for (uint8_t i = 0; i < NUM_GPIOS; i++) {
uint64_t mask = 1 << i; uint64_t mask = 1 << i;
if (p.gpio_mask & mask) { if (p.gpio_mask & mask) {
digitalWrite(i, (p.gpio_value & mask) ? 1 : 0); digitalWrite(i, (p.gpio_value & mask) ? 1 : 0);
}
} }
pinModes(p.gpio_mask, OUTPUT);
break;
case HardwareMessage_Type_READ_GPIOS: {
// Print notification to LCD screen
if (screen)
screen->print("Read GPIOs\n");
uint64_t res = digitalReads(p.gpio_mask);
// Send the reply
HardwareMessage r = HardwareMessage_init_default;
r.type = HardwareMessage_Type_READ_GPIOS_REPLY;
r.gpio_value = res;
r.gpio_mask = p.gpio_mask;
MeshPacket *p2 = allocDataProtobuf(r);
setReplyTo(p2, req);
myReply = p2;
break;
} }
pinModes(p.gpio_mask, OUTPUT);
break; case HardwareMessage_Type_WATCH_GPIOS: {
watchGpios = p.gpio_mask;
lastWatchMsec = 0; // Force a new publish soon
previousWatch = ~watchGpios; // generate a 'previous' value which is guaranteed to not match (to force an initial publish)
enabled = true; // Let our thread run at least once
DEBUG_MSG("Now watching GPIOs 0x%llx\n", watchGpios);
break;
}
case HardwareMessage_Type_READ_GPIOS: { case HardwareMessage_Type_READ_GPIOS_REPLY:
// Print notification to LCD screen case HardwareMessage_Type_GPIOS_CHANGED:
if (screen) break; // Ignore - we might see our own replies
screen->print("Read GPIOs\n");
uint64_t res = digitalReads(p.gpio_mask); default:
DEBUG_MSG("Hardware operation %d not yet implemented! FIXME\n", p.type);
// Send the reply break;
HardwareMessage r = HardwareMessage_init_default; }
r.type = HardwareMessage_Type_READ_GPIOS_REPLY;
r.gpio_value = res;
r.gpio_mask = p.gpio_mask;
MeshPacket *p2 = allocDataProtobuf(r);
setReplyTo(p2, req);
myReply = p2;
break;
}
case HardwareMessage_Type_WATCH_GPIOS: {
watchGpios = p.gpio_mask;
lastWatchMsec = 0; // Force a new publish soon
previousWatch = ~watchGpios; // generate a 'previous' value which is guaranteed to not match (to force an initial publish)
enabled = true; // Let our thread run at least once
DEBUG_MSG("Now watching GPIOs 0x%llx\n", watchGpios);
break;
}
case HardwareMessage_Type_READ_GPIOS_REPLY:
case HardwareMessage_Type_GPIOS_CHANGED:
break; // Ignore - we might see our own replies
default:
DEBUG_MSG("Hardware operation %d not yet implemented! FIXME\n", p.type);
break;
} }
return false; return false;
@ -113,7 +115,7 @@ bool RemoteHardwareModule::handleReceivedProtobuf(const MeshPacket &req, Hardwar
int32_t RemoteHardwareModule::runOnce() int32_t RemoteHardwareModule::runOnce()
{ {
if (watchGpios) { if (moduleConfig.remote_hardware.enabled && watchGpios) {
uint32_t now = millis(); uint32_t now = millis();
if (now - lastWatchMsec >= WATCH_INTERVAL_MSEC) { if (now - lastWatchMsec >= WATCH_INTERVAL_MSEC) {
@ -134,6 +136,7 @@ int32_t RemoteHardwareModule::runOnce()
} else { } else {
// No longer watching anything - stop using CPU // No longer watching anything - stop using CPU
enabled = false; enabled = false;
return INT32_MAX;
} }
return 200; // Poll our GPIOs every 200ms (FIXME, make adjustable via protobuf arg) return 200; // Poll our GPIOs every 200ms (FIXME, make adjustable via protobuf arg)

View File

@ -222,6 +222,7 @@ int32_t SerialModule::runOnce()
} else { } else {
DEBUG_MSG("Serial Module Disabled\n"); DEBUG_MSG("Serial Module Disabled\n");
enabled = false;
return INT32_MAX; return INT32_MAX;
} }
} }
@ -303,9 +304,6 @@ ProcessMessage SerialModuleRadio::handleReceived(const MeshPacket &mp)
} }
} }
} }
} else {
DEBUG_MSG("Serial Module Disabled\n");
} }
return ProcessMessage::CONTINUE; // Let others look at this message also if they want return ProcessMessage::CONTINUE; // Let others look at this message also if they want
} }

View File

@ -67,6 +67,7 @@ int32_t EnvironmentTelemetryModule::runOnce()
if (!(moduleConfig.telemetry.environment_measurement_enabled || if (!(moduleConfig.telemetry.environment_measurement_enabled ||
moduleConfig.telemetry.environment_screen_enabled)) { moduleConfig.telemetry.environment_screen_enabled)) {
// If this module is not enabled, and the user doesn't want the display screen don't waste any OSThread time on it // If this module is not enabled, and the user doesn't want the display screen don't waste any OSThread time on it
enabled = false;
return result; return result;
} }

View File

@ -139,6 +139,8 @@ AudioModule::AudioModule() : SinglePortModule("AudioModule", PortNum_AUDIO_APP),
DEBUG_MSG(" using %d frames of %d bytes for a total payload length of %d bytes\n", encode_frame_num, encode_codec_size, encode_frame_size); DEBUG_MSG(" using %d frames of %d bytes for a total payload length of %d bytes\n", encode_frame_num, encode_codec_size, encode_frame_size);
xTaskCreate(&run_codec2, "codec2_task", 30000, NULL, 5, &codec2HandlerTask); xTaskCreate(&run_codec2, "codec2_task", 30000, NULL, 5, &codec2HandlerTask);
} else { } else {
setInterval(INT32_MAX);
enabled = false;
DEBUG_MSG("Codec2 disabled (AudioModule %d, Region %s, permitted %d)\n", moduleConfig.audio.codec2_enabled, myRegion->name, myRegion->audioPermitted); DEBUG_MSG("Codec2 disabled (AudioModule %d, Region %s, permitted %d)\n", moduleConfig.audio.codec2_enabled, myRegion->name, myRegion->audioPermitted);
} }
} }
@ -259,6 +261,7 @@ int32_t AudioModule::runOnce()
return 100; return 100;
} else { } else {
DEBUG_MSG("Audio Module Disabled\n"); DEBUG_MSG("Audio Module Disabled\n");
enabled = false;
return INT32_MAX; return INT32_MAX;
} }

View File

@ -82,6 +82,7 @@ int32_t RangeTestModule::runOnce()
return (senderHeartbeat); return (senderHeartbeat);
} else { } else {
enabled = false;
return (INT32_MAX); return (INT32_MAX);
// This thread does not need to run as a receiver // This thread does not need to run as a receiver
} }
@ -94,6 +95,7 @@ int32_t RangeTestModule::runOnce()
} }
#endif #endif
enabled = false;
return (INT32_MAX); return (INT32_MAX);
} }

View File

@ -52,6 +52,7 @@ int32_t StoreForwardModule::runOnce()
return (this->packetTimeMax); return (this->packetTimeMax);
} }
#endif #endif
enabled = false; // Client doesn't need periodical
return (INT32_MAX); return (INT32_MAX);
} }
@ -458,6 +459,9 @@ StoreForwardModule::StoreForwardModule()
is_client = true; is_client = true;
DEBUG_MSG("*** Initializing Store & Forward Module in Client mode\n"); DEBUG_MSG("*** Initializing Store & Forward Module in Client mode\n");
} }
} else {
setInterval(INT32_MAX);
enabled = false;
} }
#endif #endif
} }

View File

@ -128,12 +128,18 @@ void mqttInit()
MQTT::MQTT() : concurrency::OSThread("mqtt"), pubSub(mqttClient), mqttQueue(MAX_MQTT_QUEUE) MQTT::MQTT() : concurrency::OSThread("mqtt"), pubSub(mqttClient), mqttQueue(MAX_MQTT_QUEUE)
{ {
assert(!mqtt); if(moduleConfig.mqtt.enabled) {
mqtt = this;
pubSub.setCallback(mqttCallback); assert(!mqtt);
mqtt = this;
// preflightSleepObserver.observe(&preflightSleep); pubSub.setCallback(mqttCallback);
// preflightSleepObserver.observe(&preflightSleep);
} else {
setInterval(INT32_MAX);
enabled = false;
}
} }
bool MQTT::connected() bool MQTT::connected()
@ -238,6 +244,10 @@ bool MQTT::wantsLink() const
int32_t MQTT::runOnce() int32_t MQTT::runOnce()
{ {
if(!moduleConfig.mqtt.enabled) {
enabled = false;
return INT32_MAX;
}
bool wantConnection = wantsLink(); bool wantConnection = wantsLink();
// If connected poll rapidly, otherwise only occasionally check for a wifi connection change and ability to contact server // If connected poll rapidly, otherwise only occasionally check for a wifi connection change and ability to contact server