Finished serial handling
To be tested. No arduino for now :/
This commit is contained in:
parent
4d96e938c6
commit
3d5562065a
@ -61,7 +61,7 @@ def read_serial(ser):
|
|||||||
framerate = 16000
|
framerate = 16000
|
||||||
processes = []
|
processes = []
|
||||||
frequencies = {"FA": 698.5, "SOL": 784, "LA": 880, "SIb": 932, "DO": 1046.5}
|
frequencies = {"FA": 698.5, "SOL": 784, "LA": 880, "SIb": 932, "DO": 1046.5}
|
||||||
notes = frequencies.keys()
|
notes = list(frequencies.keys())
|
||||||
thresholds = []
|
thresholds = []
|
||||||
measures = []
|
measures = []
|
||||||
|
|
||||||
@ -69,21 +69,24 @@ serial_port = "/dev/ttyACM0"
|
|||||||
serial_speed = 115200
|
serial_speed = 115200
|
||||||
|
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.getopt(sys.argv[1:], "hs:", ["help", "serial="])
|
opts, args = getopt.getopt(sys.argv[1:], "hs:S:", ["help", "serial=",
|
||||||
|
"speed="])
|
||||||
|
|
||||||
for opt, arg in opts:
|
for opt, arg in opts:
|
||||||
if opt in ("-h", "--help"):
|
if opt in ("-h", "--help"):
|
||||||
print("Touchless 3D tracking with color mapping")
|
print("Play music with chorizos ! Only a limited set of notes " +
|
||||||
|
"is available to play jingle bells.")
|
||||||
print("\nUsage : "+sys.argv[0]+" [OPTIONS]")
|
print("\nUsage : "+sys.argv[0]+" [OPTIONS]")
|
||||||
print("\nTrack the position of your hand in 3D and map it " +
|
|
||||||
"in RGB space")
|
|
||||||
print("\nOptions :")
|
print("\nOptions :")
|
||||||
print("\t-h (--help) \t display this help message")
|
print("\t-h (--help) \t display this help message")
|
||||||
print("\t-s (--serial=) \t change serial port (default is " +
|
print("\t-s (--serial=) \t change serial port (default is " +
|
||||||
"/dev/tty/ACM0")
|
"/dev/tty/ACM0")
|
||||||
|
print("\t-S (--speed=) \t change serial speed (default is 115200)")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
elif opt in ("-s", "--serial"):
|
elif opt in ("-s", "--serial"):
|
||||||
serial_port = arg
|
serial_port = arg
|
||||||
|
elif opt in ("-S", "--speed"):
|
||||||
|
serial_speed = arg
|
||||||
except getopt.GetoptError:
|
except getopt.GetoptError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -94,66 +97,63 @@ except Exception as e:
|
|||||||
sys.exit("Invalid serial port options : "+str(e))
|
sys.exit("Invalid serial port options : "+str(e))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ser.open()
|
if not ser.isOpen():
|
||||||
|
ser.open()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
sys.exit("Error while opening serial port : "+str(e))
|
sys.exit("Error while opening serial port : "+str(e))
|
||||||
|
|
||||||
if not ser.isOpen():
|
|
||||||
sys.exit("Serial port not opened")
|
|
||||||
|
|
||||||
ser.flushInput()
|
ser.flushInput()
|
||||||
ser.flushOutput()
|
ser.flushOutput()
|
||||||
|
|
||||||
# Handle calibration
|
# Handle calibration
|
||||||
for i in len(notes):
|
for i in range(len(notes)):
|
||||||
print("Calibrating note "+notes[i])
|
print("Calibrating note "+notes[i])
|
||||||
print("Touch and release the key now... Will get 10k samples.")
|
print("Touch and release the key now... Will get 1k samples.")
|
||||||
|
|
||||||
for sample in range(10000):
|
for sample in range(1000):
|
||||||
measures[sample] = read_serial(ser)
|
measure = read_serial(ser)
|
||||||
|
measures[sample] = measure[i]
|
||||||
|
|
||||||
measures.sort()
|
measures.sort()
|
||||||
max_diff = 0
|
max_diff = 0
|
||||||
max_diff_index = 0
|
max_diff_index = 0
|
||||||
|
|
||||||
for sample in range(10000-1):
|
for sample in range(1000-1):
|
||||||
if measures[sample+1]-measures[sample]:
|
if measures[sample+1]-measures[sample] > max_diff:
|
||||||
max_diff = measures[sample+1]-measures[sample]
|
max_diff = measures[sample+1]-measures[sample]
|
||||||
max_diff_index = sample
|
max_diff_index = sample
|
||||||
|
|
||||||
thresholds[i] = (measures[max_diff_index+1]+measures[max_diff_index])/2
|
thresholds[i] = (measures[max_diff_index+1]+measures[max_diff_index])/2
|
||||||
|
|
||||||
# Main loop
|
# Main loop
|
||||||
# *** TODO *** : Add fetch from the serial also
|
|
||||||
print("Running... Press q to quit.")
|
print("Running... Press q to quit.")
|
||||||
running = True
|
running = True
|
||||||
while running:
|
while running:
|
||||||
serial_input = read_serial(ser)
|
serial_input = read_serial(ser)
|
||||||
|
frequency = 0
|
||||||
|
|
||||||
for note in range(notes):
|
for note in range(len(notes)):
|
||||||
|
if serial_input[note] > thresholds[note]:
|
||||||
|
frequency = frequencies[notes[note]]
|
||||||
|
|
||||||
|
if frequency == 0:
|
||||||
char = getch()
|
char = getch()
|
||||||
if char == "q":
|
if char == "q":
|
||||||
print("Exiting...")
|
print("Exiting...")
|
||||||
running = False
|
running = False
|
||||||
continue
|
continue
|
||||||
if char == "a":
|
if char == "a":
|
||||||
frequency = frequencies[0]
|
frequency = frequencies["LA"]
|
||||||
if char == "b":
|
elif char == "b":
|
||||||
frequency = frequencies[1]
|
frequency = frequencies["SIb"]
|
||||||
if char == "c":
|
elif char == "c":
|
||||||
frequency = frequencies[2]
|
frequency = frequencies["DO"]
|
||||||
if char == "d":
|
elif char == "e":
|
||||||
frequency = frequencies[3]
|
frequency = frequencies["FA"]
|
||||||
if char == "e":
|
elif char == "g":
|
||||||
frequency = frequencies[4]
|
frequency = frequencies["SOL"]
|
||||||
if char == "f":
|
else:
|
||||||
frequency = frequencies[5]
|
continue
|
||||||
if char == "g":
|
|
||||||
frequency = frequencies[6]
|
|
||||||
else:
|
|
||||||
continue
|
|
||||||
|
|
||||||
print("Playing "+char.upper())
|
print("Playing "+char.upper())
|
||||||
processes.append(Process(target=play_wave,
|
processes.append(Process(target=play_wave,
|
||||||
|
Loading…
Reference in New Issue
Block a user