-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccel_server.py
71 lines (50 loc) · 1.36 KB
/
accel_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import serial
import time
from alsaaudio import Mixer
name_0 = '/dev/ttyACM0'
BAUD_RATE = 115200
mix = Mixer()
def checkPort(port):
try:
ser = serial.Serial(port, BAUD_RATE, timeout=1)
print("Connected!")
return True
except OSError:
return False
def checkBuffer(b):
return not (b and (b[-1] == '\n'))
def changeVolume(m, degree):
if degree:
current_volume = m.getvolume()[0]
new_volume = current_volume + degree
if new_volume < 0:
new_volume = 0
elif new_volume > 100:
new_volume = 100
m.setvolume(new_volume)
print("Volume set to {}%".format(new_volume))
def mute_unmute(m):
is_muted = bool(m.getmute()[0])
m.setmute(not is_muted)
if not is_muted:
print("Muted")
else:
print("Unmuted")
# attempt to connect once per second until success
while not checkPort(name_0):
time.sleep(1)
ser = serial.Serial(name_0, BAUD_RATE, timeout=1)
buf = []
while True:
while checkBuffer(buf):
received = ser.read(size=1).decode("utf-8")
buf.append(received)
c = ''.join(buf[:-1])
try:
accel_data = int(c)
# print("Received accel data: {}".format(accel_data))
changeVolume(mix, accel_data)
except ValueError:
if c == "BUT":
mute_unmute(mix)
buf = []