Skip to content

Commit

Permalink
midi/grid lib changes (#535)
Browse files Browse the repository at this point in the history
* progress

* midi done

* grid support. clear out send funcs
  • Loading branch information
tehn authored Sep 4, 2018
1 parent 6519740 commit c1dff16
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 89 deletions.
97 changes: 52 additions & 45 deletions lua/grid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
-- @module grid
-- @alias Grid
require 'norns'
norns.version.grid = '0.0.2'


---------------------------------
Expand All @@ -14,11 +13,12 @@ Grid.list = {}
Grid.vport = {}
for i=1,4 do
Grid.vport[i] = {
name = nil,
name = "none",
callbacks = {},
led = nil,
all = nil,
refresh = nil,
index = 0,
led = function() end,
all = function() end,
refresh = function() end,
attached = false
}
end
Expand All @@ -44,6 +44,19 @@ function Grid.new(id, serial, name, dev)
g.rows = grid_rows(dev)
g.cols = grid_cols(dev)
g.ports = {} -- list of virtual ports this device is attached to

-- autofill next postiion
local connected = {}
for i=1,4 do table.insert(connected, Grid.vport[i].name) end
if not tab.contains(connected, name) then
for i=1,4 do
if Grid.vport[i].name == "none" then
Grid.vport[i].name = name
break
end
end
end

return g
end

Expand Down Expand Up @@ -89,43 +102,43 @@ function Grid:print()
end


--- send grid:led to all devices
function Grid.led_all(x, y, val)
for _,device in pairs(Grid.devices) do
device:led(x, y, val)
end
end

--- send grid:all to all devices
function Grid.all_all(val)
for _,device in pairs(Grid.devices) do
device:all(val)
end
end
--- send grid:refresh to all devices
function Grid.refresh_all()
for _,device in pairs(Grid.devices) do
device:refresh()
end
end


--- create device, returns object with handler and send
function Grid.connect(n)
local n = n or 1
if n>4 then n=4 end

Grid.vport[n].index = Grid.vport[n].index + 1

local d = {
index = Grid.vport[n].index,
port = n,
event = function(x,y,z)
print(n..": grid input")
print("grid input")
end,
attached = function() return Grid.vport[n].attached end,
led = function(x,y,z) Grid.vport[n].led(x,y,z) end,
all = function(val) Grid.vport[n].all(val) end,
refresh = function() Grid.vport[n].refresh() end
refresh = function() Grid.vport[n].refresh() end,
disconnect = function(self)
self.send = function() print("not connected") end
table.remove(Grid.vport[self.port].callbacks, self.index)
self.index = nil
self.port = nil
end,
reconnect = function(self, p)
if self.index then
table.remove(Grid.vport[self.port].callbacks, self.index)
end
self.send = function(data) Grid.vport[p].send(data) end
attached = function() return Grid.vport[p].attached end
Grid.vport[p].index = Grid.vport[p].index + 1
self.index = Grid.vport[p].index
self.port = p
Grid.vport[p].callbacks[self.index] = function(data) self.event(data) end
end
}

table.insert(Grid.vport[n].callbacks, function(x,y,z) d.event(x,y,z) end)
Grid.vport[n].callbacks[d.index] = function(x,y,z) d.event(x,y,z) end

return d
end
Expand All @@ -134,6 +147,7 @@ end
function Grid.cleanup()
for i=1,4 do
Grid.vport[i].callbacks = {}
Grid.vport[i].index = 0
end
end

Expand All @@ -147,24 +161,17 @@ function Grid.update_devices()
-- connect available devices to vports
for i=1,4 do
Grid.vport[i].attached = false
if Grid.vport[i].name == "all" then
Grid.vport[i].led = function(x, y, val) Grid.led_all(x, y, val) end
Grid.vport[i].all = function(val) Grid.all_all(val) end
Grid.vport[i].refresh = function() Grid.refresh_all() end
Grid.vport[i].attached = tab.count(Grid.devices) > 0
for _,device in pairs(Grid.devices) do
Grid.vport[i].led = function(x, y, val) end
Grid.vport[i].all = function(val) end
Grid.vport[i].refresh = function() end
for _,device in pairs(Grid.devices) do
if device.name == Grid.vport[i].name then
Grid.vport[i].led = function(x, y, val) device:led(x, y, val) end
Grid.vport[i].all = function(val) device:all(val) end
Grid.vport[i].refresh = function() device:refresh() end
Grid.vport[i].attached = true
table.insert(device.ports, i)
end
else
for _,device in pairs(Grid.devices) do
if device.name == Grid.vport[i].name then
Grid.vport[i].led = function(x, y, val) device:led(x, y, val) end
Grid.vport[i].all = function(val) device:all(val) end
Grid.vport[i].refresh = function() device:refresh() end
Grid.vport[i].attached = true
table.insert(device.ports, i)
end
end
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions lua/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1134,19 +1134,16 @@ m.devices.list = {"midi", "grid"}
m.devices.len = #m.devices.list
function m.devices.refresh()
m.devices.options = {
midi = {},
grid = {}
midi = {"none"},
grid = {"none"}
}
-- create midi list
for _,i in pairs(midi.list) do
table.insert(m.devices.options.midi,i)
end
table.insert(m.devices.options.midi, "all")
table.insert(m.devices.options.midi, "none")
for _,i in pairs(grid.list) do
table.insert(m.devices.options.grid,i)
end
table.insert(m.devices.options.grid, "all")
table.insert(m.devices.options.grid, "none")
end

m.key[pDEVICES] = function(n,z)
Expand Down Expand Up @@ -1184,7 +1181,6 @@ m.key[pDEVICES] = function(n,z)
menu.redraw()
elseif n==3 and z==1 then
local s = m.devices.options[m.devices.section][m.devices.pos]
norns.state.ports[m.devices.section][m.devices.setpos] = s
if m.devices.section == "midi" then
midi.vport[m.devices.setpos].name = s
midi.update_devices()
Expand Down Expand Up @@ -1219,7 +1215,11 @@ m.redraw[pDEVICES] = function()
if m.devices.mode == "type" then
screen.text(string.upper(m.devices.list[i]) .. " >")
elseif m.devices.mode == "list" then
screen.text(i .. ". " .. norns.state.ports[m.devices.section][i])
if m.devices.section == "midi" then
screen.text(i .. ". " .. midi.vport[i].name)
elseif m.devices.section == "grid" then
screen.text(i .. ". " .. grid.vport[i].name)
end
elseif m.devices.mode == "select" then
screen.text(m.devices.options[m.devices.section][i])
end
Expand Down
70 changes: 46 additions & 24 deletions lua/midi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ Midi.list = {}
Midi.vport = {}
for i=1,4 do
Midi.vport[i] = {
name = nil,
name = "none",
callbacks = {},
send = nil,
index = 0,
send = function() end,
attached = false
}
end
Expand All @@ -33,6 +34,19 @@ function Midi.new(id, name, dev)
d.event = nil -- event callback
d.remove = nil -- device unplug callback
d.ports = {} -- list of virtual ports this device is attached to

-- autofill next postiion
local connected = {}
for i=1,4 do table.insert(connected, Midi.vport[i].name) end
if not tab.contains(connected, name) then
for i=1,4 do
if Midi.vport[i].name == "none" then
Midi.vport[i].name = name
break
end
end
end

return d
end

Expand Down Expand Up @@ -67,28 +81,42 @@ function Midi:send(data)
end
end

--- send midi event to all devices
function Midi.send_all(data)
for _,device in pairs(Midi.devices) do
device:send(data)
end
end


--- create device, returns object with handler and send
function Midi.connect(n)
local n = n or 1
if n>4 then n=4 end

Midi.vport[n].index = Midi.vport[n].index + 1

local d = {
index = Midi.vport[n].index,
port = n,
event = function(data)
print(n..": midi input")
print("midi input")
end,
attached = function() return Midi.vport[n].attached end,
send = function(data) Midi.vport[n].send(data) end
send = function(data) Midi.vport[n].send(data) end,
disconnect = function(self)
self.send = function() print("not connected") end
table.remove(Midi.vport[self.port].callbacks, self.index)
self.index = nil
self.port = nil
end,
reconnect = function(self, p)
if self.index then
table.remove(Midi.vport[self.port].callbacks, self.index)
end
self.send = function(data) Midi.vport[p].send(data) end
attached = function() return Midi.vport[p].attached end
Midi.vport[p].index = Midi.vport[p].index + 1
self.index = Midi.vport[p].index
self.port = p
Midi.vport[p].callbacks[self.index] = function(data) self.event(data) end
end
}

table.insert(Midi.vport[n].callbacks, function(data) d.event(data) end)
Midi.vport[n].callbacks[d.index] = function(data) d.event(data) end

-- midi send helper functions
d.note_on = function(note, vel, ch)
Expand All @@ -113,6 +141,7 @@ end
function Midi.cleanup()
for i=1,4 do
Midi.vport[i].callbacks = {}
Midi.vport[i].index = 0
end
end

Expand Down Expand Up @@ -208,20 +237,13 @@ function Midi.update_devices()
-- connect available devices to vports
for i=1,4 do
Midi.vport[i].attached = false
if Midi.vport[i].name == "all" then
Midi.vport[i].send = function(data) Midi.send_all(data) end
Midi.vport[i].attached = tab.count(Midi.devices) > 0
for _,device in pairs(Midi.devices) do
Midi.vport[i].send = function(data) end
for _,device in pairs(Midi.devices) do
if device.name == Midi.vport[i].name then
Midi.vport[i].send = function(data) device:send(data) end
Midi.vport[i].attached = true
table.insert(device.ports, i)
end
else
for _,device in pairs(Midi.devices) do
if device.name == Midi.vport[i].name then
Midi.vport[i].send = function(data) device:send(data) end
Midi.vport[i].attached = true
table.insert(device.ports, i)
end
end
end
end
end
Expand Down
17 changes: 5 additions & 12 deletions lua/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
state = {}
state.script = ''
state.clean_shutdown = false
state.ports = {
midi = {"all","none","none","none"},
grid = {"all","none","none","none"}
}

-- read state.lua and set parameters back to stored vals
state.resume = function()
Expand All @@ -22,10 +18,7 @@ state.resume = function()
dofile(data_dir..'system.lua')
end

for i=1,4 do
midi.vport[i].name = state.ports.midi[i]
grid.vport[i].name = state.ports.grid[i]
end
-- update vports
midi.update_devices()
grid.update_devices()

Expand Down Expand Up @@ -68,11 +61,11 @@ state.save_state = function()
io.write("-- system state\n")
io.write("norns.state.script = '" .. state.script .. "'\n")
io.write("norns.state.clean_shutdown = " .. (state.clean_shutdown and "true" or "false") .. "\n")
for k,v in pairs(norns.state.ports.midi) do
io.write("norns.state.ports.midi[" .. k .. "] = '" .. v .. "'\n")
for i=1,4 do
io.write("midi.vport[" .. i .. "].name = '" .. midi.vport[i].name .. "'\n")
end
for k,v in pairs(norns.state.ports.grid) do
io.write("norns.state.ports.grid[" .. k .. "] = '" .. v .. "'\n")
for i=1,4 do
io.write("grid.vport[" .. i .. "].name = '" .. grid.vport[i].name .. "'\n")
end
io.close(fd)
end
Expand Down

0 comments on commit c1dff16

Please sign in to comment.