-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbalboa-interface.ino
663 lines (559 loc) · 15.9 KB
/
balboa-interface.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//Global variables
s_bbdata bbdata_display, bbdata_controller;
s_status bbstatus;
volatile uint8_t balboa_controller_done = 0, balboa_controller_int = 0, balboa_controller_int_cnt = 0, balboa_display_done = 0;
volatile uint8_t balboa_controller_bit_skipped = 0;
volatile uint32_t balboa_controller_clock_cycle_cnt = 0, balboa_controller_clock_cycle_cnt_measured = 0;
//Function declarations
extern void IRAM_ATTR isr_0(void);
//extern void IRAM_ATTR isr_1(void);
extern uint8_t IRAM_ATTR balboa_controller_comm(uint8_t reset_routine);
//Initialisation
void balboa_interface_init(void)
{
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
pinMode(Controller_Data_Pin_i, INPUT);
pinMode(Controller_Data_Pin_i, INPUT_PULLUP);
pinMode(Controller_Button_Data_Pin_o, OUTPUT);
pinMode(Controller_Clock_Pin_i, INPUT);
pinMode(Controller_Clock_Pin_i, INPUT_PULLUP);
pinMode(Display_Data_Pin_o, OUTPUT);
pinMode(Display_Button_Data_Pin_i, INPUT);
//pinMode(Display_Button_Data_Pin_i, INPUT_PULLUP);
pinMode(Display_Clock_Pin_o, OUTPUT);
pinMode(Debug_Pin_0, OUTPUT);
pinMode(Debug_Pin_1, OUTPUT);
attachInterrupt(digitalPinToInterrupt(Controller_Clock_Pin_i), isr_0, RISING);
balboa_controller_clock_cycle_cnt = ((cpu_freq_mhz() * 1000000) / controller_clock_freq_hz);
bbdata_controller.buttons = 0x00;
__digitalWrite(Debug_Pin_0, HIGH); //Default high
__digitalWrite(Controller_Button_Data_Pin_o, HIGH); //Default high
bbdata_display.disp[0] = SevenSegmentASCII['3' - 0x20];
bbdata_display.disp[1] = SevenSegmentASCII['7' - 0x20];
bbdata_display.disp[2] = SevenSegmentASCII['0' - 0x20];
bbdata_display.disp[3] = SevenSegmentASCII['C' - 0x20];
bbdata_display.disp[4] = 0x00;
bbdata_display.disp[5] = 0x00;
convert_error_codes_to_sevenseg(balboa_error_codes, (int) (sizeof(balboa_error_codes) / sizeof(balboa_error_codes[0])), balboa_error_codes[0][0].length() + 1);
bbstatus.temperature_desired = 35.0;
}
//ISR routines
extern void IRAM_ATTR isr_0(void)
{
//__digitalWrite(Debug_Pin_0, HIGH);
balboa_controller_done = balboa_controller_comm(false);
balboa_controller_int = 1;
//__digitalWrite(Debug_Pin_0, LOW);
}
//-------- Commands --------
void balboa_set_desired_temperature(float temperature)
{
float temperature_difference = temperature - bbstatus.temperature_desired;
uint8_t button_presses = fabs(temperature_difference) / temp_increment;
if (temperature_difference > 0)
{
balboa_button_press_sequencer(true, Button_Temp_Up, button_presses);
}
else
{
balboa_button_press_sequencer(true, Button_Temp_Down, button_presses);
}
bbstatus.temperature_desired = temperature;
}
void balboa_set_lights(uint8_t lights)
{
uint8_t button_presses = 0;
if (lights > controller_lights_settings)
{
lights = controller_lights_settings;
}
if (bbstatus.lights == lights)
{
return;
}
if (lights > bbstatus.lights)
{
button_presses = lights - bbstatus.lights;
}
if (lights < bbstatus.lights)
{
button_presses = (lights - bbstatus.lights) + controller_lights_settings + 1;
}
bbstatus.lights = lights;
balboa_button_press_sequencer(true, Button_Lights, button_presses);
}
void balboa_set_jets(uint8_t jets)
{
uint8_t button_presses = 0;
if (jets > controller_jets_settings)
{
jets = controller_jets_settings;
}
if (bbstatus.jets == jets)
{
return;
}
if (jets > bbstatus.jets)
{
button_presses = jets - bbstatus.jets;
}
if (jets < bbstatus.jets)
{
button_presses = (jets - bbstatus.jets) + controller_jets_settings + 1;
}
bbstatus.jets = jets;
balboa_button_press_sequencer(true, Button_Jets, button_presses);
}
void balboa_set_blower(uint8_t blower)
{
uint8_t button_presses = 0;
if (blower > controller_blower_settings)
{
blower = controller_blower_settings;
}
if (bbstatus.blower == blower)
{
return;
}
if (blower > bbstatus.blower)
{
button_presses = blower - bbstatus.blower;
}
if (blower < bbstatus.blower)
{
button_presses = (blower - bbstatus.blower) + controller_blower_settings + 1;
}
bbstatus.blower = blower;
balboa_button_press_sequencer(true, Button_Blower, button_presses);
}
//-------- Data Processing --------
void balboa_data_process(void)
{
if (data_feedthrough_enable)
{
//Display
if (bbdata_controller.newdata == 1)
{
//display_send_data(bbdata_controller.disp, 0);
balboa_status_decode();
bbdata_controller.newdata = 0;
}
//Controller
if (bbdata_display.newdata == 1)
{
//controller_send_button(bbdata_display.buttons);
bbdata_display.newdata = 0;
}
}
}
void balboa_button_press_sequencer(uint8_t load_new_command, uint8_t button_command, uint8_t number_of_button_presses)
{
static uint8_t button_command_buf = 0, number_of_button_presses_buf = 0;
static unsigned long time_now = 0, time_prv = 0;
if (load_new_command)
{
button_command_buf = button_command;
number_of_button_presses_buf = number_of_button_presses;
}
else
{
if (button_press_sequencer_enable)
{
time_now = millis();
if (((time_now - time_prv) > button_press_sequencer_timer) && (number_of_button_presses_buf > 0))
{
balboa_controller_send_button(button_command_buf);
number_of_button_presses_buf--;
time_prv = millis();
}
}
}
}
//-------- Status Decode --------
void balboa_status_decode(void)
{
String error_buf;
bbstatus.temperature = 0;
bbstatus.temperature_desired = 35.0;
bbstatus.lights = 0;
bbstatus.jets = 0;
bbstatus.blower = 0;
bbstatus.status = 0;
error_buf = check_for_error_code(balboa_error_codes, (int) (sizeof(balboa_error_codes) / sizeof(balboa_error_codes[0])), (char *) bbdata_controller.disp, display_digits);
if (error_buf == "NULL")
{
bbstatus.temperature = (sevenseg_to_char(bbdata_controller.disp[0]) - '0') * 10;
bbstatus.temperature = bbstatus.temperature + (sevenseg_to_char(bbdata_controller.disp[1]) - '0');
bbstatus.temperature = bbstatus.temperature + ((sevenseg_to_char(bbdata_controller.disp[2]) - '0') * 0.1);
}
else
{
error_buf.toCharArray(bbstatus.code, sizeof(bbstatus.code));
}
}
//-------- Controller Communication --------
void balboa_controller_send_button(uint8_t buttons)
{
bbdata_controller.buttons = buttons;
}
extern uint8_t IRAM_ATTR balboa_controller_comm(uint8_t reset_routine)
{
static uint8_t bit_cntr = 0, byte_cntr = 0, bit_data = 0, data = 0, data_write_holdoff = 0, newdata = 0, button_send_data = 0, button_data_repeats = 0;
static uint32_t cyclecount_now = 0, cyclecount_prv = 0;
if (reset_routine) //Reset controller_comm routine when controller_timeout_ms of no interrupts has passed
{
bit_cntr = 0;
balboa_controller_bit_skipped = 0;
balboa_controller_int_cnt = 0;
byte_cntr = 0;
data = 0;
data_write_holdoff = 0;
newdata = 0;
__digitalWrite(Controller_Button_Data_Pin_o, HIGH); //Default high
return 0;
}
bit_data = 0x01 & __digitalRead(Controller_Data_Pin_i);
balboa_controller_int_cnt++;
if (bit_cntr == 0) //First bit only saves current tick count because clock is not continuous
{
cyclecount_prv = ESP.getCycleCount();
}
else
{
cyclecount_now = ESP.getCycleCount();
balboa_controller_clock_cycle_cnt_measured = (cyclecount_now - cyclecount_prv);
if (balboa_controller_clock_cycle_cnt_measured > (balboa_controller_clock_cycle_cnt + 4000)) //clock cycle missed
{
balboa_controller_bit_skipped = 1;
}
cyclecount_prv = cyclecount_now;
}
//Check if previous data has been read, otherwise hold off new writes
if ((byte_cntr == 0) && (bbdata_controller.newdata))
{
data_write_holdoff = 1;
}
write_byte_bit(&data, bit_data, bit_cntr);
//Button Data
//Set low at 5th byte
if (byte_cntr >= (Byte_Count - 2))
{
__digitalWrite(Controller_Button_Data_Pin_o, 0);
}
//Set first button data bit
if ((byte_cntr >= (Byte_Count - 2)) && (bit_cntr >= (Bit_Count - 1)))
{
//Only clock out button data when no clock cycles where skipped
if (balboa_controller_bit_skipped == 0)
{
__digitalWrite(Controller_Button_Data_Pin_o, 0x01 & bbdata_controller.buttons);
}
else
{
__digitalWrite(Controller_Button_Data_Pin_o, 0);
}
}
//Clock out rest of button data
if ((byte_cntr >= (Byte_Count - 1)) && (button_send_data) && (balboa_controller_bit_skipped == 0))
{
//Only clock out button data when no clock cycles where skipped
if (balboa_controller_bit_skipped == 0)
{
__digitalWrite(Controller_Button_Data_Pin_o, 0x01 & (bbdata_controller.buttons >> (bit_cntr + 1)));
}
else
{
__digitalWrite(Controller_Button_Data_Pin_o, 0);
}
}
bit_cntr++;
if (bit_cntr >= Bit_Count)
{
bit_cntr = 0;
if ((bbdata_controller.disp[byte_cntr] != data) && (data_write_holdoff == 0))
{
bbdata_controller.disp[byte_cntr] = data;
newdata = 1;
}
data = 0;
//Done
if (byte_cntr >= (Byte_Count - 1))
{
byte_cntr = 0;
//Signal new data
if (newdata)
{
bbdata_controller.newdata = 1;
newdata = 0;
}
//Button push send routine
if (button_send_data == 0)
{
if (bbdata_controller.buttons != 0)
{
button_send_data = 1;
}
}
else
{
if (button_data_repeats++ == Button_Send_Repeats)
{
button_send_data = 0;
button_data_repeats = 0;
bbdata_controller.buttons = 0;
}
}
return 1;
}
else
{
byte_cntr++;
}
}
return 0;
}
void balboa_controller_reset_data_pin(void)
{
static uint8_t holdoff = 0, first_error = 1;
static unsigned long time_int = 0;
__digitalWrite(Debug_Pin_1, LOW);
//The last rising edge pulse of the external interrupt on the controller clock is used to output data (to slow on falling edge) so button data line must be reset high out of the interrupt routine
if (balboa_controller_int) //Check if interrupt has been triggered
{
balboa_controller_int = 0;
holdoff = 0;
time_int = millis();
}
else
{
if (holdoff == 0)
{
if ((millis() - time_int) >= 2)
{
holdoff = 1;
if (balboa_controller_done == 0)
{
if (!first_error) //Do not print first error, this one is normal
{
__digitalWrite(Debug_Pin_1, HIGH);
Serial.println("---- ERROR ----");
Serial.println();
Serial.print("Controller routine was not ready, interrupt count (should be 42): ");
Serial.println(balboa_controller_int_cnt);
Serial.println();
serial_print_debug();
Serial.println();
Serial.println();
}
first_error = 0;
}
balboa_controller_int_cnt = 0;
//Reset controller routine
balboa_controller_comm(true);
}
}
}
}
//-------- Display Communication --------
void balboa_display_send_data(uint8_t data[], uint8_t holdoff_s)
{
static uint8_t holdoff_enable = 0, holdoff_duration_s = 0;
static unsigned long time_now = 0, time_holdoff_start = 0;
time_now = millis();
if (holdoff_enable)
{
if ((time_now - time_holdoff_start) >= (holdoff_duration_s * 1000))
{
holdoff_enable = false;
}
else
{
return;
}
}
if (holdoff_s > 0)
{
holdoff_enable = true;
holdoff_duration_s = holdoff_s;
time_holdoff_start = millis();
}
memcpy(bbdata_display.disp, data, sizeof(bbdata_display.disp));
}
void balboa_display_poll(void)
{
static uint8_t idle = 0;
static unsigned long time_now = 0, time_prv = 0; //time_poll = 0;
time_now = millis();
if (idle) //idle time between transmissions
{
if ((time_now - time_prv) >= display_period_ms)
{
idle = 0;
}
}
else
{
/*if ((time_now - time_poll) > 1)
{*/
//time_poll = millis();
if (balboa_display_comm(true, Display_Data_Pin_o, Display_Button_Data_Pin_i, Display_Clock_Pin_o))
{
balboa_display_done = 1;
idle = 1;
time_prv = millis();
}
/*}*/
}
}
extern uint8_t balboa_display_comm(uint8_t start, uint8_t data_pin_o, uint8_t button_data_pin_i, uint8_t clock_pin_o)
{
static uint8_t state = 0, bit_cntr = 0, byte_cntr = 0, button_data = 0;
// 5 states per period (25uS) -> 5uS clock -> 200kHz
switch (state)
{
case 0: //reset variables
bit_cntr = 0;
byte_cntr = 0;
button_data = 0;
if (start)
state = 1;
break;
case 1: //set data
if (bit_cntr >= Bit_Count) //neglect last bit
__digitalWrite(data_pin_o, LOW);
else
{
__digitalWrite(data_pin_o, (0x01 & (bbdata_display.disp[byte_cntr] >> bit_cntr))); //LSB first
}
state = 2;
break;
case 2: //set clock
if (bit_cntr >= Bit_Count) //neglect first bit
__digitalWrite(clock_pin_o, LOW);
else
{
__digitalWrite(clock_pin_o, HIGH);
//Clock in button data
if (byte_cntr == (Byte_Count - 1))
{
write_byte_bit(&button_data, (0x01 & __digitalRead(button_data_pin_i)), bit_cntr); //Read in bit (LSB first)
}
}
state = 3;
break;
case 3: //reset clock
__digitalWrite(clock_pin_o, LOW);
if ((bit_cntr == 0) && (byte_cntr == 0) && (__digitalRead(button_data_pin_i) == 0)) //No display detected
{
state = 0;
return 1;
}
else
state = 4;
break;
case 4: //reset data
__digitalWrite(data_pin_o, LOW);
bit_cntr++;
state = 1;
break;
}
if (bit_cntr == (Bit_Count + 1))
{
if (byte_cntr == (Byte_Count - 1)) //done
{
state = 0;
if ((button_data != 0) && (bbdata_display.newdata == 0)) //Only write new button data when previous is processed
{
bbdata_display.buttons = button_data;
bbdata_display.newdata = 1;
}
return 1;
}
else
{
bit_cntr = 0;
byte_cntr++;
}
}
return 0;
}
//Helper Functions
extern uint8_t IRAM_ATTR write_byte_bit(uint8_t* write_byte, uint8_t write_bit, uint8_t bit_location)
{
uint8_t temp;
if (write_bit)
{
*write_byte = *write_byte | (1 << bit_location);
}
else
{
temp = (1 << bit_location);
temp = ~temp;
*write_byte = *write_byte & temp;
}
return 0;
}
char sevenseg_to_char(uint8_t data)
{
for (uint16_t i = 16; i < sizeof(SevenSegmentASCII); i++) //Skip signs
{
if (data == SevenSegmentASCII[i])
{
return (char) (i + 0x20);
}
}
return 0;
}
String string_to_sevenseg(String string, int string_length)
{
char char_array_in[string_length];
char char_array_out[string_length];
string.toCharArray(char_array_in, string_length);
for (int i = 0; i < string_length; i++)
{
if (char_array_in[i] >= 0x20)
{
char_array_out[i] = SevenSegmentASCII[char_array_in[i] - 0x20];
}
else
{
char_array_out[i] = 0x00;
}
}
return String(char_array_out);
}
void convert_error_codes_to_sevenseg(String string_array[][2], int array_length, int string_length)
{
for (int i = 0; i < array_length; i++)
{
string_array[i][1] = string_to_sevenseg(string_array[i][0], string_length);
}
}
String check_for_error_code(String string_array[][2], int array_length, char data[], uint8_t char_array_length)
{
char buffer_0[char_array_length + 1];
char buffer_1[char_array_length];
bool compare_cnt = false;
for (int i = 0; i < array_length; i++)
{
string_array[i][1].toCharArray(buffer_0, char_array_length + 1);
memcpy(buffer_1, data, char_array_length);
compare_cnt = true;
for (int j = 0; j < char_array_length; j++)
{
if (buffer_0[j] == 0x00)
{
break;
}
if (buffer_0[j] != buffer_1[j])
{
compare_cnt &= false;
}
}
if (compare_cnt)
{
return string_array[i][0];
}
}
return "NULL";
}