-
Notifications
You must be signed in to change notification settings - Fork 47
/
com.ino
530 lines (462 loc) · 11.2 KB
/
com.ino
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
* STC1000+, improved firmware and Arduino based firmware uploader for the STC-1000 dual stage thermostat.
*
* Copyright 2014 Mats Staffansson
*
* This file is part of STC1000+.
*
* STC1000+ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* STC1000+ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with STC1000+. If not, see <http://www.gnu.org/licenses/>.
*
*/
#define COM_PIN 9 // ICSPCLK
#define COM_READ_EEPROM 0x20
#define COM_WRITE_EEPROM 0xE0
#define COM_READ_TEMP 0x01
#define COM_READ_COOLING 0x02
#define COM_READ_HEATING 0x03
#define COM_ACK 0x9A
#define COM_NACK 0x66
void write_bit(unsigned const char data){
pinMode(COM_PIN, OUTPUT);
digitalWrite(COM_PIN, HIGH);
delayMicroseconds(7);
if(!data){
pinMode(COM_PIN, INPUT);
digitalWrite(COM_PIN, LOW);
}
delayMicroseconds(400);
pinMode(COM_PIN, INPUT);
digitalWrite(COM_PIN, LOW);
delayMicroseconds(100);
}
unsigned char read_bit(){
unsigned char data;
pinMode(COM_PIN, OUTPUT);
digitalWrite(COM_PIN, HIGH);
delayMicroseconds(7);
pinMode(COM_PIN, INPUT);
digitalWrite(COM_PIN, LOW);
delayMicroseconds(200);
data = digitalRead(COM_PIN);
delayMicroseconds(300);
return data;
}
void write_byte(unsigned const char data){
unsigned char i;
for(i=0;i<8;i++){
write_bit(((data << i) & 0x80));
}
delayMicroseconds(500);
}
unsigned char read_byte(){
unsigned char i, data;
for(i=0;i<8;i++){
data <<= 1;
if(read_bit()){
data |= 1;
}
}
delayMicroseconds(500);
return data;
}
bool write_eeprom(const unsigned char address, unsigned const int value){
unsigned char ack;
write_byte(COM_WRITE_EEPROM);
write_byte(address);
write_byte(((unsigned char)(value >> 8)));
write_byte((unsigned char)value);
write_byte(COM_WRITE_EEPROM ^ address ^ ((unsigned char)(value >> 8)) ^ ((unsigned char)value));
delay(6); // Longer delay needed here for EEPROM write to finish, but must be shorter than 10ms
ack = read_byte();
return ack == COM_ACK;
}
bool read_eeprom(const unsigned char address, int *value){
unsigned char xorsum;
unsigned char ack;
unsigned int data;
write_byte(COM_READ_EEPROM);
write_byte(address);
data = read_byte();
data = (data << 8) | read_byte();
xorsum = read_byte();
ack = read_byte();
if(ack == COM_ACK && xorsum == (COM_READ_EEPROM ^ address ^ ((unsigned char)(data >> 8)) ^ ((unsigned char)data))){
*value = (int)data;
return true;
}
return false;
}
bool read_command(unsigned char command, int *value){
unsigned char xorsum;
unsigned char ack;
unsigned int data;
write_byte(command);
data = read_byte();
data = (data << 8) | read_byte();
xorsum = read_byte();
ack = read_byte();
if(ack == COM_ACK && xorsum == (command ^ ((unsigned char)(data >> 8)) ^ ((unsigned char)data))){
*value = (int)data;
return true;
}
return false;
}
bool read_temp(int *temperature){
return read_command(COM_READ_TEMP, temperature);
}
bool read_heating(int *heating){
return read_command(COM_READ_HEATING, heating);
}
bool read_cooling(int *cooling){
return read_command(COM_READ_COOLING, cooling);
}
/* End of communication implementation */
/* From here example implementation begins, this can be exchanged for your specific needs */
enum set_menu_enum {
setpoint, // SP (setpoint)
hysteresis, // hy (hysteresis)
temperature_correction, // tc (temperature correction)
setpoint_alarm, // SA (setpoint alarm)
step, // St (current running profile step)
duration, // dh (current running profile step duration in hours)
cooling_delay, // cd (cooling delay minutes)
heating_delay, // hd (heating delay minutes)
ramping, // rP (0=disable, 1=enable ramping)
run_mode // rn (0-5 run profile, 6=thermostat)
};
/* Defines for EEPROM config addresses */
#define EEADR_PROFILE_SETPOINT(profile, stp) (((profile)*19) + ((stp)<<1))
#define EEADR_PROFILE_DURATION(profile, stp) (EEADR_PROFILE_SETPOINT(profile, stp) + 1)
#define EEADR_SET_MENU EEADR_PROFILE_SETPOINT(6, 0)
#define EEADR_SET_MENU_ITEM(name) (EEADR_SET_MENU + (name))
#define EEADR_POWER_ON 127
const char menu_opt[][4] = {
"SP",
"hy",
"tc",
"SA",
"St",
"dh",
"cd",
"hd",
"rP",
"rn"
};
bool isBlank(char c){
return c == ' ' || c == '\t';
}
bool isDigit(char c){
return c >= '0' && c <= '9';
}
bool isEOL(char c){
return c == '\r' || c == '\n';
}
void print_temperature(int temperature){
if(temperature < 0){
temperature = -temperature;
Serial.print('-');
}
if(temperature >= 1000){
temperature /= 10;
Serial.println(temperature);
} else {
Serial.print(temperature/10);
Serial.print('.');
Serial.println(temperature%10);
}
}
void print_config_value(unsigned char address, int value){
if(address < EEADR_SET_MENU){
unsigned char profile=0;
while(address >= 19){
address-=19;
profile++;
}
if(address & 1){
Serial.print("dh");
} else {
Serial.print("SP");
}
Serial.print(profile);
Serial.print(address >> 1);
Serial.print('=');
if(address & 1){
Serial.println(value);
} else {
print_temperature(value);
}
} else {
Serial.print(menu_opt[address-EEADR_SET_MENU]);
Serial.print('=');
if(address == EEADR_SET_MENU_ITEM(run_mode)){
if(value >= 0 && value <= 5){
Serial.print("Pr");
Serial.println(value);
} else {
Serial.println("th");
}
} else if(address <= EEADR_SET_MENU_ITEM(setpoint_alarm)){
print_temperature(value);
} else {
Serial.println(value);
}
}
}
unsigned char parse_temperature(const char *str, int *temperature){
unsigned char i=0;
bool neg = false;
if(str[i] == '-'){
neg = true;
i++;
}
if(!isDigit(str[i])){
return 0;
}
*temperature = 0;
while(isDigit(str[i])){
*temperature = *temperature * 10 + (str[i] - '0');
i++;
}
*temperature *= 10;
if(str[i] == '.'){
i++;
if(isDigit(str[i])){
*temperature += (str[i] - '0');
i++;
} else {
return 0;
}
}
if(neg){
*temperature = -(*temperature);
}
return i;
}
unsigned char parse_address(const char *cmd, unsigned char *addr){
char i;
if(!strncmp("SP", cmd, 2)){
if(isDigit(cmd[2]) && isDigit(cmd[3]) && cmd[2] < '6'){
*addr = EEADR_PROFILE_SETPOINT(cmd[2]-'0', cmd[3]-'0');
return 4;
}
}
if(!strncmp("dh", cmd, 2)){
if(isDigit(cmd[2]) && isDigit(cmd[3]) && cmd[2] < '6' && cmd[3] < '9'){
*addr = EEADR_PROFILE_DURATION(cmd[2]-'0', cmd[3]-'0');
return 4;
}
}
for(i=0; i<(sizeof(menu_opt)/sizeof(menu_opt[0])); i++){
unsigned char len = strlen(menu_opt[i]);
if(!strncmp(cmd, &menu_opt[i][0], len) && (isBlank(cmd[len]) || isEOL(cmd[len]))){
*addr = EEADR_SET_MENU + i;
return strlen(menu_opt[i]);
}
}
*addr = 0;
for(i=0; i<30; i++){
if(isBlank(cmd[i]) || isEOL(cmd[i])){
break;
}
if(isDigit(cmd[i])){
if(*addr>12){
return 0;
} else {
*addr = *addr * 10 + (cmd[i] - '0');
}
} else {
return 0;
}
}
if(*addr > 127){
return 0;
}
return i;
}
unsigned char parse_config_value(const char *cmd, int address, bool pretty, int *data){
unsigned char i=0;
bool neg=false;
if(pretty){
if(address < EEADR_SET_MENU){
while(address >= 19){
address-=19;
}
if((address & 1) == 0){
return parse_temperature(cmd, data);
}
} else if(address <= EEADR_SET_MENU_ITEM(setpoint_alarm)){
return parse_temperature(cmd, data);
} else if(address == EEADR_SET_MENU_ITEM(run_mode)) {
if(!strncmp(cmd, "Pr", 2)){
*data = cmd[2] - '0';
if(*data >= 0 && *data <= 5){
return 3;
}
} else if(!strncmp(cmd, "th", 2)){
*data = 6;
return 2;
}
return 0;
}
}
if(cmd[i] == '-'){
neg = true;
i++;
}
if(!isDigit(cmd[i])){
return 0;
}
for(*data=0; i<6; i++){
if(!isDigit(cmd[i])){
break;
}
if(isDigit(cmd[i]) && *data < 3276){
*data = *data * 10 + (cmd[i] - '0');
} else {
return 0;
}
}
if((neg && *data > 32768) || (!neg && *data > 32767)){
return 0;
}
if(neg){
*data = -(*data);
}
return i;
}
void parse_command(char *cmd){
int data;
if(cmd[0] == 't'){
if(!isEOL(cmd[1])){
Serial.println("?Syntax error");
return;
}
if(read_temp(&data)){
Serial.print("Temperature=");
print_temperature(data);
} else {
Serial.println("?Communication error");
}
} else if(cmd[0] == 'h'){
if(!isEOL(cmd[1])){
Serial.println("?Syntax error");
return;
}
if(read_heating(&data)){
Serial.print("Heating=");
Serial.println(data ? "on" : "off");
} else {
Serial.println("?Communication error");
}
} else if(cmd[0] == 'c'){
if(!isEOL(cmd[1])){
Serial.println("?Syntax error");
return;
}
if(read_cooling(&data)){
Serial.print("Cooling=");
Serial.println(data ? "on" : "off");
} else {
Serial.println("?Communication error");
}
} else if(cmd[0] == 'r' || cmd[0] == 'w') {
unsigned char address=0;
unsigned char i=0, j;
bool neg = false;
if(!isBlank(cmd[1])){
Serial.println("?Syntax error");
return;
}
j = parse_address(&cmd[2], &address);
i+=j+2;
if(j==0){
Serial.println("?Syntax error");
return;
}
if(cmd[0] == 'r'){
if(!isEOL(cmd[i])){
Serial.println("?Syntax error");
return;
}
if(read_eeprom(address, &data)){
if(isDigit(cmd[2])){
Serial.print("EEPROM[");
Serial.print(address);
Serial.print("]=");
Serial.println(data);
} else {
print_config_value(address, data);
}
} else {
Serial.println("?Communication error");
}
return;
}
if(!isBlank(cmd[i])){
Serial.println("?Syntax error");
return;
}
i++;
j = parse_config_value(&cmd[i], address, !isDigit(cmd[2]), &data);
i += j;
if(j == 0){
Serial.println("?Syntax error");
return;
} else {
if(!isEOL(cmd[i])){
Serial.println("?Syntax error");
return;
}
if(write_eeprom(address, data)){
Serial.println("Ok");
} else {
Serial.println("?Communication error");
}
}
}
}
void setup() {
Serial.begin(115200);
delay(2);
Serial.println("STC-1000+ communication sketch.");
Serial.println("Copyright 2015 Mats Staffansson");
Serial.println("");
Serial.println("Commands: 't' to read temperature");
Serial.println(" 'c' to read state of cooling relay");
Serial.println(" 'h' to read state of heating relay");
Serial.println(" 'r [addr]' to read EEPROM address");
Serial.println(" 'w [addr] [data]' to write EEPROM address");
Serial.println("");
Serial.println("[addr] can be literal (0-127) or mnemonic SPxy/dhxy, hy, tc and so on");
Serial.println("[data] will also be literal (as stored in EEPROM) or human friendly");
Serial.println("depending on addressing mode");
}
void loop() {
static char cmd[32], rxchar=' ';
static unsigned char index=0;
if(Serial.available() > 0){
char c = Serial.read();
if(!(isBlank(rxchar) && isBlank(c))){
cmd[index] = c;
rxchar = c;
index++;
}
if(index>=31 || isEOL(rxchar)){
cmd[index] = '\0';
parse_command(cmd);
index = 0;
rxchar = ' ';
}
}
}