Make a Periodic class, use it to blink the LED less often

This commit is contained in:
geeksville
2020-02-15 11:15:43 -08:00
parent c128ee7376
commit 0453ffbfd7
5 changed files with 66 additions and 16 deletions

32
src/Periodic.h Normal file
View File

@@ -0,0 +1,32 @@
#pragma once
#include <Arduino.h>
/**
* Periodically invoke a callback.
*
* FIXME: currently just syntatic sugar for polling in loop (you must call .loop), but eventually
* generalize with the freertos scheduler so we can save lots of power by having everything either in
* something like this or triggered off of an irq.
*/
class Periodic
{
uint32_t lastMsec = 0;
uint32_t period = 1; // call soon after creation
uint32_t (*callback)();
public:
// callback returns the period for the next callback invocation (or 0 if we should no longer be called)
Periodic(uint32_t (*_callback)()) : callback(_callback) {}
// for the time being this must be called from loop
void loop()
{
uint32_t now = millis();
if (period && (now - lastMsec) >= period)
{
lastMsec = now;
period = (*callback)();
}
}
};

View File

@@ -41,8 +41,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
// Select which board is being used. If the outside build environment has sent a choice, just use that
#if !defined(T_BEAM_V10) && !defined(HELTEC_LORA32)
//#define T_BEAM_V10 // AKA Rev1 (second board released)
#define HELTEC_LORA32
#define T_BEAM_V10 // AKA Rev1 (second board released)
//#define HELTEC_LORA32
#endif
// If we are using the JTAG port for debugging, some pins must be left free for that (and things like GPS have to be disabled)

View File

@@ -32,6 +32,7 @@
#include "GPS.h"
#include "screen.h"
#include "NodeDB.h"
#include "Periodic.h"
#ifdef T_BEAM_V10
#include "axp20x.h"
@@ -392,6 +393,29 @@ void setup()
enableModemSleep();
}
uint32_t ledBlinker()
{
static bool ledOn;
ledOn ^= 1;
#ifdef LED_PIN
// toggle the led so we can get some rough sense of how often loop is pausing
digitalWrite(LED_PIN, ledOn);
#endif
if (axp192_found)
{
// blink the axp led
axp.setChgLEDMode(ledOn ? AXP20X_LED_LOW_LEVEL : AXP20X_LED_OFF);
}
// have a very sparse duty cycle of LED being on
return ledOn ? 2 : 1000;
}
Periodic ledPeriodic(ledBlinker);
void loop()
{
uint32_t msecstosleep = 1000 * 30; // How long can we sleep before we again need to service the main loop?
@@ -399,24 +423,13 @@ void loop()
gps.loop();
msecstosleep = min(screen_loop(), msecstosleep);
service.loop();
ledPeriodic.loop();
loopBLE();
static bool ledon;
ledon ^= 1;
#ifdef LED_PIN
// toggle the led so we can get some rough sense of how often loop is pausing
digitalWrite(LED_PIN, ledon);
#endif
// DEBUG_MSG("led %d\n", ledon);
#ifdef T_BEAM_V10
if (axp192_found)
{
// blink the axp led
axp.setChgLEDMode(ledon ? AXP20X_LED_LOW_LEVEL : AXP20X_LED_OFF);
#ifdef PMU_IRQ
if (pmu_irq)
{