added multiple games [broken]

This commit is contained in:
willem oldemans
2020-11-08 22:06:41 +01:00
parent a16ccf479d
commit 0123e4cc9f
20 changed files with 3951 additions and 73 deletions

26
src/board.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef BOARDH
#define BOARDH
#define LED1 PB0 //D3
#define LED2 PB7 //D4
#define LED3 PB6 //D5
#define DETECT1 PA6 //A5
#define DETECT2 PA5 //A4
#define DETECT3 PA4 //A3
#define SWITCH1 PA7 //A6
#define SWITCH12 PA2 //A7
#define SWITCH2 PA1 //A1
#define SWITCH22 PA3 //A2
#define SWITCH3 PB3 //D13
#define SWITCH32 PB4 //D12
#define LD3LED PB3
#define REDLEDRES
#define YELLOWLEDRES
#define GREENLEDRES
#endif //BOARDH

112
src/buttons.cpp Normal file
View File

@@ -0,0 +1,112 @@
#include "buttons.h"
#include <vector>
#include "Arduino.h"
std::vector<buttons *> buttonlist;
buttons::buttons(uint32_t pin, unsigned long shortpress, unsigned long longpress, unsigned int index):
_buttonIndex(index), _buttonPin(pin)
{
_buttonDelayShort = shortpress;
_buttonDelayLong = longpress;
_buttonState = INVALID;
_lastState = INVALID;
buttonlist.push_back(this);
}
void buttons::begin()
{
pinMode(_buttonPin, INPUT_PULLUP);
}
buttonState_t buttons::state()
{
return _buttonState;
}
buttonState_t buttons::lastState(void)
{
return _lastState;
}
bool buttons::raw(void)
{
return _buttonFlag;
}
void buttons::update(void)
{
unsigned long currentMillis = millis();
_buttonFlag = !digitalRead(_buttonPin);
if (_buttonFlag)
{
if (_buttonState == RELEASED)
{
//button not detected yet, check timer
if ((currentMillis - _buttonTimer) >= _buttonDelayShort)
{
_buttonState = SHORT;
_lastState = SHORT;
}
}
else if (_buttonState == SHORT)
{
if ((currentMillis - _buttonTimer) >= _buttonDelayLong)
{
_buttonState = LONG;
_lastState = LONG;
}
}
}
else
{
//button is not pressed, keep updating the timer
_buttonState = RELEASED;
_buttonTimer = millis();
}
}
unsigned int buttons::index( void )
{
return _buttonIndex;
}
void initbuttons(void)
{
for (auto &&i : buttonlist)
{
i->begin();
}
}
void handleButtons(void)
{
for (auto &&i : buttonlist)
{
i->update();
}
}
bool anybutton(void)
{
handleButtons();
for (auto &&i : buttonlist)
{
if (i->raw())
{
return true;
}
}
return false;
}
buttons* getButton(unsigned int index)
{
for (auto &&i : buttonlist)
{
if( i->index() == index)
return i;
}
return NULL;
}

47
src/buttons.h Normal file
View File

@@ -0,0 +1,47 @@
#ifndef BUTTONSH
#define BUTTONSH
#include <Arduino.h>
#include <vector>
typedef enum buttonState_e
{
RELEASED,
SHORT,
LONG,
INVALID
}buttonState_t;
class buttons
{
const unsigned int _buttonIndex;
const uint32_t _buttonPin;
unsigned long _buttonTimer;
buttonState_t _buttonState;
buttonState_t _lastState;
bool _buttonFlag;
unsigned long _buttonDelayShort;
unsigned long _buttonDelayLong;
public:
buttons(uint32_t pin, unsigned long shortpress, unsigned long longpress, unsigned int index);
void begin();
void update( void );
bool raw( void );
buttonState_t state();
buttonState_t lastState();
unsigned int index( void );
};
bool anybutton( void );
void initbuttons( void );
void handleButtons( void );
buttons* getButton(unsigned int index);
#endif //BUTTONSH

87
src/chainGame.cpp Normal file
View File

@@ -0,0 +1,87 @@
#include "chainGame.h"
#include "Arduino.h"
#include "buttons.h"
extern buttons button1;
extern buttons button2;
extern buttons button3;
uint8_t patternIndex = 0;
bool patternFlag = false;
bool firstpattern = false;
uint8_t ledpattern[4][3] = {
{1,0,0},
{0,0,1},
{1,0,0},
{0,1,0},
};
int patternlength = sizeof(ledpattern)/sizeof(ledpattern[0]);
void nextPattern(void)
{
if (patternIndex < patternlength-1)
{
patternIndex++;
}
else
{
patternIndex = 0;
}
}
void HandleChainGame(void)
{
if (button1.state())
{
if (!patternFlag)
{
//button detected, increase pattern
if(!firstpattern)
{
firstpattern = true;
}
else
{
nextPattern();
}
patternFlag = true;
}
}
if (button2.state())
{
if (!patternFlag)
{
if(!firstpattern)
{
firstpattern = true;
}
else
{
//second input, skip a pattern
nextPattern();
nextPattern();
}
patternFlag = true;
}
}
if (button1.state() | button2.state())
{
//write pattern to the LEDs
digitalWrite(LED1, ledpattern[patternIndex][0]);
digitalWrite(LED2, ledpattern[patternIndex][1]);
digitalWrite(LED3, ledpattern[patternIndex][2]);
}
else
{
//leds off
digitalWrite(LED1, 0);
digitalWrite(LED2, 0);
digitalWrite(LED3, 0);
patternFlag = false;
}
}

8
src/chainGame.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef CHAINGAMEH
#define CHAINGAMEH
void HandleChainGame( void );
#endif //CHAINGAMEH

27
src/detectled.cpp Normal file
View File

@@ -0,0 +1,27 @@
#include "Arduino.h"
#include "detectled.h"
#include "buttons.h"
#include "board.h"
extern buttons button1;
extern buttons button2;
extern buttons button3;
uint32_t detectled[3] = {0,0,0};
void handleDetectLed( void )
{
detectled[0] = analogRead(DETECT1);
detectled[1] = analogRead(DETECT2);
detectled[2] = analogRead(DETECT3);
}
void initDetectLed( void )
{
pinMode(DETECT1, INPUT_ANALOG);
pinMode(DETECT2, INPUT_ANALOG);
pinMode(DETECT3, INPUT_ANALOG);
analogReadResolution(10);
}

8
src/detectled.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef DETECTLEDH
#define DETECTLEDH
void handleDetectLed( void );
void initDetectLed( void );
#endif //DETECTLED

122
src/magicSwitchBoard.cpp Normal file
View File

@@ -0,0 +1,122 @@
#include "magicSwitchBoard.h"
#include "Arduino.h"
#include "buttons.h"
#define CHANNELS 3
typedef enum
{
idle,
learn,
active,
last
} states;
states state = last;
uint8_t sequence[CHANNELS] = {0, 0, 0};
const uint8_t buttonIndex[CHANNELS] = {4, 5, 3};
const uint32_t leds[CHANNELS] = {LED1, LED2, LED3};
uint8_t learnIndex = 0;
void showLeds(void)
{
//loop through the button list
for (int i = 0; i < CHANNELS; i++)
{
//check if the position is already programmed
if (sequence[i])
{
//get the button pointer
buttons *currentbutton = getButton(buttonIndex[i]);
//verify that the button pointer is not NULL
if (currentbutton == NULL)
{
return;
}
//if the button is pressed, show LED or not
if (currentbutton->state() == !RELEASED)
{
//write sequence led on
digitalWrite(leds[sequence[i] + 1], 1);
}
else
{
//write sequence led off
digitalWrite(leds[sequence[i] + 1], 0);
}
}
}
}
void handleMagicSwitchBoard(void)
{
switch (state)
{
case idle:
{
if (anybutton())
{
state = learn;
}
else
{
for (auto &&i : leds)
{
digitalWrite(i, 0);
}
}
}
break;
case learn:
{
for (int i = 0; i < CHANNELS; i++)
{
buttons *currentbutton = getButton(buttonIndex[i]);
if (currentbutton == NULL)
{
return;
}
if (currentbutton->state() == !RELEASED)
{
bool duplicate = false;
for (auto &&n : sequence)
{
if (currentbutton->index() == n)
{
duplicate = true;
}
}
if (!duplicate)
{
sequence[learnIndex] = currentbutton->index();
learnIndex++;
}
}
}
if (learnIndex == CHANNELS)
{
state = active;
}
}
break;
case active:
{
}
break;
default:
{
state = idle;
}
break;
}
showLeds();
}

6
src/magicSwitchBoard.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef MAGICSWITCHBOARDH
#define MAGICSWITCHBOARDH
void handleMagicSwitchBoard( void );
#endif //MAGICSWITCHBOARDH

View File

@@ -1,91 +1,82 @@
#include <Arduino.h>
#include "buttons.h"
#include "board.h"
#include "chainGame.h"
#include "detectled.h"
#include "magicSwitchBoard.h"
#define LED1 PB14
#define LED2 PB15
#define LED3 PA8
#define BUTTON1 PA0
#define BUTTON2 PA1
buttons button1(SWITCH12, 100, 1000, 1);
buttons button2(SWITCH22, 100, 1000, 2);
buttons button3(SWITCH32, 100, 1000, 3);
buttons button4(SWITCH1, 100, 1000, 4);
buttons button5(SWITCH2, 100, 1000, 5);
buttons button6(SWITCH3, 100, 1000, 6);
uint8_t patternIndex = 0;
bool patternFlag = false;
uint8_t ledpattern[5][3] = {
{1,0,0},
{0,0,1},
{0,1,0},
{0,1,0},
{0,0,1}
};
int patternlength = sizeof(ledpattern)/sizeof(ledpattern[0]);
void nextPattern( void )
typedef enum
{
patternIndex++;
if(patternIndex > patternlength)
{
patternIndex = 0;
}
}
none,
ChainGame,
magicSwitchBoard,
detectLED,
last
} game;
buttons button1(BUTTON1, 100,1000);
buttons button2(BUTTON2, 100,1000);
game currentGame = none;
uint8_t gameState = 0;
unsigned long gameTimeout = 0;
void setup()
void setup()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
button1.begin();
button2.begin();
initbuttons();
initDetectLed();
while (anybutton())
{
digitalWrite(LED1, 1);
digitalWrite(LED2, 1);
digitalWrite(LED3, 1);
if (button4.raw())
{
currentGame = ChainGame;
}
if (button5.raw())
{
currentGame = magicSwitchBoard;
}
if (button6.raw())
{
currentGame = detectLED;
}
}
}
void loop()
void loop()
{
button1.update();
button2.update();
if(button1.state())
{
if(!patternFlag)
{
//button detected, increase pattern
nextPattern();
patternFlag = true;
}
}
if(button2.state())
{
if(!patternFlag)
{
//second input, skip a pattern
nextPattern();
nextPattern();
patternFlag = true;
}
}
handleButtons();
if(button1.state() | button2.state() )
switch (currentGame)
{
//write pattern to the LEDs
digitalWrite(LED1,!ledpattern[patternIndex][0]);
digitalWrite(LED2,!ledpattern[patternIndex][1]);
digitalWrite(LED3,!ledpattern[patternIndex][2]);
case magicSwitchBoard:
{
handleMagicSwitchBoard();
}
else
break;
case detectLED:
{
//leds off
digitalWrite(LED1,1);
digitalWrite(LED2,1);
digitalWrite(LED3,1);
patternFlag = false;
handleDetectLed();
}
break;
case ChainGame:
default:
{
HandleChainGame();
}
}
}