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 SWITCH2 PA1 //A1
#define SWITCH22 PA3 //A2
#define SWITCH3 PB3 //D13
#define SWITCH32 PB4 //D12
#define SWITCH3 PB5 //D11
#define SWITCH32 PA0 //PB4 //D12
#define LD3LED PB3

View File

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