-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArlecRC210Switch.cpp
60 lines (51 loc) · 1.69 KB
/
ArlecRC210Switch.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
#include <Arduino.h>
#include "ArlecRC210Switch.h"
void ArlecRC210Switch::begin(const int rf433OutputPin) {
pinMode(rf433OutputPin, OUTPUT);
_rf433OutputPin = rf433OutputPin;
}
void ArlecRC210Switch::setArlecSwitchState(const int switchId, const bool on, const int timesToSend) {
if(switchId < 0 || switchId > 7) {
return;
}
const int periodMicroseconds = 385;
auto sendArlecCycle = [&](int highCycles, int lowCycles) {
digitalWrite(_rf433OutputPin, HIGH);
delayMicroseconds(highCycles * periodMicroseconds);
digitalWrite(_rf433OutputPin, LOW);
delayMicroseconds(lowCycles * periodMicroseconds);
};
auto sendArlecBit = [&](bool high) {
sendArlecCycle(high ? 2 : 1, high ? 1 : 2);
};
auto sendArlecFrame = [&](const char * code) {
for(int i = 0; i != 32; ++i) {
sendArlecBit(code[i] == '1');
}
sendArlecCycle(1, 17);
};
auto writeBinary = [](int value, char* writeTo) {
for(int i = 2; i >= 0; --i) {
writeTo[i] = value & 1 ? '1' : '0';
value >>= 1;
}
};
// A = Fixed bit stream.
// B = Switch ID.
// C = on = 1, off = 0.
// D = Always 1001.
// E = Same as B.
// F = Inverse of C.
// G = 0.
// 0 1 2 3
// 012345678901234567890123456789012
// AAAAAAAAAAAAAAAAAAAABBBCDDDDEEEFG
char code[] = "011101101101100000000000100100000";
writeBinary(switchId, &code[20]);
writeBinary(switchId, &code[28]);
code[23] = on ? '1' : '0';
code[31] = on ? '0' : '1';
for(int i = 0; i != timesToSend; ++i) {
sendArlecFrame(code);
}
}