Move components to lib folder

This commit is contained in:
Robert Marshall 2022-09-08 13:20:51 +01:00
parent ae3c20b59d
commit b9b68f08cb
8 changed files with 1 additions and 0 deletions

76
lib/LED/LED.cpp Normal file
View file

@ -0,0 +1,76 @@
#ifndef LED_cpp
#define LED_cpp
#include <Arduino.h>
#include "LEDOutput.cpp"
class LED {
std::string _name;
LEDOutput* _output;
bool _on;
unsigned long _fadeDurationOn, _fadeDurationOff, _fadeStart, _fadeEnd;
float _brightness;
unsigned long getFadeDuration(){
return _on ? _fadeDurationOn : _fadeDurationOff;
}
unsigned long getRemainingFadeTime(){
unsigned long now = millis();
return _fadeEnd >= now ? constrain(_fadeEnd - now, 0, getFadeDuration()) : 0;
}
float getMultiplier() {
float value = getRemainingFadeTime() / (float)getFadeDuration();
return _brightness - value;
}
float getOutputMultiplier(){
float value = getMultiplier();
return _on ? value : _brightness - value;
}
void reset(bool on){
if (on == _on)
return;
float oldMultiplier = getMultiplier();
_on = on;
_fadeStart = millis();
_fadeEnd = _fadeStart + (getFadeDuration() * oldMultiplier);
}
public:
LED(std::string name, LEDOutput* output, unsigned long fadeDurationOn, unsigned long fadeDurationOff) {
_name = name;
_output = output;
_fadeDurationOn = fadeDurationOn;
_fadeDurationOff = fadeDurationOff;
_brightness = 1.0f;
}
std::string getName(){
return _name;
}
void on(){
reset(true);
}
void off(){
reset(false);
}
void toggle(){
reset(!_on);
}
void loop() {
_output->writeFraction(getOutputMultiplier());
}
void setBrightness(float brightness){
_brightness = brightness;
}
};
#endif

33
lib/LED/LEDOutput.cpp Normal file
View file

@ -0,0 +1,33 @@
#ifndef LEDOutput_cpp
#define LEDOutput_cpp
#include <Arduino.h>
#define LED_OUTPUT_FREQUENCY 490
#define LED_OUTPUT_RESOLUTION 15
#define LED_OUTPUT_PWM_RANGE 32767
class LEDOutput {
unsigned int _channel;
bool _invert;
public:
LEDOutput(unsigned int channel, bool invert = false) {
_channel = channel;
_invert = invert;
ledcSetup(channel, LED_OUTPUT_FREQUENCY, LED_OUTPUT_RESOLUTION);
}
void attach(unsigned int pin){
ledcAttachPin(pin, _channel);
}
void writeFraction(float fraction){
auto value = fraction * LED_OUTPUT_PWM_RANGE;
if (_invert)
value = LED_OUTPUT_PWM_RANGE - value;
ledcWrite(_channel, value);
}
};
#endif

48
lib/LED/LedManager.cpp Normal file
View file

@ -0,0 +1,48 @@
#ifndef LedManager_cpp
#define LedManager_cpp
#include "LED.cpp"
#include "TimerManager.cpp"
#include <map>
#include <functional>
class LedManager {
TimerManager *_timers;
std::map<int, LED *> _leds;
public:
LedManager(TimerManager *timers) {
_timers = timers;
}
void registerLEDs(int id, LED *leds) {
_leds[id] = leds;
_timers->add(id, [&]() { leds->off(); });
}
void loop() {
for (auto iterator = _leds.begin(); iterator != _leds.end(); ++iterator) {
auto leds = iterator->second;
leds->loop();
}
}
void setLedProperties(int id, bool on, float brightness, unsigned long timer) {
if (!_leds.count(id))
return;
auto led = _leds.find(id)->second;
led->setBrightness(brightness);
if (on)
led->on();
else
led->off();
if (timer > 0)
_timers->reset(id, timer);
}
};
#endif

46
lib/README Normal file
View file

@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

38
lib/Timer/Timer.cpp Normal file
View file

@ -0,0 +1,38 @@
#ifndef Timer_cpp
#define Timer_cpp
#include <Arduino.h>
#include <functional>
class Timer{
private:
unsigned long _interval, _lastTick;
std::function<void()> _callback;
bool _running;
public:
Timer(std::function<void()> callback){
_callback = callback;
_running = false;
}
void reset(unsigned long interval){
_interval = interval;
_lastTick = millis();
_running = true;
}
void loop(){
if (!_running)
return;
unsigned long tick = millis();
if (tick - _lastTick >= _interval){
_callback();
_running = false;
}
}
};
#endif

View file

@ -0,0 +1,32 @@
#ifndef TimerManager_cpp
#define TimerManager_cpp
#include <map>
#include <functional>
#include "Timer.cpp"
class TimerManager{
std::map<int, Timer*> _timers;
public:
void add(int id, std::function<void()> callback){
_timers[id] = new Timer(callback);
}
void reset(int id, unsigned long interval){
if (!_timers.count(id))
return;
_timers.find(id)->second->reset(interval);
}
void loop(){
for(auto iterator = _timers.begin(); iterator != _timers.end(); ++iterator)
{
auto timer = iterator->second;
timer->loop();
}
}
};
#endif

View file

@ -0,0 +1,39 @@
#ifndef TouchInput_cpp
#define TouchInput_cpp
#include <Arduino.h>
#include <functional>
class TouchInput {
const unsigned int _debounceDelay = 50;
int _pin, _touchThreshold;
std::function<void()> _callback;
unsigned long _touchStart;
bool _currentTouchValue, _prevTouchValue;
public:
TouchInput(int pin, int touchThreshold, std::function<void()> callback){
_pin = pin;
_touchThreshold = touchThreshold;
_callback = callback;
}
void loop(){
bool touching = touchRead(_pin) <= _touchThreshold;
if (_prevTouchValue != touching) {
_touchStart = millis();
}
if (millis() - _touchStart >= _debounceDelay && touching != _currentTouchValue){
_currentTouchValue = touching;
if (touching)
_callback();
}
_prevTouchValue = touching;
}
};
#endif