author | Edouard Tisserant |
Sat, 05 Dec 2020 16:59:27 +0100 | |
branch | svghmi |
changeset 3085 | 6b1b23971960 |
parent 3062 | 9ec338a99a18 |
child 3086 | a70a97196654 |
permissions | -rw-r--r-- |
2944 | 1 |
// widget_button.ysl2 |
2 |
||
3085 | 3 |
// Finite state machine |
4 |
decl fsm(name); |
|
5 |
decl state(name); |
|
6 |
decl on_mouse(position); |
|
7 |
decl on_dispatch(value); |
|
8 |
decl jump(state); |
|
9 |
decl show(eltname); |
|
10 |
decl hmi_value(value); |
|
11 |
||
12 |
// State machine to drive HMI_BOOL on a potentially laggy connection |
|
13 |
// TODO: make more robust in case other widget or PLC change value on their own |
|
14 |
const "_button_fsm" fsm { |
|
15 |
state "init" { |
|
16 |
on_dispatch "false" jump "released"; |
|
17 |
on_dispatch "true" jump "pressed"; |
|
18 |
} |
|
19 |
||
20 |
state "pressing" { |
|
21 |
// show "waitactive"; |
|
22 |
hmi_value "true"; |
|
23 |
on_dispatch "true" jump "pressed"; |
|
24 |
on_mouse "up" jump "shortpress"; |
|
25 |
} |
|
26 |
state "pressed" { |
|
27 |
show "active"; |
|
28 |
on_mouse "up" jump "releasing"; |
|
29 |
on_dispatch "false" jump "released"; |
|
30 |
} |
|
31 |
state "shortpress" { |
|
32 |
on_dispatch "true" jump "releasing"; |
|
33 |
on_mouse "down" jump "pressing"; |
|
34 |
} |
|
35 |
||
36 |
state "releasing" { |
|
37 |
// show "waitinactive"; |
|
38 |
hmi_value "false"; |
|
39 |
on_dispatch "false" jump "released"; |
|
40 |
on_mouse "down" jump "shortrelease"; |
|
41 |
} |
|
42 |
state "released" { |
|
43 |
show "inactive"; |
|
44 |
on_mouse "down" jump "pressing"; |
|
45 |
on_dispatch "true" jump "pressed"; |
|
46 |
} |
|
47 |
state "shortrelease" { |
|
48 |
on_dispatch "false" jump "pressing"; |
|
49 |
on_mouse "up" jump "releasing"; |
|
50 |
} |
|
51 |
} |
|
52 |
||
53 |
template "fsm", mode="dispatch_transition" { |
|
54 |
| switch (this.state) { |
|
55 |
apply "state", mode="dispatch_transition"; |
|
56 |
| } |
|
57 |
} |
|
58 |
template "state", mode="dispatch_transition" { |
|
59 |
| case "«@name»": |
|
60 |
apply "on-dispatch"; |
|
61 |
| break; |
|
62 |
} |
|
63 |
template "on-dispatch" { |
|
64 |
| if(value == «@value») { |
|
65 |
apply "jump", mode="transition"; |
|
66 |
| } |
|
67 |
} |
|
68 |
||
69 |
template "fsm", mode="mouse_transition" { |
|
70 |
param "position"; |
|
71 |
| switch (this.state) { |
|
72 |
apply "state", mode="mouse_transition" with "position", "$position"; |
|
73 |
| } |
|
74 |
} |
|
75 |
template "state", mode="mouse_transition" { |
|
76 |
param "position"; |
|
77 |
| case "«@name»": |
|
78 |
apply "on-mouse[@position = $position]"; |
|
79 |
| break; |
|
80 |
} |
|
81 |
template "on-mouse" { |
|
82 |
// up or down state is already assumed because apply statement filters it |
|
83 |
apply "jump", mode="transition"; |
|
84 |
} |
|
85 |
||
86 |
template "jump", mode="transition" { |
|
87 |
| this.state = "«@state»"; |
|
88 |
| this.«@state»_action(); |
|
89 |
} |
|
90 |
||
91 |
template "fsm", mode="actions" { |
|
92 |
apply "state", mode="actions"; |
|
93 |
} |
|
94 |
template "state", mode="actions" { |
|
95 |
| «@name»_action(){ |
|
96 |
//| console.log("Entering state «@name»"); |
|
97 |
apply "*", mode="actions"; |
|
98 |
| } |
|
99 |
} |
|
100 |
template "show", mode="actions" { |
|
101 |
| this.display = "«@eltname»"; |
|
102 |
| this.request_animate(); |
|
103 |
} |
|
104 |
template "hmi-value", mode="actions" { |
|
105 |
| this.apply_hmi_value(0, «@value»); |
|
106 |
} |
|
107 |
||
3024 | 108 |
template "widget[@type='Button']", mode="widget_class"{ |
3085 | 109 |
const "fsm","exsl:node-set($_button_fsm)"; |
110 |
| class ButtonWidget extends Widget{ |
|
111 |
| frequency = 5; |
|
3009 | 112 |
|
3085 | 113 |
| display = "inactive"; |
114 |
| state = "init"; |
|
3059
e0db3f6a5f39
Button and toggle reworked to use animate and dispatch
usveticic
parents:
3058
diff
changeset
|
115 |
|
3085 | 116 |
| dispatch(value) { |
117 |
// | console.log("dispatch"+value); |
|
118 |
apply "$fsm", mode="dispatch_transition"; |
|
119 |
| } |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3024
diff
changeset
|
120 |
|
3085 | 121 |
| onmouseup(evt) { |
122 |
| svg_root.removeEventListener("pointerup", this.bound_onmouseup, true); |
|
123 |
// | console.log("onmouseup"); |
|
124 |
apply "$fsm", mode="mouse_transition" with "position", "'up'"; |
|
125 |
| } |
|
126 |
| onmousedown(evt) { |
|
127 |
| svg_root.addEventListener("pointerup", this.bound_onmouseup, true); |
|
128 |
// | console.log("onmousedown"); |
|
129 |
apply "$fsm", mode="mouse_transition" with "position", "'down'"; |
|
130 |
| } |
|
3009 | 131 |
|
3085 | 132 |
apply "$fsm", mode="actions"; |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3024
diff
changeset
|
133 |
|
3085 | 134 |
| animate(){ |
135 |
| if (this.active_elt && this.inactive_elt) { |
|
136 |
foreach "str:split('active inactive')" { |
|
137 |
| if(this.display == "«.»") |
|
138 |
| this.«.»_elt.style.display = ""; |
|
139 |
| else |
|
140 |
| this.«.»_elt.style.display = "none"; |
|
141 |
} |
|
142 |
| } |
|
143 |
| } |
|
3009 | 144 |
|
3085 | 145 |
| init() { |
146 |
| this.bound_onmouseup = this.onmouseup.bind(this); |
|
147 |
| this.element.addEventListener("pointerdown", this.onmousedown.bind(this)); |
|
148 |
| } |
|
149 |
| } |
|
3024 | 150 |
} |
3009 | 151 |
|
152 |
||
2976
99c4521bb844
SVGHMI: Changed widget button handler to element attribute.
dgaberscek
parents:
2961
diff
changeset
|
153 |
template "widget[@type='Button']", mode="widget_defs" { |
99c4521bb844
SVGHMI: Changed widget button handler to element attribute.
dgaberscek
parents:
2961
diff
changeset
|
154 |
param "hmi_element"; |
99c4521bb844
SVGHMI: Changed widget button handler to element attribute.
dgaberscek
parents:
2961
diff
changeset
|
155 |
optional_labels("active inactive"); |
3009 | 156 |
|, |
3000
a9a45977bac0
SVGHMI: prefer apply_hmi_value() to change_hmi_value() when possible
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2976
diff
changeset
|
157 |
} |