updated magicswitchboard

This commit is contained in:
willem oldemans
2020-11-15 15:34:17 +01:00
parent 0123e4cc9f
commit 8c9176ccbe
2 changed files with 112 additions and 62 deletions

View File

@@ -13,8 +13,8 @@
#define SWITCH12 PA2 //A7 #define SWITCH12 PA2 //A7
#define SWITCH2 PA1 //A1 #define SWITCH2 PA1 //A1
#define SWITCH22 PA3 //A2 #define SWITCH22 PA3 //A2
#define SWITCH3 PB3 //D13 #define SWITCH3 PB5 //D11
#define SWITCH32 PB4 //D12 #define SWITCH32 PA0 //PB4 //D12
#define LD3LED PB3 #define LD3LED PB3

View File

@@ -3,6 +3,7 @@
#include "buttons.h" #include "buttons.h"
#define CHANNELS 3 #define CHANNELS 3
#define TIMEOUT 7000 //game timeout
typedef enum typedef enum
{ {
@@ -13,10 +14,12 @@ typedef enum
} states; } states;
states state = last; states state = last;
uint8_t sequence[CHANNELS] = {0, 0, 0}; uint8_t sequence[CHANNELS] = {0xFF, 0xFF, 0xFF};
const uint8_t buttonIndex[CHANNELS] = {4, 5, 3}; const uint8_t buttonIndex[CHANNELS] = {4, 5, 6};
const uint32_t leds[CHANNELS] = {LED1, LED2, LED3}; const uint32_t leds[CHANNELS] = {LED1, LED2, LED3};
uint64_t lastTime = 0;
uint8_t learnIndex = 0; uint8_t learnIndex = 0;
void showLeds(void) void showLeds(void)
@@ -24,91 +27,136 @@ 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++)
{ {
//check if the position is already programmed //get the button pointer
if (sequence[i]) buttons *currentbutton = getButton(buttonIndex[sequence[i]]);
//verify that the button pointer is not NULL
if (currentbutton == NULL)
{ {
//get the button pointer break;
buttons *currentbutton = getButton(buttonIndex[i]); }
//verify that the button pointer is not NULL //if the button is pressed, show LED or not
if (currentbutton == NULL) if (currentbutton->raw() == true)
{
//check if the position is already programmed
//write sequence led on
digitalWrite(leds[i], 1);
}
else
{
//write sequence led off
digitalWrite(leds[i], 0);
}
}
}
void resetMagicSwitchBoard(void)
{
state = idle;
lastTime = 0;
learnIndex = 0;
for (int i = 0; i < CHANNELS; i++)
{
sequence[i] = 0xff;
digitalWrite(leds[i], 0);
}
}
bool CheckTimeOut(void)
{
uint64_t currentmillis = millis();
if(!lastTime)
{
lastTime = currentmillis;
}
//check if lastTime is initialized or timeout expired
if ((currentmillis - lastTime > TIMEOUT))
{
//handle timeout
resetMagicSwitchBoard();
return true;
}
else
{
if (anybutton())
{
//game in progress, update timer
lastTime = currentmillis;
}
}
return false;
}
void handleLearn(void)
{
for (int i = 0; i < CHANNELS; i++)
{
buttons *currentbutton = getButton(buttonIndex[i]);
if (currentbutton == NULL)
{
return;
}
if (currentbutton->state() == !RELEASED)
{
bool duplicate = false;
for (int n = 0; n < CHANNELS; n++)
{ {
return; if (currentbutton->index() == buttonIndex[sequence[n]])
{
duplicate = true;
}
} }
//if the button is pressed, show LED or not if (!duplicate)
if (currentbutton->state() == !RELEASED)
{ {
//write sequence led on sequence[learnIndex] = i;
digitalWrite(leds[sequence[i] + 1], 1); learnIndex++;
}
else
{
//write sequence led off
digitalWrite(leds[sequence[i] + 1], 0);
} }
} }
} }
if (learnIndex == CHANNELS)
{
state = active;
}
}
void handleIdle(void)
{
if (anybutton())
{
state = learn;
}
else
{
for (auto &&i : leds)
{
digitalWrite(i, 0);
}
}
} }
void handleMagicSwitchBoard(void) void handleMagicSwitchBoard(void)
{ {
switch (state) switch (state)
{ {
case idle: case idle:
{ {
if (anybutton()) handleIdle();
{
state = learn;
}
else
{
for (auto &&i : leds)
{
digitalWrite(i, 0);
}
}
} }
break; break;
case learn: case learn:
{ {
for (int i = 0; i < CHANNELS; i++) handleLearn();
{
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; break;
case active: case active:
{ {
CheckTimeOut();
} }
break; break;
@@ -119,4 +167,6 @@ void handleMagicSwitchBoard(void)
break; break;
} }
showLeds(); showLeds();
} }