firmware recovery 1.3RC

This commit is contained in:
2021-06-14 08:19:45 +02:00
parent d32deb73b5
commit f4e23386ef
15 changed files with 237 additions and 203 deletions

View File

@@ -2,7 +2,7 @@
#define BOARDH #define BOARDH
#define IDLESHUTDOWN 900000 // 15min* 60 sec * 1000ms #define IDLESHUTDOWN 900000 // 15min* 60 sec * 1000ms
#define VBATTMIN 3000 #define VBATTMIN 3200
#define VBATTMAX 4180 #define VBATTMAX 4180
#define VBATTREF 3300 #define VBATTREF 3300

View File

@@ -63,11 +63,11 @@ bool anyButtonChanged(void)
bool allButtons(void) bool allButtons(void)
{ {
for( auto&& thisbutton : buttonlist) for (auto &&thisbutton : buttonlist)
{ {
if(thisbutton->getType() == type_switch) if (thisbutton->getType() == type_switch)
{ {
if(!thisbutton->isPressed()) if (!thisbutton->isPressed())
{ {
return false; return false;
} }
@@ -151,4 +151,20 @@ c_button *getButton(e_ledcolor color)
std::vector<c_button *> *getButtonlist(void) std::vector<c_button *> *getButtonlist(void)
{ {
return &buttonlist; return &buttonlist;
}
uint8_t buttonPressedCount(void)
{
uint8_t count = 0;
for (auto &&button : buttonlist)
{
if (!button == NULL)
{
if (button->isPressed())
{
count++;
}
}
}
return count;
} }

View File

@@ -44,6 +44,7 @@ std::vector<c_button *>* getButtonlist(void);
bool buttonIsPressed(e_ledcolor index); bool buttonIsPressed(e_ledcolor index);
bool buttonIsPressed(uint16_t index); bool buttonIsPressed(uint16_t index);
bool buttonWasPressed(e_ledcolor index); bool buttonWasPressed(e_ledcolor index);
uint8_t buttonPressedCount( void );

View File

@@ -2,21 +2,6 @@
#include "chainGame.h" #include "chainGame.h"
//uint8_t patternIndex = 0;
//bool patternFlag = false;
//bool firstpattern = false;
// uint16_t cheatbutton = 0;
//bool cheatButtonFlag = false;
// uint16_t ledpattern[4] =
// {
// 1,
// 3,
// 1,
// 2};
// int patternlength = sizeof(ledpattern) / sizeof(ledpattern[0]);
void c_chaingame::nextPattern(void) void c_chaingame::nextPattern(void)
{ {
if (patternIndex < patternlength - 1) if (patternIndex < patternlength - 1)
@@ -96,12 +81,13 @@ void c_chaingame::runGame(void)
cheatbutton = 3; cheatbutton = 3;
} }
} }
void c_chaingame::initGame(void) bool c_chaingame::initGame(void)
{ {
patternIndex = 0; patternIndex = 0;
patternFlag = false; patternFlag = false;
cheatbutton = 0; cheatbutton = 0;
cheatButtonFlag = false; cheatButtonFlag = false;
return true;
} }
void c_chaingame::resetGame(void) void c_chaingame::resetGame(void)
{ {

View File

@@ -18,9 +18,9 @@ private:
void nextPattern(void); void nextPattern(void);
public: public:
c_chaingame(): c_game{chaingame} {} c_chaingame(e_ledcolor gamecolor): c_game{chaingame, gamecolor} {}
void runGame(void); void runGame(void);
void initGame(void); bool initGame(void);
void resetGame(void); void resetGame(void);
}; };

View File

@@ -1,37 +1,45 @@
#include "detectled.h" #include "detectled.h"
void c_detectled::runGame(void)
#define CHANNELS 3
#define SAMPLES 20
extern std::vector<c_button *> buttonlist;
c_leds *ledlist_ptr;
void handleDetectLed(bool newstate)
{ {
//check if buttonlist is a valid pointer
for(auto &&button : buttonlist) if (buttonlist != NULL)
{ {
if(button->isPressed()) for (auto &&button : *buttonlist)
{ {
ledlist_ptr->turnOnLed(button->getColor()); if (button->isPressed())
} {
else ledlist_ptr->turnOnLed(button->getColor());
{ }
ledlist_ptr->turnOffLed(button->getColor()); else
{
ledlist_ptr->turnOffLed(button->getColor());
}
} }
} }
else
{
setStatus(disabled);
}
} }
void initDetectLed(void)
bool c_detectled::initGame(void)
{ {
ledlist_ptr = getledlist(); ledlist_ptr = getledlist();
#ifndef UNIT_TEST if(ledlist_ptr == NULL)
return false;
buttonlist = getButtonlist();
if(buttonlist == NULL)
return false;
analogReadResolution(10); analogReadResolution(10);
#endif
return true;
} }
void c_detectled::resetGame(void)
{
initGame();
}

View File

@@ -1,12 +1,19 @@
#ifndef DETECTLEDH #pragma once
#define DETECTLEDH
#include "game.h"
#include "buttons.h" #include "buttons.h"
#include "board.h"
#include "led.h" #include "led.h"
#include "vector" #include <vector>
void handleDetectLed(bool newstate); class c_detectled : public c_game
void initDetectLed(void); {
c_leds *ledlist_ptr;
std::vector<c_button *> *buttonlist;
#endif //DETECTLED public:
c_detectled(e_ledcolor gamecolor): c_game{detectled, gamecolor} {}
void runGame(void);
bool initGame(void);
void resetGame(void);
};

View File

@@ -4,17 +4,24 @@
#include "simpleled.h" #include "simpleled.h"
#include "magicSwitchBoard.h" #include "magicSwitchBoard.h"
#include "chainGame.h" #include "chainGame.h"
#include "detectled.h"
std::vector<c_game *> gameslist; std::vector<c_game *> gameslist;
uint64_t GameSelectTimer = 0; uint64_t GameSelectTimer = 0;
e_state currentState = state_init; e_state currentState = state_init;
c_game *game_simpleled = new c_simpleLed(YELLOW);
c_game *game_magicswitchboard = new c_magicSwitchBoard(RED);
c_game *game_chaingame = new c_chaingame(GREEN);
c_game *game_detectled = new c_detectled(NONE);
void initGames(void) void initGames(void)
{ {
gameslist.clear(); gameslist.clear();
gameslist.push_back(new c_simpleLed()); gameslist.push_back(game_simpleled);
gameslist.push_back(new c_magicSwitchBoard()); gameslist.push_back(game_magicswitchboard);
gameslist.push_back(new c_chaingame()); gameslist.push_back(game_chaingame);
gameslist.push_back(game_detectled);
activateGame(simpleled); activateGame(simpleled);
} }
@@ -31,6 +38,18 @@ c_game *getGame(e_game game)
return NULL; return NULL;
} }
c_game *getGame(e_ledcolor gamecolor)
{
for (auto &&thisgame : gameslist)
{
if (thisgame->getGameColor() == gamecolor)
{
return thisgame;
}
}
return NULL;
}
void runGames(void) void runGames(void)
{ {
for (auto &&game : gameslist) for (auto &&game : gameslist)
@@ -62,8 +81,14 @@ void runGame(e_game game)
} }
if (thisgame->getStatus() == init) if (thisgame->getStatus() == init)
{ {
thisgame->initGame(); if (thisgame->initGame())
thisgame->setStatus(active); {
thisgame->setStatus(active);
}
else
{
thisgame->setStatus(init_error);
}
return; return;
} }
} }
@@ -88,6 +113,14 @@ void activateGame(e_game nextgame)
} }
} }
void c_game::setStatus(e_gamestate newstate)
{
if (_status != init_error || newstate == init)
{
_status = newstate;
}
}
void disableAllGames(void) void disableAllGames(void)
{ {
for (auto &&thisgame : gameslist) for (auto &&thisgame : gameslist)
@@ -127,28 +160,16 @@ void HandleGameSelectTimeout(void)
void HandleGameIdle(void) void HandleGameIdle(void)
{ {
e_game nextGame = none; e_game nextGame = none;
//yellow button first released
if (onlyButton(YELLOW) && (nextGame == none))
{
//prepare for next game
nextGame = chaingame;
turnOffLed(YELLOW);
}
//red button first released for (auto &&thisgame : gameslist)
if (onlyButton(RED) && (nextGame == none))
{ {
//prepare for next game if (onlyButton(thisgame->getGameColor()) && (nextGame == none))
nextGame = magicswitchboard; {
turnOffLed(RED); //prepare for next game
} nextGame = thisgame->getIndex();
turnOffLed(thisgame->getGameColor());
//green button first released break;
if (onlyButton(GREEN) && (nextGame == none)) }
{
//prepare for next game
nextGame = simpleled;
turnOffLed(GREEN);
} }
//wait for all buttons to be switched off //wait for all buttons to be switched off
@@ -164,14 +185,14 @@ void setGameState(e_state newstate)
currentState = state_play; currentState = state_play;
} }
e_state getCurrentState( void ) e_state getCurrentState(void)
{ {
return currentState; return currentState;
} }
void handleGames( void ) void handleGames(void)
{ {
HandleGameSelectTimeout(); HandleGameSelectTimeout();
switch (getCurrentState()) switch (getCurrentState())
{ {
@@ -189,24 +210,19 @@ HandleGameSelectTimeout();
case state_init: case state_init:
{ {
delay(1000); //delay(1000);
turnOffAllLed(); turnOffAllLed();
activateGame(simpleled); activateGame(simpleled);
if (buttonIsPressed(GREEN)) for (auto &&thisgame : gameslist)
{ {
activateGame(chaingame); if (onlyButton(thisgame->getGameColor()))
turnOnLed(GREEN); {
} //prepare for next game
if (buttonIsPressed(RED)) activateGame(thisgame->getIndex());
{ turnOnLed(thisgame->getGameColor());
activateGame(magicswitchboard); break;
turnOnLed(RED); }
}
if (buttonIsPressed(YELLOW))
{
activateGame(simpleled);
turnOnLed(YELLOW);
} }
//wait for all buttons idle //wait for all buttons idle

View File

@@ -1,60 +1,59 @@
#ifndef GAMEH #ifndef GAMEH
#define GAMEH #define GAMEH
#include "led.h"
#define GAMESELECTTIMEOUT 10000 // 10sec * 1000ms #define GAMESELECTTIMEOUT 10000 // 10sec * 1000ms
enum e_game
typedef enum
{ {
none, none,
simpleled, simpleled,
chaingame, chaingame,
magicswitchboard magicswitchboard,
} e_game; detectled
};
typedef enum enum e_gamestate
{ {
disabled, disabled,
init, init,
active active,
} e_gamestate; init_error
};
enum e_state
typedef enum
{ {
state_init, state_init,
state_idle, state_idle,
state_play state_play
}e_state; };
class c_game class c_game
{ {
protected: protected:
const e_game _gameindex; const e_game _gameindex;
e_gamestate _status; e_gamestate _status;
const e_ledcolor _gamecolor;
public: public:
c_game(e_game index) : _gameindex(index) { _status = disabled; } c_game(e_game index, e_ledcolor gamecolor) : _gameindex(index), _gamecolor(gamecolor) { _status = disabled; }
virtual void runGame(void); virtual void runGame(void);
virtual void initGame(void); virtual bool initGame(void);
virtual void resetGame(void); virtual void resetGame(void);
e_gamestate getStatus(void) { return _status; } e_gamestate getStatus(void) { return _status; }
e_game getIndex(void) { return _gameindex; } e_game getIndex(void) { return _gameindex; }
void setStatus(e_gamestate newstate) { _status = newstate; } void setStatus(e_gamestate newstate);
e_ledcolor getGameColor(void) { return _gamecolor; }
}; };
c_game *getGame(e_game game); c_game *getGame(e_game game, e_ledcolor gamecolor);
void runGame(e_game game); void runGame(e_game game);
void runGames(void); void runGames(void);
void activateGame(e_game nextgame); void activateGame(e_game nextgame);
void initGames(void); void initGames(void);
//void HandleGameSelectTimeout(void);
//void HandleGameIdle( void );
//void setGameState(e_state newstate);
//e_state getCurrentState( void );
void handleGames(); void handleGames();
#endif //GAMEH #endif //GAMEH

View File

@@ -1,30 +1,6 @@
#include "magicSwitchBoard.h" #include "magicSwitchBoard.h"
#define CHANNELS 3 void c_magicSwitchBoard::showLeds(void)
#define TIMEOUT 7000 //game timeout
typedef enum
{
wait,
learn,
play
} states;
states state = wait;
uint8_t sequence[CHANNELS] = {0xFF, 0xFF, 0xFF};
const uint8_t buttonIndex[CHANNELS] = {1, 2, 3};
const uint16_t leds[CHANNELS] = {1, 2, 3};
uint64_t lastTime = 0;
uint8_t learnIndex = 0;
void showLeds(void)
{ {
//loop through the button list //loop through the button list
for (int i = 0; i < CHANNELS; i++) for (int i = 0; i < CHANNELS; i++)
@@ -44,30 +20,16 @@ void showLeds(void)
//check if the position is already programmed //check if the position is already programmed
//write sequence led on //write sequence led on
turnOnLed(leds[i]); turnOnLed(leds[i]);
//digitalWrite(leds[i], 1);
} }
else else
{ {
//write sequence led off //write sequence led off
turnOffLed(leds[i]); turnOffLed(leds[i]);
//digitalWrite(leds[i], 0);
} }
} }
} }
void resetMagicSwitchBoard(void) bool c_magicSwitchBoard::CheckTimeOut(void)
{
// state = idle;
// lastTime = 0;
// learnIndex = 0;
// for (int i = 0; i < CHANNELS; i++)
// {
// sequence[i] = 0xff;
// }
// turnOffAllLed();
}
bool CheckTimeOut(void)
{ {
uint64_t currentmillis = millis(); uint64_t currentmillis = millis();
if(!lastTime) if(!lastTime)
@@ -79,7 +41,7 @@ bool CheckTimeOut(void)
if ((currentmillis - lastTime > TIMEOUT)) if ((currentmillis - lastTime > TIMEOUT))
{ {
//handle timeout //handle timeout
resetMagicSwitchBoard(); initGame();
return true; return true;
} }
else else
@@ -93,7 +55,7 @@ bool CheckTimeOut(void)
return false; return false;
} }
void handleLearn(void) void c_magicSwitchBoard::handleLearn(void)
{ {
for (int i = 0; i < CHANNELS; i++) for (int i = 0; i < CHANNELS; i++)
{ {
@@ -127,7 +89,7 @@ void handleLearn(void)
} }
} }
void handleIdle(void) void c_magicSwitchBoard::handleIdle(void)
{ {
if (anybutton()) if (anybutton())
{ {
@@ -171,7 +133,7 @@ void c_magicSwitchBoard::runGame(void)
showLeds(); showLeds();
} }
void c_magicSwitchBoard::initGame(void) bool c_magicSwitchBoard::initGame(void)
{ {
state = wait; state = wait;
lastTime = 0; lastTime = 0;
@@ -181,6 +143,7 @@ void c_magicSwitchBoard::initGame(void)
sequence[i] = 0xff; sequence[i] = 0xff;
} }
turnOffAllLed(); turnOffAllLed();
return true;
} }
void c_magicSwitchBoard::resetGame(void) void c_magicSwitchBoard::resetGame(void)

View File

@@ -1,20 +1,34 @@
#ifndef MAGICSWITCHBOARDH #pragma once
#define MAGICSWITCHBOARDH
#include "buttons.h" #include "buttons.h"
#include "game.h" #include "game.h"
#define CHANNELS 3
#define TIMEOUT 7000 //game timeout
class c_magicSwitchBoard : public c_game class c_magicSwitchBoard : public c_game
{ {
enum states
{
wait,
learn,
play
};
states state = wait;
uint8_t sequence[CHANNELS] = {0xFF, 0xFF, 0xFF};
const uint8_t buttonIndex[CHANNELS] = {1, 2, 3};
const uint16_t leds[CHANNELS] = {1, 2, 3};
uint64_t lastTime = 0;
uint8_t learnIndex = 0;
void showLeds(void);
bool CheckTimeOut(void);
void handleLearn(void);
void handleIdle(void);
public: public:
c_magicSwitchBoard(): c_game{magicswitchboard} {} c_magicSwitchBoard(e_ledcolor gamecolor) : c_game{magicswitchboard, gamecolor} {}
void runGame(void); void runGame(void);
void initGame(void); bool initGame(void);
void resetGame(void); void resetGame(void);
}; };
void handleMagicSwitchBoard( bool newstate );
void initMagicSwitchBoard(void);
#endif //MAGICSWITCHBOARDH

View File

@@ -9,7 +9,7 @@ Button buttonPower(BTN_PWR, 250UL, 1U, 0);
Battery battery(VBATTMIN, VBATTMAX, VBATTPIN); Battery battery(VBATTMIN, VBATTMAX, VBATTPIN);
#endif #endif
uint64_t delay_timer = 0; uint64_t measure_timer = 0;
uint64_t powerOnOffDelay = 0; uint64_t powerOnOffDelay = 0;
powerStates powerstate = off; powerStates powerstate = off;
@@ -18,8 +18,6 @@ void initBattery(void)
#ifdef VBATTPIN #ifdef VBATTPIN
#ifdef MEAS_EN #ifdef MEAS_EN
battery.onDemand(MEAS_EN, LOW); battery.onDemand(MEAS_EN, LOW);
//pinMode(MEAS_EN, OUTPUT);
//digitalWrite(MEAS_EN, HIGH);
battery.begin(VBATTREF, (R12 + R13) / R13); //R1 = 220K, R2 = 100K, factor = (R1+R2)/R2 battery.begin(VBATTREF, (R12 + R13) / R13); //R1 = 220K, R2 = 100K, factor = (R1+R2)/R2
#else #else
@@ -59,17 +57,24 @@ void powerOff(void)
digitalWrite(PW_HOLD, LOW); digitalWrite(PW_HOLD, LOW);
} }
bool measureBattery(void)
{
uint16_t vbatt = battery.voltage();
if (vbatt < VBATTMIN)
{
return true;
}
return false;
}
bool handleBattery(void) bool handleBattery(void)
{ {
#ifdef VBATTPIN #ifdef VBATTPIN
uint64_t currentmillis = millis(); uint64_t currentmillis = millis();
if (currentmillis - delay_timer > BATTERYMEASUREDELAY) if (currentmillis - measure_timer > BATTERYMEASUREDELAY)
{ {
uint32_t vbatt = battery.voltage(); return measureBattery();
if (vbatt < VBATTMIN) measure_timer = currentmillis;
{
return true;
}
} }
#endif #endif
@@ -126,11 +131,11 @@ void handlePowerState(void)
turnOnLed(1); turnOnLed(1);
powerstate = poweringOn2; powerstate = poweringOn2;
} }
else if (buttonPower.pressedFor(800)) else if (buttonPower.pressedFor(500))
{ {
turnOnLed(2); turnOnLed(2);
} }
else if (buttonPower.pressedFor(100)) else if (buttonPower.pressedFor(200))
{ {
turnOnLed(3); turnOnLed(3);
} }
@@ -148,10 +153,14 @@ void handlePowerState(void)
powerstate = on; powerstate = on;
powerOn(); powerOn();
turnOffAllLed(); turnOffAllLed();
delay(500); delay(200);
batterydisplay(); batterydisplay();
delay(1000); delay(500);
turnOffAllLed(); turnOffAllLed();
if(measureBattery())
{
powerstate = lowBatt;
}
} }
} }
break; break;
@@ -161,14 +170,17 @@ void handlePowerState(void)
{ {
powerstate = poweringOff; powerstate = poweringOff;
turnOnAllLed(); turnOnAllLed();
break;
} }
else if (HandleTimeOut()) if (HandleTimeOut())
{ {
powerstate = timeOut; powerstate = timeOut;
break;
} }
else if (handleBattery()) if (handleBattery())
{ {
powerstate = lowBatt; powerstate = lowBatt;
break;
} }
} }
break; break;
@@ -179,11 +191,11 @@ void handlePowerState(void)
turnOffLed(1); turnOffLed(1);
powerstate = poweringOff2; powerstate = poweringOff2;
} }
else if (buttonPower.pressedFor(950)) else if (buttonPower.pressedFor(500))
{ {
turnOffLed(2); turnOffLed(2);
} }
else if (buttonPower.pressedFor(450)) else if (buttonPower.pressedFor(200))
{ {
turnOffLed(3); turnOffLed(3);
} }
@@ -196,7 +208,7 @@ void handlePowerState(void)
break; break;
case poweringOff2: case poweringOff2:
{ {
if(!buttonread) if (!buttonread)
{ {
powerstate = off; powerstate = off;
} }
@@ -224,13 +236,15 @@ void handlePowerState(void)
} }
} }
uint64_t lasttimeOut = 0;
bool buttonChanged = false;
uint8_t buttonCount = 0;
//handleTimeout //handleTimeout
//return true when timed out //return true when timed out
bool HandleTimeOut(void) bool HandleTimeOut(void)
{ {
uint64_t currentmillis = millis(); uint64_t currentmillis = millis();
static uint64_t lasttimeOut = 0;
static bool buttonChanged = false;
if (!lasttimeOut) if (!lasttimeOut)
{ {
@@ -245,12 +259,18 @@ bool HandleTimeOut(void)
} }
else else
{ {
if (buttonChanged != anybutton()) if (buttonPressedCount() != buttonCount)
{ {
buttonChanged = anybutton(); buttonCount = buttonPressedCount();
//game in progress, update timer
lasttimeOut = currentmillis; lasttimeOut = currentmillis;
} }
// if (buttonChanged != anybutton())
// {
// buttonChanged = anybutton();
// //game in progress, update timer
// lasttimeOut = currentmillis;
// }
} }
return false; return false;
} }

View File

@@ -13,8 +13,8 @@
#include "Battery.h" #include "Battery.h"
#endif #endif
#define POWERBUTTONDELAY 1500 #define POWERBUTTONDELAY 1000
#define BATTERYMEASUREDELAY 5000 #define BATTERYMEASUREDELAY 60000
typedef enum typedef enum
{ {

View File

@@ -1,11 +1,11 @@
#include "simpleled.h" #include "simpleled.h"
extern std::vector<c_button *> buttonlist;
void c_simpleLed::runGame(void) void c_simpleLed::runGame(void)
{ {
for (auto &&button : buttonlist) for (auto &&button : *buttonlist)
{ {
if (button->isPressed()) if (button->isPressed())
{ {
@@ -18,8 +18,14 @@ void c_simpleLed::runGame(void)
} }
} }
void c_simpleLed::initGame(void) bool c_simpleLed::initGame(void)
{ {
buttonlist = getButtonlist();
if(buttonlist == NULL)
{
return false;
}
return true;
//no init required //no init required
} }

View File

@@ -1,18 +1,16 @@
#ifndef SIMPLELEDH #pragma once
#define SIMPLELEDH
#include "led.h" #include "led.h"
#include "buttons.h" #include "buttons.h"
#include "vector" #include "vector"
#include "game.h" #include "game.h"
class c_simpleLed : public c_game class c_simpleLed : public c_game
{ {
std::vector<c_button *> *buttonlist;
public: public:
c_simpleLed(): c_game{simpleled} {} c_simpleLed(e_ledcolor gamecolor): c_game{simpleled, gamecolor} {}
void runGame(void); void runGame(void);
void initGame(void); bool initGame(void);
void resetGame(void); void resetGame(void);
}; };
#endif //SIMPLELEDH