Maracas working
authorNeil Smith <neil.github@njae.me.uk>
Thu, 18 Apr 2013 19:18:53 +0000 (20:18 +0100)
committerNeil Smith <neil.github@njae.me.uk>
Thu, 18 Apr 2013 19:18:53 +0000 (20:18 +0100)
arduino/maracas.ino [deleted file]
raspberrypi/music-maker-handler.py

diff --git a/arduino/maracas.ino b/arduino/maracas.ino
deleted file mode 100644 (file)
index a50a53a..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-int newState;\r
-int states[] = {-1,-1};\r
-const int wait = 500;\r
-\r
-void setup() {\r
-  Serial.begin(9600);\r
-}\r
-void loop() {\r
-  for (int i = 0; i < 3; i+=2) {\r
-    newState = digitalRead(i);\r
-    if (newState != states[i]) {\r
-      Serial.print(i);\r
-      Serial.print(",1023");\r
-      Serial.println();\r
-      states[i] = newState;\r
-      delay(wait);\r
-    } \r
-  }\r
-}
\ No newline at end of file
index 21ba31b17be79a468a6b4293882f1d54f6c23189..90cde8b3bd2da912e4e56f686f009a9ab517a490 100755 (executable)
@@ -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