changeset 2325:81b48e21ba46

multi device supt
author drewp@bigasterisk.com
date Thu, 01 Jun 2023 14:47:35 -0700
parents 4b331ac7784f
children e9caffe926df
files light9/midifade/midifade.py
diffstat 1 files changed, 41 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/light9/midifade/midifade.py	Thu Jun 01 14:21:51 2023 -0700
+++ b/light9/midifade/midifade.py	Thu Jun 01 14:47:35 2023 -0700
@@ -5,6 +5,7 @@
 import asyncio
 import logging
 import traceback
+from typing import Dict
 
 import mido
 from rdfdb.syncedgraph.syncedgraph import SyncedGraph
@@ -28,24 +29,35 @@
     graph.patchObject(ctx, fadeSet, L9['value'], valueLit)
 
 
-def onMessage(graph: SyncedGraph, ctx: URIRef, m: mido.Message):
-    md = m.dict()
-    if md['type'] == 'active_sensing':
+def onMessage(graph: SyncedGraph, ctx: URIRef, m: Dict):
+    if m['type'] == 'active_sensing':
         return
-    if m.is_cc():
-        if md['control'] == 81: setFader(graph, ctx, L9['show/dance2023/fadePage1f0'], md['value'] / 127)
-        if md['control'] == 82: setFader(graph, ctx, L9['show/dance2023/fadePage1f1'], md['value'] / 127)
-        if md['control'] == 83: setFader(graph, ctx, L9['show/dance2023/fadePage1f2'], md['value'] / 127)
-        if md['control'] == 84: setFader(graph, ctx, L9['show/dance2023/fadePage1f3'], md['value'] / 127)
-        if md['control'] == 85: setFader(graph, ctx, L9['show/dance2023/fadePage1f4'], md['value'] / 127)
-        if md['control'] == 86: setFader(graph, ctx, L9['show/dance2023/fadePage1f5'], md['value'] / 127)
-        if md['control'] == 87: setFader(graph, ctx, L9['show/dance2023/fadePage1f6'], md['value'] / 127)
-        if md['control'] == 88: setFader(graph, ctx, L9['show/dance2023/fadePage1f7'], md['value'] / 127)
-        else:
-            log.info(f'unhandled cc message {md}')
+    if m['type'] == 'control_change':
 
+        try:
+            fader = {
+                'quneo': {
+                    44: L9['show/dance2023/fadePage1f0'],
+                    45: L9['show/dance2023/fadePage1f0'],
+                    46: L9['show/dance2023/fadePage1f0'],
+                },
+                'bcf2000': {
+                    81: L9['show/dance2023/fadePage1f0'],
+                    82: L9['show/dance2023/fadePage1f1'],
+                    83: L9['show/dance2023/fadePage1f2'],
+                    84: L9['show/dance2023/fadePage1f3'],
+                    85: L9['show/dance2023/fadePage1f4'],
+                    86: L9['show/dance2023/fadePage1f5'],
+                    87: L9['show/dance2023/fadePage1f6'],
+                    88: L9['show/dance2023/fadePage1f7'],
+                }
+            }[m['dev']][m['control']]
+        except KeyError:
+            log.info(f'unknown control {m}')
+            return
+        setFader(graph, ctx, fader, m['value'] / 127)
     else:
-        log.info(f'unhandled message {md}')
+        log.info(f'unhandled message {m}')
 
 
 async def main():
@@ -59,8 +71,8 @@
     msgs = asyncio.Queue()
     loop = asyncio.get_event_loop()
 
-    def onMessageMidoThread(message):
-        loop.call_soon_threadsafe(msgs.put_nowait, message)
+    def onMessageMidoThread(dev, message):
+        loop.call_soon_threadsafe(msgs.put_nowait, message.dict() | {'dev': dev})
 
     async def reader():
         while True:
@@ -75,11 +87,18 @@
             await asyncio.sleep(1 / MAX_SEND_RATE)
 
     asyncio.create_task(reader())
-    log.info(f'{mido.get_input_names()}')
-    port = mido.open_input(
-        #'Keystation:Keystation MIDI 1 20:0',
-        'BCF2000:BCF2000 MIDI 1 20:0',
-         callback=onMessageMidoThread)
+
+    for inputName in mido.get_input_names():
+        if inputName.startswith('Keystation'):
+            dev = "keystation"
+        elif inputName.startswith('BCF2000'):
+            dev = 'bcf2000'
+        elif inputName.startswith('QUNEO'):
+            dev = 'quneo'
+        else:
+            continue
+        log.info(f'listening on input {inputName} {dev=}')
+        port = mido.open_input(inputName, callback=lambda message: onMessageMidoThread(dev, message))
 
     while True:
         await asyncio.sleep(1)