-
Notifications
You must be signed in to change notification settings - Fork 7
/
bluetooth.tcl
2151 lines (1708 loc) · 74 KB
/
bluetooth.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_bluetooth 1.1
package require de1_comms
package require de1_device_scale 1.0
package require de1_logging 1.2
namespace eval ::bt {
proc ::bt::msg {first args} {
::logging::default_logger $first "bluetooth:" {*}$args
}
}
## Scales
### Generics
proc scale_enable_lcd {} {
::bt::msg -NOTICE scale_enable_lcd
if {$::settings(scale_type) == "atomaxskale"} {
skale_enable_lcd
} elseif {$::settings(scale_type) == "decentscale"} {
decentscale_enable_lcd
# double-sending command, half a second later, because sometimes the decent scale command buffer has not finished the previous command and drops the next one
after 1000 decentscale_enable_lcd
}
}
proc scale_disable_lcd {} {
::bt::msg -NOTICE scale_disable_lcd
if {$::settings(scale_type) == "atomaxskale"} {
set do_this 1
if {$do_this == 1} {
skale_disable_lcd
}
} elseif {$::settings(scale_type) == "decentscale"} {
set do_this 1
if {$do_this == 1} {
# disabled the LCD off for Decent Scale, so that we don't give false impression tha the scale is off
# ideally in future firmware we can find out if they are on usb power, and disable LEDs if they are
decentscale_disable_lcd
# double-sending command, half a second later, because sometimes the decent scale command buffer has not finished the previous command and drops the next one
after 500 decentscale_disable_lcd
}
}
}
proc scale_timer_start {} {
::bt::msg -NOTICE scale_timer_start
if {$::settings(scale_type) == "atomaxskale"} {
skale_timer_start
} elseif {$::settings(scale_type) == "decentscale"} {
decentscale_timer_start
# double-sending command, half a second later, because sometimes the decent scale command buffer has not finished the previous command and drops the next one
after 500 decentscale_timer_start
} elseif {$::settings(scale_type) == "felicita"} {
felicita_start_timer
}
}
proc scale_timer_stop {} {
::bt::msg -NOTICE scale_timer_stop
if {$::settings(scale_type) == "atomaxskale"} {
skale_timer_stop
} elseif {$::settings(scale_type) == "decentscale"} {
decentscale_timer_stop
# double-sending command, half a second later, because sometimes the decent scale command buffer has not finished the previous command and drops the next one
after 500 decentscale_timer_stop
} elseif {$::settings(scale_type) == "felicita"} {
felicita_stop_timer
}
}
# timer off is badly named, it actually is "timer set to zero"
proc scale_timer_reset {} {
::bt::msg -NOTICE scale_timer_reset
if {$::settings(scale_type) == "atomaxskale"} {
skale_timer_reset
} elseif {$::settings(scale_type) == "decentscale"} {
decentscale_timer_reset
# double-sending command, half a second later, because sometimes the decent scale command buffer has not finished the previous command and drops the next one
after 500 decentscale_timer_reset
} elseif {$::settings(scale_type) == "felicita"} {
felicita_timer_reset
}
}
proc scale_tare {} {
::bt::msg -NOTICE scale_tare
::bt::msg -WARNING "DEPRECATED: scale_tare is deprecated in favor of ::device::scale::tare"
::device::scale::tare
}
proc scale_enable_weight_notifications {} {
if {$::settings(scale_type) == "atomaxskale"} {
skale_enable_weight_notifications
} elseif {$::settings(scale_type) == "decentscale"} {
decentscale_enable_notifications
} elseif {$::settings(scale_type) == "acaiascale"} {
acaia_enable_weight_notifications $::de1(suuid_acaia_ips) $::de1(cuuid_acaia_ips_age)
} elseif {$::settings(scale_type) == "acaiapyxis"} {
#acaia_enable_weight_notifications $::de1(suuid_acaia_pyxis) $::de1(cuuid_acaia_pyxis_status)
} elseif {$::settings(scale_type) == "felicita"} {
felicita_enable_weight_notifications
} elseif {$::settings(scale_type) == "hiroiajimmy"} {
hiroia_enable_weight_notifications
}
}
proc scale_enable_button_notifications {} {
::bt::msg -NOTICE scale_enable_button_notifications
if {$::settings(scale_type) == "atomaxskale"} {
skale_enable_button_notifications
} elseif {$::settings(scale_type) == "decentscale"} {
# nothing
}
}
proc scale_enable_grams {} {
::bt::msg -NOTICE scale_enable_grams
if {$::settings(scale_type) == "atomaxskale"} {
skale_enable_grams
} elseif {$::settings(scale_type) == "decentscale"} {
# nothing to do, as this is already set as part of LED on command
}
}
#### Atomax Skale
proc skale_timer_start {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "atomaxskale"} {
return
}
if {[ifexists ::sinstance($::de1(suuid_skale))] == ""} {
::bt::msg -DEBUG "Skale not connected, cannot start timer"
return
}
set timeron [binary decode hex "DD"]
userdata_append "SCALE: Skale : timer start" [list ble write $::de1(scale_device_handle) $::de1(suuid_skale) $::sinstance($::de1(suuid_skale)) $::de1(cuuid_skale_EF80) $::cinstance($::de1(cuuid_skale_EF80)) $timeron] 0
}
proc skale_enable_button_notifications {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "atomaxskale"} {
return
}
if {[ifexists ::sinstance($::de1(suuid_skale))] == ""} {
::bt::msg -DEBUG "Skale not connected, cannot enable button notifications"
return
}
userdata_append "SCALE: enable Skale button notifications" [list ble enable $::de1(scale_device_handle) $::de1(suuid_skale) $::sinstance($::de1(suuid_skale)) $::de1(cuuid_skale_EF82) $::cinstance($::de1(cuuid_skale_EF82))] 1
}
proc skale_enable_grams {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "atomaxskale"} {
return
}
set grams [binary decode hex "03"]
if {[ifexists ::sinstance($::de1(suuid_skale))] == ""} {
::bt::msg -DEBUG "Skale not connected, cannot enable grams"
return
}
userdata_append "SCALE: Skale : enable grams" [list ble write $::de1(scale_device_handle) $::de1(suuid_skale) $::sinstance($::de1(suuid_skale)) $::de1(cuuid_skale_EF80) $::cinstance($::de1(cuuid_skale_EF80)) $grams] 1
}
proc skale_enable_lcd {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "atomaxskale"} {
return
}
set screenon [binary decode hex "ED"]
set displayweight [binary decode hex "EC"]
if {[ifexists ::sinstance($::de1(suuid_skale))] == ""} {
::bt::msg -DEBUG "Skale not connected, cannot enable LCD"
return
}
userdata_append "SCALE: Skale : enable LCD" [list ble write $::de1(scale_device_handle) $::de1(suuid_skale) $::sinstance($::de1(suuid_skale)) $::de1(cuuid_skale_EF80) $::cinstance($::de1(cuuid_skale_EF80)) $screenon] 0
userdata_append "SCALE: Skale : display weight on LCD" [list ble write $::de1(scale_device_handle) $::de1(suuid_skale) $::sinstance($::de1(suuid_skale)) $::de1(cuuid_skale_EF80) $::cinstance($::de1(cuuid_skale_EF80)) $displayweight] 0
}
proc skale_disable_lcd {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "atomaxskale"} {
return
}
set screenoff [binary decode hex "EE"]
if {[ifexists ::sinstance($::de1(suuid_skale))] == ""} {
::bt::msg -DEBUG "Skale not connected, cannot disable LCD"
return
}
userdata_append "SCALE: Skale : disable LCD" [list ble write $::de1(scale_device_handle) $::de1(suuid_skale) $::sinstance($::de1(suuid_skale)) $::de1(cuuid_skale_EF80) $::cinstance($::de1(cuuid_skale_EF80)) $screenoff] 0
}
proc skale_timer_stop {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "atomaxskale"} {
return
}
set tare [binary decode hex "D1"]
if {[ifexists ::sinstance($::de1(suuid_skale))] == ""} {
::bt::msg -DEBUG "Skale not connected, cannot stop timer"
return
}
userdata_append "SCALE: Skale: timer stop" [list ble write $::de1(scale_device_handle) $::de1(suuid_skale) $::sinstance($::de1(suuid_skale)) $::de1(cuuid_skale_EF80) $::cinstance($::de1(cuuid_skale_EF80)) $tare] 0
}
proc skale_timer_reset {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "atomaxskale"} {
return
}
set tare [binary decode hex "D0"]
if {[ifexists ::sinstance($::de1(suuid_skale))] == ""} {
::bt::msg -DEBUG "Skale not connected, cannot reset timer"
return
}
userdata_append "SCALE: Skale: timer reset" [list ble write $::de1(scale_device_handle) $::de1(suuid_skale) $::sinstance($::de1(suuid_skale)) $::de1(cuuid_skale_EF80) $::cinstance($::de1(cuuid_skale_EF80)) $tare] 0
}
proc skale_tare {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "atomaxskale"} {
return
}
set tare [binary decode hex "10"]
# if this was a scheduled tare, indicate that the tare has completed
unset -nocomplain ::scheduled_scale_tare_id
#set ::de1(final_espresso_weight) 0
if {[ifexists ::sinstance($::de1(suuid_skale))] == ""} {
::bt::msg -DEBUG "Skale not connected, cannot tare"
return
}
userdata_append "SCALE: Skale: tare" [list ble write $::de1(scale_device_handle) $::de1(suuid_skale) $::sinstance($::de1(suuid_skale)) $::de1(cuuid_skale_EF80) $::cinstance($::de1(cuuid_skale_EF80)) $tare] 0
}
proc skale_enable_weight_notifications {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "atomaxskale"} {
return
}
if {[ifexists ::sinstance($::de1(suuid_skale))] == ""} {
::bt::msg -DEBUG "Skale not connected, cannot enable weight notifications"
return
}
userdata_append "SCALE: enable Skale weight notifications" [list ble enable $::de1(scale_device_handle) $::de1(suuid_skale) $::sinstance($::de1(suuid_skale)) $::de1(cuuid_skale_EF81) $::cinstance($::de1(cuuid_skale_EF81))] 1
}
#### Felicita
proc felicita_enable_weight_notifications {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "felicita"} {
return
}
if {[ifexists ::sinstance($::de1(suuid_felicita))] == ""} {
error "Felicita Scale not connected, cannot enable weight notifications"
return
}
userdata_append "SCALE: enable felicita scale weight notifications" [list ble enable $::de1(scale_device_handle) $::de1(suuid_felicita) $::sinstance($::de1(suuid_felicita)) $::de1(cuuid_felicita) $::cinstance($::de1(cuuid_felicita))] 1
}
proc felicita_tare {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "felicita"} {
return
}
if {[ifexists ::sinstance($::de1(suuid_felicita))] == ""} {
error "Felicita Scale not connected, cannot send tare cmd"
return
}
set tare [binary decode hex "54"]
userdata_append "SCALE: felicita tare" [list ble write $::de1(scale_device_handle) $::de1(suuid_felicita) $::sinstance($::de1(suuid_felicita)) $::de1(cuuid_felicita) $::cinstance($::de1(cuuid_felicita)) $tare] 0
# The tare is not yet confirmed to us, we can therefore assume it worked out
}
proc felicita_timer_reset {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "felicita"} {
return
}
if {[ifexists ::sinstance($::de1(suuid_felicita))] == ""} {
error "Felicita Scale not connected, cannot send timer cmd"
return
}
set tare [binary decode hex "43"]
userdata_append "SCALE: felicita timer reset" [list ble write $::de1(scale_device_handle) $::de1(suuid_felicita) $::sinstance($::de1(suuid_felicita)) $::de1(cuuid_felicita) $::cinstance($::de1(cuuid_felicita)) $tare] 0
}
proc felicita_start_timer {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "felicita"} {
return
}
if {[ifexists ::sinstance($::de1(suuid_felicita))] == ""} {
error "Felicita Scale not connected, cannot send timer cmd"
return
}
set tare [binary decode hex "52"]
userdata_append "SCALE: felicita timer start" [list ble write $::de1(scale_device_handle) $::de1(suuid_felicita) $::sinstance($::de1(suuid_felicita)) $::de1(cuuid_felicita) $::cinstance($::de1(cuuid_felicita)) $tare] 0
}
proc felicita_stop_timer {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "felicita"} {
return
}
if {[ifexists ::sinstance($::de1(suuid_felicita))] == ""} {
error "Felicita Scale not connected, cannot send timer cmd"
return
}
set tare [binary decode hex "53"]
userdata_append "SCALE: felicita timer stop" [list ble write $::de1(scale_device_handle) $::de1(suuid_felicita) $::sinstance($::de1(suuid_felicita)) $::de1(cuuid_felicita) $::cinstance($::de1(cuuid_felicita)) $tare] 0
}
proc felicita_parse_response { value } {
if {[string bytelength $value] >= 9} {
binary scan $value cucua1a6 h1 h2 sign weight
if {[info exists weight] && $h1 == 1 && $h2 == 2 } {
set weight [ scan $weight %d ]
if {$weight == ""} { return }
if {$sign == "-"} {
set weight [expr $weight * -1]
}
::device::scale::process_weight_update [expr $weight / 100.0] ;# $event_time
}
}
}
#### Hiroia Jimmy
proc hiroia_enable_weight_notifications {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "hiroiajimmy"} {
return
}
if {[ifexists ::sinstance($::de1(suuid_hiroiajimmy))] == ""} {
error "Hiroia Jimmy Scale not connected, cannot enable weight notifications"
return
}
userdata_append "SCALE: enable hiroiajimmy scale weight notifications" [list ble enable $::de1(scale_device_handle) $::de1(suuid_hiroiajimmy) $::sinstance($::de1(suuid_hiroiajimmy)) $::de1(cuuid_hiroiajimmy_status) $::cinstance($::de1(cuuid_hiroiajimmy_status))] 1
}
proc hiroia_tare {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "hiroiajimmy"} {
return
}
if {[ifexists ::sinstance($::de1(suuid_hiroiajimmy))] == ""} {
error "Hiroia Jimmy Scale not connected, cannot send tare cmd"
return
}
set tare [binary decode hex "0700"]
userdata_append "SCALE: hiroiajimmy tare" [list ble write $::de1(scale_device_handle) $::de1(suuid_hiroiajimmy) $::sinstance($::de1(suuid_hiroiajimmy)) $::de1(cuuid_hiroiajimmy_cmd) $::cinstance($::de1(cuuid_hiroiajimmy_cmd)) $tare] 0
# The tare is not yet confirmed to us, we can therefore assume it worked out
}
proc hiroia_parse_response { value } {
if {[string bytelength $value] >= 7} {
append value [binary decode hex 00]
binary scan $value cucucucui h1 h2 h3 h4 weight
if {[info exists weight]} {
if {$weight >= 8388608} {
set weight [expr (0xFFFFFF - $weight) * -1]
}
::device::scale::process_weight_update [expr $weight / 10.0] ;# $event_time
} else {
error "weight non exist"
}
}
}
#### Acaia
set ::acaia_command_buffer ""
proc acaia_encode {msgType payload} {
set HEADER1 [binary decode hex "EF"];
set HEADER2 [binary decode hex "DD"];
set TYPE [binary decode hex $msgType];
# TODO calculate checksum instead of hardcofig it
set data "$HEADER1${HEADER2}${TYPE}[binary decode hex $payload]"
return $data
}
proc acaia_tare {suuid cuuid} {
if {[ifexists ::sinstance($suuid)] == "" || $::de1(scale_device_handle) == 0} {
::bt::msg -DEBUG "Acaia Scale not connected, cannot send tare cmd"
return
}
set sinstance $::sinstance($suuid)
set cinstance $::cinstance($cuuid)
set tare [acaia_encode 04 0000000000000000000000000000000000]
userdata_append "SCALE: send acaia tare" [list ble write $::de1(scale_device_handle) $suuid $sinstance $cuuid $cinstance $tare] 1
# The tare is not yet confirmed to us, we can therefore assume it worked out
}
proc acaia_send_heartbeat {suuid cuuid} {
if {[ifexists ::sinstance($suuid)] == "" || $::de1(scale_device_handle) == 0} {
::bt::msg -DEBUG "Acaia Scale not connected ($suuid), cannot send app heartbeat"
return
}
set heartbeat [acaia_encode 00 02000200]
set sinstance $::sinstance($suuid)
set cinstance $::cinstance($cuuid)
userdata_append "SCALE: send acaia heartbeat" [list ble write $::de1(scale_device_handle) $suuid $sinstance $cuuid $cinstance $heartbeat] 1
if { $::settings(force_acaia_heartbeat) == 1 } {
after 1000 [list acaia_send_config $suuid $cuuid]
after 2000 [list acaia_send_heartbeat $suuid $cuuid]
}
}
proc acaia_send_ident {suuid cuuid} {
if {[ifexists ::sinstance($suuid)] == "" || $::de1(scale_device_handle) == 0} {
::bt::msg -DEBUG "Acaia Scale not connected, cannot send app ident"
return
}
set ident [acaia_encode 0B 3031323334353637383930313233349A6D]
set sinstance $::sinstance($suuid)
set cinstance $::cinstance($cuuid)
userdata_append "SCALE: send acaia ident" [list ble write $::de1(scale_device_handle) $suuid $sinstance $cuuid $cinstance $ident] 1
}
proc acaia_send_config {suuid cuuid} {
if {[ifexists ::sinstance($suuid)] == "" || $::de1(scale_device_handle) == 0} {
::bt::msg -DEBUG "Acaia Scale not connected, cannot send app config"
return
}
set ident [acaia_encode 0C 0900010102020103041106]
set sinstance $::sinstance($suuid)
set cinstance $::cinstance($cuuid)
userdata_append "SCALE: send acaia config" [list ble write $::de1(scale_device_handle) $suuid $sinstance $cuuid $cinstance $ident] 1
}
proc acaia_enable_weight_notifications {suuid cuuid} {
if {[ifexists ::sinstance($suuid)] == "" || $::de1(scale_device_handle) == 0} {
::bt::msg -DEBUG "Acaia Scale not connected, cannot enable weight notifications"
return
}
set sinstance $::sinstance($suuid)
set cinstance $::cinstance($cuuid)
userdata_append "SCALE: enable acaia scale weight notifications" [list ble enable $::de1(scale_device_handle) $suuid $sinstance $cuuid $cinstance] 1
}
proc acaia_parse_response { value } {
append ::acaia_command_buffer $value
if {[string bytelength $::acaia_command_buffer] > 4} {
# 0xEF
set HEADER1 239
# 0xDD
set HEADER2 221
binary scan $::acaia_command_buffer cucucucucuicucu \
h1 h2 msgtype len event_type weight unit neg
if { [info exists h1] && [info exists h2] && [info exists len]} {
if { ($h1 == $HEADER1) && ($h2 == $HEADER2) \
&& [info exists neg] \
&& $msgtype == 12 && $event_type == 5 } {
# we have valid data, extract it
set calulated_weight [expr {$weight / pow(10.0, $unit)}]
set is_negative [expr {$neg > 1.0}]
if {$is_negative} {
set calulated_weight [expr {$calulated_weight * -1.0}]
}
set sensorweight $calulated_weight
::device::scale::process_weight_update $sensorweight ;# $event_time
}
if { [string bytelength $::acaia_command_buffer] >= $len } {
set ::acaia_command_buffer ""
}
}
}
}
#### Decent Scale
# cmdtype is either 0x0A for LED (cmddata 00=off, 01=on), or 0x0F for tare (cmdata = incremented char counter for each TARE use)
proc decent_scale_calc_xor {cmdtype cmdddata} {
set xor [format %02X [expr {0x03 ^ $cmdtype ^ $cmdddata ^ 0x00 ^ 0x00 ^ 0x00}]]
::bt::msg -DEBUG "decent_scale_calc_xor for '$cmdtype' '$cmdddata' is '$xor'"
return $xor
}
proc decent_scale_calc_xor4 {cmdtype cmdddata1 cmdddata2} {
set xor [format %02X [expr {0x03 ^ $cmdtype ^ $cmdddata1 ^ $cmdddata2 ^ 0x00 ^ 0x00}]]
::bt::msg -DEBUG "decent_scale_calc_xor4 for '$cmdtype' '$cmdddata1' '$cmdddata2' is '$xor'"
return $xor
}
proc decent_scale_calc_xor8 {cmdtype cmdddata1 cmdddata2 cmdddata3} {
set xor [format %02X [expr {0x03 ^ $cmdtype ^ $cmdddata1 ^ $cmdddata2 ^ $cmdddata3 ^ 0x00}]]
::bt::msg -DEBUG "decent_scale_calc_xor4 for '$cmdtype' '$cmdddata1' '$cmdddata2' '$cmdddata3' is '$xor'"
return $xor
}
proc decent_scale_make_command {cmdtype cmdddata {cmddata2 {}} {cmddata3 {}} } {
::bt::msg -DEBUG "decent_scale_make_command $cmdtype $cmdddata $cmddata2"
if {$cmddata2 == ""} {
# 1 command
::bt::msg -DEBUG "1 part decent scale command"
set hex [subst {03${cmdtype}${cmdddata}000000[decent_scale_calc_xor "0x$cmdtype" "0x$cmdddata"]}]
#set hex2 [subst {03${cmdtype}${cmdddata}000000[decent_scale_calc_xor4 "0x$cmdtype" "0x$cmdddata" "0x00"]}]
} elseif {$cmddata3 == ""} {
# 2 commands
::bt::msg -DEBUG "2 part decent scale command"
set hex [subst {03${cmdtype}${cmdddata}${cmddata2}0000[decent_scale_calc_xor4 "0x$cmdtype" "0x$cmdddata" "0x$cmddata2"]}]
} else {
# 3 commands
::bt::msg -DEBUG "3 part decent scale command"
set hex [subst {03${cmdtype}${cmdddata}${cmddata2}${cmddata3}00[decent_scale_calc_xor8 "0x$cmdtype" "0x$cmdddata" "0x$cmddata2" "0x$cmddata3"]}]
}
::bt::msg -DEBUG "hex is '$hex' for '$cmdtype' '$cmdddata' '$cmddata2' '$cmddata3'"
return [binary decode hex $hex]
}
proc tare_counter_incr {} {
# testing that tare counter can in fact be any not-recently-used integer
#set ::decent_scale_tare_counter [expr {int(rand() * 255)}]
#msg "tare counter is $::decent_scale_tare_counter"
if {[info exists ::decent_scale_tare_counter] != 1} {
set ::decent_scale_tare_counter 253
} elseif {$::decent_scale_tare_counter >= 255} {
set ::decent_scale_tare_counter 0
} else {
incr ::decent_scale_tare_counter
}
}
proc decent_scale_tare_cmd {} {
tare_counter_incr
set cmd [decent_scale_make_command "0F" [format %02X $::decent_scale_tare_counter]]
return $cmd
}
proc decentscale_enable_notifications {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "decentscale"} {
return
}
if {[ifexists ::sinstance($::de1(suuid_decentscale))] == ""} {
::bt::msg -DEBUG "decent scale not connected, cannot enable weight notifications"
return
}
userdata_append "SCALE: enable decent scale weight notifications" [list ble enable $::de1(scale_device_handle) $::de1(suuid_decentscale) $::sinstance($::de1(suuid_decentscale)) $::de1(cuuid_decentscale_read) $::cinstance($::de1(cuuid_decentscale_read))] 1
}
proc decentscale_enable_lcd {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "decentscale"} {
return
}
if {[ifexists ::sinstance($::de1(suuid_decentscale))] == ""} {
::bt::msg -DEBUG "decent scale not connected, cannot enable LCD"
return
}
if {$::settings(enable_fluid_ounces) != 1} {
# grams on display
set screenon [decent_scale_make_command 0A 01 01 00]
} else {
# ounces on display
set screenon [decent_scale_make_command 0A 01 01 01]
}
::bt::msg -DEBUG "decent scale screen on: '[::logging::format_asc_bin $screenon]'"
userdata_append "SCALE: decentscale : enable LCD" [list ble write $::de1(scale_device_handle) $::de1(suuid_decentscale) $::sinstance($::de1(suuid_decentscale)) $::de1(cuuid_decentscale_write) $::cinstance($::de1(cuuid_decentscale_write)) $screenon] 0
}
proc decentscale_disable_lcd {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "decentscale"} {
return
}
set screenoff [decent_scale_make_command 0A 00 00]
if {[ifexists ::sinstance($::de1(suuid_decentscale))] == ""} {
::bt::msg -DEBUG "decentscale not connected, cannot disable LCD"
return
}
userdata_append "SCALE: decentscale : disable LCD" [list ble write $::de1(scale_device_handle) $::de1(suuid_decentscale) $::sinstance($::de1(suuid_decentscale)) $::de1(cuuid_decentscale_write) $::cinstance($::de1(cuuid_decentscale_write)) $screenoff] 0
}
proc decentscale_timer_start {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "decentscale"} {
::bt::msg -DEBUG "decentscale_timer_start - no scale_device_handle"
return
}
if {[ifexists ::sinstance($::de1(suuid_decentscale))] == ""} {
::bt::msg -DEBUG "decentscale not connected, cannot start timer"
return
}
set ::de1(decentscale_timer_on) 1
::bt::msg -DEBUG "decentscale_timer_start"
set timeron [decent_scale_make_command 0B 03 00]
::bt::msg -DEBUG "decent scale timer on: '[::logging::format_asc_bin $timeron]'"
userdata_append "SCALE: decentscale : timer on" [list ble write $::de1(scale_device_handle) $::de1(suuid_decentscale) $::sinstance($::de1(suuid_decentscale)) $::de1(cuuid_decentscale_write) $::cinstance($::de1(cuuid_decentscale_write)) $timeron] 0
# decent scale v1.0 occasionally drops commands, which is being fixed in decent scale v1.1.
# So for now we send the same command twice.
# In the future we'll check for the decent scale firmare version
# and only send the command twice if needed for the older decent scale firmware.
userdata_append "SCALE: decentscale : timer on" [list ble write $::de1(scale_device_handle) $::de1(suuid_decentscale) $::sinstance($::de1(suuid_decentscale)) $::de1(cuuid_decentscale_write) $::cinstance($::de1(cuuid_decentscale_write)) $timeron] 0
}
proc decentscale_timer_stop {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "decentscale"} {
return
}
if {[ifexists ::sinstance($::de1(suuid_decentscale))] == ""} {
::bt::msg -DEBUG "decentscale not connected, cannot stop timer"
return
}
::bt::msg -DEBUG "decentscale_timer_stop"
set ::de1(decentscale_timer_on) 0
set timeroff [decent_scale_make_command 0B 00 00]
::bt::msg -DEBUG "decent scale timer stop: '[::logging::format_asc_bin $timeroff]'"
userdata_append "SCALE: decentscale : timer off" [list ble write $::de1(scale_device_handle) $::de1(suuid_decentscale) $::sinstance($::de1(suuid_decentscale)) $::de1(cuuid_decentscale_write) $::cinstance($::de1(cuuid_decentscale_write)) $timeroff] 0
# decent scale v1.0 occasionally drops commands, which is being fixed in decent scale v1.1.
# So for now we send the same command twice.
# In the future we'll check for the decent scale firmare version
# and only send the command twice if needed for the older decent scale firmware.
userdata_append "SCALE: decentscale : timer off" [list ble write $::de1(scale_device_handle) $::de1(suuid_decentscale) $::sinstance($::de1(suuid_decentscale)) $::de1(cuuid_decentscale_write) $::cinstance($::de1(cuuid_decentscale_write)) $timeroff] 0
}
proc decentscale_timer_reset {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "decentscale"} {
return
}
if {[ifexists ::sinstance($::de1(suuid_decentscale))] == ""} {
::bt::msg -DEBUG "decentscale not connected, cannot RESET timer"
return
}
set timeroff [decent_scale_make_command 0B 02 00]
::bt::msg -DEBUG "decent scale timer reset: '[::logging::format_asc_bin $timeroff]'"
userdata_append "SCALE: decentscale : timer reset" [list ble write $::de1(scale_device_handle) $::de1(suuid_decentscale) $::sinstance($::de1(suuid_decentscale)) $::de1(cuuid_decentscale_write) $::cinstance($::de1(cuuid_decentscale_write)) $timeroff] 0
# decent scale v1.0 occasionally drops commands, which is being fixed in decent scale v1.1.
# So for now we send the same command twice.
# In the future we'll check for the decent scale firmare version
# and only send the command twice if needed for the older decent scale firmware.
userdata_append "SCALE: decentscale : timer reset" [list ble write $::de1(scale_device_handle) $::de1(suuid_decentscale) $::sinstance($::de1(suuid_decentscale)) $::de1(cuuid_decentscale_write) $::cinstance($::de1(cuuid_decentscale_write)) $timeroff] 0
}
proc decentscale_tare {} {
if {$::de1(scale_device_handle) == 0 || $::settings(scale_type) != "decentscale"} {
return
}
set tare [binary decode hex "10"]
# if this was a scheduled tare, indicate that the tare has completed
unset -nocomplain ::scheduled_scale_tare_id
if {[ifexists ::sinstance($::de1(suuid_decentscale))] == ""} {
::bt::msg -DEBUG "decent scale not connected, cannot tare"
return
}
set tare [decent_scale_tare_cmd]
userdata_append "SCALE: decentscale : tare" [list ble write $::de1(scale_device_handle) $::de1(suuid_decentscale) $::sinstance($::de1(suuid_decentscale)) $::de1(cuuid_decentscale_write) $::cinstance($::de1(cuuid_decentscale_write)) $tare] 0
# decent scale v1.0 occasionally drops commands, which is being fixed in decent scale v1.1.
# So for now we send the same command twice.
# In the future we'll check for the decent scale firmare version
# and only send the command twice if needed for the older decent scale firmware.
userdata_append "SCALE: decentscale : tare" [list ble write $::de1(scale_device_handle) $::de1(suuid_decentscale) $::sinstance($::de1(suuid_decentscale)) $::de1(cuuid_decentscale_write) $::cinstance($::de1(cuuid_decentscale_write)) $tare] 0
}
proc close_all_ble_and_exit {} {
::bt::msg -NOTICE close_all_ble_and_exit
###
### NB: Disconnect events are intentionally not called here
###
::bt::msg -DEBUG "close_all_ble_and_exit, at entrance: [ble info]"
if {$::scanning == 1} {
catch {
ble stop $::ble_scanner
}
}
::bt::msg -DEBUG "Closing de1"
if {$::de1(device_handle) != 0} {
catch {
ble close $::de1(device_handle)
}
}
::bt::msg -DEBUG "Closing scale"
if {$::de1(scale_device_handle) != 0} {
catch {
ble close $::de1(scale_device_handle)
}
}
close_misc_bluetooth_handles
::bt::msg -DEBUG "close_all_ble_and_exit, at exit: [ble info]"
foreach h [ble info] {
::bt::msg -INFO "Closing this open BLE handle: [ble info $h]"
ble close $h
}
::bt::msg -DEBUG "close_all_ble_and_exit, at exit: [ble info]"
foreach h [ble info] {
::bt::msg -WARNING "Open BLE handle: [ble info $h]"
}
::logging::close_logfiles
exit 0
}
proc app_exit {} {
::bt::msg -NOTICE app_exit
tcl_introspection
if {$::android != 1} {
close_all_ble_and_exit
}
# john 1/15/2020 this is a bit of a hack to work around a firmware bug in 7C24F200 that has the ve turn on during sleep, if the fan threshold is set > 0
if {[firmware_has_fan_sleep_bug] == 1} {
set_fan_temperature_threshold 0
}
set ::exit_app_on_sleep 1
start_sleep
# fail-over, if the DE1 doesn't to to sleep
set since_last_ping [expr {[clock seconds] - $::de1(last_ping)}]
if {$since_last_ping > 10} {
# wait less time for the fail-over if we don't have any temperature pings from the DE1
after 1000 close_all_ble_and_exit
} else {
after 5000 close_all_ble_and_exit
}
after 5000 { .can itemconfigure $::message_button_label -text [translate "Quit"] }
after 10000 { ::logging::close_logfiles ; exit 0 }
}
proc de1_read_hotwater {} {
::bt::msg -NOTICE de1_read_hotwater
userdata_append "read de1 hot water/steam" [list de1_ble read "ShotSettings"] 1
}
proc de1_read_shot_header {} {
::bt::msg -NOTICE de1_read_shot_header
userdata_append "read shot header" [list de1_ble read "HeaderWrite"] 1
}
proc de1_read_shot_frame {} {
::bt::msg -NOTICE de1_read_shot_frame
userdata_append "read shot frame" [list de1_ble read "FrameWrite"] 1
}
proc remove_null_terminator {instr} {
set pos [string first "\x00" $instr]
if {$pos == -1} {
return $instr
}
incr pos -1
return [string range $instr 0 $pos]
}
proc android_8_or_newer {} {
if {$::android != 1} {
::bt::msg -INFO "android_8_or_newer reports: not android (0)"
return 0
}
catch {
set x [borg osbuildinfo]
array set androidprops $x
::bt::msg -NOTICE [array get androidprops]
::bt::msg -NOTICE "Android release reported: '$androidprops(version.release)'"
}
set test 0
catch {
# john note: Android 7 behaves like 8
set test [expr {$androidprops(version.release) >= 7}]
}
return $test
}
proc close_misc_bluetooth_handles {} {
set count 0
set handles {}
catch {
set handles [ble info]
}
foreach handle $handles {
::bt::msg -NOTICE "Closing misc bluetooth handle $handle"
catch {
ble close $handle
}
incr count
}
return $count
}
set ::ble_scanner {}
set ::scanning -1
if {$::android == 1} {
# at startup, if we have any hanldes, close them
set blecount [close_misc_bluetooth_handles]
if {$blecount != 0} {
::bt::msg -NOTICE "Closed $blecount misc bluetooth handles"
}
}
proc check_if_initial_connect_didnt_happen_quickly {} {
::bt::msg -NOTICE "check_if_initial_connect_didnt_happen_quickly"
# on initial startup, if a direct connection to DE1 doesn't work quickly, start a scan instead
set ble_scan_started 0
if {$::de1(device_handle) == 0 } {
catch {
ble close $::currently_connecting_de1_handle
# TODO: Evaluate: Probably not appropriate to call here
# as though possibly connected, on_connect hasn't been called
::de1::event::apply::on_disconnect_callbacks \
[dict create event_time [expr {[clock milliseconds] / 1000.0}]]
}
catch {
set ::currently_connecting_de1_handle 0
}
set ble_scan_started 1
} else {
::bt::msg -NOTICE "DE1 device handle is $::de1(device_handle)"
}
if {$::settings(scale_bluetooth_address) != "" && $::de1(scale_device_handle) == 0} {
::bt::msg -NOTICE "on initial startup, if a direct connection to scale doesn't work quickly, start a scan instead"
catch {
ble close $::currently_connecting_scale_handle
# TODO: Evaluate: Probably not appropriate to call here
# as though possibly connected, on_connect hasn't been called
::device::scale::event::apply::on_disconnect_callbacks \
[dict create event_time [expr {[clock milliseconds] / 1000.0}]]
}
catch {
set ::currently_connecting_scale_handle 0
}
set ble_scan_started 1
}
if {$ble_scan_started == 1} {
scanning_restart
}
}
proc stop_scanner {} {
if {$::scanning != 1} {
return
}
if {$::de1(device_handle) == 0} {
# don't stop scanning if there is no DE1 connection
after 30000 stop_scanner
return
}
set ::scanning 0
::bt::msg -NOTICE "Stopping ble_scanner from ::stop_scanner"
ble stop $::ble_scanner
}
proc bluetooth_connect_to_devices {} {
#@return
::bt::msg -NOTICE "bluetooth_connect_to_devices"
if {$::android != 1} {
ble_connect_to_de1
}
if {$::settings(bluetooth_address) != ""} {
if {[android_8_or_newer] == 1} {
# on bootpup, android 8 won't connect directly to a BLE device unless it's found by a scan
# this step below waits 4 seconds to see if a direct connection worked, and if not, activates a scan
# when a scan finds the device, then it will initiate a new connection request and that one will work
ble_connect_to_de1
after 4000 check_if_initial_connect_didnt_happen_quickly
::bt::msg -INFO "will launch check_if_initial_connect_didnt_happen_quickly in 4000ms"
} else {