This repository was archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradio.py
More file actions
137 lines (107 loc) · 3.1 KB
/
radio.py
File metadata and controls
137 lines (107 loc) · 3.1 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# radio.py
import subprocess
import sys
import os
picdir = '/home/pi/Radio/pic'
libdir = '/home/pi/Radio/lib'
if os.path.exists(libdir):
sys.path.append(libdir)
import logging
from waveshare_epd import epd2in7
from gpiozero import Button
import time
from PIL import Image,ImageDraw,ImageFont
import traceback
file = open("./stations.txt", "r")
stations = []
for line in file:
strippedLine = line.rstrip()
stations.append(strippedLine.split(' - '))
logging.basicConfig(level=logging.DEBUG)
cursorPoint = 0
isRadioPlaying = False
key1 = Button(5)
key2 = Button(6)
key3 = Button(13)
key4 = Button(19)
font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
font18 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)
font35 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 35)
epd = epd2in7.EPD()
epd.init()
def displayUI(himageFromCursor):
try:
epd.Clear(0xFF)
Himage = himageFromCursor
draw = ImageDraw.Draw(Himage)
for idx,station in enumerate(stations):
draw.text((10, idx*15), str(station[0]), font = font18, fill = 0)
epd.display(epd.getbuffer(Himage))
except IOError as e:
logging.info(e)
except KeyboardInterrupt:
logging.info("ctrl + c:")
logging.info("Clear...")
epd.Clear(0xFF)
logging.info("Goto Sleep...")
epd.sleep()
epd2in7.epdconfig.module_exit()
exit()
def displayCursor():
try:
Himage = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
draw = ImageDraw.Draw(Himage)
draw.text((0, 15*cursorPoint), ">", font = font18, fill = 0)
displayUI(Himage)
except IOError as e:
logging.info(e)
except KeyboardInterrupt:
logging.info("ctrl + c:")
logging.info("Clear...")
epd.Clear(0xFF)
logging.info("Goto Sleep...")
epd.sleep()
epd2in7.epdconfig.module_exit()
exit()
def cursorUp():
global cursorPoint
if (cursorPoint-1<0):
cursorPoint=len(stations)-1
else:
cursorPoint -= 1
displayCursor()
def cursorDown():
global cursorPoint
if (cursorPoint+1>len(stations)-1):
cursorPoint = 0
else:
cursorPoint += 1
displayCursor()
def killRadio():
global isRadioPlaying
if(isRadioPlaying):
print("Radio was killed")
killCommand = "pkill mpv"
process = subprocess.Popen(killCommand.split(), stdout=subprocess.PIPE)
isRadioPlaying = False
else:
print("No radio running to be killed")
def startRadio():
time.sleep(2)
global isRadioPlaying
killRadio()
isRadioPlaying = True
bashCommand = "mpv " + stations[cursorPoint][1]
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
key1.when_pressed = cursorUp
key2.when_pressed = cursorDown
key3.when_pressed = startRadio
key4.when_pressed = killRadio
displayCursor()
for station in stations:
print station
while True:
pass