added detectledgame
This commit is contained in:
@@ -2,56 +2,55 @@
|
|||||||
#include "detectled.h"
|
#include "detectled.h"
|
||||||
#include "buttons.h"
|
#include "buttons.h"
|
||||||
#include "board.h"
|
#include "board.h"
|
||||||
|
#include "led.h"
|
||||||
|
|
||||||
#define CHANNELS 3
|
#define CHANNELS 3
|
||||||
#define SAMPLES 20
|
#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];
|
uint32_t detectledRaw[CHANNELS][SAMPLES];
|
||||||
const uint32_t inputs[CHANNELS] = {DETECT1, DETECT2, DETECT3};
|
const uint32_t inputs[CHANNELS] = {DETECT1, DETECT2, DETECT3};
|
||||||
const uint32_t leds[CHANNELS] = {LED1, LED2, LED3};
|
const uint32_t leds[CHANNELS] = {LED1, LED2, LED3};
|
||||||
uint32_t sampleIndex = 0;
|
uint32_t sampleIndex = 0;
|
||||||
|
|
||||||
|
c_leds *ledlist_ptr;
|
||||||
|
|
||||||
void handleDetectLed( void )
|
void handleDetectLed(void)
|
||||||
{
|
{
|
||||||
for(int i = 0;i < CHANNELS;i++)
|
bool ledon = false;
|
||||||
{
|
|
||||||
detectledRaw[i][sampleIndex] = analogRead(inputs[i]);
|
|
||||||
}
|
|
||||||
sampleIndex++;
|
|
||||||
|
|
||||||
if(sampleIndex == SAMPLES)
|
if (button4.isPressed())
|
||||||
{
|
{
|
||||||
for(int i = 0;i<CHANNELS;i++)
|
ledlist_ptr->turnOnLed(YELLOW);
|
||||||
{
|
ledon = true;
|
||||||
uint64_t sum = 0;
|
}
|
||||||
for(int n = 0;n < SAMPLES;n++)
|
|
||||||
{
|
if (button5.isPressed())
|
||||||
sum += detectledRaw[i][n];
|
{
|
||||||
}
|
ledlist_ptr->turnOnLed(RED);
|
||||||
detectled[i] = sum / SAMPLES;
|
ledon = true;
|
||||||
}
|
}
|
||||||
printf("L1 = %u, L2 = %u, L3 = %u",detectled[0],detectled[1],detectled[2]);
|
|
||||||
sampleIndex = 0;
|
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)
|
ledlist_ptr = getledlist();
|
||||||
{
|
|
||||||
pinMode(i, INPUT_ANALOG);
|
|
||||||
}
|
|
||||||
|
|
||||||
analogReadResolution(10);
|
analogReadResolution(10);
|
||||||
}
|
}
|
||||||
125
src/led.cpp
Normal file
125
src/led.cpp
Normal file
@@ -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;
|
||||||
|
}
|
||||||
74
src/led.h
Normal file
74
src/led.h
Normal file
@@ -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<c_ledport> v_ledports;
|
||||||
|
std::vector<c_led> 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
|
||||||
26
src/main.cpp
26
src/main.cpp
@@ -7,6 +7,7 @@
|
|||||||
#include "buttons.h"
|
#include "buttons.h"
|
||||||
#include "JC_Button.h"
|
#include "JC_Button.h"
|
||||||
#include "vector"
|
#include "vector"
|
||||||
|
#include "led.h"
|
||||||
|
|
||||||
// extern ToggleButton button1;
|
// extern ToggleButton button1;
|
||||||
// extern ToggleButton button2;
|
// extern ToggleButton button2;
|
||||||
@@ -35,6 +36,14 @@ void setup()
|
|||||||
pinMode(LED2, OUTPUT);
|
pinMode(LED2, OUTPUT);
|
||||||
pinMode(LED3, 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();
|
initButtons();
|
||||||
initDetectLed();
|
initDetectLed();
|
||||||
}
|
}
|
||||||
@@ -66,7 +75,7 @@ void loop()
|
|||||||
|
|
||||||
case none:
|
case none:
|
||||||
{
|
{
|
||||||
|
currentGame = ChainGame;
|
||||||
digitalWrite(LED1, 0);
|
digitalWrite(LED1, 0);
|
||||||
digitalWrite(LED2, 0);
|
digitalWrite(LED2, 0);
|
||||||
digitalWrite(LED3, 0);
|
digitalWrite(LED3, 0);
|
||||||
@@ -79,21 +88,14 @@ void loop()
|
|||||||
currentGame = magicSwitchBoard;
|
currentGame = magicSwitchBoard;
|
||||||
digitalWrite(LED2, 1);
|
digitalWrite(LED2, 1);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
currentGame = ChainGame;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (button6.isPressed())
|
if (button6.isPressed())
|
||||||
{
|
{
|
||||||
digitalWrite(LED3, 1);
|
digitalWrite(LED3, 1);
|
||||||
|
currentGame = detectLED;
|
||||||
}
|
}
|
||||||
|
|
||||||
while(anybutton());
|
while (anybutton());
|
||||||
// if (button6.isPressed())
|
|
||||||
// {
|
|
||||||
// currentGame = detectLED;
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user