-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgui.tcl
3035 lines (2326 loc) · 93.1 KB
/
gui.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_gui 1.3
package require de1_de1 1.1
package require de1_event 1.0
package require de1_logging 1.1
package require de1_plugins 1.0
package require de1_dui 1.0
package require de1_history_viewer 1.1
###
### ::gui namespace defined after globals
###
proc load_skin {} {
# optional callback for skins, which is reset to normal always, before loading the skin
eval {
proc skins_page_change_due_to_de1_state_change { textstate } {
page_change_due_to_de1_state_change $textstate
}
}
if {[catch {
source "[skin_directory]/skin.tcl"
} err opts_dict ] != 0} {
::logging::log_error_result_opts_dict $err $opts_dict
catch {
# reset the skin back to default, if their skin failed to load correctly
# but don't do so if ::debugging flag is enabled
if {[ifexists ::debugging] != 1} {
reset_skin
}
}
catch {
message_page [subst {[translate "Your choice of skin had an error and cannot be used."]}] [translate "Ok"] [strip_crlf $err]
}
msg -ERROR "Failed to 'load_skin' because: '$err'"
after 10000 exit
}
}
proc strip_crlf {in} {
regsub -all {\r|\n} $in {} out
return $out
}
proc page_change_due_to_de1_state_change {textstate} {
if {$textstate == "Idle"} {
page_display_change $::de1(current_context) "off"
} elseif {$textstate == "GoingToSleep"} {
page_display_change $::de1(current_context) "sleep"
} elseif {$textstate == "Sleep"} {
page_display_change $::de1(current_context) "saver"
} elseif {$textstate == "Steam"} {
page_display_change $::de1(current_context) "steam"
} elseif {$textstate == "Espresso"} {
page_display_change $::de1(current_context) "espresso"
} elseif {$textstate == "HotWater"} {
page_display_change $::de1(current_context) "water"
} elseif {$textstate == "Refill"} {
page_display_change $::de1(current_context) "tankempty"
} elseif {$textstate == "SteamRinse"} {
page_display_change $::de1(current_context) "steamrinse"
} elseif {$textstate == "HotWaterRinse"} {
page_display_change $::de1(current_context) "hotwaterrinse"
} elseif {$textstate == "Descale"} {
page_display_change $::de1(current_context) "descaling"
} elseif {$textstate == "Clean"} {
page_display_change $::de1(current_context) "cleaning"
} elseif {$textstate == "AirPurge"} {
page_display_change $::de1(current_context) "travel_do"
}
}
proc setup_images_for_other_pages {} {
borg spinner on
load_skin
borg spinner off
borg systemui $::android_full_screen_flags
return
}
proc chart_refresh {} {
}
proc Double2Fraction { dbl {eps 0.000001}} {
for {set den 1} {$den<1024} {incr den} {
set num [expr {round($dbl*$den)}]
if {abs(double($num)/$den - $dbl) < $eps} break
}
list $num $den
}
proc photoscale {img sx {sy ""} } {
if {($::android == 1 && $::undroid != 1)} {
#photoscale_not_android $img $sx $sy
photoscale_android $img $sx $sy
} elseif {$::undroid == 1} {
# no undroid support for this yet
photoscale_android $img $sx $sy
#photoscale_not_android $img $sx $sy
} else {
photoscale_not_android $img $sx $sy
}
}
proc photoscale_not_android {img sx {sy ""} } {
msg -DEBUG "photoscale $img $sx $sy"
if { $sx == 1 && ($sy eq "" || $sy == 1) } {
return; # Nothing to do!
}
foreach {sx_m sx_f} [Double2Fraction $sx] break
if { $sy eq "" } {
foreach {sy sy_x sy_f} [list $sx $sx_m $sx_f] break; # Multi-set!
} else {
foreach {sy_m sy_f} [Double2Fraction $sy] break
}
set tmp [image create photo]
if {[catch {
catch {$tmp copy $img -zoom $sx_m $sy_m -compositingrule set}
} err] != 0} {
# note that not all resolution resizes will work. Some take more memory than is available,
# especially if the resolution change is a long float, and not an even number (ie, 2560->2559)
msg -ERROR "photoscale_not_android failed because: '$err'"
}
$img blank
$img copy $tmp -shrink -subsample $sx_f $sy_f -compositingrule set
image delete $tmp
}
proc photoscale_android {img sx {sy ""} } {
msg -DEBUG "photoscale $img $sx $sy"
if { $sx == 1 && ($sy eq "" || $sy == 1) } {
return; # Nothing to do!
}
# create a new tmp image
set tmp [image create photo]
# resize to the tmp image
$tmp copy $img -scale $sx $sy
# recreate the original image and copy the tmp over it
image delete $img
image create photo $img
$img copy $tmp
# clean up
image delete $tmp
}
proc add_de1_page {names filename {skin ""} } {
dui page add $names -bg_img $filename
}
proc set_de1_screen_saver_directory {{dirname {}}} {
# force use of our default saver directory if the black screen saver is enabled, otherwise use whatever the skin chooses
if {$::settings(screen_saver_change_interval) == 0} {
set dirname "[homedir]/saver"
}
global saver_directory
if {$dirname != ""} {
set saver_directory $dirname
}
msg -INFO "set_de1_screen_saver_directory: $::saver_directory"
#set pngfilename [random_saver_file]
set names "saver"
#image create photo $names -file $pngfilename
image create photo $names
foreach name $names {
.can create image {0 0} -anchor nw -image $names -tag [list saver $name] -state hidden
}
setup_display_time_in_screen_saver
}
proc setup_display_time_in_screen_saver {} {
if {$::settings(display_time_in_screen_saver) != 1} {
return
}
set ::clocktime [clock seconds]
set ::previous_clocktime 0
if {$::settings(screen_saver_change_interval) == 0} {
# black screen saver
set ::saver_clock2 [add_de1_variable "saver" 1278 898 -justify center -anchor "center" -text "" -font Helv_30_bold -fill "#111111" -width 2000 -textvariable {[time_format $::clocktime 1]}]
set ::saver_clock3 [add_de1_variable "saver" 1282 902 -justify center -anchor "center" -text "" -font Helv_30_bold -fill "#222222" -width 2000 -textvariable {[time_format $::clocktime 1]}]
set ::saver_clock [add_de1_variable "saver" 1280 900 -justify center -anchor "center" -text "" -font Helv_30_bold -fill "#444444" -width 2000 -textvariable {[time_format $::clocktime 1]}]
} else {
set ::saver_clock2 [add_de1_variable "saver" 1278 898 -justify center -anchor "center" -text "" -font Helv_30_bold -fill "#CCCCCC" -width 2000 -textvariable {[time_format $::clocktime 1]}]
set ::saver_clock3 [add_de1_variable "saver" 1282 902 -justify center -anchor "center" -text "" -font Helv_30_bold -fill "#666666" -width 2000 -textvariable {[time_format $::clocktime 1]}]
set ::saver_clock [add_de1_variable "saver" 1280 900 -justify center -anchor "center" -text "" -font Helv_30_bold -fill "#F8F8F8" -width 2000 -textvariable {[time_format $::clocktime 1]}]
}
after 1000 saver_clock_move
proc saver_clock_move {} {
set ::clocktime [clock seconds]
set force 0
if {[time_format $::clocktime] != [time_format $::previous_clocktime] || $force == 1} {
set newx [expr {[rescale_x_skin 600] + (rand() * [rescale_x_skin 1400])}]
set newy [expr {[rescale_y_skin 200] + (rand() * [rescale_y_skin 1200])}]
set newx2 [expr {$newx - [rescale_x_skin 2]}]
set newy2 [expr {$newy - [rescale_y_skin 2]}]
set newx3 [expr {$newx + [rescale_x_skin 2]}]
set newy3 [expr {$newy + [rescale_y_skin 2]}]
.can coords $::saver_clock2 "$newx2 $newy2"
.can coords $::saver_clock3 "$newx3 $newy3"
.can coords $::saver_clock "$newx $newy"
set ::previous_clocktime $::clocktime
}
after 1000 saver_clock_move
}
}
proc vertical_slider {varname minval maxval x y x0 y0 x1 y1} {
set yrange [expr {$y1 - $y0}]
set yoffset [expr {$y - $y0}]
set range [expr {($yoffset * 1.0)/$yrange}]
set destrange [expr {$maxval - $minval}]
set gain [expr {$destrange * $range}]
set finalvalue [expr {$maxval - $gain}]
if {$finalvalue < $minval} {
set finalvalue $minval
} elseif {$finalvalue > $maxval} {
set finalvalue $maxval
}
#set $var $finalvalue
eval set $varname $finalvalue
}
# on android we track finger-down, instead of button-press, as it gives us lower latency by avoding having to distinguish a potential gesture from a tap
# finger down gives a http://blog.tcl.tk/39474
proc translate_coordinates_finger_down_x { x } {
return [dui::platform::translate_coordinates_finger_down_x $x]
}
proc translate_coordinates_finger_down_y { y } {
return [dui platform translate_coordinates_finger_down_y $y ]
}
proc is_fast_double_tap { key } {
return [dui platform is_fast_double_tap $key]
}
proc vertical_clicker {bigincrement smallincrement varname minval maxval x y x0 y0 x1 y1 {b 0} } {
# b = which button was tapped
set x [translate_coordinates_finger_down_x $x]
set y [translate_coordinates_finger_down_y $y]
set yrange [expr {$y1 - $y0}]
set yoffset [expr {$y - $y0}]
set midpoint [expr {$y0 + ($yrange / 2)}]
set onequarterpoint [expr {$y0 + ($yrange / 4)}]
set threequarterpoint [expr {$y1 - ($yrange / 4)}]
set onethirdpoint [expr {$y0 + ($yrange / 3)}]
set twothirdpoint [expr {$y1 - ($yrange / 3)}]
if {[info exists $varname] != 1} {
# if the variable doesn't yet exist, initialize it with a zero value
set $varname 0
}
set currentval [subst \$$varname]
set newval $currentval
# check for a fast double tap
set b 0
if {[is_fast_double_tap $varname] == 1} {
#set the button to 3, which is the same as a long press, or middle button (ie button 3) on a mouse
set b 3
}
if {$y < $onethirdpoint} {
if {$b == 3} {
set newval [expr "1.0 * \$$varname + $bigincrement"]
} else {
set newval [expr "1.0 * \$$varname + $smallincrement"]
}
} elseif {$y > $twothirdpoint} {
if {$b == 3} {
set newval [expr "1.0 * \$$varname - $bigincrement"]
} else {
set newval [expr "1.0 * \$$varname - $smallincrement"]
}
}
set newval [round_to_two_digits $newval]
if {$newval > $maxval} {
set $varname $maxval
} elseif {$newval < $minval} {
set $varname $minval
} else {
set $varname [round_to_two_digits $newval]
}
update_onscreen_variables
return
}
proc random_pick {lst} {
set pick [expr {int(rand() * [llength $lst])}]
return [lindex $lst $pick]
}
proc ifexists {fieldname2 {defvalue {}} } {
upvar $fieldname2 fieldname
if {[info exists fieldname] == 1} {
return [subst "\$fieldname"]
} else {
if {$defvalue != ""} {
set fieldname $defvalue
return $defvalue
} else {
return ""
}
}
}
proc set_dose_goal_weight {weight} {
global current_weight_setting
set current_weight_setting $weight
.can itemconfig .current_setting_grams_label -text [round_one_digits $weight]
}
proc round_one_digits {amount} {
set x $amount
catch {set x [expr round($amount * 10)/10.00]}
return $x
}
proc canvas'textvar {canvas tag _var args} {
msg -WARNING "Unexpected use of canvas'textvar"
upvar 1 $_var var
if { [llength $args] } {
$canvas itemconfig $tag -text $var
} else {
uplevel 1 trace var $_var w \
[list [list canvas'textvar $canvas $tag]]
}
}
# optional "alsobind" allows us to put text on top of a pressable button and have the text also bound to the same down/up/leave actions
proc up_down_button_create {actionscript btn_images img_loc key_loc buttontype {alsobind {}} } {
msg -DEBUG "up_down_button_create"
if {$buttontype != "onetime" && $buttontype != "holdrepeats"} {
msg -ERROR "unknown buttontype $buttontype"
return
}
set img_xloc [lindex $img_loc 0]
set img_yloc [lindex $img_loc 1]
set up_png [lindex $btn_images 0]
set down_png [lindex $btn_images 1]
set key_xloc [lindex $key_loc 0]
set key_yloc [lindex $key_loc 1]
set key_xdistance [lindex $key_loc 2]
set key_ydistance [lindex $key_loc 3]
image create photo ${up_png}_img -file $up_png
image create photo ${down_png}_img -file $down_png
.can create image [list $img_xloc $img_yloc] -anchor nw -image ${down_png}_img -tag $down_png -state hidden
.can create image [list $img_xloc $img_yloc] -anchor nw -image ${up_png}_img -tag $up_png -state hidden
set rect "${up_png}_rect"
.can create rect $key_xloc $key_yloc [expr {$key_xloc + $key_xdistance}] [expr {$key_yloc + $key_ydistance}] -fill {} -outline white -width 0 -tag $rect
lappend alsobind $rect
foreach tobind $alsobind {
.can bind $tobind [platform_button_press] [list generic_push_button_settings $up_png $down_png $actionscript down $buttontype]
.can bind $tobind [platform_button_unpress] [list generic_push_button_settings $up_png $down_png $actionscript up $buttontype]
.can bind $tobind <Leave> [list generic_push_button_settings $up_png $down_png $actionscript leave $buttontype]
lappend image_tags_created $up_png $down_png
}
return
}
proc generic_push_button_settings {btnup btndown action change buttontype} {
global genericstate
if {$change == "down"} {
#borg beep
.can itemconfigure $btndown -state normal
.can itemconfigure $btnup -state hidden
update
set genericstate($btndown) "down"
if {$buttontype == "holdrepeats"} {
set afterid [after 700 [list generic_button_held $btnup $btndown $action]]
set genericstate($btnup) $afterid
}
} else {
if {$buttontype == "holdrepeats"} {
if {[ifexists genericstate($btnup)] != ""} {
# cancel the held-button timer when they
after cancel $genericstate($btnup)
}
}
if {$change == "leave"} {
if {[ifexists genericstate($btndown)] != "down"} {
return
}
.can itemconfigure $btnup -state normal
.can itemconfigure $btndown -state hidden
update
set genericstate($btndown) "up"
set genericstate($btnup) ""
} elseif {$change == "up"} {
# this is the up button event
if {$genericstate($btndown) == "up"} {
return
}
.can itemconfigure $btnup -state normal
.can itemconfigure $btndown -state hidden
update
set genericstate($btnup) ""
if {$genericstate($btndown) != "held"} {
eval $action
}
set genericstate($btndown) "up"
} else {
msg -ERROR "unknown action $change"
}
}
}
proc generic_button_held {btnup btndown action} {
global genericstate
# button has been pressed down for a while, so activate a down/up press
if {$genericstate($btndown) == "held" || $genericstate($btndown) == "down"} {
set genericstate($btndown) "held"
eval $action
update
after 150 [list generic_button_held $btnup $btndown $action]
} elseif {$genericstate($btndown) == "up"} {
#no longer held
} else {
msg -ERROR "unknown held state: '$genericstate($btndown)'"
}
}
proc appdir {} {
return [file dirname [DirectPathname .]]
}
# copied from https://wiki.tcl-lang.org/page/Making+a+Path+Absolute
proc DirectPathname {filename} {
set savewd [pwd]
set realFile [file join $savewd $filename]
# Hmm. This (unusually) looks like a job for do...while!
cd [file dirname $realFile]
set dir [pwd] ;# Always gives a canonical directory name
set filename [file tail $realFile]
while {![catch {file readlink $filename} realFile]} {
cd [file dirname $realFile]
set dir [pwd]
set filename [file tail $realFile]
}
cd $savewd
return [file join $dir $filename]
}
proc install_update_app_icon {} {
package require base64
set icondata_de1 [read_binary_file "[appdir]/cloud_download_icon.png"]
set iconbase64_de1 [::base64::encode -maxlen 0 $icondata_de1]
if {$icondata_de1 == ""} {
set icondata_de1 [read_binary_file "cloud_download_icon.png"]
}
set appurl "file://[appdir]/appupdate.tcl"
catch {
set x [borg shortcut add "Decent Update" $appurl $iconbase64_de1]
msg -NOTICE "shortcut added: '$x'"
}
}
proc install_de1_app_icon {} {
package require base64
set icondata_de1 [read_binary_file "[appdir]/de1_icon_v2.png"]
if {$icondata_de1 == ""} {
set icondata_de1 [read_binary_file "de1_icon_v2.png"]
}
set iconbase64_de1 [::base64::encode -maxlen 0 $icondata_de1]
set appurl "file://[appdir]/de1.tcl"
msg -DEBUG "appurl: $appurl"
catch {
set x [borg shortcut add "DE1" $appurl $iconbase64_de1]
msg -NOTICE "shortcut added: '$x'"
}
}
proc install_de1plus_app_icon {} {
package require base64
msg -DEBUG "icon file: '[appdir]/de1plus_icon_v2.png'"
set icondata_de1plus [read_binary_file "[appdir]/de1plus_icon_v2.png"]
set iconbase64_de1plus [::base64::encode -maxlen 0 $icondata_de1plus]
set appurl "file://[appdir]/de1plus.tcl"
#catch {
set x [borg shortcut add "Decent" $appurl $iconbase64_de1plus]
msg -NOTICE "shortcut added: '$x'"
#}
}
proc platform_button_press {} {
return [dui platform button_press]
}
proc platform_button_native_press {} {
return {<ButtonPress-1>}
}
proc platform_button_long_press {} {
return [dui platform button_long_press]
}
proc platform_finger_down {} {
return [dui platform finger_down]
}
proc platform_button_unpress {} {
return [dui platform button_unpress]
}
proc add_variable_item_to_context {context label_name varcmd} {
msg -WARNING "add_variable_item_to_context is DEPRECATED, please use 'dui page add_variable' instead"
# This may or may not work as dui::page::add_variable takes canvas IDs instead of labels.
dui::page::add_variable $context $label_name $varcmd
}
proc add_visual_item_to_context {context label_name} {
dui page add_items $context $label_name
}
#set button_cnt 0
proc add_de1_action {context tclcmd} {
dui page add_action $context show $tclcmd
}
proc add_de1_button {displaycontexts tclcode x0 y0 x1 y1 {options {}}} {
return [dui add dbutton $displaycontexts $x0 $y0 $x1 $y1 -command $tclcode -theme none]
}
# truncates strings that are too long to display and add a ...message on the end.
proc maxstring {in maxlength {optmsg {}} } {
if {[string length $in] > $maxlength} {
return "[string range $in 0 $maxlength]...$optmsg"
}
return $in
}
# truncates strings that are too long to display and add a ...message on the end.
# this version counts paragraph breaks as having "crlfequiv" equivalent characters
proc maxstring_with_crlf_count {in maxlength crlfequiv {optmsg {}} } {
set crlfs [regexp -all {\n\n} [string range $in 0 $maxlength]]
set thislen [expr {[string length $in] + ($crlfs * $crlfequiv)}]
if {$thislen > $maxlength} {
return "[string range $in 0 [expr {$maxlength - ($crlfs * $crlfequiv)}]]...$optmsg"
}
return $in
}
#set text_cnt 0
proc add_de1_text {args} {
return [dui add dtext [lindex $args 0] [lindex $args 1] [lindex $args 2] -compatibility_mode 1 {*}[lrange $args 3 end]]
}
#set image_cnt 0
proc add_de1_image {args} {
return [dui add image [lindex $args 0] [lindex $args 1] [lindex $args 2] [lindex $args 3] -theme none]
}
# derivced from sample code at http://wiki.tcl.tk/17067
#set widget_cnt 0
proc add_de1_widget {args} {
return [dui add widget [lindex $args 1] [lindex $args 0] [lindex $args 2] [lindex $args 3] -tclcode [lindex $args 4] \
-theme none {*}[lrange $args 5 end]]
}
proc add_de1_variable {args} {
set varcmd [lindex [unshift args] 0]
set lastcmd [unshift args]
if {$lastcmd != "-textvariable"} {
msg -WARN add_de1_variable "last -command needs to be -textvariable on a add_de1_variable line. You entered: '$lastcmd'"
return
}
return [dui add variable [lindex $args 0] [lindex $args 1] [lindex $args 2] -compatibility_mode 1 \
-textvariable $varcmd {*}[lrange $args 3 end]]
# set contexts [lindex $args 0]
# set label_name [eval add_de1_text $args]
#
# # john 24-1-20 now unneeded code https://3.basecamp.com/3671212/buckets/7351439/messages/2360038011
# #set x [rescale_x_skin [lindex $args 1]]
# #set y [rescale_y_skin [lindex $args 2]]
# #set torun [concat [list .can create text] $x $y [lrange $args 3 end] -tag $label_name -state hidden]
# #eval $torun
# #incr ::text_cnt
#
# foreach context $contexts {
# # keep track of what labels are displayed in what contexts
# add_variable_item_to_context $context $label_name $varcmd
# }
# return $label_name
}
proc stop_screen_saver_timer {} {
if {[info exists ::screen_saver_alarm_handle] == 1} {
after cancel $::screen_saver_alarm_handle
unset -nocomplain ::screen_saver_alarm_handle
}
}
proc delay_screen_saver {} {
stop_screen_saver_timer
if {$::settings(screen_saver_delay) != 0 } {
set ::screen_saver_alarm_handle [after [expr {60 * 1000 * $::settings(screen_saver_delay)}] "show_going_to_sleep_page"]
}
}
proc after_info {} {
set t {}
foreach id [after info] {
append t $id:[after info $id]\n
}
return $t
}
proc show_going_to_sleep_page {} {
if {$::settings(scheduler_enable) == 1} {
set wake [current_alarm_time $::settings(scheduler_wake)]
set sleep [current_alarm_time $::settings(scheduler_sleep)]
if {[clock seconds] > $wake && [clock seconds] < $sleep} {
msg -INFO "Delaying screen saver because we are during scheduled forced-awake time"
delay_screen_saver
return
}
}
if {$::de1_num_state($::de1(state)) != "Idle"} {
# never go to sleep if the DE1 is not idle
msg -INFO "delaying screen saver because de1 is not idle: '$::de1_num_state($::de1(state))'"
delay_screen_saver
return
}
if {[ifexists ::app_updating] == 1} {
msg -INFO "delaying screen saver because tablet app is updating"
delay_screen_saver
return
}
if {$::de1(currently_updating_firmware) == 1 || [ifexists ::de1(in_fw_update_mode)] == 1} {
msg -INFO "delaying screen saver because firmware is updating"
delay_screen_saver
return
}
msg -INFO "show_going_to_sleep_page"
if {$::de1(current_context) == "sleep" || $::de1(current_context) == "saver"} {
return
}
page_display_change $::de1(current_context) "sleep"
start_sleep
}
proc resized_filename {infile} {
set resized_filename "[file rootname $infile]-resized-${::screen_size_width}x${::screen_size_height}.jpg"
#set resized_filename "[file rootname $infile]-${::screen_size_width}x${::screen_size_height}.png"
return $resized_filename
}
proc change_screen_saver_img {} {
if {[llength [ifexists ::saver_files_cache]] == 1} {
# no need to change the background screen saver image if it's only 1
return
}
#if {$::de1(current_context) == "saver"} {
#catch {
# image delete is not needed, as Tk silently replaces the existing image if the object has the same name
#image delete saver
#}
set fn [random_saver_file]
set err ""
set errcode [catch {
# this can happen during an upgrade
image create photo saver -file $fn
# BUG FIX: this was causing a new canvas item to be created each time a screen saver object was created
# switching to itemconfigure instead
# .can create image {0 0} -anchor nw -image saver -tag saver -state hidden
#.can itemconfigure -anchor nw -image saver -tag saver -state hidden
#.can lower saver
} err]
if {$errcode != 0} {
error $err
}
#update
#}
if {[info exists ::change_screen_saver_image_handle] == 1} {
after cancel $::change_screen_saver_image_handle
unset -nocomplain ::change_screen_saver_image_handle
}
if {$::settings(screen_saver_change_interval) != 0} {
set ::change_screen_saver_image_handle [after [expr {60 * 1000 * $::settings(screen_saver_change_interval)}] change_screen_saver_img]
}
#set ::change_screen_saver_image_handle [after 1 change_screen_saver_img]
#set ::change_screen_saver_image_handle [after 100 change_screen_saver_img]
}
proc update_chart {} {
espresso_elapsed notify now
}
proc de1_connected_state { {hide_delay 0} } {
set hide_delay $::settings(display_connected_msg_seconds)
set since_last_ping [expr {[clock seconds] - $::de1(last_ping)}]
set elapsed [expr {[clock seconds] - $::de1(connect_time)}]
if {$::android == 0} {
if {$elapsed > $hide_delay && $hide_delay != 0} {
if {$::de1(substate) != 0} {
return [translate Wait]
}
return ""
} else {
return [translate Connected]
}
}
if {$since_last_ping < 5} {
#borg spinner off
if {$::de1(substate) != 0} {
if {$::de1(substate) == 4 || $::de1(substate) == 5} {
# currently making espresso.
return ""
}
return [translate Wait]
}
if {$elapsed > $hide_delay && $hide_delay != 0} {
# only show the "connected" message for 5 seconds
return ""
}
#return "[translate Connected] : $elapsed"
#borg toast "[translate Connected]"
#borg toast "[translate Connected]"
return [translate Connected]
#return "[translate Connected] $elapsed [translate seconds] - last ping: $::de1(last_ping) $::de1_device_list"
} else {
# if {[ifexists ::de1(in_fw_update_mode)] == 1} {
# return ""
# }
if {$::de1(device_handle) == 0} {
#return "[translate Connecting]"
if {$elapsed > 600} {
if {$::scanning == 1} {
return "[translate Searching]"
} elseif {$::scanning == -1} {
return [translate "Disconnected"]
#return "[translate Starting]"
}
return "[translate Connecting]"
} else {
if {$::scanning == 1} {
return "[translate Searching] : $elapsed"
} else {
return "[translate Connecting] : $elapsed"
}
return "[translate Connecting] : $elapsed"
}
} else {
if {$since_last_ping > 59} {
#ble_find_de1s
#ble_connect_to_de1
return [translate "Disconnected"]
}
return [subst {[translate "Disconnected"] : $since_last_ping}]
}
}
}
#
# Proc to generate a string of (given) characters
# Range defaults to "ABCDEF...wxyz'
#
proc randomRangeString {length {chars "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}} {
set range [expr {[string length $chars]-1}]
set txt ""
for {set i 0} {$i < $length} {incr i} {
set pos [expr {int(rand()*$range)}]
append txt [string range $chars $pos $pos]
}
return $txt
}
proc cancel_borg_notifications {} {
borg notification delete
}
proc display_popup_android_message_if_necessary {intxt} {
if {[string first "*" $intxt] != -1} {
# beep if a * is found in the description
borg beep
}
set msg ""
regexp {\[(.*?)\]} $intxt discard msg
if {$msg != ""} {
# post the message 1 second after the start, so that there's a slight delay
after 1000 [list borg toast $msg 1]
msg -DEBUG "Popup: $msg"
}
}
# For JB's GUI-driver, get some reasonable "t0" to derive DE1 SampleTime
# It also needs the period, in seconds, which can't be determined from the DE1
# ::settings() hasn't been loaded as this point, nor has ::machine(hertz)
# With no viable information available, hard-wire 50 Hz for now
if {$::android == 0} {
namespace eval ::gui {
variable _arbitrary_t0 [expr { [clock milliseconds] / 1000.0 }]
variable _st_period [expr { 1.0 / ( 2.0 * 50 ) }]
msg -DEBUG "GUI driver using 50 Hz, hard-wired, for DE1 SampleTime"
}
}
set _last_st 0
# TODO (EB): Move the first part of this proc to a parametrized action run from 'dui page update_onscreen_variables'
proc update_onscreen_variables { {state {}} } {
dui page update_onscreen_variables
return
}
# Define fake / dummy espresso variables on workstations
proc set_dummy_espresso_vars {} {
if { $::android } { return }
if {[expr {int(rand() * 100)}] > 96} {
set ::gui::state::_state_change_chart_value \
[expr {$::gui::state::_state_change_chart_value * -1}]
if {[expr {rand()}] > 0.5} {
set ::settings(current_frame_description) [translate "pouring"]
} else {
set ::settings(current_frame_description) [translate "preinfusion"]
}
}
if {$::de1(state) == 2} {
# idle
if {$::de1(substate) == 0} {
if {[expr {int(rand() * 100)}] > 92} {
# occasionally set the de1 to heating mode
#set ::de1(substate) 1
#update_de1_state "$::de1_state(Idle)\x1"
}
} else {
if {[expr {int(rand() * 100)}] > 90} {
# occasionally set the de1 to heating mode
update_de1_state "$::de1_state(Idle)\x0"
}
}
} elseif {$::de1(state) == 4} {
# espresso
if {$::de1(substate) == 0} {
} elseif {$::de1(substate) < 4} {
if {[expr {int(rand() * 100)}] > 80} {
# occasionally set the de1 to heating mode
#set ::de1(substate) 4
update_de1_state "$::de1_state(Espresso)\x4"
}
} elseif {$::de1(substate) == 4} {
if {[expr {int(rand() * 100)}] > 80} {
# occasionally set the de1 to heating mode
#set ::de1(substate) 5
update_de1_state "$::de1_state(Espresso)\x5"
}
}
}
#set timerkey "$::de1(state)-$::de1(substate)"
#set ::timers($timerkey) [clock milliseconds]
#if {$::de1(substate) > 6} {
# set ::de1(substate) 0
#}
# JB's GUI driver needs an event_dict
# NB: This seems to be getting called at a 10 Hz rate
# which is faster than the DE1's 25/(2 * line frequency)