Skip to content

Instantly share code, notes, and snippets.

@Bolukan
Created April 24, 2020 21:47
Show Gist options
  • Save Bolukan/a11de3a92faa50813409aee30f5d438c to your computer and use it in GitHub Desktop.
Save Bolukan/a11de3a92faa50813409aee30f5d438c to your computer and use it in GitHub Desktop.
myled.h - Library to blink led
#pragma once
#include <Arduino.h>
#include <Ticker.h>
class MyLed
{
public:
// * Custom led
MyLed(uint8_t led)
: _led(led)
{
initialise();
}
// * Builtin led (LED_BUILTIN)
MyLed()
: _led(LED_BUILTIN)
{
initialise();
}
// * Turn led off
void off()
{
_ticker.detach();
_off();
};
// * Turn led on
void on()
{
_ticker.detach();
_on();
};
// * Toggle state of led
void toggle()
{
digitalWrite(_led, !digitalRead(_led));
};
// * Blink led
void blink(float seconds)
{
_ticker.attach(seconds, std::bind(&MyLed::toggle, this));
_on();
}
private:
// * led pin
const uint8_t _led = LED_BUILTIN;
// * Initiate led blinker library
Ticker _ticker;
// * Initialise led
void initialise()
{
pinMode(_led, OUTPUT);
_off();
};
void _on()
{
digitalWrite(_led, LOW);
}
void _off()
{
digitalWrite(_led, HIGH);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment