forked from dmslabsbr/esphome-somfy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SomfyRts.cpp
237 lines (208 loc) · 6.31 KB
/
SomfyRts.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
#include "SomfyRts.h"
#include <FS.h>
#include <LITTLEFS.h>
SomfyRts::SomfyRts(uint32_t remoteID, bool debug) {
_debug = debug;
_remoteId = remoteID;
}
SomfyRts::SomfyRts(uint32_t remoteID) {
_debug = false;
_remoteId = remoteID;
}
void SomfyRts::init() {
pinMode(REMOTE_TX_PIN, OUTPUT);
digitalWrite(REMOTE_TX_PIN, LOW);
rollingCode = _readRemoteRollingCode();
if (rollingCode == 0 ) {
// Make rolling code set to work.
if (rollingCode < newRollingCode) {
_writeRemoteRollingCode(newRollingCode);
}
}
if (Serial) {
Serial.print("Simulated remote number : "); Serial.println(_remoteId, HEX);
Serial.print("Current rolling code : "); Serial.println(rollingCode);
}
}
void SomfyRts::buildFrame(unsigned char *frame, unsigned char button) {
unsigned int code = _readRemoteRollingCode();
frame[0] = 0xA7; // Encryption key. Doesn't matter much
frame[1] = button << 4; // Which button did you press? The 4 LSB will be the checksum
frame[2] = code >> 8; // Rolling code (big endian)
frame[3] = code; // Rolling code
frame[4] = _remoteId >> 16; // Remote address
frame[5] = _remoteId >> 8; // Remote address
frame[6] = _remoteId; // Remote address
if (Serial) {
Serial.print("Frame : ");
for(byte i = 0; i < 7; i++) {
if(frame[i] >> 4 == 0) { // Displays leading zero in case the most significant
Serial.print("0"); // nibble is a 0.
}
Serial.print(frame[i],HEX); Serial.print(" ");
}
}
// Checksum calculation: a XOR of all the nibbles
checksum = 0;
for(byte i = 0; i < 7; i++) {
checksum = checksum ^ frame[i] ^ (frame[i] >> 4);
}
checksum &= 0b1111; // We keep the last 4 bits only
//Checksum integration
frame[1] |= checksum; // If a XOR of all the nibbles is equal to 0, the blinds will
// consider the checksum ok.
if (Serial) {
Serial.println(""); Serial.print("With checksum : ");
for(byte i = 0; i < 7; i++) {
if(frame[i] >> 4 == 0) {
Serial.print("0");
}
Serial.print(frame[i],HEX); Serial.print(" ");
}
}
// Obfuscation: a XOR of all the bytes
for(byte i = 1; i < 7; i++) {
frame[i] ^= frame[i-1];
}
if (Serial) {
Serial.println(""); Serial.print("Obfuscated : ");
for(byte i = 0; i < 7; i++) {
if(frame[i] >> 4 == 0) {
Serial.print("0");
}
Serial.print(frame[i],HEX); Serial.print(" ");
}
Serial.println("");
Serial.print("Rolling Code : "); Serial.println(code);
}
// We store the value of the rolling code in the FS
_writeRemoteRollingCode(code + 1);
}
void SomfyRts::sendCommand(unsigned char *frame, unsigned char sync) {
if(sync == 2) { // Only with the first frame.
// Wake-up pulse & Silence
digitalWrite(REMOTE_TX_PIN, HIGH);
delayMicroseconds(9415);
digitalWrite(REMOTE_TX_PIN, LOW);
delayMicroseconds(89565);
}
// Hardware sync: two sync for the first frame, seven for the following ones.
for (int i = 0; i < sync; i++) {
digitalWrite(REMOTE_TX_PIN, HIGH);
delayMicroseconds(4*SYMBOL);
digitalWrite(REMOTE_TX_PIN, LOW);
delayMicroseconds(4*SYMBOL);
}
// Software sync
digitalWrite(REMOTE_TX_PIN, HIGH);
delayMicroseconds(4550);
digitalWrite(REMOTE_TX_PIN, LOW);
delayMicroseconds(SYMBOL);
//Data: bits are sent one by one, starting with the MSB.
for(byte i = 0; i < 56; i++) {
if(((frame[i/8] >> (7 - (i%8))) & 1) == 1) {
digitalWrite(REMOTE_TX_PIN, LOW);
delayMicroseconds(SYMBOL);
// PORTD ^= 1<<5;
digitalWrite(REMOTE_TX_PIN, HIGH);
delayMicroseconds(SYMBOL);
}
else {
digitalWrite(REMOTE_TX_PIN, HIGH);
delayMicroseconds(SYMBOL);
// PORTD ^= 1<<5;
digitalWrite(REMOTE_TX_PIN, LOW);
delayMicroseconds(SYMBOL);
}
}
digitalWrite(REMOTE_TX_PIN, LOW);
delayMicroseconds(30415); // Inter-frame silence
}
void SomfyRts::sendCommandUp() {
buildFrame(_frame, HAUT);
sendCommand(_frame, 2);
for(int i = 0; i<2; i++) {
sendCommand(_frame, 7);
}
}
void SomfyRts::sendCommandDown() {
buildFrame(_frame, BAS);
sendCommand(_frame, 2);
for(int i = 0; i<2; i++) {
sendCommand(_frame, 7);
}
}
void SomfyRts::sendCommandStop() {
buildFrame(_frame, STOP);
sendCommand(_frame, 2);
for(int i = 0; i<2; i++) {
sendCommand(_frame, 7);
}
}
void SomfyRts::sendCommandProg() {
buildFrame(_frame, PROG);
sendCommand(_frame, 2);
for(int i = 0; i<2; i++) {
sendCommand(_frame, 7);
}
}
// support for grail is limited: I can only pair one remote
void SomfyRts::sendCommandProgGrail() {
buildFrame(_frame, PROG);
sendCommand(_frame, 2);
for(int i = 0; i<35; i++) {
sendCommand(_frame, 7);
yield();
}
for(int i = 0; i<40; i++) {
delayMicroseconds(100000);
yield();
}
sendCommandProg();
}
uint16_t SomfyRts::_readRemoteRollingCode() {
uint16_t code = 0;
if (!LittleFS.begin()) {
Serial.println("Failed to mount file system");
}
if (LittleFS.exists(_getConfigFilename())) {
Serial.println("Reading config");
File f = LittleFS.open(_getConfigFilename(), "r");
if (f) {
String line = f.readStringUntil('\n');
code = line.toInt();
f.close();
}
else {
Serial.println("File open failed");
}
}
// mudar
// if (_remoteId==1184513) code=0;
LittleFS.end();
return code;
}
void SomfyRts::_writeRemoteRollingCode(uint16_t code) {
if (!LittleFS.begin()) {
Serial.println("Failed to mount file system");
}
Serial.println("Writing config");
File f = LittleFS.open(_getConfigFilename(), "w");
if (f) {
f.println(code);
f.close();
Serial.print("Written code:");
Serial.println(code);
}
else {
Serial.println("File creation failed");
}
LittleFS.end();
}
String SomfyRts::_getConfigFilename() {
String path = "/data/remote/";
path += _remoteId;
path += ".txt";
Serial.println(path);
return path;
}