-
Notifications
You must be signed in to change notification settings - Fork 1
/
blocks.pd_lua
223 lines (203 loc) · 5.83 KB
/
blocks.pd_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
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
local MB = pd.Class:new():register("blocks")
function MB:initialize(name, atoms)
self.inlets = 1
self.outlets = 2
self.period = #atoms>0 and atoms[1] or nil
if type(self.period) ~= "number" then
self.period = 10 -- hard-coded default
end
return true
end
function MB:postinitialize()
self.clock = pd.Clock:new():register(self, "tick")
end
function MB:finalize()
self.clock:destruct()
myblocks.stop()
end
myblocks = require("myblocks")
function MB:in_1_float(f)
if f ~= 0 then
myblocks.start()
self:tick()
else
myblocks.stop()
self.clock:unset()
end
end
function MB:in_1_count()
self:outlet(1, "float", {myblocks.count_blocks()})
end
function MB:in_1_blocknum(atoms)
local uid = tonumber(atoms[1], 16)
if uid ~= nil then
self:outlet(1, "float", {myblocks.blocknum(uid)})
else
self:error("blocks: blocknum expects a string of hex digits")
end
end
function MB:in_1_info(atoms)
function mkinfo(i)
local info = myblocks.info(i)
if info ~= nil then
-- We rearrange the order a bit (uid comes last, code is
-- omitted). Also, we convert flags (booleans) to numbers, and uid,
-- which is a 64 bit number, is converted to a symbol (hex
-- representation).
return {i, info.type,
info.is_master and 1 or 0, info.is_charging and 1 or 0,
info.battery_level, info.nbuttons, info.nleds,
info.descr, info.type_descr, info.serial, info.version,
string.format("%0x", info.uid)}
else
return nil
end
end
if type(atoms[1]) == "number" then
local info = mkinfo(math.floor(atoms[1]))
if info ~= nil then
self:outlet(1, "info", info)
else
self:error("blocks: info expects a valid block number")
end
else
local n = myblocks.count_blocks()
for i = 0, n-1 do
self:outlet(1, "info", mkinfo(i))
end
end
end
function MB:in_1_reset(atoms)
if type(atoms[1]) == "number" then
myblocks.reset(atoms[1])
else
local n = myblocks.count_blocks()
for i = 0, n-1 do
myblocks.reset(i)
end
end
end
function MB:in_1_factoryreset(atoms)
if type(atoms[1]) == "number" then
myblocks.factory_reset(atoms[1])
else
local n = myblocks.count_blocks()
for i = 0, n-1 do
myblocks.factory_reset(i)
end
end
end
function MB:in_1_load(atoms)
if type(atoms[1]) == "number" and type(atoms[2]) == "string" then
if not myblocks.load_program(atoms[1], atoms[2]) then
self:error("blocks: " .. myblocks.msg())
end
else
self:error("blocks: load expects a block number and a program file name")
end
end
function MB:in_1_save(atoms)
if type(atoms[1]) == "number" then
myblocks.save_program(atoms[1])
else
self:error("blocks: save expects a block number")
end
end
function MB:in_1_msg(atoms)
local n = #atoms
local ok = n > 1
if ok then
for i = 1, n do
ok = type(atoms[i]) == "number"
if not ok then break end
end
end
if ok then
local t = table.pack(table.unpack(atoms, 2))
myblocks.send(atoms[1], t)
else
self:error("blocks: msg expects a block number followed by a list of numbers")
end
end
function MB:in_1_button(atoms)
local blocknum = atoms[1]
local num = atoms[2]
local color = atoms[3]
if type(blocknum) == "number" and type(num) == "number" and
type(color) == "number" then
myblocks.set_button(blocknum, num, color)
else
self:error("blocks: button expects a block number followed by a button number and a color")
end
end
function MB:in_1_leds(atoms)
local blocknum = atoms[1]
local num = atoms[2]
local color = atoms[3]
if type(blocknum) == "number" and type(num) == "number" and
type(color) == "number" then
myblocks.set_leds(blocknum, num, color)
else
self:error("blocks: leds expects a block number followed by an led number and a color")
end
end
function MB:in_1_getbyte(atoms)
local blocknum = atoms[1]
local offset = atoms[2]
if type(blocknum) == "number" and type(offset) == "number" then
self:outlet(1, "byte", {myblocks.get_byte(blocknum, offset)})
else
self:error("blocks: getbyte expects a block number followed by an offset")
end
end
function MB:in_1_setbyte(atoms)
local blocknum = atoms[1]
local offset = atoms[2]
local data = atoms[3]
if type(blocknum) == "number" and type(offset) == "number" and
type(data) == "number" then
myblocks.set_byte(blocknum, offset, data)
else
self:error("blocks: setbyte expects a block number followed by an offset and a number")
end
end
function MB:in_1_getint(atoms)
local blocknum = atoms[1]
local offset = atoms[2]
if type(blocknum) == "number" and type(offset) == "number" then
self:outlet(1, "int", {myblocks.get_int(blocknum, offset)})
else
self:error("blocks: getint expects a block number followed by an offset")
end
end
function MB:in_1_setint(atoms)
local blocknum = atoms[1]
local offset = atoms[2]
local data = atoms[3]
if type(blocknum) == "number" and type(offset) == "number" and
type(data) == "number" then
myblocks.set_int(blocknum, offset, data)
else
self:error("blocks: setint expects a block number followed by an offset and a number")
end
end
function MB:tick()
if myblocks.process() and myblocks.changed() then
self:outlet(2, "float", {myblocks.count_blocks()})
end
local i, msg = myblocks.receive()
while i ~= nil do
if msg.name ~= nil then
-- button
print(string.format("%d: button %d %d (%s) %d", i, msg.num,
msg.type, msg.name, msg.pressed and 1 or 0))
self:outlet(1, "button", { i, msg.num, msg.name, msg.type,
msg.pressed and 1 or 0 })
else
-- program message
self:outlet(1, "msg", { i, table.unpack(msg) })
end
i, msg = myblocks.receive()
end
self.clock:delay(self.period)
end