-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path9_grid.lua
135 lines (115 loc) · 2.24 KB
/
9_grid.lua
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
--
-- ////\\\\
-- ////\\\\ TUTORIAL
-- ////\\\\ PART 9
-- \\\\////
-- \\\\//// GRID
-- \\\\////
--
local g
local viewport = { width = 128, height = 64, frame = 0 }
local focus = { x = 1, y = 1, brightness = 15 }
-- Main
function init()
connect()
-- Render Style
screen.level(15)
screen.aa(0)
screen.line_width(1)
-- Render
update()
end
function connect()
g = grid.connect()
g.key = on_grid_key
g.add = on_grid_add
g.remove = on_grid_remove
end
function is_connected()
return g.device ~= nil
end
function on_grid_key(x,y,z)
focus.x = x
focus.y = y
update()
end
function on_grid_add(g)
print('on_add')
end
function on_grid_remove(g)
print('on_remove')
end
function update()
g:all(0)
g:led(focus.x,focus.y,focus.brightness)
g:refresh()
redraw()
end
-- Interactions
function key(id,state)
if id == 2 and state == 1 then
focus.brightness = 15
elseif id == 3 and state == 1 then
focus.brightness = 5
end
update()
end
function enc(id,delta)
if id == 2 then
focus.x = clamp(focus.x + delta, 1, 16)
elseif id == 3 then
focus.y = clamp(focus.y + delta, 1, 8)
end
update()
end
-- Render
function draw_frame()
screen.level(15)
screen.rect(1, 1, viewport.width-1, viewport.height-1)
screen.stroke()
end
function draw_pixel(x,y)
if focus.x == x and focus.y == y then
screen.stroke()
screen.level(15)
end
screen.pixel((x*offset.spacing) + offset.x, (y*offset.spacing) + offset.y)
if focus.x == x and focus.y == y then
screen.stroke()
screen.level(1)
end
end
function draw_grid()
if is_connected() ~= true then return end
screen.level(1)
offset = { x = 30, y = 13, spacing = 4 }
for x=1,16,1 do
for y=1,8,1 do
draw_pixel(x,y)
end
end
screen.stroke()
end
function draw_label()
screen.level(15)
local line_height = 8
screen.move(5,viewport.height - (line_height * 1))
if is_connected() ~= true then
screen.text('Grid is not connected.')
else
screen.text(focus.x..','..focus.y)
end
screen.stroke()
end
function redraw()
screen.clear()
draw_frame()
draw_grid()
draw_label()
screen.stroke()
screen.update()
end
-- Utils
function clamp(val,min,max)
return val < min and min or val > max and max or val
end