extend power statemachine
This commit is contained in:
@@ -105,6 +105,14 @@ void initAudio()
|
||||
void setAudioState(bool state)
|
||||
{
|
||||
audioState = state;
|
||||
if (state)
|
||||
{
|
||||
digitalWrite(DAC_SDMODE, HIGH);
|
||||
}
|
||||
else
|
||||
{
|
||||
digitalWrite(DAC_SDMODE, LOW);
|
||||
}
|
||||
log_i("set Audio state %d", state);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,28 @@
|
||||
CRGB leds[NUM_LEDS];
|
||||
|
||||
bool ledstate = false;
|
||||
bool blinkState = false;
|
||||
CRGB ledcolor = CRGB::Black;
|
||||
uint32_t lastLedTime = 0;
|
||||
|
||||
void setLedBlink(bool blink)
|
||||
{
|
||||
blinkState = blink;
|
||||
}
|
||||
|
||||
void SetLedColor(CRGB color)
|
||||
{
|
||||
ledcolor = color;
|
||||
setLedBlink(false);
|
||||
}
|
||||
|
||||
void SetLedColor(CRGB color, bool blink)
|
||||
{
|
||||
SetLedColor(color);
|
||||
setLedBlink(blink);
|
||||
}
|
||||
|
||||
|
||||
void initLed(void)
|
||||
{
|
||||
FastLED.addLeds<SK6812, LED_PIN, GRB>(leds, NUM_LEDS); // GRB ordering is typical
|
||||
@@ -16,42 +36,58 @@ void handleLed(void)
|
||||
uint32_t timeNow = millis();
|
||||
if (timeNow - lastLedTime > LEDTIMEOUT)
|
||||
{
|
||||
if (ledstate)
|
||||
if (blinkState)
|
||||
{
|
||||
if (getPowerState() == POWERSTATES::on)
|
||||
{
|
||||
if(getAudioState())
|
||||
{
|
||||
leds[0] = CRGB::Purple;
|
||||
}
|
||||
else if( getRFIDlastUID() != "")
|
||||
{
|
||||
leds[0] = CRGB::Yellow;
|
||||
}
|
||||
else
|
||||
{
|
||||
leds[0] = CRGB::Green;
|
||||
}
|
||||
}
|
||||
else if (getPowerState() == POWERSTATES::poweringOn2)
|
||||
{
|
||||
leds[0] = CRGB::Blue;
|
||||
}
|
||||
else if (getPowerState() == POWERSTATES::poweringOff2)
|
||||
{
|
||||
leds[0] = CRGB::Orange;
|
||||
}
|
||||
else
|
||||
{
|
||||
leds[0] = CRGB::Red;
|
||||
}
|
||||
}
|
||||
else
|
||||
if (!ledstate)
|
||||
{
|
||||
leds[0] = CRGB::Black;
|
||||
}
|
||||
FastLED.show();
|
||||
else
|
||||
{
|
||||
leds[0] = ledcolor;
|
||||
}
|
||||
ledstate = !ledstate;
|
||||
}
|
||||
else
|
||||
{
|
||||
leds[0] = ledcolor;
|
||||
}
|
||||
|
||||
// if (ledstate)
|
||||
// {
|
||||
// if (getPowerState() == POWERSTATES::on)
|
||||
// {
|
||||
// if(getAudioState())
|
||||
// {
|
||||
// leds[0] = CRGB::Purple;
|
||||
// }
|
||||
// else if( getRFIDlastUID() != "")
|
||||
// {
|
||||
// leds[0] = CRGB::Yellow;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// leds[0] = CRGB::Green;
|
||||
// }
|
||||
// }
|
||||
// else if (getPowerState() == POWERSTATES::poweringOn2)
|
||||
// {
|
||||
// leds[0] = CRGB::Blue;
|
||||
// }
|
||||
// else if (getPowerState() == POWERSTATES::poweringOff2)
|
||||
// {
|
||||
// leds[0] = CRGB::Orange;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// leds[0] = CRGB::Red;
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// leds[0] = CRGB::Black;
|
||||
// }
|
||||
FastLED.show();
|
||||
lastLedTime = timeNow;
|
||||
}
|
||||
}
|
||||
@@ -10,3 +10,7 @@
|
||||
|
||||
void initLed(void);
|
||||
void handleLed(void);
|
||||
|
||||
void setLedBlink(bool blink);
|
||||
void SetLedColor(CRGB color);
|
||||
void SetLedColor(CRGB color, bool blink);
|
||||
|
||||
@@ -45,7 +45,15 @@ void loop()
|
||||
handleSensor();
|
||||
handleHallSensor();
|
||||
handleGame();
|
||||
handleOta();
|
||||
log_v("main: looptime = %d", millis() - looptime);
|
||||
}
|
||||
else if(getPowerState() == POWERSTATES::overTheAir2)
|
||||
{
|
||||
handleOta();
|
||||
}
|
||||
else
|
||||
{
|
||||
/* noting */
|
||||
}
|
||||
log_v("main: looptime = %d", millis() - looptime);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#include "ota.h"
|
||||
|
||||
OtaProcess_class ota(100);
|
||||
|
||||
|
||||
bool OtaProcess_class::initialize(void)
|
||||
{
|
||||
if (m_newState)
|
||||
@@ -47,6 +50,7 @@ bool OtaProcess_class::initialize(void)
|
||||
.onStart([]()
|
||||
{
|
||||
String type;
|
||||
ota.m_otaState = otaStart;
|
||||
if (ArduinoOTA.getCommand() == U_FLASH)
|
||||
{
|
||||
type = "sketch";
|
||||
@@ -59,12 +63,13 @@ bool OtaProcess_class::initialize(void)
|
||||
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
|
||||
Serial.println("Start updating " + type); })
|
||||
.onEnd([]()
|
||||
{ log_i("End"); })
|
||||
{ log_i("End"); ota.m_otaState = otaDone; })
|
||||
.onProgress([](unsigned int progress, unsigned int total)
|
||||
{ log_i("Progress: %u%%\r", (progress / (total / 100))); })
|
||||
{ log_i("Progress: %u%%\r", (progress / (total / 100))); ota.m_otaState = otaBusy; })
|
||||
.onError([](ota_error_t error)
|
||||
{
|
||||
log_e("Error[%u]: ", error);
|
||||
ota.m_otaState = otaError;
|
||||
if (error == OTA_AUTH_ERROR) log_e("Auth Failed");
|
||||
else if (error == OTA_BEGIN_ERROR) log_e("Begin Failed");
|
||||
else if (error == OTA_CONNECT_ERROR) log_e("Connect Failed");
|
||||
@@ -151,10 +156,27 @@ void OtaProcess_class::stopped(void)
|
||||
}
|
||||
}
|
||||
|
||||
void otaEnable(void)
|
||||
{
|
||||
ota.setProcessState(PROCESS_STATE::processInit);
|
||||
}
|
||||
|
||||
void otaDisable(void)
|
||||
{
|
||||
ota.setProcessState(PROCESS_STATE::processDisabled);
|
||||
}
|
||||
|
||||
void initOta(void)
|
||||
{
|
||||
/* noting */
|
||||
}
|
||||
|
||||
OTASTATES getOtaState(void)
|
||||
{
|
||||
return ota.m_otaState;
|
||||
}
|
||||
|
||||
void handleOta(void)
|
||||
{
|
||||
ota.run();
|
||||
}
|
||||
@@ -6,18 +6,20 @@
|
||||
|
||||
#include "LITTLEFS.h"
|
||||
|
||||
class OtaProcess_class : public processClass
|
||||
{
|
||||
typedef enum{
|
||||
otaInit,
|
||||
otaConnect,
|
||||
otaSetup,
|
||||
otaStart,
|
||||
otaInitDone,
|
||||
otaStart,
|
||||
otaBusy,
|
||||
otaDone,
|
||||
otaError
|
||||
}OTASTATES;
|
||||
class OtaProcess_class : public processClass
|
||||
{
|
||||
|
||||
|
||||
OTASTATES m_otaState = otaInit;
|
||||
|
||||
void active(void);
|
||||
bool initialize(void);
|
||||
@@ -28,7 +30,12 @@ class OtaProcess_class : public processClass
|
||||
|
||||
public:
|
||||
OtaProcess_class(uint32_t timeout):processClass(timeout){}
|
||||
|
||||
OTASTATES m_otaState = otaInit;
|
||||
|
||||
};
|
||||
|
||||
void initOta(void);
|
||||
void handleOta(void);
|
||||
OTASTATES getOtaState(void);
|
||||
void otaEnable(void);
|
||||
@@ -9,6 +9,7 @@ uint64_t measure_timer = 0;
|
||||
POWERSTATES powerstate = off;
|
||||
|
||||
Button buttonPower(PWR_BTN, 250UL, 1U, 0);
|
||||
extern OtaProcess_class ota;
|
||||
|
||||
Battery battery(VBATTMIN, VBATTMAX, MEAS_ADC, &getvbatt);
|
||||
|
||||
@@ -46,7 +47,7 @@ void powerOff(void)
|
||||
digitalWrite(DAC_SDMODE, LOW);
|
||||
digitalWrite(PWR_HOLD, LOW);
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
// ESP.restart();
|
||||
}
|
||||
|
||||
bool measureBattery(void)
|
||||
@@ -83,6 +84,7 @@ void handlePowerState(void)
|
||||
powerstate = poweringOn;
|
||||
}
|
||||
powerOff();
|
||||
SetLedColor(CRGB::Red);
|
||||
log_w("Powered-off");
|
||||
}
|
||||
break;
|
||||
@@ -91,17 +93,22 @@ void handlePowerState(void)
|
||||
if (buttonPower.pressedFor(POWERBUTTONDELAY))
|
||||
{
|
||||
powerstate = poweringOn2;
|
||||
SetLedColor(CRGB::White);
|
||||
|
||||
log_i("poweron 3/3 => Go");
|
||||
}
|
||||
else if (buttonPower.pressedFor(500))
|
||||
else if (buttonPower.pressedFor(POWERBUTTONDELAY / 2))
|
||||
{
|
||||
log_i("poweron 2/3");
|
||||
SetLedColor(CRGB::WhiteSmoke);
|
||||
}
|
||||
else if (buttonPower.pressedFor(200))
|
||||
{
|
||||
log_i("poweron 1/3");
|
||||
SetLedColor(CRGB::GhostWhite);
|
||||
}
|
||||
else
|
||||
|
||||
if (!buttonread)
|
||||
{
|
||||
powerstate = off;
|
||||
}
|
||||
@@ -119,12 +126,21 @@ void handlePowerState(void)
|
||||
// powerstate = lowBatt;
|
||||
// }
|
||||
}
|
||||
else
|
||||
{
|
||||
log_i("Release for poweron, hold for %d to OTA", (POWERBUTTONOTADELAY - buttonPower.getPressedFor()));
|
||||
}
|
||||
if (buttonPower.pressedFor(POWERBUTTONOTADELAY))
|
||||
{
|
||||
powerstate = overTheAir;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case powerinit:
|
||||
{
|
||||
// init all
|
||||
log_i("powerinit");
|
||||
SetLedColor(CRGB::Green);
|
||||
powerstate = on;
|
||||
}
|
||||
break;
|
||||
@@ -149,15 +165,19 @@ void handlePowerState(void)
|
||||
{
|
||||
powerstate = poweringOff2;
|
||||
setAudioState(false);
|
||||
SetLedColor(CRGB::Red, true);
|
||||
|
||||
log_w("poweringoff: 3/3 ==> powerOff");
|
||||
}
|
||||
else if (buttonPower.pressedFor(500))
|
||||
{
|
||||
log_w("poweringoff: 2/3");
|
||||
SetLedColor(CRGB::Orange, true);
|
||||
}
|
||||
else if (buttonPower.pressedFor(200))
|
||||
{
|
||||
log_w("poweringoff: 1/3");
|
||||
SetLedColor(CRGB::Brown, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -170,6 +190,7 @@ void handlePowerState(void)
|
||||
if (!buttonread)
|
||||
{
|
||||
powerstate = off;
|
||||
SetLedColor(CRGB::Red, true);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -177,6 +198,7 @@ void handlePowerState(void)
|
||||
{
|
||||
log_w("timeout ==> off");
|
||||
powerstate = off;
|
||||
SetLedColor(CRGB::Red);
|
||||
powerOff();
|
||||
delay(5000);
|
||||
}
|
||||
@@ -189,6 +211,33 @@ void handlePowerState(void)
|
||||
log_w("lowbatt");
|
||||
}
|
||||
}
|
||||
case overTheAir:
|
||||
{
|
||||
if (!buttonread)
|
||||
{
|
||||
powerstate = overTheAir2;
|
||||
otaEnable();
|
||||
SetLedColor(CRGB::Blue);
|
||||
powerOn();
|
||||
}
|
||||
log_i("ota state active, release powerbutton");
|
||||
}
|
||||
case overTheAir2:
|
||||
{
|
||||
if (getOtaState() == OTASTATES::otaBusy)
|
||||
{
|
||||
SetLedColor(CRGB::Blue, true);
|
||||
log_i("ota state active, ota busy ==> On");
|
||||
|
||||
}
|
||||
if (getOtaState() == OTASTATES::otaDone)
|
||||
{
|
||||
log_i("ota state active, ota Done ==> On");
|
||||
|
||||
powerstate = POWERSTATES::on;
|
||||
SetLedColor(CRGB::Green, true);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,14 @@
|
||||
#include "Battery.h"
|
||||
#include "sensor.h"
|
||||
#include "audio.h"
|
||||
#include "ota.h"
|
||||
#include "led.h"
|
||||
|
||||
|
||||
#define TIMEOUT_POWER (5 * 1000 * 60) //5minutes timeout
|
||||
#define POWERBUTTONDELAY 1000
|
||||
#define BATTERYMEASUREDELAY 60000
|
||||
#define POWERBUTTONOTADELAY 10000
|
||||
|
||||
typedef enum
|
||||
{
|
||||
@@ -22,7 +25,9 @@ typedef enum
|
||||
poweringOff,
|
||||
poweringOff2,
|
||||
timeOut,
|
||||
lowBatt
|
||||
lowBatt,
|
||||
overTheAir,
|
||||
overTheAir2
|
||||
} POWERSTATES;
|
||||
|
||||
POWERSTATES getPowerState( void );
|
||||
|
||||
Reference in New Issue
Block a user