From 0c8e0efed2a01e3a47bffebcb22b2982c550b4cc Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Sun, 11 Oct 2020 08:12:53 +0800 Subject: [PATCH] new threading finished- saves about 10mA for the high activity states --- src/PowerFSM.h | 1 + src/concurrency/InterruptableDelay.cpp | 4 ++-- src/concurrency/OSThread.h | 15 +++--------- src/main.cpp | 33 +++++++++++++++++++++----- 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/PowerFSM.h b/src/PowerFSM.h index 4a219f570..d996acbd3 100644 --- a/src/PowerFSM.h +++ b/src/PowerFSM.h @@ -20,5 +20,6 @@ #define EVENT_POWER_DISCONNECTED 14 extern Fsm powerFSM; +extern State statePOWER; void PowerFSM_setup(); diff --git a/src/concurrency/InterruptableDelay.cpp b/src/concurrency/InterruptableDelay.cpp index dd7d6f86e..4b4ccc914 100644 --- a/src/concurrency/InterruptableDelay.cpp +++ b/src/concurrency/InterruptableDelay.cpp @@ -20,12 +20,12 @@ InterruptableDelay::~InterruptableDelay() bool InterruptableDelay::delay(uint32_t msec) { if (msec) { - DEBUG_MSG("delay %u ", msec); + // DEBUG_MSG("delay %u ", msec); // sem take will return false if we timed out (i.e. were not interrupted) bool r = xSemaphoreTake(semaphore, pdMS_TO_TICKS(msec)); - DEBUG_MSG("interrupt=%d\n", r); + // DEBUG_MSG("interrupt=%d\n", r); return !r; } else { return true; diff --git a/src/concurrency/OSThread.h b/src/concurrency/OSThread.h index 3c0c62dc9..dc600387e 100644 --- a/src/concurrency/OSThread.h +++ b/src/concurrency/OSThread.h @@ -17,21 +17,12 @@ extern InterruptableDelay mainDelay; /** * @brief Base threading + * + * This is a pseudo threading layer that is super easy to port, well suited to our slow network and very ram & power efficient. * * TODO FIXME @geeksville * - * make bluetooth wake cpu immediately (because it puts a message in a queue?) - * - * don't sleep at all if in POWER mode - * - * wake for serial character received - * - * add concept of 'low priority' threads that are not used to block sleep? - * - * make everything use osthread - * - * if we wake once because of a ble packet we might need to run loop multiple times before we can truely sleep - * + * move more things into OSThreads * remove lock/lockguard * * move typedQueue into concurrency diff --git a/src/main.cpp b/src/main.cpp index 0bdb29205..175fbc607 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -53,7 +53,6 @@ meshtastic::NodeStatus *nodeStatus = new meshtastic::NodeStatus(); bool ssd1306_found; bool axp192_found; - Router *router = NULL; // Users of router don't care what sort of subclass implements that API // ----------------------------------------------------------------------------- @@ -115,7 +114,28 @@ static int32_t ledBlinker() return powerStatus->getIsCharging() ? 1000 : (ledOn ? 1 : 1000); } +/// Wrapper to convert our powerFSM stuff into a 'thread' +class PowerFSMThread : public OSThread +{ + public: + // callback returns the period for the next callback invocation (or 0 if we should no longer be called) + PowerFSMThread() : OSThread("PowerFSM") {} + + protected: + int32_t runOnce() + { + powerFSM.run_machine(); + + /// If we are in power state we force the CPU to wake every 10ms to check for serial characters (we don't yet wake + /// cpu for serial rx - FIXME) + canSleep = (powerFSM.getState() != &statePOWER); + + return 10; + } +}; + static Periodic *ledPeriodic; +static OSThread *powerFSMthread; // Prepare for button presses #ifdef BUTTON_PIN @@ -349,6 +369,7 @@ void setup() // This must be _after_ service.init because we need our preferences loaded from flash to have proper timeout values PowerFSM_setup(); // we will transition to ON in a couple of seconds, FIXME, only do this for cold boots, not waking from SDS + powerFSMthread = new PowerFSMThread(); // setBluetoothEnable(false); we now don't start bluetooth until we enter the proper state setCPUFast(false); // 80MHz is fine for our slow peripherals @@ -377,8 +398,6 @@ axpDebugOutput.setup(); void loop() { - powerFSM.run_machine(); - // axpDebugOutput.loop(); #ifdef DEBUG_PORT @@ -416,9 +435,11 @@ void loop() long delayMsec = mainController.runOrDelay(); - if(mainController.nextThread && delayMsec) - DEBUG_MSG("Next %s in %ld\n", mainController.nextThread->ThreadName.c_str(), mainController.nextThread->tillRun(millis())); - + /* if (mainController.nextThread && delayMsec) + DEBUG_MSG("Next %s in %ld\n", mainController.nextThread->ThreadName.c_str(), + mainController.nextThread->tillRun(millis())); + */ + // We want to sleep as long as possible here - because it saves power mainDelay.delay(delayMsec); }