-
Notifications
You must be signed in to change notification settings - Fork 7
/
binary.tcl
1688 lines (1336 loc) · 45.5 KB
/
binary.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_binary 1.1
package require lambda
package require de1_event 1.0
package require de1_logging 1.0
package require de1_profile 2.0
# from http://wiki.tcl.tk/12148
namespace eval fields {
variable endianness ""
variable cache
}
proc fields::2form {spec array {endian ""}} {
variable cache
variable endianness
if {$endian == ""} {
set endian $endianness
}
if {[info exists cache($endian,$array,$spec)]} {
return $cache($endian,$array,$spec)
}
set form ""
set vars {}
foreach {name qual} $spec {
foreach {type count fendian signed extra} $qual break
set t [string index $type 0]
set s [string index $signed 0]
if {$fendian == ""} {
set fendian [string tolower [string index $endian 0]]
} else {
set fendian [string tolower [string index $fendian 0]]
}
# special forms skip n, back n, jump n
if {$name == "skip" && [string is integer $type]} {
set count $type
set type "x"
} elseif {$name == "back" && [string is integer $type]} {
set count $type
set type "X"
} elseif {$name == "jump" && [string is integer $type]} {
set count $type
set type "@"
}
if {$fendian == "h" || $fendian == "b"} {
set ty [string toupper $t]
} elseif {$fendian == "l"} {
set ty [string tolower $t]
} else {
# john this seems to be a case which throws an error for integers
set ty $t
}
switch [string tolower $t] {
a {
# ascii - char string of $count
# Ascii - pad with " "
}
b {
# bits - low2high
# Bits - high2low
}
c {
# char - 8 bit integer values
set ty [string tolower $t]
}
h {
# hex low2high
# Hex high2low
}
i {
# integer - 32bits low2high
# Integer - 32bits high2low
}
s {
# short - 16bits low2high
# Short - 16bits high2low
set ty $t
}
w {
# wide-integer - 64bits low2high
# Wide-integer - 64bits high2low
}
f {
# float
set ty $t ;# don't play with endianness
}
d {
# double
set ty $t ;# don't play with endianness
}
@ {
# skip to absolute location
set name ""
}
x {
# x - move relative forward
# X - move relative back
set ty $t ;# don't play with endianness
set name ""
}
}
if {$name != ""} {
append outvars "$array\($name\) "
append invars "\$$array\($name\) "
}
if {$ty == "I" && $s == "s"} {
# signed integers are by default, and need no modifier
#set ty "s1"
set s ""
}
append form $ty$s$count
}
set cache($endian,$array,$spec) [list $form $outvars $invars]
return $cache($endian,$array,$spec)
}
# pack the fields contained in array into a binary string according to spec
proc ::fields::pack {spec array {endian ""}} {
upvar $array Record
foreach {form out in} [::fields::2form $spec Record $endian] break
return [eval binary format [list $form] {*}$in]
}
# pack the fields from $packed contained into array according to spec
proc ::fields::unpack {packed spec array {endian ""}} {
upvar $array Record
foreach {form out in} [::fields::2form $spec Record $endian] break
return [binary scan $packed [list $form] {*}$out]
}
# binary scan the fields from $packed according to spec
proc ::fields::scan {spec packed {endian ""}} {
::fields::unpack $packed $spec Record $endian
foreach {form out in} [::fields::2form $spec Record $endian] break
set result {}
foreach var $out {
lappend result [set $var]
}
return $result
}
# binary format the args according to spec
proc ::fields::format {spec endian args} {
foreach {form out in} [::fields::2form $spec Record $endian] break
set result {}
foreach var $out arg $args {
set $var $arg
}
return [::fields::pack $form Record $endian]
}
proc return_de1_packed_steam_hotwater_settings {} {
set arr(SteamSettings) [expr {0 & 0x80 & 0x40}]
# turn the steam heater off completely, if the heater is set to below 130ºC
set steam_temperature $::settings(steam_temperature)
if {$steam_temperature < 130} {
set steam_temperature 0
}
set arr(TargetSteamTemp) [convert_float_to_U8P0 $steam_temperature]
set arr(TargetSteamLength) [convert_float_to_U8P0 $::settings(steam_timeout)]
set arr(TargetHotWaterTemp) [convert_float_to_U8P0 $::settings(water_temperature)]
if {$::de1(scale_device_handle) != 0} {
# "hot water: stop on weight" feature. Works with the scale, so it's more accurate.
# we ask for more water than we need, so that we can definitely get enough
# to stop on weight.
set arr(TargetHotWaterVol) [convert_float_to_U8P0 [expr { 2 * $::settings(water_volume)}] ]
#set arr(TargetHotWaterVol) [convert_float_to_U8P0 $::settings(water_volume)]
} else {
set arr(TargetHotWaterVol) [convert_float_to_U8P0 $::settings(water_volume)]
}
set arr(TargetHotWaterLength) [convert_float_to_U8P0 $::settings(water_time_max)]
set arr(TargetEspressoVol) [convert_float_to_U8P0 $::settings(espresso_typical_volume)]
set arr(TargetGroupTemp) [convert_float_to_U16P8 $::settings(espresso_temperature)]
return [make_packed_steam_hotwater_settings arr]
}
proc return_de1_packed_waterlevel_settings {} {
set arr(Level) [convert_float_to_U16P8 0]
set arr(StartFillLevel) [convert_float_to_U16P8 $::settings(water_refill_point)]
return [make_packed_waterlevel_settings arr]
}
proc make_packed_steam_hotwater_settings {arrname} {
upvar $arrname arr
return [::fields::pack [hotwater_steam_settings_spec] arr]
}
proc make_packed_waterlevel_settings {arrname} {
upvar $arrname arr
return [::fields::pack [waterlevel_spec] arr]
}
proc make_packed_maprequest {arrname} {
upvar $arrname arr
return [::fields::pack [maprequest_spec] arr]
}
proc make_packed_calibration {arrname} {
upvar $arrname arr
return [::fields::pack [calibrate_spec] arr]
}
proc make_U32P0 {val} {
set arr(highest) [expr {($val >> 24) & 0xFF}]
set arr(hi) [expr {($val >> 16) & 0xFF}]
set arr(mid) [expr {($val >> 8 ) & 0xFF}]
set arr(low) [expr {($val ) & 0xFF}]
return [::fields::pack [U32P0_spec] arr]
}
proc make_U24P0 {val} {
set arr(hi) [expr {($val >> 16) & 0xFF}]
set arr(mid) [expr {($val >> 8 ) & 0xFF}]
set arr(low) [expr {($val ) & 0xFF}]
return [::fields::pack [U24P0_spec] arr]
}
proc make_U24P0_3_chars {val} {
set hi [expr {($val >> 16) & 0xFF}]
set mid [expr {($val >> 8 ) & 0xFF}]
set lo [expr {($val ) & 0xFF}]
return [list $hi $mid $lo]
}
proc make_U32P0_4_chars {val} {
set highest [expr {($val >> 24) & 0xFF}]
set hi [expr {($val >> 16) & 0xFF}]
set mid [expr {($val >> 8 ) & 0xFF}]
set lo [expr {($val ) & 0xFF}]
return [list $highest $hi $mid $lo]
}
proc U24P0_spec {} {
set spec {
hi {char {} {} {unsigned} {}}
mid {char {} {} {unsigned} {}}
low {char {} {} {unsigned} {}}
}
return $spec
}
proc U32P0_spec {} {
set spec {
highest {char {} {} {unsigned} {}}
hi {char {} {} {unsigned} {}}
mid {char {} {} {unsigned} {}}
low {char {} {} {unsigned} {}}
}
return $spec
}
proc decent_scale_generic_read_spec {} {
set spec {
model {char {} {} {unsigned} {}}
command {char {} {} {unsigned} {}}
data3 {char {} {} {unsigned} {}}
data4 {char {} {} {unsigned} {}}
data5 {char {} {} {unsigned} {}}
data6 {char {} {} {unsigned} {}}
xor {char {} {} {unsigned} {}}
}
return $spec
}
proc decent_scale_weight_read_spec {} {
set spec {
model {char {} {} {unsigned} {}}
wtype {char {} {} {unsigned} {}}
weight {Short {} {} {signed} {}}
rate {Short {} {} {unsigned} {}}
xor {char {} {} {unsigned} {}}
}
return $spec
}
proc decent_scale_weight_read_spec2 {} {
set spec {
model {char {} {} {unsigned} {}}
wtype {char {} {} {unsigned} {}}
weight {Short {} {} {signed} {}}
rate {Short {} {} {unsigned} {}}
xor {char {} {} {unsigned} {}}
}
return $spec
}
# typedef struct {
# U32 CheckSum; // The checksum of the rest of the encrypted image. Includes "CheckSums" + "Data" fields, not "Header"
# U32 BoardMarker; // 0xDE100001
# U32 Version; // The version of this image
# U32 ByteCount; // Number of bytes in image body, ignoring padding.
# U32 CPUBytes; // The first CPUBytes of the image are for the CPU. Remainder is for BLE.
# U32 Unused; // Blank spot for future extension. Always zero for now
# U32 DCSum; // Checksum of decrypted image
# U8 IV[32]; // Initialization vector for the firmware
# U32 HSum; // Checksum of this header.
# } T_FirmwareHeader;
proc firmware_file_spec {} {
set spec {
CheckSum {int {} {} {unsigned} {[format %X $val]}}
BoardMarker {int {} {} {unsigned} {[format %X $val]}}
Version {int {} {} {unsigned} {}}
ByteCount {int {} {} {unsigned} {}}
CPUBytes {int {} {} {unsigned} {}}
Unused {int {} {} {unsigned} {}}
DCSum {int {} {} {unsigned} {[format %X $val]}}
}
return $spec
}
proc decent_scale_timing_read_spec {} {
set spec {
minute {char {} {} {unsigned} {}}
seconds {char {} {} {unsigned} {}}
}
return $spec
}
proc maprequest_spec {} {
set spec {
WindowIncrement {Short {} {} {unsigned} {$val / 1.0}}
FWToErase {char {} {} {unsigned} {}}
FWToMap {char {} {} {unsigned} {}}
FirstError1 {char {} {} {unsigned} {}}
FirstError2 {char {} {} {unsigned} {}}
FirstError3 {char {} {} {unsigned} {}}
}
return $spec
}
proc calibrate_spec {} {
set spec {
WriteKey {Int {} {} {unsigned} {[format %X $val]}}
CalCommand {char {} {} {unsigned} {}}
CalTarget {char {} {} {unsigned} {}}
DE1ReportedVal {Int {} {} {unsigned} {double(round(100*($val / 65536.0)))/100}}
MeasuredVal {Int {} {} {signed} {double(round(100*($val / 65536.0)))/100}}
}
return $spec
}
proc version_spec {} {
set spec {
BLE_APIVersion {char {} {} {unsigned} {}}
BLE_Release {char {} {} {unsigned} {[convert_F8_1_7_to_float $val]}}
BLE_Commits {Short {} {} {undsigned} {}}
BLE_Changes {char {} {} {unsigned} {}}
BLE_Sha {int {} {} {unsigned} {[format %X $val]}}
FW_APIVersion {char {} {} {unsigned} {}}
FW_Release {char {} {} {unsigned} {[convert_F8_1_7_to_float $val]}}
FW_Commits {Short {} {} {unsigned} {}}
FW_Changes {char {} {} {unsigned} {}}
FW_Sha {int {} {} {unsigned} {[format %X $val]}}
}
return $spec
}
proc waterlevel_spec {} {
set spec {
Level {Short {} {} {unsigned} {$val / 256.0}}
StartFillLevel {Short {} {} {unsigned} {$val / 256.0}}
}
return $spec
}
proc hotwater_steam_settings_spec {} {
set spec {
SteamSettings {char {} {} {unsigned} {}}
TargetSteamTemp {char {} {} {unsigned} {}}
TargetSteamLength {char {} {} {unsigned} {}}
TargetHotWaterTemp {char {} {} {unsigned} {}}
TargetHotWaterVol {char {} {} {unsigned} {}}
TargetHotWaterLength {char {} {} {unsigned} {}}
TargetEspressoVol {char {} {} {unsigned} {}}
TargetGroupTemp {Short {} {} {unsigned} {$val / 256.0}}
}
return $spec
}
proc bintest {} {
set packed "\x15\x09\x4c\x5e\x0d\x5b\x2d"
set packed "\x02\xDE\x03\x36\x5D\xCD\x5B\x07\x5D\xD0\x5B\x00\x05\x34\x01"
#write_binary_file "compare.dat" $packed
set spec [hotwater_steam_settings_spec]
array set specarr $spec
::fields::unpack $packed $spec ShotSample bigeendian
foreach {field val} [array get ShotSample] {
set specparts $specarr($field)
set extra [lindex $specparts 4]
if {$extra != ""} {
set ShotSample($field) [expr $extra]
}
}
foreach {field val} [array get ShotSample] {
msg -DEBUG "$field : $val "
}
}
proc convert_F8_1_7_to_float {in} {
set highbit [expr {$in & 128}]
if {$highbit == 0} {
set out [expr {$in / 10.0}]
} else {
set out [expr {$in & 127}]
}
return $out
}
proc convert_bottom_10_of_U10P0 {in} {
set lowbits [expr {$in & 1023}]
return $lowbits
}
proc make_packed_shot_sample {arrname} {
upvar $arrname arr
return [::fields::pack [shot_sample_spec] arr]
}
proc convert_float_to_U8P4 {in} {
if {$in > 16} {
set in 16
}
return [expr {round($in * 16)}]
}
proc convert_float_to_U8P1 {in} {
if {$in > 128} {
set in 128
}
return [expr {round($in * 2)}]
}
proc convert_float_to_U8P0 {in} {
if {$in > 256} {
set in 256
}
return [expr {round($in)}]
}
proc convert_float_to_U16P8 {in} {
if {$in > 256} {
set in 256
}
return [expr {round($in * 256.0)}]
}
proc convert_float_to_S32P16 {in} {
if {$in > 65536} {
set in 65536
}
return [expr {round($in * 65536.0)}]
}
proc convert_float_to_F8_1_7 {in} {
if {$in >= 12.75} {
if {$in > 127} {
msg -ERROR "Numbers over 127 are not allowed this F8_1_7; limiting at 127"
set in 127
}
return [expr {round($in) | 128}]
} else {
return [expr {round($in * 10)}]
}
}
proc convert_float_to_U10P0 {in} {
return [expr {round($in) | 1024}]
}
# enum T_E_FrameFlags : U8 {
#
# // FrameFlag of zero and pressure of 0 means end of shot, unless we are at the tenth frame, in which case it's the end of shot no matter what
# CtrlF = 0x01, // Are we in Pressure or Flow priority mode?
# DoCompare = 0x02, // Do a compare, early exit current frame if compare true
# DC_GT = 0x04, // If we are doing a compare, then 0 = less than, 1 = greater than
# DC_CompF = 0x08, // Compare Pressure or Flow?
# TMixTemp = 0x10, // Disable shower head temperature compensation. Target Mix Temp instead.
# Interpolate = 0x20, // Hard jump to target value, or ramp?
# IgnoreLimit = 0x40, // Ignore minimum pressure and max flow settings
#
# DontInterpolate = 0, // Don't interpolate, just go to or hold target value
# CtrlP = 0,
# DC_CompP = 0,
# DC_LT = 0,
# TBasketTemp = 0 // Target the basket temp, not the mix temp
#};
proc make_shot_flag {enabled_features} {
set num 0
foreach feature $enabled_features {
if {$feature == "CtrlF"} {
set num [expr {$num | 0x01}]
} elseif {$feature == "DoCompare"} {
set num [expr {$num | 0x02}]
} elseif {$feature == "DC_GT"} {
set num [expr {$num | 0x04}]
} elseif {$feature == "DC_CompF"} {
set num [expr {$num | 0x08}]
} elseif {$feature == "TMixTemp"} {
set num [expr {$num | 0x10}]
} elseif {$feature == "Interpolate"} {
set num [expr {$num | 0x20}]
} elseif {$feature == "IgnoreLimit"} {
set num [expr {$num | 0x40}]
} else {
err "unknown shot flat: '$feature'"
}
}
return $num
}
proc parse_shot_flag {num} {
if {$num == {}} {
return {}
}
set enabled_features {}
if {[expr {$num & 0x01}] } {
lappend enabled_features "CtrlF"
}
if {[expr {$num & 0x02}] } {
lappend enabled_features "DoCompare"
}
if {[expr {$num & 0x04}] } {
lappend enabled_features "DC_GT"
}
if {[expr {$num & 0x08}] } {
lappend enabled_features "DC_CompF"
}
if {[expr {$num & 0x10}] } {
lappend enabled_features "TMixTemp"
}
if {[expr {$num & 0x20}] } {
lappend enabled_features "Interpolate"
}
if {[expr {$num & 0x40}] } {
lappend enabled_features "IgnoreLimit"
}
return $enabled_features
}
proc parse_binary_shotdescheader {packed destarrname} {
upvar $destarrname ShotSample
unset -nocomplain ShotSample
set spec [spec_shotdescheader]
array set specarr $spec
::fields::unpack $packed $spec ShotSample bigeendian
foreach {field val} [array get ShotSample] {
set specparts $specarr($field)
set extra [lindex $specparts 4]
if {$extra != ""} {
set ShotSample($field) [expr $extra]
}
}
}
proc parse_binary_shotframe {packed destarrname} {
upvar $destarrname ShotSample
unset -nocomplain ShotSample
set spec [spec_shotframe]
array set specarr $spec
::fields::unpack $packed $spec ShotSample bigeendian
if {$ShotSample(FrameToWrite) >= 32} {
set spec [spec_extshotframe]
array unset specarr *
array unset ShotSample *
array set specarr $spec
::fields::unpack $packed $spec ShotSample bigeendian
}
foreach {field val} [array get ShotSample] {
set specparts $specarr($field)
set extra [lindex $specparts 4]
if {$extra != ""} {
set ShotSample($field) [expr $extra]
}
}
}
# C code:
# struct PACKEDATTR T_ReadFromMMR {
# U8P0 Len; // Length of data to read, in words-1. ie. 0 = 4 bytes, 1 = 8 bytes, 255 = 2014 bytes, etc.
# U24P0 Address; // Address of window. Will autoincrement if set up in MapRequest
# U8P0 Data[16]; // If data reaches past the end of a region, bytes will be zero filled
# };
proc spec_ReadFromMMR {} {
set spec {
Len {char {} {} {unsigned} {}}
Address1 {char {} {} {unsigned} {}}
Address2 {char {} {} {unsigned} {}}
Address3 {char {} {} {unsigned} {}}
Data0 {char {} {} {unsigned} {}}
Data1 {char {} {} {unsigned} {}}
Data2 {char {} {} {unsigned} {}}
Data3 {char {} {} {unsigned} {}}
Data4 {char {} {} {unsigned} {}}
Data5 {char {} {} {unsigned} {}}
Data6 {char {} {} {unsigned} {}}
Data7 {char {} {} {unsigned} {}}
Data8 {char {} {} {unsigned} {}}
Data9 {char {} {} {unsigned} {}}
Data10 {char {} {} {unsigned} {}}
Data11 {char {} {} {unsigned} {}}
Data12 {char {} {} {unsigned} {}}
Data13 {char {} {} {unsigned} {}}
Data14 {char {} {} {unsigned} {}}
Data15 {char {} {} {unsigned} {}}
}
}
proc spec_ReadFromMMR_int {} {
set spec {
Len {char {} {} {unsigned} {}}
Address1 {char {} {} {unsigned} {}}
Address2 {char {} {} {unsigned} {}}
Address3 {char {} {} {unsigned} {}}
Data0 {int {} {} {unsigned} {}}
Data1 {int {} {} {unsigned} {}}
Data2 {int {} {} {unsigned} {}}
Data3 {int {} {} {unsigned} {}}
}
}
# C code:
# struct PACKEDATTR T_WriteToMMR {
# U8P0 Len; // Length of data
# U24P0 Address; // Address within the MMR
# U8P0 Data[16]; // Data, zero padded
# };
proc spec_WriteToMMR {} {
set spec {
Len {char {} {} {unsigned} {}}
Address1 {char {} {} {unsigned} {}}
Address2 {char {} {} {unsigned} {}}
Address3 {char {} {} {unsigned} {}}
Data0 {char {} {} {unsigned} {}}
Data1 {char {} {} {unsigned} {}}
Data2 {char {} {} {unsigned} {}}
Data3 {char {} {} {unsigned} {}}
Data4 {char {} {} {unsigned} {}}
Data5 {char {} {} {unsigned} {}}
Data6 {char {} {} {unsigned} {}}
Data7 {char {} {} {unsigned} {}}
Data8 {char {} {} {unsigned} {}}
Data9 {char {} {} {unsigned} {}}
Data10 {char {} {} {unsigned} {}}
Data11 {char {} {} {unsigned} {}}
Data12 {char {} {} {unsigned} {}}
Data13 {char {} {} {unsigned} {}}
Data14 {char {} {} {unsigned} {}}
Data15 {char {} {} {unsigned} {}}
}
}
proc spec_shotdescheader {} {
set spec {
HeaderV {char {} {} {unsigned} {}}
NumberOfFrames {char {} {} {unsigned} {}}
NumberOfPreinfuseFrames {char {} {} {unsigned} {}}
MinimumPressure {char {} {} {unsigned} {$val / 16.0}}
MaximumFlow {char {} {} {unsigned} {$val / 16.0}}
}
}
proc spec_shotframe {} {
set spec {
FrameToWrite {char {} {} {unsigned} {}}
Flag {char {} {} {unsigned} {}}
SetVal {char {} {} {unsigned} {$val / 16.0}}
Temp {char {} {} {unsigned} {$val / 2.0}}
FrameLen {char {} {} {unsigned} {[convert_F8_1_7_to_float $val]}}
TriggerVal {char {} {} {unsigned} {$val / 16.0}}
MaxVol {Short {} {} {unsigned} {[convert_bottom_10_of_U10P0 $val]}}
}
return $spec
}
proc spec_extshotframe {} {
set spec {
FrameToWrite {char {} {} {unsigned} {$val}}
MaxFlowOrPressure {char {} {} {unsigned} {$val / 16.0}}
MaxFoPRange {char {} {} {unsigned} {$val / 16.0}}
Pad1 {char {} {} {unsigned} {$val}}
Pad2 {char {} {} {unsigned} {$val}}
Pad3 {char {} {} {unsigned} {$val}}
Pad4 {char {} {} {unsigned} {$val}}
Pad5 {char {} {} {unsigned} {$val}}
}
return $spec
}
proc spec_shottail {} {
# Unused. Use highest bit to enable / disable preinfusion tracking
#MaxTotalVolume {char {} {} {unsigned} {$val }}
set spec {
FrameToWrite {char {} {} {unsigned} {$val}}
MaxTotalVolume {Short {} {} {unsigned} {[convert_bottom_10_of_U10P0 $val]}}
Pad1 {char {} {} {unsigned} {$val}}
Pad2 {char {} {} {unsigned} {$val}}
Pad3 {char {} {} {unsigned} {$val}}
Pad4 {char {} {} {unsigned} {$val}}
Pad5 {char {} {} {unsigned} {$val}}
}
return $spec
}
proc make_chunked_packed_shot_sample {hdrarrname framenames extension_framenames tail_framename} {
upvar $hdrarrname hdrarr
set packed_header [::fields::pack [spec_shotdescheader] hdrarr]
set packed_frames {}
foreach framearrname $framenames {
upvar $framearrname $hdrarrname
lappend packed_frames [::fields::pack [spec_shotframe] $hdrarrname]
}
foreach framearrname $extension_framenames {
upvar $framearrname $hdrarrname
lappend packed_frames [::fields::pack [spec_extshotframe] $hdrarrname]
}
upvar $tail_framename tailarr
lappend packed_frames [::fields::pack [spec_shottail] tailarr]
return [list $packed_header $packed_frames]
}
proc de1_packed_shot {shot_list} {
set hdr(HeaderV) 1
set hdr(MinimumPressure) 0
set hdr(MaximumFlow) [convert_float_to_U8P4 6]
set cnt 0
array set profile $shot_list
# for now, we are defaulting to IgnoreLimit as our starting flag, because we are not setting constraints of max pressure or max flow
set frame_names ""
set extension_frames ""
set this_profile $profile(advanced_shot)
if {[ifexists ::settings(insert_preinfusion_pause)] == 1} {
msg -DEBUG "Prefixing profile with a 2 seconds slow start preinfusion pause"
set pause [list \
name [translate "Pause"] \
temperature $::settings(espresso_temperature) \
sensor "coffee" \
pump "flow" \
transition "fast" \
pressure 0 \
flow 0 \
seconds 2 \
volume 0 \
exit_if 0 \
exit_pressure_over 0 \
exit_pressure_under 0 \
exit_flow_over 6 \
exit_flow_under 0 \
]
set this_profile [concat [list $pause] $this_profile]
}
foreach step $this_profile {
unset -nocomplain props
array set props $step
set frame_name "frame_$cnt"
set extension_frame "ext_frame_$cnt"
lappend frame_names $frame_name
set features {IgnoreLimit}
# flow control
if {$props(pump) == "flow"} {
lappend features "CtrlF"
set SetVal $props(flow)
} else {
set SetVal $props(pressure)
}
# use boiler water temperature as the goal
if {$props(sensor) == "water"} {
lappend features "TMixTemp"
}
if {$props(transition) == "smooth"} {
lappend features "Interpolate"
}
# "move on if...."
if {$props(exit_if) == 1} {
if {[ifexists props(exit_type)] == "pressure_under"} {
lappend features "DoCompare"
set TriggerVal $props(exit_pressure_under)
} elseif {[ifexists props(exit_type)] == "pressure_over"} {
lappend features "DoCompare"
lappend features "DC_GT"
set TriggerVal $props(exit_pressure_over)
} elseif {[ifexists props(exit_type)] == "flow_under"} {
lappend features "DoCompare"
lappend features "DC_CompF"
set TriggerVal $props(exit_flow_under)
} elseif {[ifexists props(exit_type)] == "flow_over"} {
lappend features "DoCompare"
lappend features "DC_GT"
lappend features "DC_CompF"
set TriggerVal $props(exit_flow_over)
} else {
# no exit condition was checked
set TriggerVal 0
}
} else {
set TriggerVal 0
}
array set $frame_name [list FrameToWrite $cnt]
array set $frame_name [list Flag [make_shot_flag $features]]
array set $frame_name [list SetVal [convert_float_to_U8P4 $SetVal]]
array set $frame_name [list Temp [convert_float_to_U8P1 $props(temperature)]]
array set $frame_name [list FrameLen [convert_float_to_F8_1_7 $props(seconds)]]
array set $frame_name [list TriggerVal [convert_float_to_U8P4 $TriggerVal]]
# max water volume feature, per-step
array set $frame_name [list MaxVol [convert_float_to_U10P0 $props(volume)]]
#Extension Frame
if {[ifexists props(max_flow_or_pressure)] != 0 && [ifexists props(max_flow_or_pressure)] != {}} {
array set $extension_frame [list FrameToWrite [expr $cnt + 32]]
array set $extension_frame [list MaxFlowOrPressure [convert_float_to_U8P4 $props(max_flow_or_pressure)]]
array set $extension_frame [list MaxFoPRange [convert_float_to_U8P4 $props(max_flow_or_pressure_range)]]
array set $extension_frame [list Pad1 0]
array set $extension_frame [list Pad2 0]
array set $extension_frame [list Pad3 0]
array set $extension_frame [list Pad4 0]
array set $extension_frame [list Pad5 0]
lappend extension_frames $extension_frame
msg -DEBUG "Settings extension frame for " $cnt [array get $extension_frame]
}
incr cnt
}
set hdr(NumberOfFrames) $cnt
# advanced shots can define when to start counting pour
set NumberOfPreinfuseFrames [ifexists profile(final_desired_shot_volume_advanced_count_start)]
if {$NumberOfPreinfuseFrames == ""} {
set NumberOfPreinfuseFrames 0
}
if {[ifexists ::setting(insert_preinfusion_pause)] == 1} {
incr NumberOfPreinfuseFrames
}
set hdr(NumberOfPreinfuseFrames) $NumberOfPreinfuseFrames
set tail(FrameToWrite) $cnt
set tail(MaxTotalVolume) 0
set tail(Pad1) 0
set tail(Pad2) 0
set tail(Pad3) 0
set tail(Pad4) 0
set tail(Pad5) 0
return [make_chunked_packed_shot_sample hdr $frame_names $extension_frames tail]
}
# return two values as a list, with the 1st being the packed header, and the 2nd value itself
# being a list of packed frames
proc de1_packed_shot_wrapper {} {
if {[ifexists ::settings(settings_profile_type)] == "settings_2b"} {
return [de1_packed_shot [::profile::flow_to_advanced_list]]
} elseif {([ifexists ::settings(settings_profile_type)] == "settings_2c" || [ifexists ::settings(settings_profile_type)] == "settings_2c2")} {
return [de1_packed_shot [::profile::settings_to_advanced_list]]
} else {
return [de1_packed_shot [::profile::pressure_to_advanced_list]]
}
}
#
# a shot is a packed struct of this type:
#
# struct PACKEDATTR T_ShotDesc {
# U8P0 HeaderV; // Set to 1 for this type of shot description
# U8P0 NumberOfFrames; // Total number of frames.
# U8P0 NumberOfPreinfuseFrames; // Number of frames that are preinfusion
# U8P4 MinimumPressure; // In flow priority modes, this is the minimum pressure we'll allow
# U8P4 MaximumFlow; // In pressure priority modes, this is the maximum flow rate we'll allow
# T_ShotFrame Frames[10];
# };
#
# where T_ShotFrame is:
#
# struct PACKEDATTR T_ShotFrame {
# U8P0 Flag; // See T_E_FrameFlags
# U8P4 SetVal; // SetVal is a 4.4 fixed point number, setting either pressure or flow rate, as per mode
# U8P1 Temp; // Temperature in 0.5 C steps from 0 - 127.5
# F8_1_7 FrameLen; // FrameLen is the length of this frame. It's a 1/7 bit floating point number as described in the F8_1_7 a struct
# U8P4 TriggerVal; // Trigger value. Could be a flow or pressure.
# U10P0 MaxVol; // Exit current frame if the volume/weight exceeds this value. 0 means ignore
# };
#
proc shot_sample_spec {} {
set spec {
00_HeaderV {char {} {} {unsigned} {}}
00_NumberOfFrames {char {} {} {unsigned} {}}
00_NumberOfPreinfuseFrames {char {} {} {unsigned} {}}
00_MinimumPressure {char {} {} {unsigned} {$val / 16.0}}
00_MaximumFlow {char {} {} {unsigned} {$val / 16.0}}
01_Flag {char {} {} {unsigned} {}}
01_SetVal {char {} {} {unsigned} {$val / 16.0}}