Skip to content

Instantly share code, notes, and snippets.

@youchan
Last active December 13, 2015 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youchan/4972144 to your computer and use it in GitHub Desktop.
Save youchan/4972144 to your computer and use it in GitHub Desktop.
require 'sinatra'
set :port, 8000
post '/mml' do
File.open('mml.txt', "w") do |f|
f.write("#mml\n");
f.write(params[:mml]);
end
params[:mml]
end
get '/mml' do
if File.exist?('mml.txt') then
content = File.read('mml.txt')
File.delete('mml.txt')
content
else
""
end
end
import time
import serial
import sys
import httplib, urllib
tmp_msg = "/dev/ttyACM0"
ser = serial.Serial(tmp_msg)
ser.baudrate = 115200
ser.timeout = 1
print ser.portstr
_SEVER_ = "youchan.org:8000"
_ADDR_ = "/mml"
friquency = {
"f": 369.99,
"g": 391.99,
"a": 440,
"b": 493.88,
"C": 554.36,
"D": 587.32,
"E": 659.25,
"F": 739.98,
"G": 783.99,
"A": 880,
"B": 987.76,
}
cnt = 0
while 1 :
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = httplib.HTTPConnection( _SEVER_ )
conn.request("GET", _ADDR_ , "", headers)
response = conn.getresponse()
data = response.read().split('\n')
conn.close()
if data[0] == "#mml" :
time.sleep(3)
mml = data[1]
print(mml)
for i in range(0, len(mml)/2):
if mml[i*2] == "r":
time.sleep(.3 * int(mml[i*2+1]))
continue
ser.write("13,%d,%d\n" % (friquency[mml[i*2]], 240*int(mml[i*2+1])))
time.sleep(.3 * int(mml[i*2+1]))
time.sleep(1)
data = []
ser.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment