From d184f1785cf94629ff76173cb3d1306337305dd8 Mon Sep 17 00:00:00 2001 From: willem oldemans Date: Thu, 19 Nov 2020 08:27:52 +0100 Subject: [PATCH] Changed to JC_button lib --- platformio.ini | 2 + src/board.h | 2 +- src/buttons.cpp | 105 +++++++++++---------------------------- src/buttons.h | 40 ++------------- src/chainGame.cpp | 16 +++--- src/detectled.cpp | 52 +++++++++++++++---- src/magicSwitchBoard.cpp | 11 ++-- src/main.cpp | 74 ++++++++++++++++----------- 8 files changed, 135 insertions(+), 167 deletions(-) diff --git a/platformio.ini b/platformio.ini index f487923..5dcec65 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,3 +14,5 @@ board = nucleo_l031K6 framework = arduino upload_port = stlink debug_tool = stlink +lib_deps = http://192.168.2.3/Bonobo.Git.Server/JCButton.git + diff --git a/src/board.h b/src/board.h index 41d7401..d49eb28 100644 --- a/src/board.h +++ b/src/board.h @@ -14,7 +14,7 @@ #define SWITCH2 PA1 //A1 #define SWITCH22 PA3 //A2 #define SWITCH3 PB5 //D11 -#define SWITCH32 PA0 //PB4 //D12 +#define SWITCH32 PB4 //D12 #define LD3LED PB3 diff --git a/src/buttons.cpp b/src/buttons.cpp index 0bf7a78..5195232 100644 --- a/src/buttons.cpp +++ b/src/buttons.cpp @@ -1,90 +1,42 @@ #include "buttons.h" +#include "board.h" #include #include "Arduino.h" +#include "JC_Button.h" -std::vector buttonlist; -buttons::buttons(uint32_t pin, unsigned long shortpress, unsigned long longpress, unsigned int index): -_buttonIndex(index), _buttonPin(pin) +std::vector buttonlist; + +ToggleButton button1(SWITCH12); +ToggleButton button2(SWITCH22); +ToggleButton button3(SWITCH32); +ToggleButton button4(SWITCH1); +ToggleButton button5(SWITCH2); +ToggleButton button6(SWITCH3); + + +void buttonbegin( ToggleButton *thisbutton ) { - _buttonDelayShort = shortpress; - _buttonDelayLong = longpress; - _buttonState = INVALID; - _lastState = INVALID; - buttonlist.push_back(this); + thisbutton->begin(); + buttonlist.push_back(thisbutton); } -void buttons::begin() +void initButtons( void ) { - pinMode(_buttonPin, INPUT_PULLUP); + buttonbegin(&button1); + buttonbegin(&button2); + buttonbegin(&button3); + buttonbegin(&button4); + buttonbegin(&button5); + buttonbegin(&button6); } -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(); + i->read(); } } @@ -93,7 +45,7 @@ bool anybutton(void) handleButtons(); for (auto &&i : buttonlist) { - if (i->raw()) + if (i->isPressed()) { return true; } @@ -101,12 +53,11 @@ bool anybutton(void) return false; } -buttons* getButton(unsigned int index) +ToggleButton* getButton(unsigned int index) { - for (auto &&i : buttonlist) + if(index > buttonlist.size()) { - if( i->index() == index) - return i; + return NULL; } - return NULL; + return buttonlist[index-1]; } diff --git a/src/buttons.h b/src/buttons.h index d79a3c2..07c581a 100644 --- a/src/buttons.h +++ b/src/buttons.h @@ -3,45 +3,11 @@ #include #include - - -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 ); -}; +#include "JC_Button.h" bool anybutton( void ); -void initbuttons( void ); +void initButtons( void ); void handleButtons( void ); -buttons* getButton(unsigned int index); +ToggleButton* getButton(unsigned int index); #endif //BUTTONSH \ No newline at end of file diff --git a/src/chainGame.cpp b/src/chainGame.cpp index 325fb09..c8cdb65 100644 --- a/src/chainGame.cpp +++ b/src/chainGame.cpp @@ -1,10 +1,10 @@ #include "chainGame.h" #include "Arduino.h" -#include "buttons.h" +#include "JC_Button.h" -extern buttons button1; -extern buttons button2; -extern buttons button3; +extern ToggleButton button4; +extern ToggleButton button5; +extern ToggleButton button6; uint8_t patternIndex = 0; @@ -31,9 +31,9 @@ void nextPattern(void) } } -void HandleChainGame(void) +void HandleChainGame( void ) { - if (button1.state()) + if ((button4.isPressed()) | (button6.isPressed())) { if (!patternFlag) { @@ -51,7 +51,7 @@ void HandleChainGame(void) } } - if (button2.state()) + if (button5.isPressed()) { if (!patternFlag) { @@ -69,7 +69,7 @@ void HandleChainGame(void) } } - if (button1.state() | button2.state()) + if ((button4.isPressed()) | (button5.isPressed()) | (button6.isPressed())) { //write pattern to the LEDs digitalWrite(LED1, ledpattern[patternIndex][0]); diff --git a/src/detectled.cpp b/src/detectled.cpp index ae1f138..e866626 100644 --- a/src/detectled.cpp +++ b/src/detectled.cpp @@ -3,25 +3,55 @@ #include "buttons.h" #include "board.h" -extern buttons button1; -extern buttons button2; -extern buttons button3; +#define CHANNELS 3 +#define SAMPLES 20 + + +unsigned int detectled[CHANNELS] = {0,0,0}; +uint32_t detectledRaw[CHANNELS][SAMPLES]; +const uint32_t inputs[CHANNELS] = {DETECT1, DETECT2, DETECT3}; +const uint32_t leds[CHANNELS] = {LED1, LED2, LED3}; +uint32_t sampleIndex = 0; -uint32_t detectled[3] = {0,0,0}; void handleDetectLed( void ) { - detectled[0] = analogRead(DETECT1); - detectled[1] = analogRead(DETECT2); - detectled[2] = analogRead(DETECT3); + for(int i = 0;i < CHANNELS;i++) + { + detectledRaw[i][sampleIndex] = analogRead(inputs[i]); + } + sampleIndex++; + + if(sampleIndex == SAMPLES) + { + for(int i = 0;iraw() == true) + if (currentbutton->isPressed() == true) { //check if the position is already programmed //write sequence led on @@ -93,18 +94,18 @@ void handleLearn(void) { for (int i = 0; i < CHANNELS; i++) { - buttons *currentbutton = getButton(buttonIndex[i]); + ToggleButton *currentbutton = getButton(buttonIndex[i]); if (currentbutton == NULL) { return; } - if (currentbutton->state() == !RELEASED) + if (currentbutton->isPressed()) { bool duplicate = false; for (int n = 0; n < CHANNELS; n++) { - if (currentbutton->index() == buttonIndex[sequence[n]]) + if (i == sequence[n]) { duplicate = true; } diff --git a/src/main.cpp b/src/main.cpp index 880407c..b571ebf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,20 +1,24 @@ #include -#include "buttons.h" +//#include "buttons.h" #include "board.h" #include "chainGame.h" #include "detectled.h" #include "magicSwitchBoard.h" +#include "buttons.h" +#include "JC_Button.h" +#include "vector" -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); +// extern ToggleButton button1; +// extern ToggleButton button2; +// extern ToggleButton button3; +extern ToggleButton button4; +extern ToggleButton button5; +extern ToggleButton button6; typedef enum { none, + powerOff, ChainGame, magicSwitchBoard, detectLED, @@ -31,28 +35,8 @@ void setup() pinMode(LED2, OUTPUT); pinMode(LED3, OUTPUT); - initbuttons(); - + 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() @@ -78,5 +62,39 @@ void loop() { HandleChainGame(); } + break; + + case none: + { + + digitalWrite(LED1, 0); + digitalWrite(LED2, 0); + digitalWrite(LED3, 0); + if (button4.isPressed()) + { + digitalWrite(LED1, 1); + } + if (button5.isPressed()) + { + currentGame = magicSwitchBoard; + digitalWrite(LED2, 1); + } + else + { + currentGame = ChainGame; + } + + if (button6.isPressed()) + { + digitalWrite(LED3, 1); + } + + while(anybutton()); + // if (button6.isPressed()) + // { + // currentGame = detectLED; + // } + } + break; } } \ No newline at end of file