Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ To work properly library needs to observe the main loop. Thats why you need to c
#define LED_GPIO 13
// Initialize winker on LED GPIO number
LedWinker winker(LED_GPIO);
// LedWinker winker(LED_GPIO, false); // If your LED is Active-Low (Like ESP bultin LED)

// serial incoming message
String incoming;
Expand Down Expand Up @@ -80,6 +81,7 @@ void loop() {
if (incoming == "OFF") winker.Wink(LED_OFF); // OFF LED
if (incoming == "FAST") winker.Wink(LED_FAST); // Blink Fast
if (incoming == "SLOW") winker.Wink(LED_SLOW); // Blink Slow
if (incoming == "1200") winker.Wink(1200); // Blink every 1200 ms
}
}
```
81 changes: 28 additions & 53 deletions src/LedWinker.cpp
Original file line number Diff line number Diff line change
@@ -1,63 +1,38 @@
#include "Arduino.h"
#include <Arduino.h>
#include "LedWinker.hpp"

#define SLOW_BLINK_DELAY 900
#define FAST_BLINK_DELAY 180

LedWinker::LedWinker(int GPIO){
_GPIO = GPIO;
LedWinker::LedWinker(uint8_t pin, bool activeHigh) : pin(pin), activeHigh(activeHigh)
{
// Set to default LOW (off) the LED
pinMode(GPIO, OUTPUT);
digitalWrite(_GPIO, LOW);
pinMode(pin, OUTPUT);
off();
}

void LedWinker::Wink(winkType TYPE){
// Just a setter
_winkType = TYPE;
void LedWinker::off()
{
this->winkSpeed = winkType::LED_OFF;
digitalWrite(pin, !(activeHigh ^ LOW));
lastState = false;
}

void LedWinker::Loop(){
// Check for type
switch (_winkType) {
case LED_SLOW:
checkBlinkTime(SLOW_BLINK_DELAY);
break;
case LED_FAST:
checkBlinkTime(FAST_BLINK_DELAY);
break;
case LED_ON:
if(_lastState == LOW){
_lastState = HIGH;
digitalWrite(_GPIO, _lastState);
};
break;
case LED_OFF:
if(_lastState == HIGH){
_lastState = LOW;
digitalWrite(_GPIO, _lastState);
};
break;
};
void LedWinker::on()
{
this->winkSpeed = winkType::LED_ON;
digitalWrite(pin, !(activeHigh ^ HIGH));
lastState = true;
}

// Check is time to ON or OFF led based on delay
void LedWinker::checkBlinkTime(int DELAY){
const unsigned long currentMillis = millis();
if (currentMillis - _lastBlinkedTime >= DELAY) {
// save the last time you blinked the LED
_lastBlinkedTime = currentMillis;
// if the LED is off turn it on and vice-versa:
if (_lastState == LOW) {
_lastState = HIGH;
} else {
_lastState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(_GPIO, _lastState);
void LedWinker::Loop()
{
if (winkSpeed == winkType::LED_ON || winkSpeed == winkType::LED_OFF)
{
return;
}
}

// Getter of the current state
winkType LedWinker::GetState(){
return _winkType;
}
const auto currentMillis = millis();
if (currentMillis - lastBlinkedTime >= winkSpeed)
{
lastBlinkedTime = currentMillis;
digitalWrite(pin, !(activeHigh ^ !lastState));
lastState = !lastState;
}
}
46 changes: 31 additions & 15 deletions src/LedWinker.hpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
#ifndef LEDWINKER_H
#define LEDWINKER_H

#include "Arduino.h"
#include <stdint.h>

enum winkType {
LED_SLOW,
LED_FAST,
LED_ON,
LED_OFF
enum winkType : uint16_t
{
LED_SLOW = 900,
LED_FAST = 180,
LED_ON = UINT16_MAX,
LED_OFF = 0,
};

class LedWinker
{
private:
uint8_t pin;
uint16_t winkSpeed = winkType::LED_OFF;
bool lastState = false;
bool activeHigh;
unsigned long lastBlinkedTime = 0;

public:
LedWinker(int GPIO);
void Wink(winkType TYPE);
LedWinker(uint8_t pin, bool activeHigh = true);
void Loop();
winkType GetState();
private:
int _GPIO{0};
winkType _winkType{LED_OFF};
uint8_t _lastState{LOW};
unsigned long _lastBlinkedTime{0};

void checkBlinkTime(int);
inline bool GetState() const
{
return lastState;
}

inline void Wink(uint16_t winkSpeed)
{
this->winkSpeed = winkSpeed;
}

void off();
void on();

inline uint16_t getWinkSpeed() const {
return winkSpeed;
}
};

#endif