diff --git a/.vscode/settings.json b/.vscode/settings.json index 2972200..6ff6680 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -44,5 +44,6 @@ "streambuf": "cpp", "cinttypes": "cpp", "typeinfo": "cpp" - } + }, + "cmake.configureOnOpen": true } \ No newline at end of file diff --git a/img/WhatsApp Image 2021-01-13 at 19.46.24.jpeg b/img/WhatsApp Image 2021-01-13 at 19.46.24.jpeg new file mode 100644 index 0000000..f4f610f Binary files /dev/null and b/img/WhatsApp Image 2021-01-13 at 19.46.24.jpeg differ diff --git a/lib/JC_Button/src/JC_Button.cpp b/lib/JC_Button/src/JC_Button.cpp new file mode 100644 index 0000000..aaa50de --- /dev/null +++ b/lib/JC_Button/src/JC_Button.cpp @@ -0,0 +1,100 @@ +// Arduino Button Library +// https://github.com/JChristensen/JC_Button +// Copyright (C) 2018 by Jack Christensen and licensed under +// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html + +#include "JC_Button.h" + +/*----------------------------------------------------------------------* +/ initialize a Button object and the pin it's connected to. * +/-----------------------------------------------------------------------*/ +void Button::begin() +{ + pinMode(m_pin, m_puEnable ? INPUT_PULLUP : INPUT); + m_state = digitalRead(m_pin); + if (m_invert) m_state = !m_state; + m_time = millis(); + m_lastState = m_state; + m_changed = false; + m_lastChange = m_time; +} + +/*----------------------------------------------------------------------* +/ returns the state of the button, true if pressed, false if released. * +/ does debouncing, captures and maintains times, previous state, etc. * +/-----------------------------------------------------------------------*/ +bool Button::read() +{ + uint32_t ms = millis(); + bool pinVal = digitalRead(m_pin); + if (m_invert) pinVal = !pinVal; + if (ms - m_lastChange < m_dbTime) + { + m_changed = false; + } + else + { + m_lastState = m_state; + m_state = pinVal; + m_changed = (m_state != m_lastState); + if (m_changed) m_lastChange = ms; + } + m_time = ms; + return m_state; +} + +/*----------------------------------------------------------------------* + * isPressed() and isReleased() check the button state when it was last * + * read, and return false (0) or true (!=0) accordingly. * + * These functions do not cause the button to be read. * + *----------------------------------------------------------------------*/ +bool Button::isPressed() +{ + return m_state; +} + +bool Button::isReleased() +{ + return !m_state; +} + +/*----------------------------------------------------------------------* + * wasPressed() and wasReleased() check the button state to see if it * + * changed between the last two reads and return false (0) or * + * true (!=0) accordingly. * + * These functions do not cause the button to be read. * + *----------------------------------------------------------------------*/ +bool Button::wasPressed() +{ + return m_state && m_changed; +} + +bool Button::wasReleased() +{ + return !m_state && m_changed; +} + +/*----------------------------------------------------------------------* + * pressedFor(ms) and releasedFor(ms) check to see if the button is * + * pressed (or released), and has been in that state for the specified * + * time in milliseconds. Returns false (0) or true (!=0) accordingly. * + * These functions do not cause the button to be read. * + *----------------------------------------------------------------------*/ +bool Button::pressedFor(uint32_t ms) +{ + return m_state && m_time - m_lastChange >= ms; +} + +bool Button::releasedFor(uint32_t ms) +{ + return !m_state && m_time - m_lastChange >= ms; +} + +/*----------------------------------------------------------------------* + * lastChange() returns the time the button last changed state, * + * in milliseconds. * + *----------------------------------------------------------------------*/ +uint32_t Button::lastChange() +{ + return m_lastChange; +} diff --git a/lib/JC_Button/src/JC_Button.h b/lib/JC_Button/src/JC_Button.h new file mode 100644 index 0000000..7170294 --- /dev/null +++ b/lib/JC_Button/src/JC_Button.h @@ -0,0 +1,111 @@ +// Arduino Button Library +// https://github.com/JChristensen/JC_Button +// Copyright (C) 2018 by Jack Christensen and licensed under +// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html + +#ifndef JC_BUTTON_H_INCLUDED +#define JC_BUTTON_H_INCLUDED + +#include + +class Button +{ + public: + // Button(pin, dbTime, puEnable, invert) instantiates a button object. + // + // Required parameter: + // pin The Arduino pin the button is connected to + // + // Optional parameters: + // dbTime Debounce time in milliseconds (default 25ms) + // puEnable true to enable the AVR internal pullup resistor (default true) + // invert true to interpret a low logic level as pressed (default true) + Button(uint8_t pin, uint32_t dbTime=25, uint8_t puEnable=true, uint8_t invert=true) + : m_pin(pin), m_dbTime(dbTime), m_puEnable(puEnable), m_invert(invert) {} + + // Initialize a Button object and the pin it's connected to + void begin(); + + // Returns the current debounced button state, true for pressed, + // false for released. Call this function frequently to ensure + // the sketch is responsive to user input. + bool read(); + + // Returns true if the button state was pressed at the last call to read(). + // Does not cause the button to be read. + bool isPressed(); + + // Returns true if the button state was released at the last call to read(). + // Does not cause the button to be read. + bool isReleased(); + + // Returns true if the button state at the last call to read() was pressed, + // and this was a change since the previous read. + bool wasPressed(); + + // Returns true if the button state at the last call to read() was released, + // and this was a change since the previous read. + bool wasReleased(); + + // Returns true if the button state at the last call to read() was pressed, + // and has been in that state for at least the given number of milliseconds. + bool pressedFor(uint32_t ms); + + // Returns true if the button state at the last call to read() was released, + // and has been in that state for at least the given number of milliseconds. + bool releasedFor(uint32_t ms); + + // Returns the time in milliseconds (from millis) that the button last + // changed state. + uint32_t lastChange(); + + private: + uint8_t m_pin; // arduino pin number connected to button + uint32_t m_dbTime; // debounce time (ms) + bool m_puEnable; // internal pullup resistor enabled + bool m_invert; // if true, interpret logic low as pressed, else interpret logic high as pressed + bool m_state; // current button state, true=pressed + bool m_lastState; // previous button state + bool m_changed; // state changed since last read + uint32_t m_time; // time of current state (ms from millis) + uint32_t m_lastChange; // time of last state change (ms) +}; + +// a derived class for a "push-on, push-off" (toggle) type button. +// initial state can be given, default is off (false). +class ToggleButton : public Button +{ + public: + + // constructor is similar to Button, but includes the initial state for the toggle. + ToggleButton(uint8_t pin, bool initialState=false, uint32_t dbTime=25, uint8_t puEnable=true, uint8_t invert=true) + : Button(pin, dbTime, puEnable, invert), m_toggleState(initialState) {} + + // read the button and return its state. + // should be called frequently. + bool read() + { + Button::read(); + if (wasPressed()) + { + m_toggleState = !m_toggleState; + m_changed = true; + } + else + { + m_changed = false; + } + return m_toggleState; + } + + // has the state changed? + bool changed() {return m_changed;} + + // return the current state + bool toggleState() {return m_toggleState;} + + private: + bool m_toggleState; + bool m_changed; +}; +#endif diff --git a/platformio.ini b/platformio.ini index aa20788..f513dd3 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,5 +15,11 @@ framework = arduino upload_port = stlink debug_tool = stlink lib_deps = - http://192.168.2.3/Bonobo.Git.Server/JCButton.git +# http://192.168.2.3/Bonobo.Git.Server/JCButton.git + +[env:native] +platform = native +build_flags = -std=gnu++11 +lib_deps = + ArduinoFake diff --git a/src/board.h b/src/board.h index 54272d9..12d6e57 100644 --- a/src/board.h +++ b/src/board.h @@ -1,27 +1,55 @@ #ifndef BOARDH #define BOARDH -#define LED1 PB0 //D3 -#define LED2 PB7 //D4 -#define LED3 PB6 //D5 +#ifndef UNIT_TEST -#define DETECT1 PA6 //A5 -#define DETECT2 PA5 //A4 -#define DETECT3 PA4 //A3 + #define LED1 PB0 //D3 + #define LED2 PB7 //D4 + #define LED3 PB6 //D5 -#define SWITCH1 PA7 //A6 TOGGLE1 -#define SWITCH12 PA2 //A7 MOMENTARY1 -#define SWITCH2 PA1 //A1 TOGGLE1 -#define SWITCH22 PA3 //A2 MOMENTARY1 -#define SWITCH3 PB5 //D11 TOGGLE1 -#define SWITCH32 PB4 //D12 MOMENTARY1 + #define DETECT1 PA6 //A5 + #define DETECT2 PA5 //A4 + #define DETECT3 PA4 //A3 -#define LD3LED PB3 -#define WAKEUPPIN PA2 + #define SWITCH1 PA7 //A6 TOGGLE1 + #define SWITCH12 PA2 //A7 MOMENTARY1 + #define SWITCH2 PA1 //A1 TOGGLE1 + #define SWITCH22 PA3 //A2 MOMENTARY1 + #define SWITCH3 PB5 //D11 TOGGLE1 + #define SWITCH32 PB4 //D12 MOMENTARY1 -#define REDLEDRES -#define YELLOWLEDRES -#define GREENLEDRES + #define LD3LED PB3 + #define WAKEUPPIN PA2 + + #define REDLEDRES + #define YELLOWLEDRES + #define GREENLEDRES + +#else + + #define LED1 0 //D3 + #define LED2 1 //D4 + #define LED3 2 //D5 + + #define DETECT1 3 //A5 + #define DETECT2 4 //A4 + #define DETECT3 5 //A3 + + #define SWITCH1 6 //A6 TOGGLE1 + #define SWITCH12 7 //A7 MOMENTARY1 + #define SWITCH2 8 //A1 TOGGLE1 + #define SWITCH22 9 //A2 MOMENTARY1 + #define SWITCH3 10 //D11 TOGGLE1 + #define SWITCH32 11 //D12 MOMENTARY1 + + #define LD3LED 12 + #define WAKEUPPIN 13 + + #define REDLEDRES + #define YELLOWLEDRES + #define GREENLEDRES + +#endif //unit_test #endif //BOARDH \ No newline at end of file diff --git a/src/buttons.cpp b/src/buttons.cpp index 574b73e..62c28d7 100644 --- a/src/buttons.cpp +++ b/src/buttons.cpp @@ -1,8 +1,6 @@ + + #include "buttons.h" -#include "board.h" -#include -#include "Arduino.h" -#include "JC_Button.h" std::vector buttonlist; @@ -113,4 +111,4 @@ c_button *getButton(e_ledcolor color) std::vector *getButtonlist(void) { return &buttonlist; -} +} \ No newline at end of file diff --git a/src/buttons.h b/src/buttons.h index 3b93958..2ef0415 100644 --- a/src/buttons.h +++ b/src/buttons.h @@ -1,9 +1,12 @@ #ifndef BUTTONSH #define BUTTONSH -#include +#include "Arduino.h" #include -#include "JC_Button.h" +#ifndef UNIT_TEST + #include "JC_Button.h" +#endif +#include "board.h" #include "led.h" class c_button : public ToggleButton diff --git a/src/chainGame.cpp b/src/chainGame.cpp index abb3f50..bd5120d 100644 --- a/src/chainGame.cpp +++ b/src/chainGame.cpp @@ -1,30 +1,21 @@ -#include "chainGame.h" -#include "Arduino.h" -#include "buttons.h" +#ifndef UNIT_TEST +#ifdef ARDUINO +#include "chainGame.h" uint8_t patternIndex = 0; bool patternFlag = false; -bool firstpattern = false; -//e_ledcolor cheatbutton = NONE; +//bool firstpattern = false; uint16_t cheatbutton = 0; bool cheatButtonFlag = false; -// e_ledcolor ledpattern[4] = -// { -// YELLOW, -// GREEN, -// YELLOW, -// RED}; - uint16_t ledpattern[4] = -{ - 1, - 3, - 1, - 2 -}; + { + 1, + 3, + 1, + 2}; int patternlength = sizeof(ledpattern) / sizeof(ledpattern[0]); @@ -44,36 +35,44 @@ void ResetChainGame(void) { patternIndex = 0; patternFlag = false; - firstpattern = false; + //firstpattern = false; cheatbutton = 0; cheatButtonFlag = false; } void HandleChainGame(void) { - if (buttonIsPressed(ledpattern[patternIndex]) && !patternFlag && !cheatButtonFlag) + if (!patternFlag && !cheatButtonFlag) { - turnOnLed(ledpattern[patternIndex]); - patternFlag = true; - cheatbutton = 0; - } - else if (buttonIsPressed(cheatbutton) && !patternFlag && !cheatButtonFlag) - { - turnOnLed(cheatbutton); - cheatButtonFlag = true; - } - else if (anybutton() && !patternFlag && !cheatButtonFlag ) - { - cheatbutton = 0; + if (buttonIsPressed(ledpattern[patternIndex])) + { + //pattern button pressed, turn on LED, set flag + turnOnLed(ledpattern[patternIndex]); + patternFlag = true; + cheatbutton = 0; + } + else if (buttonIsPressed(cheatbutton)) + { + // cheatbutton pressed, turn on cheat led, set flag + turnOnLed(cheatbutton); + cheatButtonFlag = true; + } + else if (anybutton()) + { + // if any other button is pressed, clear cheat button + cheatbutton = 0; + } } if (!buttonIsPressed(ledpattern[patternIndex])) { + // pattern switch is open, turn off pattern LED turnOffLed(ledpattern[patternIndex]); } if (!buttonIsPressed(cheatbutton) && cheatButtonFlag) { + // cheat switch is open, turn of cheat LED turnOffLed(cheatbutton); cheatButtonFlag = false; cheatbutton = 0; @@ -81,25 +80,33 @@ void HandleChainGame(void) if (!anybutton()) { + //all switches are open, turn off all LEDs turnOffAllLed(); if (patternFlag) { + // pattern LED was triggerd, reset flag, move to next pattern patternFlag = false; nextPattern(); } } - + //check cheatbuttons if (buttonIsPressed(4) && (cheatbutton == 0)) { + // cheatbutton 4 (momentary 1) was closed, set cheatbutton to 1 cheatbutton = 1; } else if (buttonIsPressed(5) && (cheatbutton == 0)) { + // cheatbutton 5 (momentary 2) was closed, set cheatbutton to 2 cheatbutton = 2; } else if (buttonIsPressed(6) && (cheatbutton == 0)) { + // cheatbutton 5 (momentary 3) was closed, set cheatbutton to 3 cheatbutton = 3; } -} \ No newline at end of file +} + +#endif +#endif diff --git a/src/chainGame.h b/src/chainGame.h index c2c1ed8..8efecfe 100644 --- a/src/chainGame.h +++ b/src/chainGame.h @@ -1,6 +1,8 @@ #ifndef CHAINGAMEH #define CHAINGAMEH +#include "Arduino.h" +#include "buttons.h" void HandleChainGame( void ); void ResetChainGame(void); diff --git a/src/detectled.cpp b/src/detectled.cpp index b5110c5..c123fe1 100644 --- a/src/detectled.cpp +++ b/src/detectled.cpp @@ -1,9 +1,5 @@ -#include "Arduino.h" #include "detectled.h" -#include "buttons.h" -#include "board.h" -#include "led.h" -#include "vector" + #define CHANNELS 3 #define SAMPLES 20 @@ -36,4 +32,5 @@ void initDetectLed(void) ledlist_ptr = getledlist(); analogReadResolution(10); -} \ No newline at end of file +} + diff --git a/src/detectled.h b/src/detectled.h index 4359181..2a817ee 100644 --- a/src/detectled.h +++ b/src/detectled.h @@ -1,6 +1,11 @@ #ifndef DETECTLEDH #define DETECTLEDH +#include "buttons.h" +#include "board.h" +#include "led.h" +#include "vector" + void handleDetectLed( void ); void initDetectLed( void ); diff --git a/src/led.cpp b/src/led.cpp index 6066c65..c435d36 100644 --- a/src/led.cpp +++ b/src/led.cpp @@ -1,5 +1,6 @@ + + #include "led.h" -#include "Arduino.h" c_leds ledlist; @@ -237,3 +238,4 @@ bool c_led::checkIndex(uint16_t index) } return false; } + diff --git a/src/led.h b/src/led.h index 2986f2f..a4454e9 100644 --- a/src/led.h +++ b/src/led.h @@ -1,8 +1,9 @@ #ifndef LEDH #define LEDH -#include "arduino.h" +#include "Arduino.h" #include "vector" +#include "board.h" enum e_ledcolor { @@ -92,4 +93,5 @@ void turnOffLed(uint16_t index); void turnOnLed(uint16_t index); void turnOffAllLed(); -#endif //LEDH \ No newline at end of file +#endif //LEDH + diff --git a/src/magicSwitchBoard.cpp b/src/magicSwitchBoard.cpp index 3228a61..5825ec9 100644 --- a/src/magicSwitchBoard.cpp +++ b/src/magicSwitchBoard.cpp @@ -1,7 +1,6 @@ + + #include "magicSwitchBoard.h" -#include "Arduino.h" -#include "buttons.h" -//#include "JC_Button.h" #define CHANNELS 3 #define TIMEOUT 7000 //game timeout @@ -171,3 +170,5 @@ void handleMagicSwitchBoard(void) } + + diff --git a/src/magicSwitchBoard.h b/src/magicSwitchBoard.h index 23142be..bd4c1d5 100644 --- a/src/magicSwitchBoard.h +++ b/src/magicSwitchBoard.h @@ -1,6 +1,8 @@ #ifndef MAGICSWITCHBOARDH #define MAGICSWITCHBOARDH +#include "buttons.h" + void handleMagicSwitchBoard( void ); #endif //MAGICSWITCHBOARDH \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 9e8bf26..dbc3247 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,7 @@ -#include + +#include "Arduino.h" + + #include "board.h" #include "chainGame.h" #include "detectled.h" @@ -8,7 +11,7 @@ #include "led.h" #define TIMEOUT 15000 // 15sec * 1000ms -#define GAMESELECTTIMEOUT 5000 // 7sec * 1000ms +#define GAMESELECTTIMEOUT 10000 // 7sec * 1000ms typedef enum { @@ -188,4 +191,4 @@ void loop() } break; } -} \ No newline at end of file +} diff --git a/src/simpleled.cpp b/src/simpleled.cpp index 7521b8e..6c8314f 100644 --- a/src/simpleled.cpp +++ b/src/simpleled.cpp @@ -1,7 +1,5 @@ #include "simpleled.h" -#include "led.h" -#include "buttons.h" -#include "vector" + extern std::vector buttonlist; diff --git a/src/simpleled.h b/src/simpleled.h index ab030b4..6b75a6d 100644 --- a/src/simpleled.h +++ b/src/simpleled.h @@ -1,6 +1,10 @@ #ifndef SIMPLELEDH #define SIMPLELEDH +#include "led.h" +#include "buttons.h" +#include "vector" + void initSimpleLed( void ); void handleSimpleLed( void ); diff --git a/test/native/test_games.cpp b/test/native/test_games.cpp new file mode 100644 index 0000000..598b5a8 --- /dev/null +++ b/test/native/test_games.cpp @@ -0,0 +1,12 @@ +#include +#ifdef UNIT_TEST + + +int main( int argc, char **argv) { + UNITY_BEGIN(); + + UNITY_END(); +} + + +#endif //UNIT_TEST \ No newline at end of file