From 6d9f14f5b4fe8eb3a90729808933c6b23bb22dcd Mon Sep 17 00:00:00 2001 From: willem oldemans Date: Thu, 19 Nov 2020 21:37:40 +0100 Subject: [PATCH] added detectledgame --- src/detectled.cpp | 55 ++++++++++---------- src/led.cpp | 125 ++++++++++++++++++++++++++++++++++++++++++++++ src/led.h | 74 +++++++++++++++++++++++++++ src/main.cpp | 26 +++++----- 4 files changed, 240 insertions(+), 40 deletions(-) create mode 100644 src/led.cpp create mode 100644 src/led.h diff --git a/src/detectled.cpp b/src/detectled.cpp index e866626..a2439ec 100644 --- a/src/detectled.cpp +++ b/src/detectled.cpp @@ -2,56 +2,55 @@ #include "detectled.h" #include "buttons.h" #include "board.h" +#include "led.h" #define CHANNELS 3 #define SAMPLES 20 +extern ToggleButton button4; +extern ToggleButton button5; +extern ToggleButton button6; -unsigned int detectled[CHANNELS] = {0,0,0}; +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; +c_leds *ledlist_ptr; -void handleDetectLed( void ) +void handleDetectLed(void) { - for(int i = 0;i < CHANNELS;i++) - { - detectledRaw[i][sampleIndex] = analogRead(inputs[i]); - } - sampleIndex++; + bool ledon = false; - if(sampleIndex == SAMPLES) + if (button4.isPressed()) { - for(int i = 0;iturnOnLed(YELLOW); + ledon = true; + } + + if (button5.isPressed()) + { + ledlist_ptr->turnOnLed(RED); + ledon = true; + } + + if (button6.isPressed()) + { + ledlist_ptr->turnOnLed(GREEN); + ledon = true; } - for( auto &&o : leds) + if (!ledon) { - digitalWrite(o, 0); + ledlist_ptr->turnAllOff(); } - } - -void initDetectLed( void ) +void initDetectLed(void) { - for (auto &&i : inputs) - { - pinMode(i, INPUT_ANALOG); - } + ledlist_ptr = getledlist(); analogReadResolution(10); } \ No newline at end of file diff --git a/src/led.cpp b/src/led.cpp new file mode 100644 index 0000000..4ebd082 --- /dev/null +++ b/src/led.cpp @@ -0,0 +1,125 @@ +#include "led.h" +#include "Arduino.h" + +c_leds ledlist; + +#define MARGIN 20 + +//############################################# +//# common functions # +//############################################# + +c_leds *getledlist(void) +{ + return &ledlist; +} + +//############################################# +//# leds functions # +//############################################# + +void c_leds::AddLed(uint32_t pin, uint32_t analogpin, uint16_t index, uint16_t value, e_ledcolor color, bool invert) +{ + c_ledport port(pin, analogpin, index, invert); + v_ledports.push_back(port); + + c_led led(color, value, index); + v_leds.push_back(led); +} + +void c_leds::begin(void) +{ + for (auto &&thisled : v_ledports) + { + thisled.begin(); + } +} + +void c_leds::turnOnLed(e_ledcolor color) +{ + for (auto &&port : v_ledports) + { + uint16_t read = port.ledRead(); + for (auto &&thisled : v_leds) + { + if (thisled.checkThreshold(read) & thisled.checkcolor(color)) + { + port.turnOn(); + } + } + } +} + +void c_leds::turnOnLed(uint16_t index) +{ +} + +void c_leds::turnAllOff(void) +{ + for (auto &&port : v_ledports) + { + port.turnOff(); + } +} + +//############################################# +//# c_ledport functions # +//############################################# + +void c_ledport::begin(void) +{ + pinMode(_pin, OUTPUT); + pinMode(_analogPin, INPUT_ANALOG); + turnOff(); +} + +void c_ledport::turnOn(void) +{ + writeLed(true); +} + +void c_ledport::turnOff(void) +{ + writeLed(false); +} + +void c_ledport::writeLed(bool state) +{ + _state = state; + if (_invert) + { + state = !state; + } + digitalWrite(_pin, state); +} + +uint16_t c_ledport::ledRead(void) +{ + return analogRead(_analogPin); +} + + +//############################################# +//# c_led functions # +//############################################# + +bool c_led::checkThreshold(uint16_t value) +{ + uint16_t upperthreshold = _value + MARGIN; + uint16_t lowerthreshold = _value - MARGIN; + + if ((lowerthreshold < value) & (value < upperthreshold)) + { + return true; + } + return false; +} + +bool c_led::checkcolor(e_ledcolor color) +{ + if(_color == color) + { + return true; + } + return false; +} \ No newline at end of file diff --git a/src/led.h b/src/led.h new file mode 100644 index 0000000..32c2397 --- /dev/null +++ b/src/led.h @@ -0,0 +1,74 @@ +#ifndef LEDH +#define LEDH + +#include "arduino.h" +#include "vector" + + +enum e_ledcolor{ + RED, + GREEN, + YELLOW +}; + +class c_ledport +{ + const uint32_t _pin; + const uint32_t _analogPin; + const uint16_t _index; + const bool _invert; + bool _state; + +public: + c_ledport(uint32_t pin, uint32_t analogpin, uint16_t index, bool invert = false) + :_pin(pin), _analogPin(analogpin), _index(index), _invert(invert){}; + + void begin( void ); + + void turnOn( void ); + + void turnOff( void ); + + void writeLed( bool state ); + + uint16_t ledRead( void ); + +}; + +class c_led +{ + const e_ledcolor _color; + const uint16_t _value; + const uint16_t _index; + + public: + + c_led(e_ledcolor color,uint16_t value, uint16_t index) + :_color(color), _value(value), _index(index) {}; + + bool checkThreshold( uint16_t value); + bool checkcolor(e_ledcolor color); +}; + +class c_leds +{ + std::vector v_ledports; + std::vector v_leds; + + public: + c_leds(){}; + + void AddLed( uint32_t pin, uint32_t analogpin, uint16_t index, uint16_t value, e_ledcolor color, bool invert ); + void begin( void ); + + void turnOnLed(e_ledcolor color); + void turnOnLed(uint16_t index); + + void turnAllOff( void ); +}; + + +c_leds* getledlist( void ); + + +#endif //LEDH \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index b571ebf..6e5cb28 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include "buttons.h" #include "JC_Button.h" #include "vector" +#include "led.h" // extern ToggleButton button1; // extern ToggleButton button2; @@ -35,6 +36,14 @@ void setup() pinMode(LED2, OUTPUT); pinMode(LED3, OUTPUT); + c_leds *ledlist = getledlist(); + + ledlist->AddLed(LED1, DETECT1, 1, 844, YELLOW, false); + ledlist->AddLed(LED2, DETECT2, 2, 512, RED, false); + ledlist->AddLed(LED3, DETECT3, 2, 92, GREEN, false); + + ledlist->begin(); + initButtons(); initDetectLed(); } @@ -66,7 +75,7 @@ void loop() case none: { - + currentGame = ChainGame; digitalWrite(LED1, 0); digitalWrite(LED2, 0); digitalWrite(LED3, 0); @@ -79,21 +88,14 @@ void loop() currentGame = magicSwitchBoard; digitalWrite(LED2, 1); } - else - { - currentGame = ChainGame; - } - + if (button6.isPressed()) { digitalWrite(LED3, 1); + currentGame = detectLED; } - - while(anybutton()); - // if (button6.isPressed()) - // { - // currentGame = detectLED; - // } + + while (anybutton()); } break; }