-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMSG4LED.cpp
More file actions
332 lines (263 loc) · 8.89 KB
/
MSG4LED.cpp
File metadata and controls
332 lines (263 loc) · 8.89 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "MSG4LED.h"
// ----------------------------------
// Set & Init LEDs pins
// ----------------------------------
MSG4LED::MSG4LED (byte _blue, byte _red, byte _green, byte _yellow) {
// Check pins number
if (_blue >= 0) { LEDS[0] = _blue; }
if (_red >= 0) { LEDS[1] = _red; }
if (_green >= 0) { LEDS[2] = _green; }
if (_yellow >= 0) { LEDS[3] = _yellow; }
// Init LEDs Array
for (int i = 0; i < 4; i++) {
pinMode(LEDS[i], OUTPUT);
digitalWrite(LEDS[i], LOW);
}
ChangeOrder(LEDS[0], LEDS[1], LEDS[2], LEDS[3]); // Apply change for LEDorder
}
// ==================================
// ----------------------------------
// Check LED pin in LEDS array and count them
// ----------------------------------
byte MSG4LED::CheckLEDs (byte ColorPin) {
byte c = 0;
for (int i = 0; i < 4; i++) {
if (LEDS[i] == ColorPin) { c++; }
}
return c;
}
// ==================================
// ----------------------------------
// Change LEDs order
// ----------------------------------
void MSG4LED::ChangeOrder (byte _pos_1, byte _pos_2, byte _pos_3, byte _pos_4) {
// Check pins number and apply new LEDorder value
if (CheckLEDs(_pos_1) > 0) { LEDorder[0] = _pos_1; } else { LEDorder[0] = LEDS[0]; }
if (CheckLEDs(_pos_2) > 0) { LEDorder[1] = _pos_2; } else { LEDorder[1] = LEDS[1]; }
if (CheckLEDs(_pos_3) > 0) { LEDorder[2] = _pos_3; } else { LEDorder[2] = LEDS[2]; }
if (CheckLEDs(_pos_4) > 0) { LEDorder[3] = _pos_4; } else { LEDorder[3] = LEDS[3]; }
}
// ==================================
// ----------------------------------
// Reset LEDs order
// ----------------------------------
void MSG4LED::ResetOrder () {
// LEDOrder as Init pins number
for (int i = 0; i < 4; i++) { LEDorder[i] = LEDS[i]; }
}
// ==================================
// ----------------------------------
// ReMap Any digital to 4LED
// ----------------------------------
byte MSG4LED::ReMap (long x, long max_x) {
byte ColorPin;
if (x > max_x) {max_x = x;} // Correction for wrong value
// Convert X-value, use a function called map()
ColorPin = LEDorder[map(x, 0, max_x, 0, 4)];
// (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
return ColorPin;
}
// ==================================
// ----------------------------------
// Power On/Off LED
// ----------------------------------
void MSG4LED::PowerOnColor(byte ColorPin) {
digitalWrite(ColorPin, !digitalRead(ColorPin));
}
// ==================================
// ----------------------------------
// Simple for Num
// ----------------------------------
void MSG4LED::PowerOnNum(byte OrderNum) {
PowerOnColor(LEDorder[OrderNum-1]);
}
// ==================================
// ----------------------------------
// Power Off for All LEDs
// ----------------------------------
void MSG4LED::TurnOffLine() {
for (int i = 0; i < 4; i++) {
// Power off LED
digitalWrite(LEDorder[i], LOW);
}
}
// ==================================
// ----------------------------------
// Turn On/Off Line
// ----------------------------------
void MSG4LED::TurnOnLine () {
TurnOffLine();
for (int i = 0; i < 4; i++) {
// Power On LED
PowerOnColor (LEDorder[i]);
}
}
// ==================================
// ----------------------------------
// Fill Line to Num
// ----------------------------------
void MSG4LED::FillTo (byte OrderNum, bool TurnOff) {
if (TurnOff) { TurnOffLine();}
if (OrderNum > 4) { OrderNum = 4; }
for (int i = 0; i < OrderNum; i++) {
// Power On LED
PowerOnColor (LEDorder[i]);
}
}
// ==================================
// ----------------------------------
// Fill Line from Num
// ----------------------------------
void MSG4LED::FillFrom (byte OrderNum, bool TurnOff) {
if (TurnOff) { TurnOffLine();}
if (OrderNum > 4) { OrderNum = 4; }
else if (OrderNum == 0) { OrderNum = 1; }
for (int i = OrderNum-1; i < 4; i++) {
// Power On LED
PowerOnColor (LEDorder[i]);
}
}
// ==================================
// ----------------------------------
// Blink By Color or Pin
// ----------------------------------
void MSG4LED::BlinkByColor(byte ColorPin, byte countBlink, unsigned long time) {
for (int i = 0; i < countBlink; i++)
{
PowerOnColor(ColorPin);
delay(time);
PowerOnColor(ColorPin);
delay(time);
}
}
// ==================================
// ----------------------------------
// Blink By OrderNum
// ----------------------------------
void MSG4LED::BlinkByNum (byte OrderNum, byte countBlink, unsigned long time) {
// Smart correction
if (OrderNum > 4) { OrderNum = 4; }
// Blink LED
BlinkByColor (LEDorder[OrderNum-1], countBlink, time);
}
// ==================================
// ----------------------------------
// Blink Random
// ----------------------------------
void MSG4LED::BlinkRandom (byte countBlink, unsigned long time) {
// Blink LED
BlinkByColor (LEDorder[random(0,4)], countBlink, time);
}
// ==================================
// ----------------------------------
// Blink Line
// ----------------------------------
void MSG4LED::BlinkLine (unsigned long time) {
TurnOffLine();
for (int i = 0; i < 4; i++) {
// Blink LED
BlinkByColor (LEDorder[i], 1, time);
}
}
// ----------------------------------
// Alias for BlinkLine (RTL mode)
void MSG4LED::BlinkLineRTL (unsigned long time) {
BlinkLine (time);
}
// ==================================
// ----------------------------------
// Blink Line with LTR mode
// ----------------------------------
void MSG4LED::BlinkLineLTR (unsigned long time) {
TurnOffLine();
for (int i = 3; i >= 0; i--) {
// Blink LED
BlinkByColor (LEDorder[i], 1, time);
}
}
// ==================================
// ----------------------------------
// Morse Code
// ----------------------------------
void MSG4LED::MorseCodeBlinkChar (long DigCode) {
byte CurNum = 0;
long RevCode = 0;
byte x = 0;
while (DigCode > 0) {
CurNum = DigCode % 10;
RevCode = RevCode+CurNum;
DigCode = DigCode / 10;
if (DigCode > 0) { RevCode = RevCode*10; }
x++;
}
Serial.print(F("x="));
Serial.println(x);
while (RevCode > 0) {
CurNum = RevCode % 10;
BlinkByNum (CurNum, 1);
RevCode = RevCode / 10;
}
for (int i = x; x <= 6; x++) { delay(1000); }
}
// ----------------------------------
void MSG4LED::MorseCode (String mcode) {
mcode.toUpperCase();
long DigCode;
for (int i = 0; i < mcode.length(); i++)
{
switch (mcode.charAt(i)) {
case 'A': DigCode = 13; break;
case 'B': DigCode = 321; break;
case 'C': DigCode = 3131; break;
case 'D': DigCode = 32; break;
case 'E': DigCode = 1; break;
case 'F': DigCode = 231; break;
case 'G': DigCode = 41; break;
case 'H': DigCode = 22; break;
case 'I': DigCode = 2; break;
case 'J': DigCode = 143; break;
case 'K': DigCode = 313; break;
case 'L': DigCode = 132; break;
case 'M': DigCode = 4; break;
case 'N': DigCode = 31; break;
case 'O': DigCode = 43; break;
case 'P': DigCode = 141; break;
case 'Q': DigCode = 413; break;
case 'R': DigCode = 131; break;
case 'S': DigCode = 21; break;
case 'T': DigCode = 3; break;
case 'U': DigCode = 23; break;
case 'V': DigCode = 213; break;
case 'W': DigCode = 14; break;
case 'X': DigCode = 323; break;
case 'Y': DigCode = 314; break;
case 'Z': DigCode = 42; break;
case '1': DigCode = 144; break;
case '2': DigCode = 241; break;
case '3': DigCode = 214; break;
case '4': DigCode = 223; break;
case '5': DigCode = 221; break;
case '6': DigCode = 322; break;
case '7': DigCode = 421; break;
case '8': DigCode = 432; break;
case '9': DigCode = 441; break;
case '0': DigCode = 443; break;
case ' ': DigCode = 2221; break;
case '.': DigCode = 222; break;
case ',': DigCode = 131313; break;
case ':': DigCode = 4321; break;
case ';': DigCode = 313131; break;
case '(': case ')': DigCode = 31413; break;
case '\'': DigCode = 1441; break;
case '"': DigCode = 13231; break;
case '-': DigCode = 3223; break;
case '/': DigCode = 3231; break;
case '?': DigCode = 242; break;
case '!': DigCode = 424; break;
case '@': DigCode = 14131; break;
default: DigCode = 0; TurnOffLine(); break;
}
MorseCodeBlinkChar(DigCode);
}
}
// ==================================