forked from bschwind/ir-slinger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirslinger.h
356 lines (314 loc) · 8.06 KB
/
irslinger.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#ifndef IRSLINGER_H
#define IRSLINGER_H
#include <string.h>
#include <math.h>
#include <pigpio.h>
#include <stdio.h>
#define MAX_COMMAND_SIZE 512
#define MAX_PULSES 12000
static inline void addPulse(uint32_t onPins, uint32_t offPins, uint32_t duration, gpioPulse_t *irSignal, unsigned int *pulseCount)
{
int index = *pulseCount;
irSignal[index].gpioOn = onPins;
irSignal[index].gpioOff = offPins;
irSignal[index].usDelay = duration;
(*pulseCount)++;
}
// Generates a square wave for duration (microseconds) at frequency (Hz)
// on GPIO pin outPin. dutyCycle is a floating value between 0 and 1.
static inline void carrierFrequency(uint32_t outPin, double frequency, double dutyCycle, double duration, gpioPulse_t *irSignal, unsigned int *pulseCount)
{
double oneCycleTime = 1000000.0 / frequency; // 1000000 microseconds in a second
int onDuration = (int)round(oneCycleTime * dutyCycle);
int offDuration = (int)round(oneCycleTime * (1.0 - dutyCycle));
int totalCycles = (int)round(duration / oneCycleTime);
int totalPulses = totalCycles * 2;
int i;
for (i = 0; i < totalPulses; i++)
{
if (i % 2 == 0)
{
// High pulse
addPulse(1 << outPin, 0, onDuration, irSignal, pulseCount);
}
else
{
// Low pulse
addPulse(0, 1 << outPin, offDuration, irSignal, pulseCount);
}
}
}
// bitnum is 0 based
static inline int getbit(const char * code, int bitnum, int totalLength)
{
if (code[0] == '0' && code[1] == 'x') {
size_t digitofs = strlen(code);
// find the character/nibble offset from the end of the string
// i.e. the final bit to transmit is the lsb of the final digit
int nibblebit = totalLength - 1 - bitnum;
digitofs -= 1+((nibblebit)/4);
// check the string is long enough
if (digitofs < 2)
{
printf("Code %s is not long enough for %d bits\n",code,totalLength);
return -1;
}
char x = code[digitofs];
int n;
if (x >= '0' && x<= '9') {
n=x - '0';
} else if (x >= 'a' && x<='f') {
n=x - 'a' + 10;
} else if (x >= 'A' && x<='F') {
n=x - 'A' + 10;
} else {
printf("Character %c is not valid hex\n",x);
return -1;
}
int ret = (n & (1<<(nibblebit%4)));
//printf("code %s bitnum %d totlen %d => nibblebit %d digitofs %d => %d\n",code,bitnum,totalLength,nibblebit,digitofs,ret);
return (ret>0)?1:0;
}
if (code[bitnum] == '0') {
return 0;
}
if (code[bitnum] == '1') {
return 1;
}
printf("Do not recognise bit code %c at position %d\n",code[bitnum],bitnum);
return -1;
}
// Generates a low signal gap for duration, in microseconds, on GPIO pin outPin
static inline void gap(uint32_t outPin, double duration, gpioPulse_t *irSignal, unsigned int *pulseCount)
{
addPulse(0, 0, duration, irSignal, pulseCount);
}
// Transmit generated wave
static inline int transmitWavePre(uint32_t outPin)
{
if (outPin > 31)
{
// Invalid pin number
return 1;
}
// Init pigpio
if (gpioInitialise() < 0)
{
// Initialization failed
printf("GPIO Initialization failed\n");
return 1;
}
// Setup the GPIO pin as an output pin
gpioSetMode(outPin, PI_OUTPUT);
return 0;
}
static inline int transmitWave(gpioPulse_t *irSignal, unsigned int pulseCount)
{
// Start a new wave
gpioWaveClear();
gpioWaveAddGeneric(pulseCount, irSignal);
int waveID = gpioWaveCreate();
if (waveID >= 0)
{
int result = gpioWaveTxSend(waveID, PI_WAVE_MODE_ONE_SHOT);
//printf("Result: %i\n", result);
}
else
{
printf("Wave creation failure!\n %i", waveID);
return 1;
}
// Wait for the wave to finish transmitting
while (gpioWaveTxBusy())
{
time_sleep(0.1);
}
// Delete the wave if it exists
if (waveID >= 0)
{
gpioWaveDelete(waveID);
}
return 0;
}
static inline int transmitWavePost()
{
// Cleanup
gpioTerminate();
return 0;
}
static inline int irSlingPrepareRC5(gpioPulse_t *irSignal,
unsigned int * pulseCount,
int outPin,
int frequency,
double dutyCycle,
int pulseDuration,
const char *code,
size_t codeLen)
{
if (codeLen > MAX_COMMAND_SIZE)
{
// Command is too big
return 1;
}
// Generate Code
int i;
for (i = 0; i < codeLen; i++)
{
switch (getbit(code,i,codeLen))
{
case 0:
carrierFrequency(outPin, frequency, dutyCycle, pulseDuration, irSignal, pulseCount);
gap(outPin, pulseDuration, irSignal, pulseCount);
break;
case 1:
gap(outPin, pulseDuration, irSignal, pulseCount);
carrierFrequency(outPin, frequency, dutyCycle, pulseDuration, irSignal, pulseCount);
break;
}
}
//printf("pulse count is %i\n", *pulseCount);
// End Generate Code
}
static inline int irSlingRC5(uint32_t outPin,
int frequency,
double dutyCycle,
int pulseDuration,
const char *code)
{
gpioPulse_t irSignal[MAX_PULSES];
unsigned int pulseCount = 0;
size_t codeLen = strlen(code);
if (code[0]=='0' && code[1]=='x') {
codeLen=(codeLen-2)*4;
}
printf("code size is %zu\n", codeLen);
if (irSlingPrepareRC5(irSignal, &pulseCount, outPin, frequency, dutyCycle, pulseDuration, code, codeLen)) return 1;
int ret = 1;
if (transmitWavePre(outPin)) return 1;
ret = transmitWave(irSignal, pulseCount);
transmitWavePost();
return ret;
}
static inline int irSlingPrepare(gpioPulse_t *irSignal, unsigned int * pulseCount, uint32_t outPin,
int frequency,
double dutyCycle,
int leadingPulseDuration,
int leadingGapDuration,
int onePulse,
int zeroPulse,
int oneGap,
int zeroGap,
int sendTrailingPulse,
const char *code,
size_t codeLen)
{
if (outPin > 31)
{
// Invalid pin number
return 1;
}
// Generate Code
// insert header
if (leadingPulseDuration > 0)
{
carrierFrequency(outPin, frequency, dutyCycle, leadingPulseDuration, irSignal, pulseCount);
gap(outPin, leadingGapDuration, irSignal, pulseCount);
}
int i;
for (i = 0; i < codeLen; i++)
{
switch (getbit(code,i,codeLen))
{
case 0:
carrierFrequency(outPin, frequency, dutyCycle, zeroPulse, irSignal, pulseCount);
gap(outPin, zeroGap, irSignal, pulseCount);
break;
case 1:
carrierFrequency(outPin, frequency, dutyCycle, onePulse, irSignal, pulseCount);
gap(outPin, oneGap, irSignal, pulseCount);
break;
}
}
if (sendTrailingPulse==1)
{
carrierFrequency(outPin, frequency, dutyCycle, onePulse, irSignal, pulseCount);
}
else if (sendTrailingPulse > 0)
{
carrierFrequency(outPin, frequency, dutyCycle, sendTrailingPulse, irSignal, pulseCount);
}
//printf("pulse count is %i\n", *pulseCount);
// End Generate Code
return 0;
}
static inline int irSling(uint32_t outPin,
int frequency,
double dutyCycle,
int leadingPulseDuration,
int leadingGapDuration,
int onePulse,
int zeroPulse,
int oneGap,
int zeroGap,
int sendTrailingPulse,
const char *code)
{
size_t codeLen = strlen(code);
if (code[0]=='0' && code[1]=='x') {
codeLen=(codeLen-2)*4;
}
printf("code size is %zu\n", codeLen);
if (codeLen > MAX_COMMAND_SIZE)
{
// Command is too big
return 1;
}
gpioPulse_t irSignal[MAX_PULSES];
unsigned int pulseCount = 0;
if (irSlingPrepare(irSignal, &pulseCount, outPin, frequency, dutyCycle, leadingPulseDuration, leadingGapDuration, onePulse, zeroPulse, oneGap, zeroGap, sendTrailingPulse, code, codeLen)) return 1;
int ret = 1;
if (transmitWavePre(outPin)) return 1;
ret = transmitWave(irSignal, pulseCount);
transmitWavePost();
return ret;
}
static inline int irSlingPrepareRaw(gpioPulse_t * irSignal, unsigned int * pulseCount, uint32_t outPin,
int frequency,
double dutyCycle,
const int *pulses,
int numPulses)
{
if (outPin > 31)
{
// Invalid pin number
return 1;
}
int i;
for (i = 0; i < numPulses; i++)
{
if (i % 2 == 0) {
carrierFrequency(outPin, frequency, dutyCycle, pulses[i], irSignal, pulseCount);
} else {
gap(outPin, pulses[i], irSignal, pulseCount);
}
}
//printf("pulse count is %i from %i\n", *pulseCount, numPulses);
// End Generate Code
return 0;
}
static inline int irSlingRaw(uint32_t outPin,
int frequency,
double dutyCycle,
const int *pulses,
int numPulses)
{
gpioPulse_t irSignal[MAX_PULSES];
unsigned int pulseCount = 0;
if (irSlingPrepareRaw(irSignal, &pulseCount, outPin, frequency, dutyCycle, pulses, numPulses)) return 1;
if (transmitWavePre(outPin)) return 1;
int ret = 1;
ret = transmitWave(irSignal, pulseCount);
transmitWavePost();
return ret;
}
#endif