-
Notifications
You must be signed in to change notification settings - Fork 1
/
RemoteSwitch.cpp
310 lines (234 loc) · 6.77 KB
/
RemoteSwitch.cpp
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
/*
* RemoteSwitch library v2.0.0 made by Randy Simons http://randysimons.nl
* See RemoteSwitchSender.h for details.
*
* License: "Free BSD license". See license.txt
*/
#include "RemoteSwitch.h"
/************
* RemoteSwitch
************/
RemoteSwitch::RemoteSwitch(unsigned short pin, unsigned int periodusec, unsigned short repeats) {
_pin=pin;
_periodusec=periodusec;
_repeats=repeats;
pinMode(_pin, OUTPUT);
}
unsigned long RemoteSwitch::encodeTelegram(unsigned short trits[]) {
unsigned long data = 0;
//Encode data
for (unsigned short 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 RemoteSwitch::sendTelegram(unsigned short 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 RemoteSwitch::sendTelegram(unsigned long data, unsigned short pin) {
unsigned int periodusec = (unsigned long)data >> 23;
unsigned short repeats = 5 << (((unsigned long)data >> 20) & 7); // 7 = B111
data = data & 0xfffff; //truncate to 20 bit
//Convert the base3-code to base4, to avoid lengthy calculations when transmitting.. Messes op timings.
unsigned long dataBase4 = 0;
for (unsigned short i=0; i<12; i++) {
dataBase4<<=2;
dataBase4|=(data%3);
data/=3;
}
for (unsigned short int j=0;j<repeats;j++) {
//Sent one telegram
//Use data-var as working var
data=dataBase4;
for (unsigned short i=0; i<12; i++) {
switch (data & 3) { // 3 = 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: //AKA: 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
data>>=2;
}
//Send termination/synchronisation-signal. Total length: 32 periods
digitalWrite(pin, HIGH);
delayMicroseconds(periodusec);
digitalWrite(pin, LOW);
delayMicroseconds(periodusec*31);
}
}
bool RemoteSwitch::isSameCode(unsigned long encodedTelegram, unsigned long receivedData) {
return (receivedData==(encodedTelegram & 0xFFFFF)); //Compare the 20 LSB's
}
/************
* ElroSwitch
************/
ElroSwitch::ElroSwitch(unsigned short pin, unsigned int periodusec) : RemoteSwitch(pin,periodusec,3) {
//Call contructor
}
void ElroSwitch::sendSignal(unsigned short systemCode, char device, bool on) {
sendTelegram(getTelegram(systemCode,device,on), _pin);
}
unsigned long ElroSwitch::getTelegram(unsigned short systemCode, char device, bool on) {
unsigned short trits[15];
device-=65;
for (unsigned short i=0; i<5; i++) {
//bits 0-4 contain address (2^5=32 addresses)
trits[i]=(systemCode & 1)?0:2;
systemCode>>=1;
//bits 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);
trits[12]=0;
trits[13]=2;
trits[14]=2;
trits[15]=2;
return encodeTelegram(trits);
}
/************
* ActionSwitch
************/
ActionSwitch::ActionSwitch(unsigned short pin, unsigned int periodusec) : RemoteSwitch(pin,periodusec,3) {
//Call contructor
}
void ActionSwitch::sendSignal(unsigned short systemCode, char device, bool on) {
sendTelegram(getTelegram(systemCode,device,on), _pin);
}
unsigned long ActionSwitch::getTelegram(unsigned short systemCode, char device, bool on) {
unsigned short trits[12];
device-=65;
for (unsigned short i=0; i<5; i++) {
//bits 0-4 contain address (2^5=32 addresses)
trits[i]=(systemCode & 1)?1:2;
systemCode>>=1;
//bits 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);
}
/************
* BlokkerSwitch
************/
BlokkerSwitch::BlokkerSwitch(unsigned short pin, unsigned int periodusec) : RemoteSwitch(pin,periodusec,3) {
//Call contructor
}
void BlokkerSwitch::sendSignal(unsigned short device, bool on) {
sendTelegram(getTelegram(device,on), _pin);
}
unsigned long BlokkerSwitch::getTelegram(unsigned short device, bool on) {
unsigned short trits[12]={0};
device--;
for (unsigned short i=1; i<4; i++) {
//Bits 1-3 contain device
trits[i]=(device & 1)?0:1;
device>>=1;
}
//switch on or off
trits[8]=(on?1:0);
return encodeTelegram(trits);
}
/************
* KaKuSwitch
************/
KaKuSwitch::KaKuSwitch(unsigned short pin, unsigned int periodusec) : RemoteSwitch(pin,periodusec,3) {
//Call contructor
}
void KaKuSwitch::sendSignal(char address, unsigned short device, bool on) {
sendTelegram(getTelegram(address, device, on), _pin);
}
unsigned long KaKuSwitch::getTelegram(char address, unsigned short device, bool on) {
unsigned short trits[12];
address-=65;
device-=1;
for (unsigned short i=0; i<4; i++) {
//bits 0-3 contain address (2^4 = 16 addresses)
trits[i]=(address & 1)?2:0;
address>>=1;
//bits 4-8 contain device (2^4 = 16 addresses)
trits[i+4]=(device & 1)?2:0;
device>>=1;
}
//bits 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 KaKuSwitch::sendSignal(char address, unsigned short group, unsigned short device, bool on) {
sendTelegram(getTelegram(address, group, on), _pin);
}
unsigned long KaKuSwitch::getTelegram(char address, unsigned short group, unsigned short device, bool on) {
unsigned short trits[12], i;
address-=65;
group-=1;
device-=1;
//address. M3E Pin A0-A3
for (i=0; i<4; i++) {
//bits 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;
}
//bits 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);
}