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
|
const FW = 8 # glyph width in pixels
const FH = 16 # glyph height in pixels (8x16 VGA bitmap font)
const FG = 0xd0d0d0
const CON_BG = 0x101018
global con = { col = 0, row = 0, scale = 1, cur_on = :true, blink = 0 }
# cell size in pixels at the current zoom. named cell_* (not cw/ch) so they don't
# collide with draw_glyph's `ch` character parameter.
fn cell_w() FW * con.scale
fn cell_h() FH * con.scale
fn con_cols() fb_width() / cell_w()
fn con_rows() fb_height() / cell_h()
fn draw_glyph(col, row, ch) do
const x0 = col * cell_w()
const y0 = row * cell_h()
fill_rect(x0, y0, cell_w(), cell_h(), CON_BG) # clear the cell
const code = ch:ascii()
if code < 32 return :nil
if code > 127 return :nil
const g = FONT[code - 32]
for fy in 0..FH do
const bits = g[fy]
for fx in 0..FW do
if (bits shr fx) band 1 == 1 fill_rect(x0 + fx * con.scale, y0 + fy * con.scale, con.scale, con.scale, FG)
end
end
end
fn con_newline() do
con.col = 0
con.row += 1
if con.row >= con_rows() do
fb_scroll(cell_h(), CON_BG)
con.row = con_rows() - 1
end
end
# we dont do Shift/Cntrl
# thats a job for future me lmao
fn con_putc(ch) do
const code = ch:ascii()
if code == 10 do con_newline() return :nil end # \n
if code == 13 do con.col = 0 return :nil end # \r
if code == 8 do # backspace: move left
if con.col > 0 con.col -= 1
return :nil
end
if code < 32 return :nil
draw_glyph(con.col, con.row, ch)
con.col += 1
if con.col >= con_cols() con_newline()
end
fn puts(s) serial_puts(s)
const CUR = 0x50fa7b
fn cursor_paint(color) fill_rect(con.col * cell_w(), con.row * cell_h(), cell_w(), cell_h(), color)
global flush_console = fn() do
cursor_paint(CON_BG)
let c = serial_next()
while c >= 0 do
con_putc(string.of_ascii(c))
c = serial_next()
end
# after any output the cursor is solid at the new head restart the blink phase
con.cur_on = :true
con.blink = 0
cursor_paint(CUR)
end
# the PIT fires ~100x/sec
# blink the cursor every 50 ticks (~2 Hz)
global on_tick = fn(t) do
con.blink += 1
if con.blink >= 50 do
con.blink = 0
con.cur_on = not con.cur_on
cursor_paint(if con.cur_on CUR else CON_BG)
end
end
|