-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLed.h
190 lines (162 loc) · 3.44 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
182
183
184
185
186
187
188
189
190
#pragma once
#include "Commons.h"
static const int BLINK_LIMIT = 75; // 50ms (1500 = 1s)
enum LedName
{
LED_INPUT,
LED_INPUT_PEAK,
LED_SYNC,
LED_MOD,
LED_RECORD,
LED_RANDOM,
LED_ARROW_LEFT,
LED_ARROW_RIGHT,
LED_SHIFT,
LED_MOD_AMOUNT,
LED_CV_AMOUNT,
LED_LAST
};
enum LedType
{
LED_TYPE_BUTTON,
LED_TYPE_PARAM,
};
class Led
{
private:
Patch* patch_;
TGate trigger_;
LedType type_;
int id_;
int blinks_;
int samplesBetweenBlinks_;
float value_;
bool trig_;
bool doBlink_;
bool fast_;
inline void SetLed(float value)
{
if (LedType::LED_TYPE_BUTTON == type_)
{
patch_->setButton(PatchButtonId(id_), value);
}
else
{
patch_->setParameterValue(PatchParameterId(id_), value);
}
}
public:
Led(int id, LedType type)
{
id_ = id;
type_ = type;
value_ = 0;
trig_ = false;
doBlink_ = false;
trigger_.Init(48000);
samplesBetweenBlinks_ = 0;
}
~Led() {}
static Led* create(int id, LedType type = LedType::LED_TYPE_BUTTON)
{
return new Led(id, type);
}
static void destroy(Led* obj)
{
delete obj;
}
inline void Set(float value)
{
value_ = value;
SetLed(value_);
}
inline void On()
{
Set(1);
}
inline void Off()
{
Set(0);
}
inline void Toggle()
{
Set(value_ == 0 ? 1.f : 1.f - value_);
}
inline bool IsOn()
{
return value_;
}
inline bool IsBlinking()
{
return blinks_ > 0;
}
/**
* @brief Blink the led.
*
* @param blinks number of blinks or -1 for continuous blinking or 0 for stopping
* @param fast
* @param initial the initial state of the led
*/
inline void Blink(int blinks = 1, bool fast = false, bool initial = true)
{
if (blinks == -1 && blinks_ == -1)
{
return;
}
blinks_ = blinks;
if (blinks == 0)
{
// Stop blinking.
Off();
doBlink_ = false;
trig_ = false;
return;
}
Set(initial);
trig_ = true;
doBlink_ = true;
fast_ = fast;
if (fast_ && blinks > 0)
{
trigger_.SetDuration(0.002f / blinks_);
}
}
// Called at block rate
inline void Read()
{
if (doBlink_)
{
if (!trigger_.Process(trig_))
{
Toggle();
doBlink_ = false;
// Decrement the number of blinks remaining,
// unless it's -1 (continuous blinking).
if (blinks_ > 0)
{
blinks_--;
if (blinks_ == 0)
{
Off();
}
}
samplesBetweenBlinks_ = 0;
}
trig_ = false;
}
if (!doBlink_ && blinks_ != 0)
{
// If we must blink another time, wait a bit before the next one.
if (samplesBetweenBlinks_ < (fast_ ? 75 : 150))
{
samplesBetweenBlinks_++;
}
else
{
Toggle();
doBlink_ = true;
trig_ = true;
}
}
}
};