forked from mysensors/MySensors
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteTransmitter.cpp
More file actions
305 lines (236 loc) · 7.03 KB
/
RemoteTransmitter.cpp
File metadata and controls
305 lines (236 loc) · 7.03 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
/*
* RemoteSwitch library v2.3.0 (20121229) made by Randy Simons http://randysimons.nl/
* See RemoteTransmitter.h for details.
*
* License: GPLv3. See license.txt
*/
#include "RemoteTransmitter.h"
/************
* RemoteTransmitter
************/
RemoteTransmitter::RemoteTransmitter(byte pin, unsigned int periodusec, byte repeats) {
_pin=pin;
_periodusec=periodusec;
_repeats=repeats;
pinMode(_pin, OUTPUT);
}
unsigned long RemoteTransmitter::encodeTelegram(byte trits[]) {
unsigned long data = 0;
// Encode data
for (byte i=0;i<12;i++) {
data*=3;
data+=trits[i];
}
// Encode period duration
data |= (unsigned long)_periodusec << 23;
// Encode repeats
data |= (unsigned long)_repeats << 20;
return data;
}
void RemoteTransmitter::sendTelegram(byte trits[]) {
sendTelegram(encodeTelegram(trits),_pin);
}
/**
* Format data:
* pppppppp|prrrdddd|dddddddd|dddddddd (32 bit)
* p = perioud (9 bit unsigned int
* r = repeats as 2log. Thus, if r = 3, then signal is sent 2^3=8 times
* d = data
*/
void RemoteTransmitter::sendTelegram(unsigned long data, byte pin) {
unsigned int periodusec = (unsigned long)data >> 23;
byte repeats = ((unsigned long)data >> 20) & B111;
sendCode(pin, data, periodusec, repeats);
}
void RemoteTransmitter::sendCode(byte pin, unsigned long code, unsigned int periodusec, byte repeats) {
code &= 0xfffff; // Truncate to 20 bit ;
// Convert the base3-code to base4, to avoid lengthy calculations when transmitting.. Messes op timings.
// Also note this swaps endianess in the process. The MSB must be transmitted first, but is converted to
// LSB here. This is easier when actually transmitting later on.
unsigned long dataBase4 = 0;
for (byte i=0; i<12; i++) {
dataBase4<<=2;
dataBase4|=(code%3);
code/=3;
}
repeats = 1 << (repeats & B111); // repeats := 2^repeats;
for (byte j=0;j<repeats;j++) {
// Sent one telegram
// Recycle code as working var to save memory
code=dataBase4;
for (byte i=0; i<12; i++) {
switch (code & B11) {
case 0:
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec*3);
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec*3);
break;
case 1:
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec*3);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec);
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec*3);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec);
break;
case 2: // KA: X or float
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec*3);
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec*3);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec);
break;
}
// Next trit
code>>=2;
}
// Send termination/synchronization-signal. Total length: 32 periods
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec*31);
}
}
boolean RemoteTransmitter::isSameCode(unsigned long encodedTelegram, unsigned long receivedData) {
return (receivedData==(encodedTelegram & 0xFFFFF)); // compare the 20 LSB's
}
/************
* ActionTransmitter
************/
ActionTransmitter::ActionTransmitter(byte pin, unsigned int periodusec, byte repeats) : RemoteTransmitter(pin,periodusec,repeats) {
// Call constructor
}
void ActionTransmitter::sendSignal(byte systemCode, char device, boolean on) {
sendTelegram(getTelegram(systemCode,device,on), _pin);
}
unsigned long ActionTransmitter::getTelegram(byte systemCode, char device, boolean on) {
byte trits[12];
device-=65;
for (byte i=0; i<5; i++) {
// Trits 0-4 contain address (2^5=32 addresses)
trits[i]=(systemCode & 1)?1:2;
systemCode>>=1;
// Trits 5-9 contain device. Only one trit has value 0, others have 2 (float)!
trits[i+5]=(i==device?0:2);
}
// Switch on or off
trits[10]=(!on?0:2);
trits[11]=(on?0:2);
return encodeTelegram(trits);
}
/************
* BlokkerTransmitter
************/
BlokkerTransmitter::BlokkerTransmitter(byte pin, unsigned int periodusec, byte repeats) : RemoteTransmitter(pin,periodusec,repeats) {
// Call constructor
}
void BlokkerTransmitter::sendSignal(byte device, boolean on) {
sendTelegram(getTelegram(device,on), _pin);
}
unsigned long BlokkerTransmitter::getTelegram(byte device, boolean on) {
byte trits[12]={0};
device--;
for (byte i=1; i<4; i++) {
// Trits 1-3 contain device
trits[i]=(device & 1)?0:1;
device>>=1;
}
// Switch on or off
trits[8]=(on?1:0);
return encodeTelegram(trits);
}
/************
* KaKuTransmitter
************/
KaKuTransmitter::KaKuTransmitter(byte pin, unsigned int periodusec, byte repeats) : RemoteTransmitter(pin,periodusec,repeats) {
// Call constructor
}
void KaKuTransmitter::sendSignal(char address, byte device, boolean on) {
sendTelegram(getTelegram(address, device, on), _pin);
}
unsigned long KaKuTransmitter::getTelegram(char address, byte device, boolean on) {
byte trits[12];
address-=65;
device-=1;
for (byte i=0; i<4; i++) {
// Trits 0-3 contain address (2^4 = 16 addresses)
trits[i]=(address & 1)?2:0;
address>>=1;
// Trits 4-8 contain device (2^4 = 16 addresses)
trits[i+4]=(device & 1)?2:0;
device>>=1;
}
// Trits 8-10 seem to be fixed
trits[8]=0;
trits[9]=2;
trits[10]=2;
// Switch on or off
trits[11]=(on?2:0);
return encodeTelegram(trits);
}
void KaKuTransmitter::sendSignal(char address, byte group, byte device, boolean on) {
sendTelegram(getTelegram(address, group, on), _pin);
}
unsigned long KaKuTransmitter::getTelegram(char address, byte group, byte device, boolean on) {
byte trits[12], i;
address-=65;
group-=1;
device-=1;
// Address. M3E Pin A0-A3
for (i=0; i<4; i++) {
// Trits 0-3 contain address (2^4 = 16 addresses)
trits[i]=(address & 1)?2:0;
address>>=1;
}
// Device. M3E Pin A4-A5
for (; i<6; i++) {
trits[i]=(device & 1)?2:0;
device>>=1;
}
// Group. M3E Pin A6-A7
for (; i<8; i++) {
trits[i]=(group & 1)?2:0;
group>>=1;
}
// Trits 8-10 are be fixed. M3E Pin A8/D0-A10/D2
trits[8]=0;
trits[9]=2;
trits[10]=2;
// Switch on or off, M3E Pin A11/D3
trits[11]=(on?2:0);
return encodeTelegram(trits);
}
/************
* ElroTransmitter
************/
ElroTransmitter::ElroTransmitter(byte pin, unsigned int periodusec, byte repeats) : RemoteTransmitter(pin, periodusec, repeats) {
//Call constructor
}
void ElroTransmitter::sendSignal(byte systemCode, char device, boolean on) {
sendTelegram(getTelegram(systemCode, device, on), _pin);
}
unsigned long ElroTransmitter::getTelegram(byte systemCode, char device, boolean on) {
byte trits[12];
device-=65;
for (byte i=0; i<5; i++) {
//trits 0-4 contain address (2^5=32 addresses)
trits[i]=(systemCode & 1)?0:2;
systemCode>>=1;
//trits 5-9 contain device. Only one trit has value 0, others have 2 (float)!
trits[i+5]=(i==device?0:2);
}
//switch on or off
trits[10]=(on?0:2);
trits[11]=(!on?0:2);
return encodeTelegram(trits);
}