/
CMMC_Blink.hpp
69 lines (55 loc) · 1.5 KB
/
CMMC_Blink.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef CMMCBlink_h
#define CMMCBlink_h
#include <Arduino.h>
#include <Ticker.h>
class CMMC_Blink
{
public:
typedef enum {
BLINK_TYPE_TICKER = 1,
BLINK_TYPE_INTERVAL,
} blink_t;
static const blink_t TYPE_TICKER = BLINK_TYPE_TICKER;
static const blink_t TYPE_INTERVAL = BLINK_TYPE_INTERVAL;
CMMC_Blink init(blink_t type = BLINK_TYPE_TICKER) {
if (type == BLINK_TYPE_TICKER) {
this->_ticker = new Ticker;
}
_initialized = true;
return *this;
}
void setPin(uint8_t pin) {
pinMode(_ledPin, OUTPUT);
digitalWrite(_ledPin, LOW);
_ledPin = pin;
}
CMMC_Blink(blink_t type = BLINK_TYPE_TICKER) {
_type = type;
};
CMMC_Blink(Ticker *ticker) {
_initialized = true;
this->_ticker = ticker;
};
void blink(int ms, uint8_t pin) {
this->setPin(pin);
this->blink(ms);
}
void blink(int ms) {
if (!_initialized) return;
if (_ledPin == 254) return;
static int _pin = this->_ledPin;
this->_ticker->detach();
auto lambda = []() {
int state = digitalRead(_pin); // get the current state of GPIOpin pin
digitalWrite(_pin, !state); // set pin to the opposite state
};
// auto function = static_cast<void (*)(int)>(lambda);
this->_ticker->attach_ms(ms, lambda);
}
private:
unsigned int _ledPin = 254;
Ticker *_ticker;
blink_t _type;
bool _initialized = false;
};
#endif