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