monome / norns

norns is many sound instruments.
http://monome.org
GNU General Public License v3.0
633 stars 147 forks source link

Reconnecting to midi or grid devices should clear callback #744

Closed markwheeler closed 5 years ago

markwheeler commented 5 years ago

Use case: Allow the user to switch MIDI or grid input from a script param. In norns v1 there was a reconnect() function.

Currently in 2.0, calling midi.connect(value) keeps previous callbacks. Ideally there'd be a simple way to switch device and have the existing callback function start receiving from the new device while the old one is cleaned up.

Unsure if this applies to arc or HID also.

artfwo commented 5 years ago

@markwheeler after pondering on this for some time, i think that managing callbacks lies out of scope for connect() and could be done explicitly instead, e.g:

function grid_handler() ... end

grid1 = grid.connect(1)
grid1.key = grid_handler

...

grid2 = grid.connect(2)
grid2.key = grid_handler
grid1.key = nil

or even connect to all 4 ports during initialization and manage callbacks internally if required.

otherwise, it makes it difficult for scripts, that want simultaneous access to multiple devices of the same class, to keep existing callbacks when we call connect().

markwheeler commented 5 years ago

OK cool. So for switching grids am I correct in that this would be the way to do it without creating new grid tables?

-- Connect
my_grid = grid.connect(1)
my_grid.key = grid_handler
...
-- Reconnect
my_grid.key = nil
my_grid = grid.connect(2)
my_grid.key = grid_handler
artfwo commented 5 years ago

Yes, or keep the devices in a table that you create during initialization.

markwheeler commented 5 years ago

Cool thanks!