From 29649b2e1bf560c33deddc6cc6d6da8e8c105be1 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Thu, 18 Apr 2013 15:45:03 +0100 Subject: [PATCH] Guitar working --- arduino/guitar/guitar.ino | 26 +++++ raspberrypi/music-maker-handler.py | 180 +++++++++++++++-------------- 2 files changed, 122 insertions(+), 84 deletions(-) create mode 100644 arduino/guitar/guitar.ino diff --git a/arduino/guitar/guitar.ino b/arduino/guitar/guitar.ino new file mode 100644 index 0000000..f9f530c --- /dev/null +++ b/arduino/guitar/guitar.ino @@ -0,0 +1,26 @@ + +const int THRESHOLD = 10; +const int GUITAR_PIN = A0; +const int PINLIMIT = 5000; + +int val; +int pinDelay; + +void setup() { + Serial.begin(57600); +} + +void loop() { + val = analogRead(GUITAR_PIN); + + if (val >= THRESHOLD && pinDelay < 1) { + Serial.print("guitar,"); + Serial.println(val); + pinDelay = PINLIMIT; + } + + if (pinDelay > 0) { + pinDelay = pinDelay - 1; + } +} + diff --git a/raspberrypi/music-maker-handler.py b/raspberrypi/music-maker-handler.py index eb83ce0..21ba31b 100755 --- a/raspberrypi/music-maker-handler.py +++ b/raspberrypi/music-maker-handler.py @@ -12,6 +12,7 @@ import struct import serial import io import datetime as dt +import logging ''' from Tkinter import Tk @@ -24,32 +25,42 @@ PORT = 42001 DEFAULT_HOST = '127.0.0.1' BUFFER_SIZE = 240 #used to be 100 SOCKET_TIMEOUT = 1 -DRUM_DEVICE = '/dev/ttyACM0' -GUITAR_DEVICE = '/dev/ttyUSB1' -MARACAS_DEVICE = '/dev/ttyACM1' +DEVICES = ['/dev/ttyACM0'] +#DRUM_DEVICE = '/dev/ttyACM0' +#GUITAR_DEVICE = '/dev/ttyUSB1' +#MARACAS_DEVICE = '/dev/ttyACM1' ARDUINO_BAUD_RATE = 57600 -DRUM_INSTRUMENT_NAMES = {0: 'cymbal', - 1: 'hihat', - 2: 'slowdrum', - 3: 'snare', - 4: 'tomtom'} +BROADCAST_NAMES = {'guitar': 'guitar', + 'drum': {0: 'cymbal', + 1: 'hihat', + 2: 'slowdrum', + 3: 'snare', + 4: 'tomtom'}, + 'maracas': 'maracas'} -DRUM_VALUE_NAMES = {0: 'drum-volume', - 1: 'drum-volume', - 2: 'drum-volume', - 3: 'drum-volume', - 4: 'drum-volume'} +SENSOR_NAMES = {'guitar': 'guitar_pitch'} -GUITAR_INSTRUMENT_NAMES = {0: 'guitar'} -GUITAR_VALUE_NAMES = {0: 'guitar_pitch'} +#DRUM_INSTRUMENT_NAMES = {0: 'cymbal', + #1: 'hihat', + #2: 'slowdrum', + #3: 'snare', + #4: 'tomtom'} -GUITAR_INSTRUMENT_NAMES = {0: 'guitar'} -GUITAR_VALUE_NAMES = {0: 'guitar_pitch'} +#DRUM_VALUE_NAMES = {0: 'drum-volume', + #1: 'drum-volume', + #2: 'drum-volume', + #3: 'drum-volume', + #4: 'drum-volume'} -MARACAS_INSTRUMENT_NAMES = {0: 'maracas', 2: 'maracas'} -MARACAS_VALUE_NAMES = {0: 'maracas_vigour', 2: 'maracas_vigour'} +#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) class MyError(Exception): def __init__(self, value): @@ -63,17 +74,22 @@ class ScratchSender(threading.Thread): threading.Thread.__init__(self) self.scratch_socket = socket self._stop = threading.Event() - - - def stop(self): + + 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 :) @@ -90,14 +106,21 @@ class ScratchSender(threading.Thread): class ArduinoListener(threading.Thread): def __init__(self, device, speed, sender, instruments, values): threading.Thread.__init__(self) - self.arduino_device = serial.Serial(device, speed) + self.arduino_device = serial.Serial(device, speed, timeout=0.5) self._stop = threading.Event() self.scratch_sender = sender self.instruments = instruments self.values = values - def stop(self): + 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() @@ -105,50 +128,60 @@ class ArduinoListener(threading.Thread): def run(self): self.arduino_device.readline() # discard the first (partial) line while not self.stopped(): + logging.debug('Thread waiting for a signal') try: - pin_string, pin_value_string = self.arduino_device.readline().rstrip().split(',', 1) - pin = int(pin_string) - pin_value = int(pin_value_string) - print dt.datetime.now(), 'Pin: %d, Value: %d' % (pin, pin_value) - try: - print "sensor-update %s %d" % (self.values[pin], (pin_value * 100) / 1024) - self.scratch_sender.send_scratch_command("sensor-update %s %d" % (self.values[pin], (pin_value * 100) / 1024)) - except KeyError: - # Do nothing - pass - try: - print "broadcast %s" % self.instruments[pin] - self.scratch_sender.send_scratch_command('broadcast %s' % self.instruments[pin]) - except KeyError: - # Do nothing - pass + device_line = self.arduino_device.readline() + if device_line : + instrument, instrument_value_string = device_line.rstrip().split(',', 1) + instrument_value = int(instrument_value_string) + logging.info('Instrument: %s, Value: %d' % (instrument, instrument_value)) + if instrument in self.values: + try: + logging.info("sensor-update %s %d" % (self.values[instrument], (instrument_value * 100) / 1024)) + self.scratch_sender.send_scratch_command("sensor-update %s %d" % (self.values[instrument], (instrument_value * 100) / 1024)) + 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 except serial.SerialException: - print 'Serial exception' + logging.error('Serial exception') + logging.debug('Thread run() exiting') def create_socket(host, port): while True: try: - print 'Trying' + logging.info('Connecting to Scratch') scratch_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) scratch_sock.connect((host, port)) break except socket.error: - print "There was an error connecting to Scratch!" - print "I couldn't find a Mesh session at host: %s, port: %s" % (host, port) + 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) ) time.sleep(3) #sys.exit(1) return scratch_sock def cleanup_threads(threads): - for thread in threads: - thread.stop() - + 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") if __name__ == '__main__': if len(sys.argv) > 1: @@ -160,56 +193,35 @@ if __name__ == '__main__': cycle_trace = 'start' while True: if (cycle_trace == 'disconnected'): - print "Scratch disconnected" - cleanup_threads((listener, sender)) + logging.info("Scratch disconnected") + cleanup_threads(listeners + sender) time.sleep(1) cycle_trace = 'start' if (cycle_trace == 'start'): # open the socket - print 'Starting to connect...' , + logging.info('Connecting to Scratch...') the_socket = create_socket(host, PORT) - print 'Connected!' + logging.info('Connected to Scratch') the_socket.settimeout(SOCKET_TIMEOUT) -## data = the_socket.recv(BUFFER_SIZE) -## print "Discard 1st data buffer" , data[4:].lower() sender = ScratchSender(the_socket) - drum_listener = ArduinoListener(DRUM_DEVICE, ARDUINO_BAUD_RATE, sender, DRUM_INSTRUMENT_NAMES, DRUM_VALUE_NAMES) - guitar_listener = ArduinoListener(GUITAR_DEVICE, ARDUINO_BAUD_RATE, sender, GUITAR_INSTRUMENT_NAMES, GUITAR_VALUE_NAMES) - maracas_listener = ArduinoListener(MARACAS_DEVICE, ARDUINO_BAUD_RATE, sender, MARACAS_INSTRUMENT_NAMES, MARACAS_VALUE_NAMES) + #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' - print "Running...." + logging.info("Listeners running....") sender.start() - drum_listener.start() - guitar_listener.start() - maracas_listener.start() - - #pin = 1 - #pin_value = 512 - #print 'Sending:' "sensor-update %s %d" % (VOLUME_NAMES[pin], (pin_value * 100) / 1024) - #sender.send_scratch_command("sensor-update %s %d" % (VOLUME_NAMES[pin], (pin_value * 100) / 1024)) - #print 'Sending:' 'broadcast %s' % INSTRUMENT_NAMES[pin] - #sender.send_scratch_command('broadcast %s' % INSTRUMENT_NAMES[pin]) - - #sender.send_scratch_command("sensor-update guitar-pitch 10") - #sender.send_scratch_command("broadcast cymbal") - #time.sleep(1) - #sender.send_scratch_command("broadcast snare") - #time.sleep(1) - #sender.send_scratch_command("broadcast hihat") - #time.sleep(1) - #sender.send_scratch_command("broadcast slowdrum") - #time.sleep(1) - #sender.send_scratch_command("broadcast tomtom") - #time.sleep(1) + for listener in listeners: + listener.start() # wait for ctrl+c try: #just pause - -# else: - time.sleep(0.1) + time.sleep(0.1) except KeyboardInterrupt: - cleanup_threads((sender, drum_listener, guitar_listener, maracas_listener)) + logging.warning("Interrrupted") + cleanup_threads(listeners + [sender]) sys.exit() -- 2.34.1