-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multiline_Display.spin2
365 lines (279 loc) · 9.56 KB
/
Multiline_Display.spin2
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
{
Multiline Display
This object displays text in multiple lines
}
CON
DEBOUNCE_TIMER = 500
MAX_CHARACTERS = 180
MAX_LINES = 4
MAX_CHARS_PER_LINE = MAX_CHARACTERS/MAX_LINES
DARKEN_FACTOR = 6
'Text align constants
#0, LEFT_ALIGN, CENTER_ALIGN, RIGHT_ALIGN
'Multiline order constants
#0, TOP_BOTTOM, BOTTOM_TOP
VAR
'Attributes
long id 'Unique ID - (window index << 4)|(button index)
long bg_color 'background color
long fg_color 'foreground color
long ol_color 'outline = border
word xtop 'Top Left X (Pixel)
word ytop 'Top Left Y (Pixel)
word xbot 'Bottom Right X (Pixel)
word ybot 'Bottom Right Y (Pixel)
byte border_width 'Border width in pixels
'Method Pointers
long event_handler 'Default event handler method pointer
'Internal state variables
long last_time 'Stores touch event debouncer last time value
long value 'Virtual value of the widget
byte configured 'Boolean flag to identify configured widgets in the window's list
byte enabled 'Boolean flag to indicate widget can be drawn and can check for events
byte invert_border 'Boolean flag to invert border colors
'Characters variables
byte line_contents[MAX_CHARS_PER_LINE + 1]
byte contents[MAX_CHARACTERS+1] 'Should be 1 more than the displayed characters to account for the terminating zero
long string_pointer 'Hub address of string memory (addresses are LONG but memory access in hub is byte)
byte font_size
byte padding
byte align '0 = LEFT, 1 = CENTER, 2 = RIGHT
byte compression 'compression factor for the text
'Multiline variables
byte lines 'Current lines in multi-line display
'byte max_characters_per_line
byte order
OBJ
color : "Colors"
util : "Utilities"
graphics : "Graphics"
PUB null()
''+--------------------------------------------------------------------+
'' WIDGET INTERFACE METHODS
''+--------------------------------------------------------------------+
PUB set_enable(e)
if e
enabled := true
else
enabled := false
PUB draw()| w, h, x, y, tw, th
''This method draws the graphical representation of this widget type
if enabled
'FILL
graphics.fill_rectangle(xtop+border_width,ytop+border_width,xbot-border_width,ybot-border_width,bg_color)
'BORDER
util.draw_border(xtop,ytop,xbot,ybot,ol_color,border_width,invert_border)
'TEXT
w := (xbot - xtop) - 2*border_width
h := (ybot - ytop) - 2*border_width
tw := util.get_characters(string_pointer) * (font_size==1) ? 8 : 12
th := font_size*8
if align == LEFT_ALIGN
x := xtop + border_width + padding
if align == CENTER_ALIGN
x := xtop + border_width + (w - tw)/2
if align == RIGHT_ALIGN
x := xbot - border_width - tw - padding
y := ytop + border_width + padding'+ (h - th)/2
draw_multi_text(x,y)
PUB check_event(coord):is_mine | x, y
if configured AND enabled
x := coord.WORD[0]
y := coord.WORD[1]
if (x >= xtop AND x <= xbot) AND (y >= ytop AND y <= ybot)
is_mine := true
if event_handler <> 0
if (getms()-last_time) >= DEBOUNCE_TIMER 'debounce the event
event_handler(id) 'call the event handler and pass along id of button
last_time := getms() 'reset debounce timer
''+--------------------------------------------------------------------+
'' CONFIGURATION METHODS
''+--------------------------------------------------------------------+
PUB configure(i, xt, yt, w, h, bg, fg, ol)
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
set_font_size(2)
padding := 4
align := CENTER_ALIGN
order := TOP_BOTTOM
string_pointer := @contents
clear_display()
configured := true
last_time := getms()
PUB set_max_lines(m)
lines := m <# MAX_LINES
PUB set_order(o)
order := TOP_BOTTOM #> o <# BOTTOM_TOP
PUB set_contents(addr)|c,i
'Set new contents
repeat i from 0 to MAX_CHARACTERS-1 'Reserve last character place for the terminating 0
c := BYTE[addr][i]
if c <> 0
contents[i] := c
else
quit
contents[i+1] := 0
'string_pointer := @contents
PUB is_configured():conf
conf := configured
PUB set_font_size(s)
font_size := 1 #> s <# 2
if font_size == 2
compression := 6
else
compression := 1
''+--------------------------------------------------------------------+
'' AUXILIARY METHODS
''+--------------------------------------------------------------------+
PUB set_coordinates(xt, yt, xb, yb)
xtop := xt
ytop := yt
xbot := xb
ybot := yb
PUB get_coordinates() : coords
coords.BYTE[0] := xtop
coords.BYTE[1] := ytop
coords.BYTE[2] := xbot
coords.BYTE[3] := ybot
PUB set_colors(bg, fg, ol)
bg_color := bg
fg_color := fg
ol_color := ol
PUB get_colors(): bg, fg, ol
bg := bg_color
fg := fg_color
ol := ol_color
''+--------------------------------------------------------------------+
'' EVENT HANDLING METHODS
''+--------------------------------------------------------------------+
PUB set_event_handler(Method)
event_handler := Method 'Sets the default event handler of this object
PUB call_event_handler(Method)
Method(id) 'Calls a passed event handler
''+--------------------------------------------------------------------+
'' OTHER METHODS
''+--------------------------------------------------------------------+
PUB set_border_width(bw)
border_width := bw
PUB set_align(a)
align := LEFT_ALIGN #> a <# RIGHT_ALIGN
PUB set_invert(v)
invert_border := v
PUB set_compression(c)
compression := c
PUB draw_multi_text(x,y)| chars, n, w, h, k, charn, vertical_increment, newx, newy, _lines
{
ptr - pointer to drawing_method array of method pointers
x,y - coordinates of top left corner of text
buffer - address of text buffer (contents[])
font_size - scaling factor for 8x8 font
compression - factor to reduce distance between characters in pixels
bg_color - background color
fg_color - foreground color
}
graphics.select_font_size(1 #> font_size <# 2)
chars := util.get_characters(@contents)
w := font_size * 8
h := font_size * 8
vertical_increment := (font_size == 1) ? (10) : 18
charn := 0
_lines := 0
newy := y
newx := x
'Set colors
graphics.set_background_color(bg_color)
graphics.set_foreground_color(fg_color)
'Draw the value
repeat n from 0 to chars-1
if _lines >= MAX_LINES
quit
k := BYTE[@contents+n]
'New line character
if k == 10
newx := x
newy := (newy + vertical_increment) <# ((MAX_LINES) * vertical_increment)+y
charn := 0
_lines++
else
if (order == TOP_BOTTOM) AND (_lines > 0)
graphics.set_foreground_color(color.darken(fg_color,DARKEN_FACTOR))
if (font_size == 2) AND ((k == ($6C)) OR (k == ($69)) OR (k == ($3A)) OR (k == ($2E))) 'i, l, . , :
newx := newx - 2
graphics.set_window(newx+(w-compression)*charn, newy, newx+w, newy+h)
graphics.draw_char(k)
newx := newx - 2
else
graphics.set_window(newx+(w-compression)*charn, newy, newx+w, newy+h)
graphics.draw_char(k)
charn++
PUB writeln(addr)|c,i,chars,buffer_size
util.clear_string(@line_contents, MAX_CHARS_PER_LINE)
'util.debug_string(addr)
if order == TOP_BOTTOM
move_lines_down(addr)
else
move_lines_up(addr)
'util.debug_string(string_pointer)
lines++
draw()
PUB write_val(text, v)| val[16], message[MAX_CHARS_PER_LINE]
''Write a string (text) and a value of up to 16 digits
util.set_value(v,@val)
util.join_strings(text, @val, @message, MAX_CHARS_PER_LINE)
writeln(@message)
PUB clear_display()
util.clear_string(@contents, MAX_CHARACTERS)
PRI move_lines_down(addr)|i,last_pos,new_chars
'Count chars in new string
repeat new_chars from 0 to MAX_CHARS_PER_LINE-1
'if line_contents[new_chars] == 0
if BYTE[addr][new_chars] == 0
quit
if new_chars == 0
return
'Calculate last pos in contents to start moving
last_pos := (MAX_CHARACTERS-1) - (new_chars) - 2 '-1
'Make space at the beginning of contents
'for line_contents
repeat i from last_pos to 0
contents[i+new_chars+1] := contents[i]
'Copy chars in line_contents
repeat i from 0 to new_chars-1
contents[i] := BYTE[addr][i]
contents[i] := 10 'Add EOL/New Line/ \n character
'util.debug_string(string_pointer)
PRI move_lines_up(addr)|i,j, new_chars, contents_chars
'Count chars in new string
repeat new_chars from 0 to MAX_CHARS_PER_LINE-1
if Byte[addr][new_chars] == 0
quit
if new_chars == 0
return
contents_chars := util.get_characters(string_pointer)
if lines >= MAX_LINES-1
lines := MAX_LINES
repeat i from 0 to MAX_CHARS_PER_LINE-1
if contents[i] == 10
quit
repeat j from i to (MAX_CHARACTERS-1)
contents[j-i] := contents[j+1]
'debug(udec(j), udec(i), udec(j-i), udec(j+1), uhex(contents[j-i]), uhex(contents[j+1]))
if contents[j+1] == 0
quit
contents[j-i+1] := 0
contents_chars := util.get_characters(string_pointer)
'Add newline at the end of current contents
contents[contents_chars++] := 10
'Add new characters after newline
repeat i from 0 to new_chars-1
contents[contents_chars + i] := Byte[addr][i]
'Add EOL
contents[contents_chars + i] := 0