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'
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)
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 :)
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()
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:
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")
else:
host = DEFAULT_HOST
-
cycle_trace = 'start'
while True:
if (cycle_trace == 'disconnected'):
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