-
Notifications
You must be signed in to change notification settings - Fork 30
/
irslinger.h
251 lines (209 loc) · 5.19 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
#ifndef IRSLINGER_H
#define IRSLINGER_H
#include <string.h>
#include <math.h>
#include <pigpio.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, 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, 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);
}
}
}
// 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, int *pulseCount)
{
addPulse(0, 0, duration, irSignal, pulseCount);
}
// Transmit generated wave
static inline int transmitWave(uint32_t outPin, gpioPulse_t *irSignal, unsigned int *pulseCount)
{
// 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);
// 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);
}
// Wait for the wave to finish transmitting
while (gpioWaveTxBusy())
{
time_sleep(0.1);
}
// Delete the wave if it exists
if (waveID >= 0)
{
gpioWaveDelete(waveID);
}
// Cleanup
gpioTerminate();
return 0;
}
static inline int irSlingRC5(uint32_t outPin,
int frequency,
double dutyCycle,
int pulseDuration,
const char *code)
{
if (outPin > 31)
{
// Invalid pin number
return 1;
}
size_t codeLen = strlen(code);
printf("code size is %zu\n", codeLen);
if (codeLen > MAX_COMMAND_SIZE)
{
// Command is too big
return 1;
}
gpioPulse_t irSignal[MAX_PULSES];
int pulseCount = 0;
// Generate Code
int i;
for (i = 0; i < codeLen; i++)
{
if (code[i] == '0')
{
carrierFrequency(outPin, frequency, dutyCycle, pulseDuration, irSignal, &pulseCount);
gap(outPin, pulseDuration, irSignal, &pulseCount);
}
else if (code[i] == '1')
{
gap(outPin, pulseDuration, irSignal, &pulseCount);
carrierFrequency(outPin, frequency, dutyCycle, pulseDuration, irSignal, &pulseCount);
}
else
{
printf("Warning: Non-binary digit in command\n");
}
}
printf("pulse count is %i\n", pulseCount);
// End Generate Code
return transmitWave(outPin, irSignal, &pulseCount);
}
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)
{
if (outPin > 31)
{
// Invalid pin number
return 1;
}
size_t codeLen = strlen(code);
printf("code size is %zu\n", codeLen);
if (codeLen > MAX_COMMAND_SIZE)
{
// Command is too big
return 1;
}
gpioPulse_t irSignal[MAX_PULSES];
int pulseCount = 0;
// Generate Code
carrierFrequency(outPin, frequency, dutyCycle, leadingPulseDuration, irSignal, &pulseCount);
gap(outPin, leadingGapDuration, irSignal, &pulseCount);
int i;
for (i = 0; i < codeLen; i++)
{
if (code[i] == '0')
{
carrierFrequency(outPin, frequency, dutyCycle, zeroPulse, irSignal, &pulseCount);
gap(outPin, zeroGap, irSignal, &pulseCount);
}
else if (code[i] == '1')
{
carrierFrequency(outPin, frequency, dutyCycle, onePulse, irSignal, &pulseCount);
gap(outPin, oneGap, irSignal, &pulseCount);
}
else
{
printf("Warning: Non-binary digit in command\n");
}
}
if (sendTrailingPulse)
{
carrierFrequency(outPin, frequency, dutyCycle, onePulse, irSignal, &pulseCount);
}
printf("pulse count is %i\n", pulseCount);
// End Generate Code
return transmitWave(outPin, irSignal, &pulseCount);
}
static inline int irSlingRaw(uint32_t outPin,
int frequency,
double dutyCycle,
const int *pulses,
int numPulses)
{
if (outPin > 31)
{
// Invalid pin number
return 1;
}
// Generate Code
gpioPulse_t irSignal[MAX_PULSES];
int pulseCount = 0;
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\n", pulseCount);
// End Generate Code
return transmitWave(outPin, irSignal, &pulseCount);
}
#endif