Skip to content

Instantly share code, notes, and snippets.

@binary1230
Created August 2, 2015 23:22
Show Gist options
  • Save binary1230/dfd1a44aafcdcc0995f6 to your computer and use it in GitHub Desktop.
Save binary1230/dfd1a44aafcdcc0995f6 to your computer and use it in GitHub Desktop.
play some bews with an arduino and wav file
import serial
import time
import winsound
import http.server
import socketserver
from urllib.parse import urlparse, parse_qs
import text2num
def init_serialport():
global ser
ser = serial.Serial(port=2, baudrate=230400)
print(ser.portstr) # check which port was really used
def kill_serialport():
ser.close()
def play_snd(sound):
winsound.PlaySound(sound, winsound.SND_FILENAME | winsound.SND_ASYNC)
def stop_snd(sound):
play_snd(None)
def do_bew(num_bews=1):
ser.write(bytearray('1','ascii'))
for i in range(num_bews):
play_snd('bew.wav')
time.sleep(.15)
def send_text(stream, text):
stream.wfile.write(bytes(text, 'ascii'))
class MyHandler(http.server.BaseHTTPRequestHandler):
def do_HEAD(s):
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
def do_GET(s):
"""Respond to a GET request."""
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
o = urlparse(s.path)
qs = parse_qs(o.query)
if (o.path == "/bew"):
num_bews = 0;
try:
amount = qs["count"][0]
if amount == "for": # can't complain, google's voice recognition is otherwise amazing
amount = "four"
num_bews = text2num.text2num(amount)
except Exception:
pass
if num_bews == 0:
num_bews = 1
do_bew(num_bews)
send_text(s,"#bews=" + str(num_bews))
if (o.path == "/rawk"):
play_snd('deedlydeedlies.wav')
send_text(s,"<br/><p>You accessed path: %s</p>" % s.path)
def init_http():
global httpd
HandlerClass = MyHandler
Protocol = "HTTP/1.0"
server_address = ('', 8888)
HandlerClass.protocol_version = Protocol
httpd = socketserver.TCPServer(server_address, MyHandler)
sa = httpd.socket.getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
init_http()
init_serialport()
# init_osc()
# server.serve_forever()
httpd.serve_forever()
kill_serialport()
time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment