From 95e5f22a558beeaac8895400e406837656a057e0 Mon Sep 17 00:00:00 2001 From: manuel Date: Tue, 4 Feb 2020 23:33:05 +0100 Subject: add bs_button --- bs_button/src/ledcontrol.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 bs_button/src/ledcontrol.h (limited to 'bs_button/src/ledcontrol.h') diff --git a/bs_button/src/ledcontrol.h b/bs_button/src/ledcontrol.h new file mode 100644 index 0000000..baf03e9 --- /dev/null +++ b/bs_button/src/ledcontrol.h @@ -0,0 +1,38 @@ +#include + +class LedControl +{ +public: + LedControl(int pin, int high = HIGH, int init = LOW) + { + _pin = pin; + _high = high; + pinMode(_pin, OUTPUT); + (init == high) ? on() : off(); + } + + void set(int state) + { + _state = state; + digitalWrite(_pin, state); + } + void off() { set(!_high); } + void on() { set(_high); } + void toggle() { (_state == _high) ? off() : on(); } + + void blink(const unsigned long interval) + { + static unsigned long previous_millis = 0; + unsigned long current_millis = millis(); + if (current_millis - previous_millis >= interval) + { + previous_millis = current_millis; + toggle(); + } + } + +private: + int _state; + int _high; + int _pin; +}; -- cgit v1.2.3