forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Led.h
181 lines (154 loc) · 4.47 KB
/
Led.h
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#ifndef __STATUSLED_H__
#define __STATUSLED_H__
#include "Alarm.h"
#include "Debug.h"
namespace as {
class LedStates {
public:
enum Mode { nothing=0, pairing=1,send=2, ack=3,
nack=4, bat_low=5, welcome=6, key_long=7 };
struct BlinkPattern {
uint8_t length;
uint8_t duration;
uint8_t pattern[6];
};
static const BlinkPattern single[8] PROGMEM;
static const BlinkPattern dual1[8] PROGMEM;
static const BlinkPattern dual2[8] PROGMEM;
};
template <class PINTYPE=ArduinoPins>
class Led : public Alarm, public LedStates {
private:
BlinkPattern current;
uint8_t step; // current step in pattern
uint8_t repeat; // current repeat of the pattern
uint8_t pin;
uint8_t inv;
void copyPattern (Mode stat,const BlinkPattern* patt) {
memcpy_P(¤t,patt+stat,sizeof(BlinkPattern));
}
void next (AlarmClock& clock) {
tick = decis2ticks(current.pattern[step++]);
((step & 0x01) == 0x01) ? ledOn() : ledOff();
clock.add(*this);
}
public:
Led () : Alarm(0), step(0), repeat(0), pin(0), inv(false) {
async(true);
}
virtual ~Led() {}
void init (uint8_t p) {
pin = p;
PINTYPE::setOutput(pin);
ledOff();
}
void invert (bool value) {
inv = value;
}
bool invert () const {
return inv;
}
void set(Mode stat,const BlinkPattern* patt) {
sysclock.cancel(*this);
ledOff();
copyPattern(stat,patt);
if( current.length > 0 ) {
step = 0;
repeat = 0;
next(sysclock);
}
}
void ledOff () {
if( invert() == true ) {
PINTYPE::setHigh(pin);
}
else {
PINTYPE::setLow(pin);
}
}
void ledOn () {
if( invert() == true ) {
PINTYPE::setLow(pin);
}
else {
PINTYPE::setHigh(pin);
}
}
void ledOn (uint32_t ticks) {
if( active() == false && ticks > 0 ) {
current.length = 2;
current.duration = 1;
current.pattern[0] = ticks2decis(ticks);
current.pattern[1] = 0;
// start the pattern
step = repeat = 0;
next(sysclock);
}
}
bool active () const { return current.length != 0; }
virtual void trigger (AlarmClock& clock) {
ATOMIC_BLOCK( ATOMIC_RESTORESTATE ) {
if( step < current.length ) {
next(clock);
}
else {
step = 0;
if( current.duration == 0 || ++repeat < current.duration ) {
next(clock);
}
else {
ledOff();
copyPattern(nothing,single);
}
}
}
}
};
template<uint8_t LEDPIN1, class PINTYPE=ArduinoPins>
class StatusLed : public LedStates {
Led<PINTYPE> led1;
public:
StatusLed () {}
void init () { led1.init(LEDPIN1); }
bool active () const { return led1.active(); }
void ledOn (uint32_t ticks) { led1.ledOn(ticks); }
void ledOn (uint32_t ticks,__attribute__((unused)) uint32_t tacks) { led1.ledOn(ticks); }
void set(Mode stat) { led1.set(stat,single); }
void ledOn () { led1.ledOn(); }
void ledOff () { led1.ledOff(); }
void invert (bool value) { led1.invert(value); }
};
template <uint8_t LEDPIN1,uint8_t LEDPIN2, class PINTYPE1=ArduinoPins, class PINTYPE2=ArduinoPins>
class DualStatusLed : public LedStates {
private:
Led<PINTYPE1> led1;
Led<PINTYPE1> led2;
public:
DualStatusLed () {}
void init () { led1.init(LEDPIN1); led2.init(LEDPIN2); }
bool active () const { return led1.active() || led2.active(); }
void ledOn (uint32_t ticks) { led1.ledOn(ticks); led2.ledOn(ticks); }
void ledOn (uint32_t ticks,uint32_t tacks) { led1.ledOn(ticks); led2.ledOn(tacks); }
void set(Mode stat) { led1.set(stat,dual1); led2.set(stat,dual2); }
void ledOn () { led1.ledOn(); led2.ledOn(); }
void ledOff () { led1.ledOff(); led2.ledOff(); }
void invert (bool value) { led1.invert(value); led2.invert(value); }
};
class NoLed {
public:
NoLed () {}
void init () {}
bool active () const { return false; }
void ledOn (__attribute__((unused)) uint32_t ticks) {}
void ledOn (__attribute__((unused)) uint32_t ticks,__attribute__((unused)) uint32_t tacks) {}
void set(__attribute__((unused)) LedStates::Mode stat) {}
void ledOn () {}
void ledOff () {}
void invert (__attribute__((unused)) bool value) {}
};
}
#endif