-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrend_Control.spin2
More file actions
405 lines (322 loc) · 11 KB
/
Trend_Control.spin2
File metadata and controls
405 lines (322 loc) · 11 KB
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
{
Trend Lean Object
This trend object does not have the data array.
Instead it must be passed a pointer to an existing data array
and the size of the data array.
The idea is to make the object leaner and avoid bloating it
with a pre-determined size of data values.
}
{ Based on Screen Object}
CON
MAX_WIDGETS = 8
'Button constants
#0, LEFT_BTN, RIGHT_BTN, MODE_BTN, MARKER_BTN, MAX_BUTTONS
'Text constants
#0, VALUE_TXT, POSITION_TXT, MAX_TEXTS
VAR
'Attributes
long bg_color 'background color
long fg_color 'foreground color
long ol_color 'outline = border
word xtop
word ytop
word xbot
word ybot
byte border_width
byte invert_border
long event_coordinates 'word[0]=x word[1]=y
long my_event_handler
long drawing_method_pointer
byte id
byte enabled
byte initialized
'Widgets method pointers
LONG widget_enable_method[MAX_WIDGETS]
LONG widget_draw_method[MAX_WIDGETS]
LONG widget_check_event_method[MAX_WIDGETS]
LONG widget_counter
'Trend variables
LONG data 'Pointer to values array
BYTE max_data_values 'Size of values array
byte marker_enabled
BYTE value_text[8]
BYTE buffer_text[12]
OBJ
util : "Utilities"
color: "Colors"
graphics : "Graphics"
trend: "Trend_Lean"
button[MAX_BUTTONS] : "Button"
text[MAX_TEXTS] : "Text_Display"
PUB null()
''+--------------------------------------------------------------------+
'' WIDGET INTERFACE METHODS
''+--------------------------------------------------------------------+
PUB set_enable(e):w
if e
enabled := true
else
enabled := false
if widget_counter > 0
repeat w from 0 to widget_counter-1
widget_enable_method[w](enabled)
if initialized == 0
init_ui()
initialized := 1
PUB draw()| w
if enabled
graphics.fill_rectangle(xtop,ytop,xbot,ybot,bg_color)
'BORDER
util.draw_border(xtop,ytop,xbot,ybot,ol_color,border_width,invert_border)
if widget_counter > 0
repeat w from 0 to widget_counter-1
widget_draw_method[w]()
PUB check_event(coord):is_mine | x, y, i
if enabled
x := coord.WORD[0]
y := coord.WORD[1]
'debug("Screen pre-coords", udec(x), udec(y))
if (x >= xtop AND x <= xbot) AND (y >= ytop AND y <= ybot)
is_mine := true
event_coordinates := coord
if widget_counter > 0
repeat i from 0 to widget_counter-1
if widget_check_event_method[i](event_coordinates):1
quit
PUB set_drawing_methods(METHOD_PTR)
drawing_method_pointer := METHOD_PTR
''+--------------------------------------------------------------------+
'' CONFIGURATION METHODS
''+--------------------------------------------------------------------+
PUB configure(i, addr, size, xt, yt, w, h, bg, fg, ol)|d
data := addr
max_data_values := size
id := i
xtop := xt
ytop := yt
xbot := w + xt 'xb
ybot := h + yt 'yb
bg_color := bg
fg_color := fg
ol_color := ol
border_width := 1
invert_border := false
PRI init_ui()|x,y,w,h
x := xtop + border_width
y := ytop + border_width
w := 40
h := 30
button[MODE_BTN].configure(MODE_BTN, x, y, w, h, color.lighten(color.grey,15), color.black, color.grey)
button[MODE_BTN].set_border_width(2)
button[MODE_BTN].set_compression(6)
button[MODE_BTN].set_padding(2)
button[MODE_BTN].set_contents(String("m"))
button[MODE_BTN].set_event_handler(@plot_mode_event_handler)
button[MODE_BTN].set_mode(button.TOGGLE_MODE)
button[MODE_BTN].set_enable(true)
add_widget(@button[MODE_BTN].set_enable, @button[MODE_BTN].draw, @button[MODE_BTN].check_event)
debug("Added button MODE_BTN")
x := xtop + border_width + 40
y := ytop + border_width
w := (xbot - xtop) - 2*border_width - 80
h := 30 'controls height
text[VALUE_TXT].configure(VALUE_TXT, x, y, w, h, bg_color, fg_color, ol_color)
text[VALUE_TXT].set_contents(String("0"))
text[VALUE_TXT].enable_buffering(false)
text[VALUE_TXT].set_align(text.RIGHT_ALIGN)
text[VALUE_TXT].set_padding(10)
text[VALUE_TXT].set_compression(6)
text[VALUE_TXT].set_border_width(1)
text[VALUE_TXT].set_invert(true)
text[VALUE_TXT].set_enable(true)
add_widget(@text[VALUE_TXT].set_enable, @text[VALUE_TXT].draw, @text[VALUE_TXT].check_event)
debug("Added text VALUE_TXT")
x := xbot - border_width - 40
y := ytop + border_width
w := 40
h := 30
button[MARKER_BTN].configure(MARKER_BTN, x, y, w, h, color.lighten(color.grey,15), color.black, color.grey)
button[MARKER_BTN].set_border_width(2)
button[MARKER_BTN].set_contents(String("x"))
button[MARKER_BTN].set_compression(6)
button[MARKER_BTN].set_event_handler(@marker_event_handler)
button[MARKER_BTN].set_mode(button.TOGGLE_MODE)
button[MARKER_BTN].set_enable(true)
add_widget(@button[MARKER_BTN].set_enable, @button[MARKER_BTN].draw, @button[MARKER_BTN].check_event)
debug("Added button MARKER_BTN")
x := xtop + border_width
y := ytop + border_width + 30
w := (xbot - xtop) - 2*border_width
h := (ybot - ytop) - 2*border_width - 2*30 'controls height
trend.configure(0, data, max_data_values, x, y, w, h, bg_color, fg_color, ol_color)
trend.set_auto_scale(false)
trend.set_border_width(2)
trend.enable_marker(false)
trend.set_enable(true)
add_widget(@trend.set_enable, @trend.draw, @trend.check_event)
x := xtop + border_width
y := ybot - border_width - 30
w := 40
h := 30 'controls height
button[LEFT_BTN].configure(LEFT_BTN, x, y, w, h, color.lighten(color.grey,15), color.black, color.grey)
button[LEFT_BTN].set_border_width(2)
button[LEFT_BTN].set_contents(String("<"))
button[LEFT_BTN].set_compression(6)
button[LEFT_BTN].set_debounce(100)
button[LEFT_BTN].set_event_handler(@marker_event_handler)
'button[LEFT_BTN].set_mode(button.TOGGLE_MODE)
button[LEFT_BTN].set_enable(true)
add_widget(@button[LEFT_BTN].set_enable, @button[LEFT_BTN].draw, @button[LEFT_BTN].check_event)
debug("Added button LEFT_BTN")
x := xbot - border_width - 40
y := ybot - border_width - 30
w := 40
h := 30 'controls height
button[RIGHT_BTN].configure(RIGHT_BTN, x, y, w, h, color.lighten(color.grey,15), color.black, color.grey)
button[RIGHT_BTN].set_border_width(2)
button[RIGHT_BTN].set_contents(String(">"))
button[RIGHT_BTN].set_compression(6)
button[RIGHT_BTN].set_debounce(100)
button[RIGHT_BTN].set_event_handler(@marker_event_handler)
'button[RIGHT_BTN].set_mode(button.TOGGLE_MODE)
button[RIGHT_BTN].set_enable(true)
add_widget(@button[RIGHT_BTN].set_enable, @button[RIGHT_BTN].draw, @button[RIGHT_BTN].check_event)
debug("Added button LEFT_BTN")
'POSITION_TXT
x := xtop + border_width + 40
y := ybot - border_width - 30
w := (xbot - xtop) - 2*border_width - 80
h := 30 'controls height
text[POSITION_TXT].configure(POSITION_TXT, x, y, w, h, bg_color, fg_color, ol_color)
text[POSITION_TXT].set_contents(String("p"))
text[POSITION_TXT].enable_buffering(false)
text[POSITION_TXT].set_align(text.RIGHT_ALIGN)
text[POSITION_TXT].set_padding(10)
text[POSITION_TXT].set_compression(6)
text[POSITION_TXT].set_border_width(1)
text[POSITION_TXT].set_invert(true)
text[POSITION_TXT].set_enable(true)
add_widget(@text[POSITION_TXT].set_enable, @text[POSITION_TXT].draw, @text[POSITION_TXT].check_event)
debug("Added text POSITION_TXT")
PUB plot()
trend.plot()
PUB clear_plot()
trend.clear_plot()
PUB set_data_value(val)
trend.set_data_value(val)
PUB plot_value(val)
trend.plot_value(val)
PUB enable_marker(e)
trend.enable_marker(e)
if e == false
text[VALUE_TXT].set_contents(@" ")
text[VALUE_TXT].draw()
else
text[VALUE_TXT].set_value(BYTE[data][max_data_values-1])
text[VALUE_TXT].draw()
PUB set_id(i)
id := i
PUB get_enable():e
e := enabled
PUB set_bg_color(c)|t, bg,fg,ol
bg_color := c
bg,fg,ol := trend.get_colors()
trend.set_colors(c,fg,ol)
repeat t from 0 to MAX_TEXTS-1
bg,fg,ol := text[t].get_colors()
text[t].set_colors(c, fg, ol)
PUB set_fg_color(c)
fg_color := c
PUB set_ol_color(c)
ol_color := c
PUB get_bg_color():bg
bg := bg_color
PUB get_fg_color():fg
fg := fg_color
PUB get_ol_color():ol
ol := ol_color
PUB set_ys(yt,yb)
ytop := yt
ybot := yb
PUB set_xs(xt,xb)
xtop := xt
xbot := xb
PUB set_coordinates(xt, yt, xb, yb)
xtop := xt
ytop := yt
xbot := xb
ybot := yb
PUB get_coordinates() : xt, yt, xb, yb
xt := xtop
yt := ytop
xb := xbot
yb := ybot
PUB set_border_width(bw)
border_width := bw
''+--------------------------------------------------------------------+
'' CONTAINER INTERFACE METHODS
''+--------------------------------------------------------------------+
PUB add_widget(enable_pointer, draw_pointer, check_event_pointer)
if widget_counter <= MAX_WIDGETS-1
widget_enable_method[widget_counter] := enable_pointer
widget_draw_method[widget_counter] := draw_pointer
widget_check_event_method[widget_counter] := check_event_pointer
widget_counter++
''+--------------------------------------------------------------------+
'' EVENT HANDLER METHODS
''+--------------------------------------------------------------------+
PUB plot_mode_event_handler(btn_id,val)
if val
trend.set_plot_mode(trend.PLOT_LINE)
text[VALUE_TXT].set_contents(@" ")
text[POSITION_TXT].set_contents(@" ")
else
trend.set_plot_mode(trend.PLOT_POINT)
if marker_enabled
util.set_value_ns(BYTE[data][trend.get_marker()],@value_text)
util.join_strings(@"V = ",@value_text,@buffer_text,12)
text[VALUE_TXT].set_contents(@buffer_text)
'text[VALUE_TXT].set_value(BYTE[data][max_data_values-1])
trend.draw()
text[VALUE_TXT].draw()
text[POSITION_TXT].draw()
PUB marker_event_handler(btn_id,val)
if btn_id == LEFT_BTN AND marker_enabled
if trend.get_marker() == 0
trend.set_marker((MAX_DATA_VALUES - 1))
else
trend.move_marker_left()
trend.draw()
if marker_enabled
util.set_value_ns(trend.get_value(),@value_text)
util.join_strings(@"V = ",@value_text,@buffer_text,12)
text[VALUE_TXT].set_contents(@buffer_text)
text[VALUE_TXT].draw()
util.set_value_ns(max_data_values - trend.get_marker(),@value_text)
util.join_strings(@"d = ",@value_text,@buffer_text,12)
text[POSITION_TXT].set_contents(@buffer_text)
text[POSITION_TXT].draw()
if btn_id == RIGHT_BTN AND marker_enabled
if trend.get_marker() == (MAX_DATA_VALUES - 1)
trend.set_marker(0)
else
trend.move_marker_right()
trend.draw()
if marker_enabled
util.set_value_ns(trend.get_value(),@value_text)
util.join_strings(@"V = ",@value_text,@buffer_text,12)
text[VALUE_TXT].set_contents(@buffer_text)
text[VALUE_TXT].draw()
util.set_value_ns(max_data_values - trend.get_marker(),@value_text)
util.join_strings(@"d = ",@value_text,@buffer_text,12)
text[POSITION_TXT].set_contents(@buffer_text)
text[POSITION_TXT].draw()
if btn_id == MARKER_BTN
marker_enabled := val
trend.enable_marker(val)
trend.draw()
if NOT marker_enabled
text[VALUE_TXT].set_contents(@" ")
text[VALUE_TXT].draw()
text[POSITION_TXT].set_contents(@" ")
text[POSITION_TXT].draw()