Skip to content

Commit fb890be

Browse files
authored
Merge pull request #12 from Joe7M/master
Add SSD1306
2 parents 711f708 + 1d8fcb7 commit fb890be

File tree

3 files changed

+416
-0
lines changed

3 files changed

+416
-0
lines changed

ioio/samples/canvas.bas

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
unit canvas
2+
3+
export create
4+
export draw_line
5+
export draw_pixel
6+
export draw_char
7+
export draw_circle
8+
export draw_rect
9+
export draw_rect_filled
10+
export draw_string
11+
12+
const font = load_font("font7x4.json")
13+
14+
sub iterate(w, h, filter)
15+
local x, y
16+
for y = 0 to h - 1
17+
for x = 0 to w - 1
18+
call filter, x, y
19+
next
20+
next
21+
end
22+
23+
func load_font(name)
24+
local font = {}
25+
local col = 0
26+
local row = 0
27+
local ch, txt, buffer
28+
local w = 4
29+
local h = 7
30+
31+
tload name, txt, 1
32+
buffer = array(txt)
33+
34+
func slice
35+
local result
36+
dim result(h, w)
37+
sub do_slice(x, y)
38+
result[y, x] = buffer[y + row, x + col]
39+
end
40+
iterate(w, h, @do_slice)
41+
return result
42+
end
43+
44+
for ch = asc("a") to asc("j")
45+
c = chr(ch)
46+
font[c] = slice()
47+
col += w + 1
48+
next
49+
50+
row += h + 1
51+
col = 0
52+
for ch = asc("k") to asc("t")
53+
c = chr(ch)
54+
font[c] = slice()
55+
col += w + 1
56+
next
57+
58+
row += h + 1
59+
col = 0
60+
for ch = asc("u") to asc("z")
61+
c = chr(ch)
62+
font[c] = slice()
63+
col += w + 1
64+
next
65+
66+
row += h + 1
67+
row += h + 1
68+
col = 0
69+
for ch = asc("0") to asc("9")
70+
c = chr(ch)
71+
font[c] = slice()
72+
col += w + 1
73+
next
74+
75+
col = 30
76+
row = 16
77+
font[" "] = slice()
78+
79+
col += w + 1
80+
font["."] = slice()
81+
82+
col += w + 1
83+
font[","] = slice()
84+
85+
col += w + 1
86+
font["!"] = slice()
87+
88+
return font
89+
end
90+
91+
sub draw_pixel(byref canvas, x1, y1)
92+
if (x1 > -1 and y1 > -1 and y1 <= ubound(canvas._dat, 1) and x1 <= ubound(canvas._dat, 2)) then
93+
canvas._dat[y1, x1] = canvas._pen
94+
endif
95+
end
96+
97+
func draw_char(byref canvas, c, x0, y0)
98+
local glyph = font[lower(c)]
99+
local g_height = ubound(glyph, 1)
100+
local g_width = ubound(glyph, 2)
101+
local c_height = canvas._fontSize
102+
local c_width = int(c_height * .55)
103+
local yscale = c_height / g_height
104+
local xscale = c_width / g_height
105+
106+
sub filter(x, y)
107+
local x1 = round(x / xscale)
108+
local y1 = round(y / yscale)
109+
if (y0 + y <= canvas._h and x0 + x <= canvas._w and y1 <= g_height and x1 <= g_width and glyph[y1, x1] != 0) then
110+
canvas._dat[y0 + y, x0 + x] = canvas._pen
111+
endif
112+
end
113+
114+
sub transfer(x, y)
115+
if (glyph[y, x] != 0) then
116+
canvas._dat[y0 + y, x0 + x] = canvas._pen
117+
endif
118+
end
119+
120+
if (canvas._fontSize < 19) then
121+
iterate(g_width, g_height, @transfer)
122+
else
123+
iterate(c_width, c_height, @filter)
124+
endif
125+
126+
return [g_height, g_width]
127+
end
128+
129+
sub draw_string(byref canvas, s, x0, y0)
130+
local x = x0
131+
local y = y0
132+
local c, m, spacing
133+
for c in s
134+
m = draw_char(canvas, c, x, y)
135+
spacing = m[1] / m[0] * canvas._fontSize * .35
136+
x += m[1] + spacing
137+
next
138+
end
139+
140+
sub draw_line(byref canvas, x0, y0, x1, y1)
141+
local dx = abs(x1 - x0)
142+
local dy = -abs(y1 - y0)
143+
local sx = iff(x0 < x1, 1, -1)
144+
local sy = iff(y0 < y1, 1, -1)
145+
local err = dx + dy
146+
local e2
147+
148+
while 1
149+
draw_pixel(canvas, x0, y0)
150+
if (x0 == x1 and y0 == y1) then exit loop
151+
if ((sx < 0 and x0 < 0) or (sy < 0 and y0 < 0)) then exit loop
152+
e2 = err * 2
153+
if (e2 >= dy) then
154+
err += dy
155+
x0 += sx
156+
endif
157+
if (e2 <= dx) then
158+
err += dx
159+
y0 += sy
160+
endif
161+
wend
162+
end
163+
164+
sub draw_rect(byref canvas, x0, y0, x1, y1)
165+
draw_line(canvas, x0, y0, x1, y0)
166+
draw_line(canvas, x0, y1, x1, y1)
167+
draw_line(canvas, x0, y0, x0, y1)
168+
draw_line(canvas, x1, y0, x1, y1)
169+
end
170+
171+
sub draw_rect_filled(byref canvas, x0, y0, x1, y1)
172+
local x, y
173+
for y = max(y0, 0) to min(y1, canvas._h)
174+
for x = max(x0, 0) to min(x1, canvas._w)
175+
canvas._dat[y, x] = canvas._pen
176+
next
177+
next
178+
end
179+
180+
sub draw_circle(byref canvas, x0, y0, r, as_filled)
181+
sub fill_circle(x, y, r)
182+
draw_line canvas, -x + x0, y + y0, x + x0, y + y0
183+
draw_line canvas, -x + x0, -y + y0, x + x0, -y + y0
184+
draw_line canvas, -y + x0, x + y0, y + x0, x + y0
185+
draw_line canvas, -y + x0, -x + y0, y + x0, -x + y0
186+
end
187+
188+
sub plot_circle(x, y, r)
189+
' left + right mid quads
190+
draw_pixel canvas, x + x0, y + y0
191+
draw_pixel canvas, -x + x0, y + y0
192+
draw_pixel canvas, x + x0, -y + y0
193+
draw_pixel canvas, -x + x0, -y + y0
194+
' left + right top/bottom quads
195+
draw_pixel canvas, y + x0, x + y0
196+
draw_pixel canvas, -y + x0, x + y0
197+
draw_pixel canvas, y + x0, -x + y0
198+
draw_pixel canvas, -y + x0, -x + y0
199+
end
200+
201+
local painter
202+
if (as_filled) then
203+
painter = @fill_circle
204+
else
205+
painter = @plot_circle
206+
endif
207+
208+
local x = r
209+
local y = 0
210+
local p = 1 - r
211+
212+
call painter, x, y, r
213+
while (x > y)
214+
y++
215+
if (p <= 0) then
216+
' Mid-point is inside or on the perimeter
217+
p += (2 * y) + 1
218+
else
219+
' Mid-point is outside the perimeter
220+
x--
221+
p += (2 * y) - (2 * x) + 1
222+
endif
223+
224+
' All the perimeter points have already been printed
225+
if (x < y) then exit loop
226+
call painter, x, y, r
227+
wend
228+
end
229+
230+
func create(width, height, _pen)
231+
local result = {}
232+
dim result._dat(height, width)
233+
result._pen = _pen
234+
result._fontSize = 20
235+
result._w = width
236+
result._h = height
237+
return result
238+
end

ioio/samples/font7x4.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[1,1,1,1,0,1,1,1,0,0,0,1,1,1,0,1,1,1,0,0,1,1,1,1,0,1,1,1,1,0,1,1,1,1,0,1,0,0,1,0,1,1,1,0,0,0,0,0,1,1;1,0,0,1,0,1,0,0,1,0,1,0,0,0,0,1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,1,1;1,0,0,1,0,1,0,0,1,0,1,0,0,0,0,1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,1,1;1,1,1,1,0,1,1,1,1,0,1,0,0,0,0,1,0,0,1,0,1,1,1,0,0,1,1,1,0,0,1,0,1,1,0,1,1,1,1,0,0,1,0,0,0,0,0,0,1,1;1,0,0,1,0,1,0,0,1,0,1,0,0,0,0,1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,1,0,0,1,0,0,1,0,0,0,0,0,0,1,1;1,0,0,1,0,1,0,0,1,0,1,0,0,0,0,1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,1,0,0,1,0,0,1,0,0,0,0,0,0,1,1;1,0,0,1,0,1,1,1,0,0,0,1,1,1,0,1,1,1,0,0,1,1,1,1,0,1,0,0,0,0,1,1,1,1,0,1,0,0,1,0,1,1,1,0,0,1,1,1,0,1;0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1;1,0,0,1,0,1,0,0,0,0,1,0,0,1,0,1,0,0,1,0,0,1,1,0,0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