From 2ca10368d58719641346bc28037f53cb9b7661f8 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Thu, 18 Apr 2013 20:18:53 +0100 Subject: [PATCH] Maracas working --- arduino/maracas.ino | 19 ------- raspberrypi/music-maker-handler.py | 80 +++++++----------------------- 2 files changed, 17 insertions(+), 82 deletions(-) delete mode 100644 arduino/maracas.ino diff --git a/arduino/maracas.ino b/arduino/maracas.ino deleted file mode 100644 index a50a53a..0000000 --- a/arduino/maracas.ino +++ /dev/null @@ -1,19 +0,0 @@ -int newState; -int states[] = {-1,-1}; -const int wait = 500; - -void setup() { - Serial.begin(9600); -} -void loop() { - for (int i = 0; i < 3; i+=2) { - newState = digitalRead(i); - if (newState != states[i]) { - Serial.print(i); - Serial.print(",1023"); - Serial.println(); - states[i] = newState; - delay(wait); - } - } -} \ No newline at end of file diff --git a/raspberrypi/music-maker-handler.py b/raspberrypi/music-maker-handler.py index 21ba31b..90cde8b 100755 --- a/raspberrypi/music-maker-handler.py +++ b/raspberrypi/music-maker-handler.py @@ -14,18 +14,12 @@ import io import datetime as dt import logging -''' -from Tkinter import Tk -from tkSimpleDialog import askstring -root = Tk() -root.withdraw() -''' - PORT = 42001 DEFAULT_HOST = '127.0.0.1' BUFFER_SIZE = 240 #used to be 100 SOCKET_TIMEOUT = 1 -DEVICES = ['/dev/ttyACM0'] +DEVICES = ['/dev/ttyACM2', '/dev/ttyACM3'] +#DEVICES = ['/dev/ttyACM0','/dev/ttyACM1'] #DRUM_DEVICE = '/dev/ttyACM0' #GUITAR_DEVICE = '/dev/ttyUSB1' #MARACAS_DEVICE = '/dev/ttyACM1' @@ -41,24 +35,6 @@ BROADCAST_NAMES = {'guitar': 'guitar', SENSOR_NAMES = {'guitar': 'guitar_pitch'} -#DRUM_INSTRUMENT_NAMES = {0: 'cymbal', - #1: 'hihat', - #2: 'slowdrum', - #3: 'snare', - #4: 'tomtom'} - -#DRUM_VALUE_NAMES = {0: 'drum-volume', - #1: 'drum-volume', - #2: 'drum-volume', - #3: 'drum-volume', - #4: 'drum-volume'} - -#GUITAR_INSTRUMENT_NAMES = {0: 'guitar'} -#GUITAR_VALUE_NAMES = {0: 'guitar_pitch'} - -#MARACAS_INSTRUMENT_NAMES = {0: 'maracas', 2: 'maracas'} -#MARACAS_VALUE_NAMES = {0: 'maracas_vigour', 2: 'maracas_vigour'} - logging.basicConfig(level = logging.INFO) #logging.basicConfig(level = logging.DEBUG) @@ -76,20 +52,13 @@ class ScratchSender(threading.Thread): self._stop = threading.Event() def join(self,timeout=None): - """ - Stop the thread - """ self._stop.set() threading.Thread.join(self, timeout) - #def stop(self): - #self._stop.set() - def stopped(self): return self._stop.isSet() def run(self): - # Detect sensors here while not self.stopped(): time.sleep(0.01) # be kind to cpu - not certain why :) @@ -111,17 +80,12 @@ class ArduinoListener(threading.Thread): self.scratch_sender = sender self.instruments = instruments self.values = values + logging.info("Started listener on port %s" % device) def join(self,timeout=None): - """ - Stop the thread - """ self._stop.set() threading.Thread.join(self, timeout) - #def stop(self): - #self._stop.set() - def stopped(self): return self._stop.isSet() @@ -142,23 +106,23 @@ class ArduinoListener(threading.Thread): except KeyError: # Do nothing pass - if isinstance(self.instruments[instrument], dict): - broadcast = self.instruments[instrument][instrument_value] - else: - broadcast = self.instruments[instrument] - try: - logging.info("broadcast %s" % broadcast) - self.scratch_sender.send_scratch_command('broadcast %s' % broadcast) - except KeyError: - # Do nothing - pass + if instrument in self.instruments: + if isinstance(self.instruments[instrument], dict): + broadcast = self.instruments[instrument][instrument_value] + else: + broadcast = self.instruments[instrument] + try: + logging.info("broadcast %s" % broadcast) + self.scratch_sender.send_scratch_command('broadcast %s' % broadcast) + except KeyError: + # Do nothing + pass except serial.SerialException: logging.error('Serial exception') logging.debug('Thread run() exiting') - def create_socket(host, port): while True: try: @@ -168,17 +132,12 @@ def create_socket(host, port): break except socket.error: logging.warning("There was an error connecting to Scratch!") - logging.warning("I couldn't find a Mesh session at host: %s, port: %s" % (host, port) ) + logging.warning("I couldn't find a Mesh session at host: %s, port: %s" % (host, port)) time.sleep(3) - #sys.exit(1) - return scratch_sock def cleanup_threads(threads): logging.debug("Stopping %d threads" % len(threads)) - #for thread in threads: - #thread.stop() - #logging.debug("Threads stopped") for thread in threads: thread.join() logging.debug("Threads joined") @@ -189,7 +148,6 @@ if __name__ == '__main__': else: host = DEFAULT_HOST - cycle_trace = 'start' while True: if (cycle_trace == 'disconnected'): @@ -205,15 +163,11 @@ while True: logging.info('Connected to Scratch') the_socket.settimeout(SOCKET_TIMEOUT) sender = ScratchSender(the_socket) - #listeners = [] - #for device in DEVICES: - #listeners.append(ArduinoListener(device, ARDUINO_BAUD_RATE, sender, BROADCAST_NAMES, SENSOR_NAMES)) - listeners = [ArduinoListener(device, ARDUINO_BAUD_RATE, sender, BROADCAST_NAMES, SENSOR_NAMES) for device in DEVICES] cycle_trace = 'running' logging.info("Listeners running....") - sender.start() - for listener in listeners: + sender.start() + for listener in listeners: listener.start() # wait for ctrl+c -- 2.34.1