-
Notifications
You must be signed in to change notification settings - Fork 1
/
ashore_onlymp3.py
91 lines (77 loc) · 2.07 KB
/
ashore_onlymp3.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import serial
import pygame
from time import sleep
def press_center():
print('center')
global is_play
if is_play:
m.pause()
is_play = 0
else:
m.unpause()
is_play = 1
def press_prev():
print('prev')
global current_file
current_file = (current_file + num_of_file - 1) % num_of_file
load_new_file()
def press_forw():
print('forw')
global current_file
current_file = (current_file + 1) % num_of_file
load_new_file()
def press_volume():
global vol
for i in range(5):
if c2 == VOL[i]:
vol = i
m.set_volume(vol/6)
break
#print('vol')
print(vol)
def load_new_file():
global is_play
print("file:")
print(current_file)
m.stop()
#m.unload()
m.load(file[current_file])
m.play()
is_play = 1
START = bytes.fromhex('F2')
END = bytes.fromhex('2F')
CENTER = b'\x84'
PREV = b'\x81'
FORW = b'\x82'
VOL = [b'\x01', b'\x03',b'\x07',b'\x0f',b'\x1f',b'\x1f']
#VOL2 = [bytes.fromhex('80012f'), bytes.fromhex('80032f'), bytes.fromhex('80072f'),bytes.fromhex('800f2f'), bytes.fromhex('801f2f'), bytes.fromhex('803f2f')]
c = ''
c_prev = ''
vol = 6
is_play = 1
file = ["/home/pi/Desktop/audio-stream/music/1.wav","/home/pi/Desktop/audio-stream/music/2.wav","/home/pi/Desktop/audio-stream/music/music.mp3"] # add file names here!
num_of_file = len(file)
current_file = 0
pygame.mixer.init()
pygame.mixer.music.load(file[current_file])
m = pygame.mixer.music
m.set_volume(vol/6)
m.play()
ser = serial.Serial("/dev/ttyUSB0",9600)
ser.reset_input_buffer()
while True:
if ser.inWaiting() > 3:
first = ser.read()
if first == START:
temp = ser.read()
c_prev = c
c = temp
c2 = ser.read()
if c == CENTER and c_prev != CENTER:
press_center()
elif c == PREV and c_prev != PREV:
press_prev()
elif c == FORW and c_prev != FORW:
press_forw()
elif c != '' and c2 != '':
press_volume()