-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcereal.py
More file actions
77 lines (59 loc) · 1.7 KB
/
cereal.py
File metadata and controls
77 lines (59 loc) · 1.7 KB
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
72
73
74
75
76
77
import serial
import time
def read_serial(s):
"""
Expects play_type and volume to be written to serial.
Returns the play_type and volume.
play_type = 0 -> pause
play_type = 1 -> play
play_type = 2 -> skip
play_type = 3 -> prev
volume -> between 0 to 100
An example of string read from serial is:
0,70
"""
while s.in_waiting <= 0:
#print("wait")
pass
try:
line = s.readline().decode("utf=8").rstrip()
print(line)
play_type, volume, power = line.split(sep=',')
play_type, volume, power = int(play_type), int(volume), int(power)
s.flush()
return play_type, volume, power
except:
print("Caught an error")
s.flush()
return None, None, None
def write_serial(s, on_off, time_left, curr_track_name):
"""
Writes:
<on_off>,<time_left>,<curr_track_name>
to serial
on_off -> 0 or 1
time_left -> float in ms
curr_track_name -> string
"""
wrt = f"{on_off},{time_left},{curr_track_name}:;"
s.write(wrt.encode())
s.flush()
if __name__ == "__main__":
s = serial.Serial("/dev/ttyACM0", 9600, timeout=1)
s.flush()
state = 0
while True:
state = (state + 1) % 2
play_type, volume = read_serial(s)
write_serial(s, state, state, "SONG NAME")
print(f"play type: {play_type}, volume: {volume}")
#if play_type == 0:
# run code to play
# pass
#if play_type == 1:
# run code to pause
# pass
#if play_type == 2:
# run code to skip
# pass
time.sleep(.01)