added detectledgame
This commit is contained in:
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;
|
||||
}
|
||||
Reference in New Issue
Block a user