-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
149 lines (131 loc) · 4.66 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import RN52 # for interfacing with the RN52 board.
import PyPod # python ipod emulator (port this shit also fix it/improve it).
import time
import board
# import busio
from digitalio import DigitalInOut, Direction, Pull
'''
GPIO stuff:
Setup 2 GPIO PINs.
- 1) output to RN52 to be in command mode. Set high for command mode.
- 2) input to monitor GPIO2 on RN52 (change flag/inicator).
Pulled low to indicate change.
'''
CommandMode = DigitalInOut(board.D13)
CommandMode.direction = Direction.OUTPUT
CommandMode = True
ChangeFlag = DigitalInOut(board.D7)
ChangeFlag.direction = Direction.INPUT
ChangeFlag.pull = Pull.UP
'''
Initialize things.
'''
# Init the RN52 and PyPod modules.
# This brings up the UART/Serial interface in each.
RN52.RN52init()
time.sleep(2)
PyPod.init()
# Get some metadata. Might be dummy data but its
# something stored to be used later.
MetaData = RN52.GetMetaData()
# Push the metadata to the ipod module.
PyPod.MetaDataUpdate(MetaData)
# print(time.monotonic())
# Get Connection Status.
RN52Status = RN52.GetStatus()
print('inital status : ' + str(RN52Status))
if RN52Status[3] == 'D':
print('Playing')
# PyPod.SetPlayStatus(True)
else:
print('Not Playing')
# PyPod.SetPlayStatus(False)
# print('PlayStatus: ' + str(PyPod.PlayStatus))
if RN52Status[1] == '0':
MetaData['Title'] = 'Not Connected'
# print(MetaData)
'''
Check status of ChangeFlag Input pin.
Inverting flag value as its based on an input state being true or false.
where low indicates false indicates change.
'''
def ChangeCheck():
if ChangeFlag.value is False:
return(True)
else:
return(False)
'''
Process Command Reqests.
if a play control command then send it.
can build out more commands later.
'''
def ProcessCmdReq(cmd):
cmd = cmd.lower()
if cmd == 'playpause' or cmd == 'next' or cmd == 'prev':
print('PlayCommand: ' + cmd)
if cmd == 'playpause' and PyPod.PlayStatus is True:
PyPod.PlayStatus = False
else:
PyPod.PlayStatus = True
if cmd == 'next' or cmd == 'prev':
PyPod.TrackChangeNotification('TrackUpdate')
if RN52.PlayControl(cmd):
return (True)
else:
return (False)
'''
Main loop.
'''
StartTime = round(time.monotonic() * 1000)
UpdateTimer = StartTime
MDTimer = StartTime
while(True):
#elapsed = round(time.monotonic() * 1000 - StartTime)
if time.monotonic() * 1000 - UpdateTimer > 500 and PyPod.PlayChangeNotification:
PyPod.TrackChangeNotification()
UpdateTimer = round(time.monotonic() * 1000)
#Force check for MetaData update if X time has passed.
if time.monotonic() * 1000 - MDTimer > 15000:
print('Checking Stored MetaData against RN52 MetaData')
NewMD = RN52.GetMetaData()
if NewMD != MetaData:
print('MetaData changed: sending track update')
MetaData = NewMD
PyPod.MetaDataUpdate(MetaData)
PyPod.TrackChangeNotification('TrackUpdate')
MDTimer = round(time.monotonic() * 1000)
if PyPod.MetaData != MetaData:
PyPod.MetaDataUpdate(MetaData)
PyPod.TrackChangeNotification('TrackUpdate')
if ChangeCheck():
NewStatus = RN52.GetStatus()
print('Change detected: ' + str(RN52Status) + ' to ' + str(NewStatus))
if NewStatus != RN52Status:
if NewStatus[0] == '2':
NewMD = RN52.GetMetaData()
while NewMD == MetaData:
print('Updating MetaData')
NewMD = RN52.GetMetaData()
MetaData = NewMD
PyPod.MetaDataUpdate(NewMD)
PyPod.TrackChangeNotification('TrackUpdate')
if NewStatus[3] == 'D' and RN52Status[3] == '3':
print('Play Status changed to: Playing')
PyPod.SetPlayStatus(True)
if NewStatus[3] == '3' and RN52Status[3] == 'D':
print('Play Status changed to: Not Playing')
# PyPod.SetPlayStatus(False)
if NewStatus[1] == '4' and RN52Status[1] == '0':
print('Connection Status changed to: Connected')
if NewStatus[1] == '0' and RN52Status[1] == '4':
print('Connection status changed to: Not connected')
MetaData['Title'] = 'Not Connected'
# reset status as the track change flag changes back after query.
RN52Status = RN52.GetStatus()
iPodcommand = PyPod.ReadCommand()
if iPodcommand:
PyPod.CmdProcessor(iPodcommand)
# Check if there are any Command Requests from the ipod iterface
# Next Track, Play, ETC, MetaDataUpdate...
if PyPod.CmdReqsPending():
ProcessCmdReq(PyPod.GetExtCmdReq())