-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathMeccanoIr.h
More file actions
322 lines (287 loc) · 6.53 KB
/
MeccanoIr.h
File metadata and controls
322 lines (287 loc) · 6.53 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
/*
MeccanoIr.h - Not a "real" library for MECCANO / ERECTOR IR devices - just put it in the same folder as your sketch.
Created by TheDIYGuy999, Oct 2016, December 2017
Released into the public domain.
V1.2, including STM32 ARM support
*/
#ifndef MeccanoIr_h
#define MeccanoIr_h
#include "Arduino.h"
//
// =======================================================================================================
// IR CODE DEFINITION
// =======================================================================================================
//
// Meccano binary code: // means "1", no marking means "0"
// CHANNEL A *************************************************
// A + IR code---------------------------
const byte IrSignalAplus[] PROGMEM = {
// ON, OFF (in 10's of microseconds)
180, 180,
30, 60, //
30, 30,
30, 60, //
30, 30,
30, 30,
30, 30,
30, 60, //
30, 30,
30, 60, //
30, 30,
30, 30,
30, 30,
120, 240
};
// A - IR code---------------------------
const byte IrSignalAminus[] PROGMEM = {
// ON, OFF (in 10's of microseconds)
180, 180,
30, 30,
30, 60, //
30, 60, //
30, 30,
30, 30,
30, 30,
30, 30,
30, 60, //
30, 60, //
30, 30,
30, 30,
30, 30,
120, 240
};
// A OFF IR code---------------------------
const byte IrSignalAoff[] PROGMEM = {
// ON, OFF (in 10's of microseconds)
180, 180,
30, 30,
30, 30,
30, 60, //
30, 30,
30, 30,
30, 30,
30, 30,
30, 30,
30, 60, //
30, 30,
30, 30,
30, 30,
120, 240
};
// CHANNEL B *************************************************
// B + IR code---------------------------
const byte IrSignalBplus[] PROGMEM = {
// ON, OFF (in 10's of microseconds)
180, 180,
30, 60, //
30, 30,
30, 30,
30, 60, //
30, 30,
30, 30,
30, 60, //
30, 30,
30, 30,
30, 60, //
30, 30,
30, 30,
120, 240
};
// B - IR code---------------------------
const byte IrSignalBminus[] PROGMEM = {
// ON, OFF (in 10's of microseconds)
180, 180,
30, 30,
30, 60, //
30, 30,
30, 60, //
30, 30,
30, 30,
30, 30,
30, 60, //
30, 30,
30, 60, //
30, 30,
30, 30,
120, 240
};
// B OFF IR code---------------------------
const byte IrSignalBoff[] PROGMEM = {
// ON, OFF (in 10's of microseconds)
180, 180,
30, 30,
30, 30,
30, 30,
30, 60, //
30, 30,
30, 30,
30, 30,
30, 30,
30, 30,
30, 60, //
30, 30,
30, 30,
120, 240
};
// CHANNEL C *************************************************
// C + IR code---------------------------
const byte IrSignalCplus[] PROGMEM = {
// ON, OFF (in 10's of microseconds)
180, 180,
30, 60, //
30, 30,
30, 30,
30, 30,
30, 60, //
30, 30,
30, 60, //
30, 30,
30, 30,
30, 30,
30, 60, //
30, 30,
120, 240
};
// C - IR code---------------------------
const byte IrSignalCminus[] PROGMEM = {
// ON, OFF (in 10's of microseconds)
180, 180,
30, 30,
30, 60, //
30, 30,
30, 30,
30, 60, //
30, 30,
30, 30,
30, 60, //
30, 30,
30, 30,
30, 60, //
30, 30,
120, 240
};
// C OFF IR code---------------------------
const byte IrSignalCoff[] PROGMEM = {
// ON, OFF (in 10's of microseconds)
180, 180,
30, 30,
30, 30,
30, 30,
30, 30,
30, 60, //
30, 30,
30, 30,
30, 30,
30, 30,
30, 30,
30, 60, //
30, 30,
120, 240
};
// CHANNEL D *************************************************
// D + IR code---------------------------
const byte IrSignalDplus[] PROGMEM = {
// ON, OFF (in 10's of microseconds)
180, 180,
30, 60, //
30, 30,
30, 30,
30, 30,
30, 30,
30, 60, //
30, 60, //
30, 30,
30, 30,
30, 30,
30, 30,
30, 60, //
120, 240
};
// D - IR code---------------------------
const byte IrSignalDminus[] PROGMEM = {
// ON, OFF (in 10's of microseconds)
180, 180,
30, 30,
30, 60, //
30, 30,
30, 30,
30, 30,
30, 60, //
30, 30,
30, 60, //
30, 30,
30, 30,
30, 30,
30, 60, //
120, 240
};
// D OFF IR code---------------------------
const byte IrSignalDoff[] PROGMEM = {
// ON, OFF (in 10's of microseconds)
180, 180,
30, 30,
30, 30,
30, 30,
30, 30,
30, 30,
30, 60, //
30, 30,
30, 30,
30, 30,
30, 30,
30, 30,
30, 60, //
120, 240
};
//
// =======================================================================================================
// IR SIGNAL GENERATION
// =======================================================================================================
//
void buildIrSignal(byte channel) {
const byte s = 28; // The length of the IR code arrays
byte IrSignal[s];
// 1. Read the array from PROGMEM ******
for (int i = 0; i < s; i++) {
if (channel == 1) IrSignal[i] = pgm_read_byte(&IrSignalAplus[i]); // Channel A+
if (channel == 2) IrSignal[i] = pgm_read_byte(&IrSignalAminus[i]); // Channel A-
if (channel == 3) IrSignal[i] = pgm_read_byte(&IrSignalAoff[i]); // Channel A OFF
if (channel == 4) IrSignal[i] = pgm_read_byte(&IrSignalBplus[i]); // Channel B+
if (channel == 5) IrSignal[i] = pgm_read_byte(&IrSignalBminus[i]); // Channel B-
if (channel == 6) IrSignal[i] = pgm_read_byte(&IrSignalBoff[i]); // Channel B OFF
if (channel == 7) IrSignal[i] = pgm_read_byte(&IrSignalCplus[i]); // Channel C+
if (channel == 8) IrSignal[i] = pgm_read_byte(&IrSignalCminus[i]); // Channel C-
if (channel == 9) IrSignal[i] = pgm_read_byte(&IrSignalCoff[i]); // Channel C OFF
if (channel == 10) IrSignal[i] = pgm_read_byte(&IrSignalDplus[i]); // Channel D+
if (channel == 11) IrSignal[i] = pgm_read_byte(&IrSignalDminus[i]); // Channel D-
if (channel == 12) IrSignal[i] = pgm_read_byte(&IrSignalDoff[i]); // Channel D OFF
}
// 2. Generate the IR pulses ******
for (int i = 0; i < (s - 1); i = i + 2) {
// HIGH (38KHz modulated) ----
long microsecs = IrSignal[i] * 10;
while (microsecs > 0) {
#if defined (__STM32F1__) // STM32 ARM board ----
digitalWrite(PB5, HIGH); //Pin PB5 is hardcoded!
delayMicroseconds(12);
digitalWrite(PB5, LOW); //Pin PB5 is hardcoded!
delayMicroseconds(12);
// STM32 ARM board end ----
#else // AVR board ----
digitalWrite(3, HIGH); //Pin 3 is hardcoded!
#if F_CPU == 16000000 // 16MHz CPU
delayMicroseconds(10); // We don't want the delay on an 8MHz board!
#endif
digitalWrite(3, LOW); //Pin 3 is hardcoded!
#if F_CPU == 16000000 // 16MHz CPU
delayMicroseconds(10); // We don't want the delay on an 8MHz board!
#endif
#endif // AVR board end ----
microsecs -= 26; // 38 kHz is about 13 microseconds high and 13 microseconds low
}
// LOW ----
delayMicroseconds(IrSignal[i + 1] * 10);
} // End of for loop
delay(30);
}
#endif