diff options
| author | manuel <manuel@mausz.at> | 2020-02-04 23:33:05 +0100 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2020-02-04 23:33:05 +0100 |
| commit | 95e5f22a558beeaac8895400e406837656a057e0 (patch) | |
| tree | 54c873c7e84b70f677f223abce32df3cd9ad3743 /bs_button/src/ledcontrol.h | |
| parent | bc34f55c1abfca72c2ab5f6e0722b56168202366 (diff) | |
| download | arduino-95e5f22a558beeaac8895400e406837656a057e0.tar.gz arduino-95e5f22a558beeaac8895400e406837656a057e0.tar.bz2 arduino-95e5f22a558beeaac8895400e406837656a057e0.zip | |
add bs_button
Diffstat (limited to 'bs_button/src/ledcontrol.h')
| -rw-r--r-- | bs_button/src/ledcontrol.h | 38 |
1 files changed, 38 insertions, 0 deletions
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 @@ | |||
| 1 | #include <Arduino.h> | ||
| 2 | |||
| 3 | class LedControl | ||
| 4 | { | ||
| 5 | public: | ||
| 6 | LedControl(int pin, int high = HIGH, int init = LOW) | ||
| 7 | { | ||
| 8 | _pin = pin; | ||
| 9 | _high = high; | ||
| 10 | pinMode(_pin, OUTPUT); | ||
| 11 | (init == high) ? on() : off(); | ||
| 12 | } | ||
| 13 | |||
| 14 | void set(int state) | ||
| 15 | { | ||
| 16 | _state = state; | ||
| 17 | digitalWrite(_pin, state); | ||
| 18 | } | ||
| 19 | void off() { set(!_high); } | ||
| 20 | void on() { set(_high); } | ||
| 21 | void toggle() { (_state == _high) ? off() : on(); } | ||
| 22 | |||
| 23 | void blink(const unsigned long interval) | ||
| 24 | { | ||
| 25 | static unsigned long previous_millis = 0; | ||
| 26 | unsigned long current_millis = millis(); | ||
| 27 | if (current_millis - previous_millis >= interval) | ||
| 28 | { | ||
| 29 | previous_millis = current_millis; | ||
| 30 | toggle(); | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | private: | ||
| 35 | int _state; | ||
| 36 | int _high; | ||
| 37 | int _pin; | ||
| 38 | }; | ||
