-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathde1_comms.tcl
1462 lines (1131 loc) · 44.7 KB
/
de1_comms.tcl
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package provide de1_comms 1.1
package require de1_bluetooth
package require de1_logging 1.2
### Globals
set ::failed_attempt_count_connecting_to_de1 0
set ::successful_de1_connection_count 0
## Helper
proc int_to_hex {in} {
return [format %02X $in]
}
proc long_to_little_endian_hex {in} {
set i [format %04X $in]
set i2 "[string range $i 2 3][string range $i 0 1]"
return $i2
}
# msg -DEBUG "::comms exists: [namespace exists ::comms]" (no)
namespace eval ::comms {
proc ::comms::msg {first args} {
::logging::default_logger $first "de1_comms:" {*}$args
}
}
proc comms_msg {args} {
if {$::settings(comms_debugging) == 1} {
::comms::msg {*}$args
}
}
proc userdata_append {comment cmd {vital 0} } {
#set cmds [ble userdata $::de1(device_handle)]
#lappend cmds $cmd
#ble userdata $::de1(device_handle) $cmds
lappend ::de1(cmdstack) [list $comment $cmd $vital]
set qlen [llength $::de1(cmdstack)]
::comms::msg -INFO "ENQ ($qlen): $comment"
if {$qlen >= 20} {
::comms::msg -WARNING "Warning, BLE queue is $qlen long"
}
run_next_userdata_cmd
}
proc run_next_userdata_cmd {} {
if {$::android == 1} {
# if running on android, only write one BLE command at a time
if {$::de1(wrote) == 1} {
return
}
}
if {($::de1(device_handle) == "0" || $::de1(device_handle) == "1") && $::de1(scale_device_handle) == "0"} {
comms_msg -DEBUG "run_next_userdata_cmd error: de1 not connected"
return
}
if {$::de1(cmdstack) ne {}} {
set cmd [lindex $::de1(cmdstack) 0]
set cmds [lrange $::de1(cmdstack) 1 end]
set vital [lindex $cmd 2]
set result 0
set _comment [lindex $cmd 0]
set _cmd [lindex $cmd 1]
set _cmd_command [lindex $_cmd 0]
set _readable ""
if { $_cmd_command == "ble" } {
set _readable [::logging::format_ble_command $_cmd]
} elseif { $_cmd_command == "de1_comm" \
&& [lindex $_cmd 2] in {
"WriteToMMR"
"ReadFromMMR"
} } {
set _readable [format "%s %s" \
[lindex $_cmd 2] \
[::logging::format_mmr [lindex $_cmd 3]] ]
} else {
set _readable [::logging::format_map_asc_bin $_cmd]
}
::comms::msg -INFO "DEQ ([llength $::de1(cmdstack)]) >>>" \
[expr { [string length $_comment] \
? "$_comment: " : "" }] \
"$_readable"
# setting "wrote" to 1 before running the command, so that if the command is not a BLE operation, it can choose to unset "wrote" and cause the cmd stack to continue unspooling
set ::de1(wrote) 1
set eer ""
set errcode [catch {
set result [{*}[lindex $cmd 1]]
} eer]
if {$errcode != 0} {
catch {
::comms::msg -ERROR "run_next_userdata_cmd catch error: $::errorInfo"
}
}
if {$result != 1} {
set ::de1(wrote) 0
msg -ERROR $::errorInfo
if {[string first "invalid handle" $::errorInfo] != -1 } {
::comms::msg -INFO "Not retrying this command because BLE handle for the device is now invalid"
#after 500 run_next_userdata_cmd
} elseif {$vital != 1 } {
::comms::msg -NOTICE "Preceeding command failed; not retrying as not tagged as vital"
#after 500 run_next_userdata_cmd
} else {
::comms::msg -WARNING "BLE command failed, will retry ($result):" \
[::logging::format_map_asc_bin [lindex $cmd 1]] \
"($eer)" \
[expr { $eer != 0 ? $::errorInfo : "" }]
# test idea to keep scale from interference with DE1
#if {$::de1(scale_device_handle) != 0} {
# ble abort $::de1(scale_device_handle)
#}
# john 4/28/18 not sure if we should give up on the command if it fails, or retry it
# retrying a command that will forever fail kind of kills the BLE abilities of the app
after 500 run_next_userdata_cmd
#update
return
}
}
set ::de1(cmdstack) $cmds
set ::de1(previouscmd) [lindex $cmd 1]
if {[llength $::de1(cmdstack)] == 0} {
::comms::msg -INFO "command queue is now empty"
} else {
::comms::msg -INFO "HEAD ([llength $::de1(cmdstack)]) >>>" \
[lindex [lindex $::de1(cmdstack) 0] 0]
}
# try the bluetooth stack in a second, in case there were no bluetooth commands succeeded
# and thus the queue doesn't keep getting tried
after 1000 run_next_userdata_cmd
} else {
#
}
}
### Generics
proc de1_comm {action command_name {data 0}} {
comms_msg -DEBUG "de1_comm sending action $action command $command_name data \"$data\""
if {$::de1(connectivity) == "ble"} {
return [de1_ble $action $command_name $data]
} else {
error "Unknown connectivity: $::de1(connectivity)"
}
}
proc append_to_de1_list {address name type} {
foreach { entry } $::de1_device_list {
if { [dict get $entry address] eq $address} {
return
}
}
set newlist $::de1_device_list
lappend newlist [dict create address $address name $name type $type]
::comms::msg -NOTICE "Scan found DE1: $address"
set ::de1_device_list $newlist
catch {
fill_ble_listbox
}
}
### Handler
proc de1_connect_handler { handle address name} {
if {$::settings(scale_bluetooth_address) != ""} {
ble_connect_to_scale
}
incr ::successful_de1_connection_count
set ::failed_attempt_count_connecting_to_de1 0
set ::de1(wrote) 0
set ::de1(cmdstack) {}
set ::de1(connect_time) [clock seconds]
set ::de1(last_ping) [clock seconds]
set ::currently_connecting_de1_handle 0
set ::de1(device_handle) $handle
append_to_de1_list $address $name "ble"
if {[ifexists ::de1(in_fw_update_mode)] == 1} {
::comms::msg -NOTICE "in_fw_update_mode : de1 connected"
::comms::msg -NOTICE "Tell DE1 to start to go to SLEEP (so it's asleep during firmware upgrade)"
de1_send_state "go to sleep" $::de1_state(Sleep)
set_fan_temperature_threshold 60
} else {
de1_enable_mmr_notifications
set dothis 1
if {$dothis == 1} {
de1_enable_temp_notifications
if {[info exists ::de1(first_connection_was_made)] != 1} {
# on app startup, wake the machine up
set ::de1(first_connection_was_made) 1
start_idle
}
read_de1_state
}
read_de1_version
}
}
proc de1_event_handler { command_name value {update_received 0}} {
if { $update_received == 0 } { set update_received [expr {[clock milliseconds] / 1000.0}] }
set previous_wrote 0
set previous_wrote [ifexists ::de1(wrote)]
error "Got de1_event_handler command: $command_name"
# change notification or read request
#de1_comm_new_value $cuuid $value
# change notification or read request
#de1_comm_new_value $cuuid $value
if {$command_name eq "ShotSample"} {
set ::de1(last_ping) [clock seconds]
::de1::state::update::from_shotvalue $value $update_received
#set ::de1(wrote) 0
#run_next_userdata_cmd
set do_this 0
if {$do_this == 1} {
# this tries to handle bad write situations, but it might have side effects if it is not working correctly.
# probably this should be adding a command to the top of the write queue
if {$previous_wrote == 1} {
::comms::msg -ERROR "BLE: bad write reported"
{*}$::de1(previouscmd)
set ::de1(wrote) 1
return
}
}
} elseif {$command_name eq "ReadFromMMR"} {
# MMR read
::comms::msg -DEBUG "MMR read: '[::logging::format_mmr $value]'"
parse_binary_mmr_read $value arr
set mmr_id $arr(Address)
set mmr_val [ifexists arr(Data0)]
parse_binary_mmr_read_int $value arr2
if {$mmr_id == "80381C"} {
::comms::msg -INFO "Read: GHC is installed: '$mmr_val'"
set ::settings(ghc_is_installed) $mmr_val
if {$::settings(ghc_is_installed) == 1 || $::settings(ghc_is_installed) == 2} {
# if the GHC is present but not active, check back every 10 minutes to see if its status has changed
# this is only relevant if the machine is in a debug GHC mode, where the DE1 acts as if the GHC
# is not there until it is touched. This allows the tablet to start operations. If (or once) the GHC is
# enabled, only the GHC can start operations.
after 600000 get_ghc_is_installed
}
} elseif {$mmr_id == "803808"} {
set ::de1(fan_threshold) $mmr_val
set ::settings(fan_threshold) $mmr_val
::comms::msg -INFO "MMRead: Fan threshold: '$mmr_val'"
} elseif {$mmr_id == "80380C"} {
::comms::msg -INFO "MMRead: tank temperature threshold: '$mmr_val'"
set ::de1(tank_temperature_threshold) $mmr_val
} elseif {$mmr_id == "803820"} {
::comms::msg -INFO "MMRead: group head control mode: '$mmr_val'"
set ::settings(ghc_mode) $mmr_val
} elseif {$mmr_id == "803828"} {
::comms::msg -INFO "MMRead: steam flow: '$mmr_val'"
set ::settings(steam_flow) $mmr_val
} elseif {$mmr_id == "803818"} {
::comms::msg -INFO "MMRead: hot_water_idle_temp: '[ifexists arr2(Data0)]'"
set ::settings(hot_water_idle_temp) [ifexists arr2(Data0)]
#mmr_read "espresso_warmup_timeout" "803838" "00"
} elseif {$mmr_id == "803838"} {
::comms::msg -INFO "MMRead: espresso_warmup_timeout: '[ifexists arr2(Data0)]'"
set ::settings(espresso_warmup_timeout) [ifexists arr2(Data0)]
} elseif {$mmr_id == "803810"} {
::comms::msg -INFO "MMRead: phase_1_flow_rate: '[ifexists arr2(Data0)]'"
set ::settings(phase_1_flow_rate) [ifexists arr2(Data0)]
if {[ifexists arr(Len)] >= 4} {
::comms::msg -INFO "MMRead: phase_2_flow_rate: '[ifexists arr2(Data1)]'"
set ::settings(phase_2_flow_rate) [ifexists arr2(Data1)]
}
if {[ifexists arr(Len)] >= 8} {
::comms::msg -INFO "MMRead: hot_water_idle_temp: '[ifexists arr2(Data2)]'"
set ::settings(hot_water_idle_temp) [ifexists arr2(Data2)]
}
} elseif {$mmr_id == "803834"} {
#parse_binary_mmr_read_int $value arr2
::comms::msg -INFO "MMRead: heater voltage: '[ifexists arr2(Data0)]' len=[ifexists arr(Len)]"
set ::settings(heater_voltage) [ifexists arr2(Data0)]
catch {
if {[ifexists ::settings(firmware_version_number)] != ""} {
if {$::settings(firmware_version_number) >= 1142} {
if {$::settings(heater_voltage) == 0} {
::comms::msg -WARNING "Heater voltage is unknown, please set it"
show_settings calibrate2
}
}
}
}
if {[ifexists arr(Len)] >= 8} {
::comms::msg -INFO "MMRead: espresso_warmup_timeout2: '[ifexists arr2(Data1)]'"
set ::settings(espresso_warmup_timeout) [ifexists arr2(Data1)]
#mmr_read "hot_water_idle_temp" "803818" "00"
mmr_read "phase_1_flow_rate" "803810" "02"
}
} elseif {$mmr_id == "800008"} {
#parse_binary_mmr_read_int $value arr2
if {[ifexists arr(Len)] == 12} {
# it's possibly to read all 3 MMR characteristics at once
# CPU Board Model * 1000. eg: 1100 = 1.1
::comms::msg -INFO "MMRead: CPU board model: '[ifexists arr2(Data0)]'"
set ::settings(cpu_board_model) [ifexists arr2(Data0)]
# v1.3+ Firmware Model (Unset = 0, DE1 = 1, DE1Plus = 2, DE1Pro = 3, DE1XL = 4, DE1Cafe = 5)
::comms::msg -INFO "MMRead: machine model: '[ifexists arr2(Data1)]'"
set ::settings(machine_model) [ifexists arr2(Data1)]
# CPU Board Firmware build number. (Starts at 1000 for 1.3, increments by 1 for every build)
::comms::msg -INFO "MMRead: firmware version number: '[ifexists arr2(Data2)]'"
set ::settings(firmware_version_number) [ifexists arr2(Data2)]
} else {
# CPU Board Model * 1000. eg: 1100 = 1.1
::comms::msg -INFO "MMRead: CPU board model: '[ifexists arr2(Data0)]'"
set ::settings(cpu_board_model) [ifexists arr2(Data0)]
}
} elseif {$mmr_id == "80000C"} {
parse_binary_mmr_read_int $value arr2
# v1.3+ Firmware Model (Unset = 0, DE1 = 1, DE1Plus = 2, DE1Pro = 3, DE1XL = 4, DE1Cafe = 5)
::comms::msg -INFO "MMRead: machine model: '[ifexists arr2(Data0)]'"
set ::settings(machine_model) [ifexists arr2(Data0)]
} elseif {$mmr_id == "800010"} {
parse_binary_mmr_read_int $value arr2
# CPU Board Firmware build number. (Starts at 1000 for 1.3, increments by 1 for every build)
::comms::msg -INFO "MMRead: firmware version number: '[ifexists arr2(Data0)]'"
set ::settings(firmware_version_number) [ifexists arr2(Data0)]
} elseif {$mmr_id == "80382C"} {
::comms::msg -INFO "MMRead: steam_highflow_start: '$mmr_val'"
set ::settings(steam_highflow_start) $mmr_val
} else {
::comms::msg -INFO "MMR read (undecoded): '[::logging::format_mmr $value]'"
}
} elseif {$command_name eq "Version"} {
set ::de1(last_ping) [clock seconds]
#update_de1_state $value
parse_binary_version_desc $value arr2
set ::de1(version) [array get arr2]
set v [de1_version_string]
::comms::msg -DEBUG "version data received: '$v' from [::logging::format_asc_hex $value]"
# run stuff that depends on the BLE API version
later_new_de1_connection_setup
set ::de1(wrote) 0
run_next_userdata_cmd
} elseif {$command_name eq "Calibration"} {
#set ::de1(last_ping) [clock seconds]
calibration_received $value
} elseif {$command_name eq "WaterLevels"} {
set ::de1(last_ping) [clock seconds]
parse_binary_water_level $value arr2
# compensate for the fact that we measure water level a few mm higher than the water uptake point
set mm [expr {$arr2(Level) + $::de1(water_level_mm_correction)}]
set ::de1(water_level) $mm
} elseif {$command_name eq "FWMapRequest"} {
#set ::de1(last_ping) [clock seconds]
parse_map_request $value arr2
::comms::msg -DEBUG "FWMapRequest (a009): [array get arr2]"
if {$::de1(currently_erasing_firmware) == 1 && [ifexists arr2(FWToErase)] == 0} {
::comms::msg -NOTICE "BLE recv: finished erasing fw '[ifexists arr2(FWToMap)]'"
set ::de1(currently_erasing_firmware) 0
#write_firmware_now
} elseif {$::de1(currently_erasing_firmware) == 1 && [ifexists arr2(FWToErase)] == 1} {
::comms::msg -NOTICE "BLE recv: currently erasing fw '[ifexists arr2(FWToMap)]'"
#after 1000 read_fw_erase_progress
} elseif {$::de1(currently_erasing_firmware) == 0 && [ifexists arr2(FWToErase)] == 0} {
::comms::msg -ERROR "BLE firmware find error BLE recv: '$value' [array get arr2]'"
if {[ifexists arr2(FirstError1)] == [expr 0xFF] \
&& [ifexists arr2(FirstError2)] == [expr 0xFF] \
&& [ifexists arr2(FirstError3)] == [expr 0xFD]} {
set ::de1(firmware_update_button_label) "Updated"
} else {
set ::de1(firmware_update_button_label) "Update failed"
}
set ::de1(currently_updating_firmware) 0
} else {
::comms::msg -ERROR "unknown firmware cmd ack:" \
[::logging::format_asc_bin $value] \
": [array get arr2]"
}
} elseif {$command_name eq "ShotSettings"} {
set ::de1(last_ping) [clock seconds]
#update_de1_state $value
parse_binary_hotwater_desc $value arr2
::comms::msg -INFO "hotwater data received:" \
[::logging::format_asc_bin $value] \
": [array get arr2]"
#update_de1_substate $value
} elseif {$command_name eq "DeprecatedShotDesc"} {
set ::de1(last_ping) [clock seconds]
#update_de1_state $value
parse_binary_shot_desc $value arr2
::comms::msg -INFO "shot data received:" \
[::logging::format_asc_bin $value] \
": [array get arr2]"
} elseif {$command_name eq "HeaderWrite"} {
set ::de1(last_ping) [clock seconds]
#update_de1_state $value
parse_binary_shotdescheader $value arr2
::comms::msg -INFO "READ shot header success:" \
[::logging::format_asc_bin $value] \
": [array get arr2]"
} elseif {$command_name eq "FrameWrite"} {
set ::de1(last_ping) [clock seconds]
#update_de1_state $value
parse_binary_shotframe $value arr2
::comms::msg -INFO "shot frame received" \
[::logging::format_adc_bin $value] \
": [array get arr2]"
} elseif {$command_name eq "StateInfo"} {
set ::de1(last_ping) [clock seconds]
update_de1_state $value
#if {[info exists ::globals(if_in_sleep_move_to_idle)] == 1} {
# unset ::globals(if_in_sleep_move_to_idle)
# if {$::de1_num_state($::de1(state)) == "Sleep"} {
# when making a new connection to the espresso machine, if the machine is currently asleep, then take it out of sleep
# but only do this check once, right after connection establisment
# start_idle
# }
#}
#update_de1_substate $value
set ::de1(wrote) 0
run_next_userdata_cmd
}
}
proc de1_disconnect_handler { handle } {
set ::de1(wrote) 0
set ::de1(cmdstack) {}
# close the associated handle
ble close $handle
catch {
# this should no longer be necessary since we're now explicitly closing the BLE handle associated with this disconnection notice
if {$handle != $::currently_connecting_de1_handle} {
::comms::msg -ERROR "Disconnected handle is not currently_connecting_de1_handle - closing it now though, something might not be right"
ble close $::currently_connecting_de1_handle
}
}
set ::currently_connecting_de1_handle 0
::comms::msg -NOTICE "de1 disconnected"
set ::de1(device_handle) 0
# temporarily disable this feature as it's not clear that it's needed.
#set ::settings(max_ble_connect_attempts) 99999999
set ::settings(max_ble_connect_attempts) 10
incr ::failed_attempt_count_connecting_to_de1
if {$::failed_attempt_count_connecting_to_de1 > $::settings(max_ble_connect_attempts) && $::successful_de1_connection_count > 0} {
# if we have previously been connected to a DE1 but now can't connect, then make the UI go to Sleep
# and we'll try again to reconnect when the user taps the screen to leave sleep mode
# set this to zero so that when we come back from sleep we try several times to connect
set ::failed_attempt_count_connecting_to_de1 0
update_de1_state "$::de1_state(Sleep)\x0"
} else {
if {[ifexists ::de1(disable_de1_reconnect)] != 1} {
ble_connect_to_de1
}
}
}
### Commands
proc read_de1_version {} {
::comms::msg -NOTICE "read_de1_version"
catch {
userdata_append "read_de1_version" [list de1_comm read Version] 1
}
}
# repeatedly request de1 state
proc poll_de1_state {} {
::comms::msg -DEBUG "poll_de1_state"
read_de1_state
after 1000 poll_de1_state
}
proc read_de1_state {} {
::comms::msg -NOTICE "read_de1_state"
if {$::android != 1} {
return
}
if {[catch {
userdata_append "read de1 state" [list de1_comm read StateInfo] 1
} err] != 0} {
::comms::msg -ERROR "Failed to 'read de1 state' in DE1 BLE because: '$err'"
}
}
# calibration change notifications ENABLE
proc de1_enable_calibration_notifications {} {
::comms::msg -NOTICE "de1_enable_calibration_notifications"
if {[ifexists ::sinstance($::de1(suuid))] == ""} {
::comms::msg -DEBUG "DE1 not connected, cannot send BLE command 1"
return
}
userdata_append "enable de1 calibration notifications" [list de1_comm enable Calibration] 1
}
# calibration change notifications DISABLE
proc de1_disable_calibration_notifications {} {
::comms::msg -NOTICE de1_disable_calibration_notifications
if {[ifexists ::sinstance($::de1(suuid))] == ""} {
::comms::msg -DEBUG "DE1 not connected, cannot send BLE command 2"
return
}
userdata_append "disable de1 calibration notifications" [list de1_comm disable Calibration)] 1
}
# temp changes
proc de1_enable_temp_notifications {} {
::comms::msg -NOTICE de1_enable_temp_notifications
if {[ifexists ::sinstance($::de1(suuid))] == ""} {
::comms::msg -DEBUG "DE1 not connected, cannot send BLE command 3"
return
}
userdata_append "enable de1 temp notifications" [list de1_comm enable "ShotSample"] 1
}
# status changes
proc de1_enable_state_notifications {} {
::comms::msg -NOTICE de1_enable_state_notifications
if {[ifexists ::sinstance($::de1(suuid))] == ""} {
::comms::msg -DEBUG "DE1 not connected, cannot send BLE command 4"
return
}
userdata_append "enable de1 state notifications" [list de1_comm enable "StateInfo"] 1
}
proc de1_disable_temp_notifications {} {
::comms::msg -NOTICE de1_disable_temp_notifications
if {[ifexists ::sinstance($::de1(suuid))] == ""} {
::comms::msg -DEBUG "DE1 not connected, cannot send BLE command 5"
return
}
userdata_append "disable temp notifications" [list de1_comm disable "ShotSample"] 1
}
proc de1_disable_state_notifications {} {
::comms::msg -NOTICE de1_disable_state_notifications
if {[ifexists ::sinstance($::de1(suuid))] == ""} {
::comms::msg -DEBUG "DE1 not connected, cannot send BLE command 6"
return
}
userdata_append "disable state notifications" [list de1_comm disable "StateInfo"] 1
}
set ::mmr_enabled ""
proc mmr_available {} {
#return 0
if {$::mmr_enabled == ""} {
if {$::de1(version) == ""} {
# if the version hasn't been loaded yet, use what's in the saved settings from the last time the app ran
return $::settings(mmr_enabled)
} else {
# look for the version string to determin if MMR is available
if {[de1_version_bleapi] > 3} {
# mmr feature became available at this version number
set ::settings(mmr_enabled) 1
} else {
::comms::msg -NOTICE "MMR is not enabled on this DE1 BLE API <4 #: [de1_version_bleapi]"
set ::settings(mmr_enabled) 0
}
save_settings
set ::mmr_enabled $::settings(mmr_enabled)
}
}
return $::mmr_enabled
}
proc de1_enable_mmr_notifications {} {
::comms::msg -NOTICE de1_enable_mmr_notifications
if {[mmr_available] == 0} {
::comms::msg -NOTICE "Unable to de1_enable_mmr_notifications because MMR not available"
return
}
if {[ifexists ::sinstance($::de1(suuid))] == ""} {
::comms::msg -DEBUG "DE1 not connected, cannot send BLE command 7"
return
}
#userdata_append "enable MMR write notifications" [list de1_comm enable "WriteToMMR"] 1
userdata_append "enable MMR read notifications" [list de1_comm enable "ReadFromMMR"] 1
}
# water level notifications
proc de1_enable_water_level_notifications {} {
::comms::msg -NOTICE de1_enable_water_level_notifications
if {[ifexists ::sinstance($::de1(suuid))] == ""} {
::comms::msg -DEBUG "DE1 not connected, cannot send BLE command 7"
return
}
userdata_append "enable de1 water level notifications" [list de1_comm enable "WaterLevels"] 1
}
proc de1_disable_water_level_notifications {} {
::comms::msg -NOTICE de1_disable_water_level_notifications
if {[ifexists ::sinstance($::de1(suuid))] == ""} {
::comms::msg -DEBUG "DE1 not connected, cannot send BLE command 8"
return
}
userdata_append "disable state notifications" [list de1_comm disable "WaterLevels"] 1
}
# firmware update command notifications (not writing new fw, this is for erasing and switching firmware)
proc de1_enable_maprequest_notifications {} {
::comms::msg -NOTICE de1_enable_maprequest_notifications
if {[ifexists ::sinstance($::de1(suuid))] == ""} {
::comms::msg -DEBUG "DE1 not connected, cannot send BLE command 9"
return
}
userdata_append "enable de1 maprequest notifications" [list de1_comm enable "FWMapRequest"] 1
}
proc fwfile {} {
::comms::msg -NOTICE fwfile
set fw "[homedir]/fw/bootfwupdate.dat"
if {[info exists ::de1(Firmware_file_Version)] != 1} {
::comms::msg -INFO "reading firmware file metadata"
parse_firmware_file_header [read_binary_file $fw] arr
foreach {k v} [array get arr] {
set varname "Firmware_file_$k"
set varvalue $arr($k)
::comms::msg -INFO "$varname : $varvalue"
set ::de1($varname) $varvalue
}
}
return $fw
# obsolete as of 6-6-20 a only using one firmware file again now
if {$::settings(ghc_is_installed) != 0} {
# new firmware for v1.3 machines and newer, that have a GHC.
# this dual firmware aspect is temporary, only until we have improved the firmware to be able to correctly migrate v1.0 v1.1 hardware machines to the new calibration settings.
# please do not bypass this test and load the new firmware on your v1.0 v1.1 machines yet. Once we have new firmware is known to work on those older machines, we'll get rid of the 2nd firmware image.
# note that ghc_is_installed=1 ghc hw is there but unused, whereas ghc_is_installed=3 ghc hw is required.
::comms::msg -NOTICE "using v1.3 firmware"
return "[homedir]/fw/bootfwupdate2.dat"
} else {
::comms::msg -NOTICE "using v1.1 firmware"
return "[homedir]/fw/bootfwupdate.dat"
}
}
proc start_firmware_update {} {
::comms::msg -NOTICE "start_firmware_update"
if {[ifexists ::sinstance($::de1(suuid))] == ""} {
if {$::android == 1} {
::comms::msg -DEBUG "DE1 not connected, cannot send BLE command 10"
return
}
}
if {$::settings(ghc_is_installed) != 0} {
# ok to do v1.3 fw update
#if {$::settings(force_fw_update) != 1} {
# set ::de1(firmware_update_button_label) "Up to date"
# return
# }
} else {
#if {$::settings(force_fw_update) != 1} {
# set ::de1(firmware_update_button_label) "Up to date"
# return
#}
}
if {$::de1(currently_erasing_firmware) == 1} {
::comms::msg -INFO "Already erasing firmware"
return
}
if {$::de1(currently_updating_firmware) == 1} {
::comms::msg -INFO "Already updating firmware"
return
}
#de1_enable_maprequest_notifications
set ::de1(firmware_bytes_uploaded) 0
set ::de1(firmware_update_size) [file size [fwfile]]
if {$::android != 1} {
set ::sinstance($::de1(suuid)) 0
set ::de1(cuuid_09) 0
set ::de1(cuuid_06) 0
set ::cinstance($::de1(cuuid_09)) 0
}
set arr(WindowIncrement) 0
set arr(FWToErase) 1
set arr(FWToMap) 1
set arr(FirstError1) 0
set arr(FirstError2) 0
set arr(FirstError3) 0
set data [make_packed_maprequest arr]
#set ::de1(firmware_update_button_label) "Updating"
# it'd be useful here to test that the maprequest was correctly packed
set ::de1(currently_erasing_firmware) 1
set ::de1(currently_updating_firmware) 0
set ::de1(firmware_update_button_label) "Starting"
#de1_send_state "go to sleep" $::de1_state(Sleep)
#set ::de1(firmware_update_binary) [read_binary_file [fwfile]]
#set ::de1(firmware_bytes_uploaded) 0
if {$::android == 1} {
userdata_append "Erase firmware do: [array get arr]" [list de1_comm write "FWMapRequest" $data] 1
after 10000 write_firmware_now
# if the firmware erase does not return in 15 seconds, try again, until eventually we stop trying because it succeeeded.
#after 15000 start_firmware_update
} else {
after 1000 write_firmware_now
}
}
#proc get_firmware_file_specs {} {
# parse_firmware_file_header [read_binary_file [fwfile]] arr
#}
proc write_firmware_now {} {
::comms::msg -NOTICE write_firmware_now
set ::de1(currently_updating_firmware) 1
set ::de1(currently_erasing_firmware) 0
set ::de1(firmware_update_start_time) [clock milliseconds]
::comms::msg -NOTICE "Start writing firmware now"
set ::de1(firmware_update_binary) [read_binary_file [fwfile]]
set ::de1(firmware_bytes_uploaded) 0
firmware_upload_next
}
proc firmware_upload_next {} {
::comms::msg -NOTICE "firmware_upload_next $::de1(firmware_bytes_uploaded)"
if {[ifexists ::sinstance($::de1(suuid))] == ""} {
::comms::msg -DEBUG "DE1 not connected, cannot send BLE command 11"
return
}
#delay_screen_saver
if {$::de1(firmware_bytes_uploaded) >= $::de1(firmware_update_size)} {
set ::settings(firmware_crc) [crc::crc32 -filename [fwfile]]
save_settings
if {$::android != 1} {
set ::de1(firmware_update_button_label) "Updated"
set ::de1(currently_updating_firmware) 0
} else {
# finished
de1_enable_maprequest_notifications
set ::de1(firmware_update_button_label) "Testing"
#set ::de1(firmware_update_size) 0
unset -nocomplain ::de1(firmware_update_binary)
#set ::de1(firmware_bytes_uploaded) 0
#write_FWMapRequest(self.FWMapRequest, 0, 0, 1, 0xFFFFFF, True)
#def write_FWMapRequest(ctic, WindowIncrement=0, FWToErase=0, FWToMap=0, FirstError=0, withResponse=True):
set arr(WindowIncrement) 0
set arr(FWToErase) 0
set arr(FWToMap) 1
set arr(FirstError1) [expr 0xFF]
set arr(FirstError2) [expr 0xFF]
set arr(FirstError3) [expr 0xFF]
set data [make_packed_maprequest arr]
userdata_append "Find first error in firmware update: [array get arr]" [list de1_comm write "FWMapRequest" $data] 1
}
} else {
set ::de1(firmware_update_button_label) "Updating"
set data "\x10[make_U24P0 $::de1(firmware_bytes_uploaded)][string range $::de1(firmware_update_binary) $::de1(firmware_bytes_uploaded) [expr {15 + $::de1(firmware_bytes_uploaded)}]]"
userdata_append "Write firmware: [::logging::format_mmr_short $data]" \
[list de1_comm write "WriteToMMR" $data] 1
set ::de1(firmware_bytes_uploaded) [expr {$::de1(firmware_bytes_uploaded) + 16}]
if {$::android != 1} {
set ::de1(firmware_bytes_uploaded) [expr {$::de1(firmware_bytes_uploaded) + 160}]
after 1 firmware_upload_next
#firmware_upload_next
}
}
}
proc mmr_read {note address length} {
if {[mmr_available] == 0} {
::comms::msg -NOTICE "Unable to mmr_read because MMR not available"
return
}
set mmrlen [binary decode hex $length]
set mmrloc [binary decode hex $address]
set data "$mmrlen${mmrloc}[binary decode hex 00000000000000000000000000000000]"
set cmt "MMR Read: '$note': [::logging::format_mmr $data]"
if {$::android != 1} {
::comms::msg -DEBUG "Non-Android: $cmt"
}
if {[ifexists ::sinstance($::de1(suuid))] == ""} {
::comms::msg -DEBUG "DE1 not connected, cannot send BLE command: $cmt"
return
}
userdata_append $note [list de1_comm write "ReadFromMMR" $data] 1
}
proc mmr_write { note address length value} {
if {[mmr_available] == 0} {
::comms::msg -NOTICE "Unable to mmr_write because MMR not available"
return
}
set mmrlen [binary decode hex $length]
set mmrloc [binary decode hex $address]
set mmrval [binary decode hex $value]
set data "$mmrlen${mmrloc}${mmrval}[binary decode hex 0000000000000000000000000000000000]"
if {[string length $data] > 20} {
set data [string range $data 0 19]
}
set cmt "MMR Write: '$note': [::logging::format_mmr $data]"
if {$::android != 1} {
::comms::msg -DEBUG "Non-Android: $cmt"
}
if {[ifexists ::sinstance($::de1(suuid))] == ""} {
::comms::msg -DEBUG "DE1 not connected, cannot send BLE command: $cmt"
return
}
userdata_append "$note" [list de1_comm write "WriteToMMR" $data] 1
}
proc set_tank_temperature_threshold {temp} {
::comms::msg -NOTICE set_tank_temperature_threshold "'$temp'"
###
### NB: The BLE queue is not thread safe
###
catch { after cancel $::_pending_tank_temperature_change }
remove_matching_ble_queue_entries {^MMR set_tank_temperature_threshold}
if {$temp < 10} {
# no point in circulating the water if the desired temp is <10ºC, or no preheating.
mmr_write "set_tank_temperature_threshold" "80380C" "04" [zero_pad [int_to_hex $temp] 2]
} else {
# if the water temp is being set, then set the water temp temporarily to 60º in order to force a water circulation for 2 seconds
# then a few seconds later, set it to the real, desired value
set hightemp 60
mmr_write "set_tank_temperature_threshold" "80380C" "04" [zero_pad [int_to_hex $hightemp] 2]
# Retain existing logic here
# NB: This does not guarantee a 4-second water-circulation period
# especially when the queue has a significant depth to it
set ::_pending_tank_temperature_change \
[after 4000 [list mmr_write "set_tank_temperature_threshold" \
"80380C" "04" [zero_pad [int_to_hex $temp] 2] ] ]
}
}
# /*
# * Memory Mapped Registers
# *
# * RangeNum Position Len Desc