summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarian Sigler <m@qjym.de>2013-06-08 20:51:08 +0200
committerMarian Sigler <m@qjym.de>2013-06-08 20:51:08 +0200
commit3592a18b90f5c6a02f43f2e3625aaddfb32b8513 (patch)
tree695426666a275b4b1d8e0bfb5854c9703ff9ec6a
downloadtesla-3592a18b90f5c6a02f43f2e3625aaddfb32b8513.tar.gz
tesla-3592a18b90f5c6a02f43f2e3625aaddfb32b8513.tar.bz2
tesla-3592a18b90f5c6a02f43f2e3625aaddfb32b8513.zip
initial commit
-rw-r--r--monophon/monophon.ino54
-rw-r--r--rtttl.py87
2 files changed, 141 insertions, 0 deletions
diff --git a/monophon/monophon.ino b/monophon/monophon.ino
new file mode 100644
index 0000000..1643bd4
--- /dev/null
+++ b/monophon/monophon.ino
@@ -0,0 +1,54 @@
+// play monophon sounds on the tesla coil
+//
+// receives uint16s from serial which specify the tone length in microseconds (1000000/440 is an "a" etc)
+
+
+int pin1 = 4;
+int pin2 = 5;
+unsigned int tonelength = 0;
+float high_factor = .2;
+
+void setup () {
+ pinMode(pin1, OUTPUT);
+ pinMode(pin2, OUTPUT);
+ Serial.begin(9600);
+}
+
+void loop() {
+ while ( true ) {
+ if ( Serial.available() > 1 ) {
+ tonelength = Serial.read() << 8;
+ tonelength |= Serial.read();
+ Serial.print("New tonelength: ");
+ Serial.println(tonelength);
+ }
+ if ( tonelength == 0 )
+ delayMicroseconds(10);
+ else
+ beep(tonelength);
+ }
+}
+
+void beep(long usecs) {
+ high();
+ delayMicroseconds(usecs*high_factor);
+// delayMicroseconds(usecs*on_percentage/100);
+ low();
+ delayMicroseconds(usecs*(1-high_factor));
+// delayMicroseconds(usecs*(100-on_percentage)/100);
+}
+
+void high() {
+ digitalWrite(pin1, HIGH);
+ digitalWrite(pin2, HIGH);
+}
+
+void low() {
+ digitalWrite(pin1, LOW);
+ digitalWrite(pin2, LOW);
+}
+
+long freq (double f) {
+ // convert frequency to tonelength
+ return 1000000/f;
+}
diff --git a/rtttl.py b/rtttl.py
new file mode 100644
index 0000000..b0dcc05
--- /dev/null
+++ b/rtttl.py
@@ -0,0 +1,87 @@
+#!/usr/bin/python
+"""
+play a rtttl file by sending the tone lengths to the arduino
+"""
+from __future__ import division
+import re
+import serial
+import struct
+import sys
+import time
+
+BAUDRATE = 9600
+PAUSE = .001
+
+_rtttl_re = re.compile('([0-9]+)?([a-hp]#?)(\.?)([0-9]?)(?:(?:,[ \n]*|$))')
+_rtttl_prefix_re = re.compile('d=([0-9]+),o=([1-5]),b=([0-9]+)')
+
+freqs = {
+ 'a': 440.000,
+ 'a#': 466.164,
+ 'b': 493.883,
+ 'h': 493.883,
+ 'c': 523.251,
+ 'c#': 554.365,
+ 'd': 587.330,
+ 'd#': 622.254,
+ 'e': 659.255,
+ 'f': 698.456,
+ 'f#': 739.989,
+ 'g': 783.991,
+ 'g#': 830.609,
+ 'p': 0,
+}
+
+def parse(s):
+ name, prefix, music = s.split(':')
+ pm = _rtttl_prefix_re.match(prefix)
+ d, o, b = pm.groups()
+
+ defaultlength = int(d)
+ octave = int(o)
+ timefactor = int(b)/60
+
+ for m in _rtttl_re.finditer(music):
+ l, t, ld, o = m.groups()
+
+ if l:
+ length = 1/int(l)
+ else:
+ length = 1/defaultlength
+ if ld:
+ length = length * 1.5
+ length = length*timefactor
+ tone = freqs[t.lower()]
+ if o:
+ octave = o
+ tone = tone * 2**(int(octave) - 4)
+
+
+ yield (length, tone)
+
+def send(sounds, device):
+ with serial.Serial(device, BAUDRATE) as ser:
+ for length, freq in sounds:
+ sys.stderr.write('len: %.3f, ton: %9.3f, send: %d' % (length, freq, freq2t(freq)))
+ ser.write(struct.pack('>H', freq2t(freq)))
+ time.sleep(length)
+ sys.stderr.write('\n')
+ ser.write('\0\0')
+ time.sleep(PAUSE)
+ ser.write('\0\0')
+
+
+def freq2t(freq):
+ if freq == 0:
+ return 0
+ return 2000000/freq
+
+if __name__ == '__main__':
+ if len(sys.argv) > 1:
+ if sys.argv[1] == '-d':
+ for length, freq in parse(sys.stdin.read()):
+ print 'len: %.3f, ton: %9.3f' % (length, freq)
+ else:
+ send(parse(sys.stdin.read()), sys.argv[1])
+ else:
+ print >>sys.stderr, "Usage: %s -d|/dev/ttyUSBx" % sys.argv[0]