Multiple Keyboards with RaspberryPi and OSC

Aus hyperdramatik
Wechseln zu: Navigation, Suche

Installation

Python3

$ sudo apt-get update
$ sudo apt-get install python3 python3-dev python3-pip gcc

Python EvDev

Über das Python EvDev Package können wir auf Linux' input event interface zugreifen.
$ sudo apt-get install raspberrypi-kernel-headers
$ sudo pip install evdev

Python OSC

$ sudo pip install python-osc

Code

import sys
import evdev
import argparse
import time
import os

from pythonosc import osc_message_builder
from pythonosc import udp_client
from threading import Thread


ip = "192.168.234.102"
port = 12345
station = "crisprlogin"


class MyThread(Thread):
    def __init__(self, val):
        Thread.__init__(self)
        self.val = val
        self.s = ""
    def run(self):
        print("starting thread:")
        print(self.val.fn)
        while(1):
            for event in self.val.read_loop():
                if event.type == evdev.ecodes.EV_KEY:
                    if(evdev.categorize(event).keystate):
                        if(evdev.categorize(event).keycode == "KEY_ENTER"):
                            client = udp_client.SimpleUDPClient(ip, port)
                            client.send_message("/"+station+self.val.fn[self.va$
                            print ("send osc message id#", end='')
                            print (self.s)
                            self.s=""
                        else:
                            self.s = self.s + evdev.categorize(event).keycode[4]


if __name__ == "__main__":
    devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()]
    for device in devices:
        mythread = MyThread(evdev.InputDevice(device.fn))
        mythread.start()
        time.sleep(0.1)

  • Noch herausfinden: Python findet die Headers nicht, wenn man das Script auf dem Desktop speichert. Speichert man sein Script in Documents, läuft es problemlos...
  • Wie man das Script automatisch beim einschalten des Raspis startet, findest du unter Raspberry Pi - Helfende Befehle