.mr.developer.cfg
.project
.pydevproject
+
+# Backup files
+*~
+
+# Open Office lock files
+.~lock*#
+++ /dev/null
-
-#define THRESHOLD 64
-#define PADNUM 4
-#define PINLIMIT 500
-
-int val;
-int pinDelay;
-int pinDelays[4] = { 0, 0, 0, 0 };
-
-void setup() {
- Serial.begin(57600);
-}
-
-void loop() {
- for(int i = 0; i < PADNUM; i++) {
- val = analogRead(i);
- pinDelay = pinDelays[i];
-
- if(val >= THRESHOLD && pinDelay < 1) {
- Serial.print(i);
- Serial.print(",");
- Serial.print(val);
- Serial.println();
- pinDelays[i] = PINLIMIT;
- }
-
- if (pinDelay > 0) {
- pinDelays[i] = pinDelay - 1;
- }
- }
-}
-
+++ /dev/null
-
-#define THRESHOLD 16
-#define PADNUM 1
-#define PINLIMIT 5000
-
-int val;
-int pinDelay;
-int pinDelays[4] = { 0, 0, 0, 0 };
-
-void setup() {
- Serial.begin(57600);
-}
-
-void loop() {
- for(int i = 0; i < PADNUM; i++) {
- val = analogRead(i);
- pinDelay = pinDelays[i];
-
- if (val >= THRESHOLD && pinDelay < 1) {
- Serial.print(i);
- Serial.print(",");
- Serial.print(val);
- Serial.println();
- pinDelays[i] = PINLIMIT;
- }
-
- if (pinDelay > 0) {
- pinDelays[i] = pinDelay - 1;
- }
- }
-}
-
+++ /dev/null
-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
+++ /dev/null
-# This code is copyright ...... under the GPL v2.
-# This code is derived from scratch_gpio_handler by Simon Walters, which
-# is derived from scratch_handler by Thomas Preston
-# Version 0.1: It's kind of working.
-
-from array import *
-import threading
-import socket
-import time
-import sys
-import struct
-import serial
-import io
-import datetime as dt
-
-'''
-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
-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'}
-
-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'}
-
-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'}
-
-
-class MyError(Exception):
- def __init__(self, value):
- self.value = value
-
- def __str__(self):
- return repr(self.value)
-
-class ScratchSender(threading.Thread):
- def __init__(self, socket):
- threading.Thread.__init__(self)
- self.scratch_socket = socket
- self._stop = threading.Event()
-
-
- 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 :)
-
- def send_scratch_command(self, cmd):
- n = len(cmd)
- a = array('c')
- a.append(chr((n >> 24) & 0xFF))
- a.append(chr((n >> 16) & 0xFF))
- a.append(chr((n >> 8) & 0xFF))
- a.append(chr(n & 0xFF))
- self.scratch_socket.send(a.tostring() + cmd)
-
-
-class ArduinoListener(threading.Thread):
- def __init__(self, device, speed, sender, instruments, values):
- threading.Thread.__init__(self)
- self.arduino_device = serial.Serial(device, speed)
- self._stop = threading.Event()
- self.scratch_sender = sender
- self.instruments = instruments
- self.values = values
-
- def stop(self):
- self._stop.set()
-
- def stopped(self):
- return self._stop.isSet()
-
- def run(self):
- self.arduino_device.readline() # discard the first (partial) line
- while not self.stopped():
- 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
-
- except serial.SerialException:
- print 'Serial exception'
-
-
-
-def create_socket(host, port):
- while True:
- try:
- print 'Trying'
- 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)
- time.sleep(3)
- #sys.exit(1)
-
- return scratch_sock
-
-def cleanup_threads(threads):
- for thread in threads:
- thread.stop()
-
- for thread in threads:
- thread.join()
-
-if __name__ == '__main__':
- if len(sys.argv) > 1:
- host = sys.argv[1]
- else:
- host = DEFAULT_HOST
-
-
-cycle_trace = 'start'
-while True:
- if (cycle_trace == 'disconnected'):
- print "Scratch disconnected"
- cleanup_threads((listener, sender))
- time.sleep(1)
- cycle_trace = 'start'
-
- if (cycle_trace == 'start'):
- # open the socket
- print 'Starting to connect...' ,
- the_socket = create_socket(host, PORT)
- print 'Connected!'
- 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)
- cycle_trace = 'running'
- print "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)
-
- # wait for ctrl+c
- try:
- #just pause
-
-# else:
- time.sleep(0.1)
- except KeyboardInterrupt:
- cleanup_threads((sender, drum_listener, guitar_listener, maracas_listener))
- sys.exit()
-
--- /dev/null
+const int NUMBER_OF_MARACAS = 2;
+const int MARACAS_PINS[] = {6, 7};
+const int MARACAS_DELAY = 5000;
+
+const int NUMBER_OF_DRUMS= 4;
+const int DRUM_PINS[] = {8, 9, 10, 11};
+const int DRUM_DELAY = 2500;
+
+const int GUITAR_THRESHOLD = 20;
+const int GUITAR_PIN = A0;
+const int GUITAR_DELAY = 5000;
+
+int maracas_states[] = {-1,-1};
+int maracas_delays[] = {0, 0};
+
+int drum_delays[] = {0, 0, 0, 0};
+
+int guitar_delay = 0;
+
+void setup() {
+ for (int i = 0; i < NUMBER_OF_MARACAS; i++) {
+ pinMode(MARACAS_PINS[i], INPUT);
+ }
+ for (int i = 0; i < NUMBER_OF_DRUMS; i++) {
+ pinMode(DRUM_PINS[i], INPUT);
+ }
+ Serial.begin(9600);
+}
+
+void loop() {
+ maracas();
+ drums();
+ guitar();
+ update_delays();
+}
+
+
+void maracas() {
+ for (int i = 0; i < NUMBER_OF_MARACAS; i++) {
+ int newState = digitalRead(MARACAS_PINS[i]);
+ if (newState != maracas_states[i] && maracas_delays[i] < 1) {
+ Serial.print("maracas,");
+ Serial.print(i);
+ Serial.println();
+ maracas_states[i] = newState;
+ maracas_delays[i] = MARACAS_DELAY;
+ }
+ }
+}
+
+void drums() {
+ for (int i = 0; i < NUMBER_OF_DRUMS; i++) {
+ if (digitalRead(DRUM_PINS[i]) == HIGH && drum_delays[i] < 1) {
+ Serial.print("drum,");
+ Serial.print(i);
+ Serial.println();
+ drum_delays[i] = DRUM_DELAY;
+ }
+ }
+}
+
+void guitar() {
+ int pitch = analogRead(GUITAR_PIN);
+ if (pitch >= GUITAR_THRESHOLD && guitar_delay < 1) {
+ Serial.print("guitar,");
+ Serial.print(pitch);
+ Serial.println();
+ guitar_delay = GUITAR_DELAY;
+ }
+}
+
+void update_delays() {
+ for (int i = 0; i < NUMBER_OF_MARACAS; i++) {
+ if (maracas_delays[i] > 0) {
+ maracas_delays[i] -= 1;
+ }
+ }
+ for (int i = 0; i < NUMBER_OF_DRUMS; i++) {
+ if (drum_delays[i] > 0) {
+ drum_delays[i] -= 1;
+ }
+ }
+ if (guitar_delay > 0) {
+ guitar_delay -= 1;
+ }
+}
--- /dev/null
+const int NUMBER_OF_MARACAS = 2;
+const int MARACAS_PINS[] = {6, 7};
+const int MARACAS_DELAY = 5000;
+
+int maracas_states[] = {-1,-1};
+int maracas_delays[] = {0, 0};
+
+void setup() {
+ for (int i = 0; i < NUMBER_OF_MARACAS; i++) {
+ pinMode(MARACAS_PINS[i], INPUT);
+ }
+ Serial.begin(9600);
+}
+
+void loop() {
+ maracas();
+ update_delays();
+}
+
+
+void maracas() {
+ for (int i = 0; i < NUMBER_OF_MARACAS; i++) {
+ int newState = digitalRead(MARACAS_PINS[i]);
+ if (newState != maracas_states[i] && maracas_delays[i] < 1) {
+ Serial.print("maracas,");
+ Serial.print(i);
+ Serial.println();
+ maracas_states[i] = newState;
+ maracas_delays[i] = MARACAS_DELAY;
+ }
+ }
+}
+
+void update_delays() {
+ for (int i = 0; i < NUMBER_OF_MARACAS; i++) {
+ if (maracas_delays[i] > 0) {
+ maracas_delays[i] -= 1;
+ }
+ }
+}
--- /dev/null
+const int NUMBER_OF_MARACAS = 2;
+const int MARACAS_PINS[] = {6, 7};
+const int MARACAS_DELAY = 5000;
+
+const int NUMBER_OF_DRUMS= 4;
+const int DRUM_PINS[] = {8, 9, 10, 11};
+const int DRUM_DELAY = 2500;
+
+int maracas_states[] = {-1,-1};
+int maracas_delays[] = {0, 0};
+
+int drum_delays[] = {0, 0, 0, 0};
+
+void setup() {
+ for (int i = 0; i < NUMBER_OF_MARACAS; i++) {
+ pinMode(MARACAS_PINS[i], INPUT);
+ }
+ for (int i = 0; i < NUMBER_OF_DRUMS; i++) {
+ pinMode(DRUM_PINS[i], INPUT);
+ }
+ Serial.begin(9600);
+}
+
+void loop() {
+ maracas();
+ drums();
+ update_delays();
+}
+
+
+void maracas() {
+ for (int i = 0; i < NUMBER_OF_MARACAS; i++) {
+ int newState = digitalRead(MARACAS_PINS[i]);
+ if (newState != maracas_states[i] && maracas_delays[i] < 1) {
+ Serial.print("maracas,");
+ Serial.print(i);
+ Serial.println();
+ maracas_states[i] = newState;
+ maracas_delays[i] = MARACAS_DELAY;
+ }
+ }
+}
+
+void drums() {
+ for (int i = 0; i < NUMBER_OF_DRUMS; i++) {
+ if (digitalRead(DRUM_PINS[i]) == HIGH && drum_delays[i] < 1) {
+ Serial.print("drum,");
+ Serial.print(i);
+ Serial.println();
+ drum_delays[i] = DRUM_DELAY;
+ }
+ }
+}
+
+void update_delays() {
+ for (int i = 0; i < NUMBER_OF_MARACAS; i++) {
+ if (maracas_delays[i] > 0) {
+ maracas_delays[i] -= 1;
+ }
+ }
+ for (int i = 0; i < NUMBER_OF_DRUMS; i++) {
+ if (drum_delays[i] > 0) {
+ drum_delays[i] -= 1;
+ }
+ }
+}
--- /dev/null
+const int NUMBER_OF_MARACAS = 2;
+const int MARACAS_PINS[] = {6, 7};
+const int MARACAS_DELAY = 5000;
+
+const int NUMBER_OF_DRUMS= 4;
+const int DRUM_PINS[] = {8, 9, 10, 11};
+const int DRUM_DELAY = 2500;
+
+const int GUITAR_THRESHOLD = 20;
+const int GUITAR_PIN = A0;
+const int GUITAR_DELAY = 5000;
+
+int maracas_states[] = {-1,-1};
+int maracas_delays[] = {0, 0};
+
+int drum_delays[] = {0, 0, 0, 0};
+
+int guitar_delay = 0;
+
+void setup() {
+ for (int i = 0; i < NUMBER_OF_MARACAS; i++) {
+ pinMode(MARACAS_PINS[i], INPUT);
+ }
+ for (int i = 0; i < NUMBER_OF_DRUMS; i++) {
+ pinMode(DRUM_PINS[i], INPUT);
+ }
+ Serial.begin(9600);
+}
+
+void loop() {
+ maracas();
+ drums();
+ guitar();
+ update_delays();
+}
+
+
+void maracas() {
+ for (int i = 0; i < NUMBER_OF_MARACAS; i++) {
+ int newState = digitalRead(MARACAS_PINS[i]);
+ if (newState != maracas_states[i] && maracas_delays[i] < 1) {
+ Serial.print("maracas,");
+ Serial.print(i);
+ Serial.println();
+ maracas_states[i] = newState;
+ maracas_delays[i] = MARACAS_DELAY;
+ }
+ }
+}
+
+void drums() {
+ for (int i = 0; i < NUMBER_OF_DRUMS; i++) {
+ if (digitalRead(DRUM_PINS[i]) == HIGH && drum_delays[i] < 1) {
+ Serial.print("drum,");
+ Serial.print(i);
+ Serial.println();
+ drum_delays[i] = DRUM_DELAY;
+ }
+ }
+}
+
+void guitar() {
+ int pitch = analogRead(GUITAR_PIN);
+ if (pitch >= GUITAR_THRESHOLD && guitar_delay < 1) {
+ Serial.print("guitar,");
+ Serial.print(pitch);
+ Serial.println();
+ guitar_delay = GUITAR_DELAY;
+ }
+}
+
+void update_delays() {
+ for (int i = 0; i < NUMBER_OF_MARACAS; i++) {
+ if (maracas_delays[i] > 0) {
+ maracas_delays[i] -= 1;
+ }
+ }
+ for (int i = 0; i < NUMBER_OF_DRUMS; i++) {
+ if (drum_delays[i] > 0) {
+ drum_delays[i] -= 1;
+ }
+ }
+ if (guitar_delay > 0) {
+ guitar_delay -= 1;
+ }
+}
--- /dev/null
+# This code is copyright ...... under the GPL v2.
+# This code is derived from scratch_gpio_handler by Simon Walters, which
+# is derived from scratch_handler by Thomas Preston
+# Version 0.1: It's kind of working.
+
+from array import *
+import threading
+import socket
+import time
+import sys
+import struct
+import serial
+import io
+import logging
+
+PORT = 42001
+DEFAULT_HOST = '127.0.0.1'
+BUFFER_SIZE = 240 #used to be 100
+SOCKET_TIMEOUT = 1
+DEVICES = ['/dev/ttyACM3']
+#DEVICES = ['/dev/ttyACM0', '/dev/ttyACM1','/dev/ttyACM3']
+ARDUINO_BAUD_RATE = 9600
+
+BROADCAST_NAMES = {'guitar': 'guitar',
+ 'drum': {0: 'cymbal',
+ 1: 'hihat',
+ 2: 'slowdrum',
+ 3: 'snare',
+ 4: 'tomtom'},
+ 'maracas': 'maracas'}
+
+SENSOR_NAMES = {'guitar': 'guitar_pitch'}
+
+logging.basicConfig(level = logging.INFO)
+#logging.basicConfig(level = logging.DEBUG)
+
+class MyError(Exception):
+ def __init__(self, value):
+ self.value = value
+
+ def __str__(self):
+ return repr(self.value)
+
+class ScratchSender(threading.Thread):
+ def __init__(self, socket):
+ threading.Thread.__init__(self)
+ self.scratch_socket = socket
+ self._stop = threading.Event()
+
+ def join(self,timeout=None):
+ self._stop.set()
+ threading.Thread.join(self, timeout)
+
+ def stopped(self):
+ return self._stop.isSet()
+
+ def run(self):
+ while not self.stopped():
+ time.sleep(0.01) # be kind to cpu - not certain why :)
+
+ def send_scratch_command(self, cmd):
+ n = len(cmd)
+ a = array('c')
+ a.append(chr((n >> 24) & 0xFF))
+ a.append(chr((n >> 16) & 0xFF))
+ a.append(chr((n >> 8) & 0xFF))
+ a.append(chr(n & 0xFF))
+ self.scratch_socket.send(a.tostring() + cmd)
+
+
+class ArduinoListener(threading.Thread):
+ def __init__(self, device, speed, sender, instruments, values):
+ threading.Thread.__init__(self)
+ self.arduino_device = serial.Serial(device, speed, timeout=0.5)
+ self._stop = threading.Event()
+ self.scratch_sender = sender
+ self.instruments = instruments
+ self.values = values
+ logging.info("Started listener on port %s" % device)
+
+ def join(self,timeout=None):
+ self._stop.set()
+ threading.Thread.join(self, timeout)
+
+ def stopped(self):
+ return self._stop.isSet()
+
+ def run(self):
+ self.arduino_device.readline() # discard the first (partial) line
+ while not self.stopped():
+ logging.debug('Thread waiting for a signal')
+ try:
+ 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 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:
+ logging.info('Connecting to Scratch')
+ scratch_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ scratch_sock.connect((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))
+ time.sleep(3)
+ return scratch_sock
+
+def cleanup_threads(threads):
+ logging.debug("Stopping %d threads" % len(threads))
+ for thread in threads:
+ thread.join()
+ logging.debug("Threads joined")
+
+if __name__ == '__main__':
+ if len(sys.argv) > 1:
+ host = sys.argv[1]
+ else:
+ host = DEFAULT_HOST
+
+cycle_trace = 'start'
+while True:
+ if (cycle_trace == 'disconnected'):
+ logging.info("Scratch disconnected")
+ cleanup_threads(listeners + sender)
+ time.sleep(1)
+ cycle_trace = 'start'
+
+ if (cycle_trace == 'start'):
+ # open the socket
+ logging.info('Connecting to Scratch...')
+ the_socket = create_socket(host, PORT)
+ logging.info('Connected to Scratch')
+ the_socket.settimeout(SOCKET_TIMEOUT)
+ sender = ScratchSender(the_socket)
+ 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:
+ listener.start()
+
+ # wait for ctrl+c
+ try:
+ #just pause
+ time.sleep(0.1)
+ except KeyboardInterrupt:
+ logging.warning("Interrrupted")
+ cleanup_threads(listeners + [sender])
+ sys.exit()
+
--- /dev/null
+[Dolphin]
+Timestamp=2013,4,19,20,12,1
+Version=3
+ViewMode=1