firmware release 1.2

fixed MSboard, game class prep, power impr.
This commit is contained in:
2021-03-29 11:10:57 +02:00
parent 095e650457
commit 04b1235f1a
17 changed files with 222 additions and 113 deletions

View File

@@ -8,23 +8,19 @@
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:LedBoardV10]
[env]
platform = ststm32
board = STM32L031K6
framework = arduino
upload_port = stlink
debug_tool = stlink
lib_ldf_mode = deep+
[env:LedBoardV10]
board = STM32L031K6
build_flags =
-DHARDWAREVERSION=10
[env:ledboard_PROTO]
platform = ststm32
board = nucleo_l031K6
framework = arduino
upload_port = stlink
debug_tool = stlink
boards_dir = boards
lib_ldf_mode = deep+
build_flags =
-DHARDWAREVERSION=09

View File

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

View File

@@ -48,6 +48,20 @@ bool anybutton(void)
return false;
}
bool anyButtonChanged(void)
{
handleButtons();
for (auto &&i : buttonlist)
{
if (i->isChanged())
{
return true;
}
}
return false;
}
bool buttonIsPressed(e_ledcolor index)
{
c_button *thisbutton = getButton(index);

View File

@@ -18,10 +18,12 @@ public:
e_ledcolor getColor( void ){return _color;}
uint8_t getIndex( void ) {return _index;}
bool isChanged( void ) {return changed();}
};
bool anybutton(void);
bool anyButtonChanged(void);
void initButtons(void);
void handleButtons(void);
c_button *getButton(unsigned int index);
@@ -34,4 +36,5 @@ bool buttonIsPressed(uint16_t index);
bool buttonWasPressed(e_ledcolor index);
#endif //BUTTONSH

View File

@@ -40,7 +40,7 @@ void ResetChainGame(void)
cheatButtonFlag = false;
}
void HandleChainGame(void)
void HandleChainGame(bool newstate)
{
if (!patternFlag && !cheatButtonFlag)
{

View File

@@ -4,7 +4,7 @@
#include "Arduino.h"
#include "buttons.h"
void HandleChainGame( void );
void HandleChainGame( bool newstate );
void ResetChainGame(void);

View File

@@ -11,7 +11,7 @@ extern std::vector<c_button *> buttonlist;
c_leds *ledlist_ptr;
void handleDetectLed(void)
void handleDetectLed(bool newstate)
{
for(auto &&button : buttonlist)

View File

@@ -6,8 +6,7 @@
#include "led.h"
#include "vector"
void handleDetectLed( void );
void initDetectLed( void );
void handleDetectLed(bool newstate);
void initDetectLed(void);
#endif //DETECTLED

54
src/game.cpp Normal file
View File

@@ -0,0 +1,54 @@
#include "game.h"
#include <vector>
#include "simpleled.h"
std::vector<c_game *> gameslist;
void initGames(void)
{
gameslist.clear();
gameslist.push_back(new c_simpleLed(simpleled));
}
void runGame(void)
{
for(auto &&game: gameslist)
{
if(game->getStatus() == active)
{
game->runGame();
return;
}
if(game->getStatus() == init)
{
game->initGame();
return;
}
}
}
void activateGame(e_game nextgame)
{
for(auto &&game: gameslist)
{
if(game->getIndex() == nextgame)
{
if(game->getStatus() != active)
{
game->setStatus(init);
}
}
else
{
game->setStatus(disabled);
}
}
}

View File

@@ -1,18 +1,35 @@
#ifndef GAMEH
#define GAMEH
typedef enum
{
simpleled,
chaingame,
magicswitchboard,
detectled
} e_game;
typedef enum
{
disabled,
init,
active
} e_state;
class c_game
{
protected:
bool _status;
private:
const e_game _gameindex;
e_state _status;
public:
c_game(void): _status(false) {};
c_game(e_game index) : _gameindex(index) {_status = disabled;}
void runGame(void);
void initGame(void);
bool getStatus(void) { return _status;}
virtual void runGame(void);
virtual void initGame(void);
e_state getStatus(void) { return _status; }
e_game getIndex(void) { return _gameindex; }
void setStatus(e_state newstate) { _status = newstate;}
};
#endif //GAMEH

View File

@@ -16,7 +16,7 @@ typedef enum
states state = last;
uint8_t sequence[CHANNELS] = {0xFF, 0xFF, 0xFF};
const uint8_t buttonIndex[CHANNELS] = {1, 2, 3};
const uint32_t leds[CHANNELS] = {LED1, LED2, LED3};
const uint16_t leds[CHANNELS] = {1, 2, 3};
uint64_t lastTime = 0;
@@ -41,12 +41,14 @@ void showLeds(void)
{
//check if the position is already programmed
//write sequence led on
digitalWrite(leds[i], 1);
turnOnLed(leds[i]);
//digitalWrite(leds[i], 1);
}
else
{
//write sequence led off
digitalWrite(leds[i], 0);
turnOffLed(leds[i]);
//digitalWrite(leds[i], 0);
}
}
}
@@ -59,8 +61,8 @@ void resetMagicSwitchBoard(void)
for (int i = 0; i < CHANNELS; i++)
{
sequence[i] = 0xff;
digitalWrite(leds[i], 0);
}
turnOffAllLed();
}
bool CheckTimeOut(void)
@@ -131,14 +133,11 @@ void handleIdle(void)
}
else
{
for (auto &&i : leds)
{
digitalWrite(i, 0);
}
turnOffAllLed();
}
}
void handleMagicSwitchBoard(void)
void handleMagicSwitchBoard(bool newstate)
{
switch (state)
{
@@ -171,4 +170,7 @@ void handleMagicSwitchBoard(void)
}
void initMagicSwitchBoard(void)
{
resetMagicSwitchBoard();
}

View File

@@ -3,6 +3,7 @@
#include "buttons.h"
void handleMagicSwitchBoard( void );
void handleMagicSwitchBoard( bool newstate );
void initMagicSwitchBoard(void);
#endif //MAGICSWITCHBOARDH

View File

@@ -10,9 +10,7 @@
#include "led.h"
#include "power.h"
#define TIMEOUT 900000 // 15min* 60 sec * 1000ms
#define GAMESELECTTIMEOUT 10000 // 7sec * 1000ms
#define GAMESELECTTIMEOUT 10000 // 10sec * 1000ms
typedef enum
{
@@ -28,12 +26,20 @@ typedef enum
game currentGame = none;
game nextGame = none;
bool newstate = false;
uint8_t gameState = 0;
uint64_t lasttimeOut = 0;
uint64_t GameSelectTimer = 0;
bool buttonChanged = false;
void HandleIdle(void)
void setNewState(game nextstate)
{
if (nextstate != currentGame)
{
currentGame = nextstate;
newstate = true;
}
}
void HandleIdle(bool newstate)
{
//green button first released
if (!buttonIsPressed(YELLOW) && buttonIsPressed(RED) && buttonIsPressed(GREEN) && (nextGame == none))
@@ -63,7 +69,7 @@ void HandleIdle(void)
//wait for all buttons to be switched off
if (!anybutton())
{
currentGame = nextGame;
setNewState(nextGame);
nextGame = none;
}
}
@@ -84,7 +90,7 @@ void HandleGameSelectTimeout(void)
//check timeout
if (currentmillis - GameSelectTimer > GAMESELECTTIMEOUT)
{
currentGame = idle;
setNewState(idle);
GameSelectTimer = 0;
}
}
@@ -96,109 +102,94 @@ void HandleGameSelectTimeout(void)
}
}
void HandleTimeOut(void)
{
uint64_t currentmillis = millis();
if (!lasttimeOut)
{
lasttimeOut = currentmillis;
buttonChanged = anybutton();
}
//check if lastTime is initialized or timeout expired
if ((currentmillis - lasttimeOut > TIMEOUT))
{
currentGame = sleep;
turnOffAllLed();
shutdown();
}
else
{
if (buttonChanged != anybutton())
{
buttonChanged = anybutton();
//game in progress, update timer
lasttimeOut = currentmillis;
}
}
}
void setup()
{
initLeds();
initButtons();
initDetectLed();
initSimpleLed();
initLowPower();
initBattery();
initDetectLed();
initMagicSwitchBoard();
initSimpleLed();
}
void loop()
{
handleButtons();
HandleTimeOut();
HandlePower();
HandleGameSelectTimeout();
switch (currentGame)
{
case idle:
{
HandleIdle();
HandleIdle(newstate);
newstate = false;
}
break;
case SimpleLed:
{
handleSimpleLed();
handleSimpleLed(newstate);
newstate = false;
}
break;
case magicSwitchBoard:
{
handleMagicSwitchBoard();
handleMagicSwitchBoard(newstate);
newstate = false;
}
break;
case detectLED:
{
handleDetectLed();
handleDetectLed(newstate);
newstate = false;
}
break;
case ChainGame:
default:
{
HandleChainGame();
HandleChainGame(newstate);
newstate = false;
}
break;
case none:
{
currentGame = SimpleLed;
batteryCheck();
batterydisplay();
delay(1000);
if (buttonIsPressed(GREEN))
turnOffAllLed();
if (buttonIsPressed(GREEN) && currentGame == none)
{
currentGame = ChainGame;
setNewState(ChainGame);
turnOnLed(GREEN);
}
if (buttonIsPressed(RED))
if (buttonIsPressed(RED) && currentGame == none)
{
currentGame = magicSwitchBoard;
setNewState(magicSwitchBoard);
turnOnLed(RED);
}
if (buttonIsPressed(YELLOW))
if (buttonIsPressed(YELLOW) && currentGame == none)
{
currentGame = SimpleLed;
setNewState(SimpleLed);
turnOnLed(YELLOW);
}
if (currentGame == none)
{
setNewState(SimpleLed);
}
//wait for all buttons idle
while (anybutton())
{}
{
}
turnOffAllLed();
}

View File

@@ -3,35 +3,25 @@
#include "rtc.h"
#include "low_Power.h"
#include "led.h"
#include "buttons.h"
#ifdef VBATTPIN
#include "Battery.h"
Battery battery(2500, 4160, VBATTPIN);
Battery battery(VBATTMIN, VBATTMAX, VBATTPIN);
#endif
void initBattery(void)
{
#ifdef VBATTPIN
battery.begin(3300, (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
#endif
}
uint16_t batteryGetVoltage( void )
{
#ifdef VBATTPIN
return battery.voltage();
#endif
return 4200;
}
void batterydisplay(void)
{
#ifdef VBATTPIN
uint16_t currentlevel = battery.level();
uint16_t currentvoltage = batteryGetVoltage();
if(currentvoltage)
{
if (currentlevel > 90)
{
turnOnLed(3);
@@ -44,14 +34,14 @@ void batterydisplay(void)
{
turnOnLed(1);
}
}
#endif
}
void batteryCheck(void)
{
#ifdef VBATTPIN
if (battery.level() < 10)
if (battery.voltage() < VBATTMIN)
{
for( int i = 0; i < 10;i++)
{
@@ -76,3 +66,39 @@ void shutdown(void)
{
LowPower_shutdown();
}
void HandlePower(void)
{
HandleTimeOut();
batteryCheck();
}
void HandleTimeOut(void)
{
uint64_t currentmillis = millis();
static uint64_t lasttimeOut = 0;
static bool buttonChanged = false;
if (!lasttimeOut)
{
lasttimeOut = currentmillis;
buttonChanged = anybutton();
}
//check if lastTime is initialized or timeout expired
if ((currentmillis - lasttimeOut > IDLESHUTDOWN))
{
turnOffAllLed();
shutdown();
}
else
{
if (buttonChanged != anybutton())
{
buttonChanged = anybutton();
//game in progress, update timer
lasttimeOut = currentmillis;
}
}
}

View File

@@ -1,10 +1,12 @@
#pragma once
//battery
void initBattery( void );
void batterydisplay( void );
void initBattery(void);
void batterydisplay(void);
void batteryCheck(void);
//low power
void initLowPower( void );
void shutdown( void );
void initLowPower(void);
void shutdown(void);
void HandleTimeOut(void);
void HandlePower(void);

View File

@@ -1,15 +1,16 @@
#include "simpleled.h"
void c_simpleLed::runGame(void)
{
handleSimpleLed();
handleSimpleLed(false);
}
void c_simpleLed::initGame(void)
{
initSimpleLed();
_status = true;
setStatus(active);
}
@@ -22,7 +23,7 @@ void initSimpleLed(void)
status = true;
}
void handleSimpleLed(void)
void handleSimpleLed(bool newstate)
{
for (auto &&button : buttonlist)
{

View File

@@ -8,14 +8,14 @@
class c_simpleLed : public c_game
{
public:
c_simpleLed(e_game game): c_game{game} {}
void runGame(void);
void initGame(void);
};
void initSimpleLed( void );
void handleSimpleLed( void );
bool getStatusSimpleLed( void );
void initSimpleLed(void);
void handleSimpleLed(bool newstate);
bool getStatusSimpleLed(void);
#endif //SIMPLELEDH