-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy patholed_phoniebox.py
395 lines (387 loc) · 21.2 KB
/
oled_phoniebox.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# For more details go to https://github.com/splitti/oled_phoniebox/
import signal
import sys
sys.path.append("/home/pi/oled_phoniebox/scripts/")
from o4p_functions import init_config, get_device, get_currcontrast, set_characters, get_mpc, get_wificonn, get_specialinfos, set_newmode
from time import sleep
from datetime import datetime
import os
from luma.core.render import canvas
from PIL import ImageFont, Image
FONT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__),
'fonts', 'Bitstream Vera Sans Mono Roman.ttf'))
FONT_STANDARD = ImageFont.truetype(FONT_PATH, 12)
FONT_SMALL = ImageFont.truetype(FONT_PATH, 10)
FONT_MIDTOWER = ImageFont.truetype(FONT_PATH, 42)
FONT_HIGHTOWER = ImageFont.truetype(FONT_PATH, 54)
FONT_PATH_WIFI = os.path.abspath(os.path.join(os.path.dirname(__file__),
'fonts', 'WIFI.ttf'))
FONT_WIFI = ImageFont.truetype(FONT_PATH_WIFI, 64)
FONT_WIFI_MIX = ImageFont.truetype(FONT_PATH_WIFI, 48)
CONFFILE = "/home/pi/oled_phoniebox/oled_phoniebox.conf"
TEMPFILE = "/tmp/o4p_overview.temp"
SYNC_TEMPFILE="/tmp/phoniebox_sync_state.tmp"
VERSION = "2.0.1 - 20220314"
def showimage(imgname):
img_path = os.path.abspath(os.path.join(os.path.dirname(__file__),'images', imgname+'.png'))
logo = Image.open(img_path).convert("RGBA")
fff = Image.new(logo.mode, logo.size, (255,) * 4)
background = Image.new("RGBA", device.size, "black")
posn = ((device.width - logo.width) // 2, 0)
img = Image.composite(logo, fff, logo)
background.paste(img, posn)
device.display(background.convert(device.mode))
def sigterm_handler(*_):
# save the state here or do whatever you want
showimage("poweroff")
sleep(1)
sys.exit(0)
#os._exit(0)
def main(num_iterations=sys.maxsize):
oldcontrast = get_currcontrast(CONFFILE)
device.contrast(oldcontrast)
showimage("music")
tmpcard = 3
linepos = 1
line1 = 4
line2 = 19
line3 = 34
line4org = 49
line4 = device.height-1-10
lenline1 = -1
lenline2 = -1
lenline3 = -1
subline1 = 0
subline2 = 0
subline3 = 0
widthletter = 7
spacejump = 7
oldmpc = ""
oldplaying = "-"
displaytime = 0.5
oldvol = "FirstStart"
wificonn = get_wificonn()
special = 0
while num_iterations > 0:
num_iterations = 1
curr_time = datetime.now()
seconds = curr_time.strftime('%S')
sleep(0.8)
seconds = int(seconds)%5
if seconds == 0:
wificonn = get_wificonn()
try:
#if wificonn != "BUGFIXING_LINE":
if (os.path.exists(TEMPFILE)) or (special == 1):
specialinfos = get_specialinfos()
if special == 0:
special = 1
timetoshow = 10
os.remove(TEMPFILE)
with canvas(device) as draw:
draw.text((0, line1), "WLAN: "+specialinfos[0],font=FONT_SMALL, fill="white")
draw.text((0, line2), "IP: "+specialinfos[1],font=FONT_SMALL, fill="white")
draw.text((0, line3), "Version:",font=FONT_SMALL, fill="white")
draw.text((0, line4org),VERSION,font=FONT_SMALL, fill="white")
draw.rectangle((device.width-4,0,device.width,device.height/10*timetoshow), outline="white", fill="white")
#draw.line((device.width, , device.width, device.height), fill="white")
#draw.text((110, line4org),timetoshow,font=FONT_SMALL, fill="white")
sleep(1)
timetoshow = timetoshow - 1
if timetoshow == 0:
special = 0
if os.path.exists(TEMPFILE):
timetoshow = 10
os.remove(TEMPFILE)
newmode = set_newmode(CONFFILE)
initvars.set('GENERAL', 'mode', newmode)
with canvas(device) as draw:
draw.text((0, line1),initvars['GENERAL']['mode'],font=FONT_HIGHTOWER, fill="white")
sleep(displaytime)
elif os.path.exists(SYNC_TEMPFILE):
with open(SYNC_TEMPFILE, 'r') as file:
syncdata = file.read()
with canvas(device) as draw:
draw.text((0, line1), syncdata,font=FONT_SMALL, fill="white")
else:
currcontrast = get_currcontrast(CONFFILE)
if currcontrast != oldcontrast:
device.contrast(currcontrast)
oldcontrast = currcontrast
mpcstatus = set_characters(get_mpc("mpc status"))
playing = mpcstatus.split("\n")[1].split(" ")[0] #Split to see if mpc is playing at the moment
currmpc = mpcstatus.split("\n")[0]
if (playing == "[playing]") or (playing == "[paused]"):
volume = mpcstatus.split("\n")[2].split(" ")[0].split(":")[1]
else:
volume = mpcstatus.split(" ")[0].split(":")[1]
vol = "V"+str(volume.replace("%",""))
if oldplaying != playing:
if playing == "[playing]":
with canvas(device) as draw:
draw.polygon([(49, 10), (79, 32), (49, 54)], outline="white", fill="white")
sleep(displaytime)
if playing == "[paused]":
with canvas(device) as draw:
draw.rectangle((51,10,59,54), outline="white", fill="white")
draw.rectangle((69,10,77,54), outline="white", fill="white")
sleep(displaytime)
oldplaying = playing
volume = int(volume.replace(" ","").replace("%",""))
if (oldvol != volume) and (oldvol != "FirstStart"):
with canvas(device) as draw:
draw.rectangle((30,22,45,42), outline="white", fill="white")
draw.polygon([(45, 22),(60, 10), (60,54), (45, 42)], outline="white", fill="white")
if volume != 0:
draw.rectangle((75,28,105,36), outline="white", fill="white")
if oldvol < volume:
draw.rectangle((86,17,94,47), outline="white", fill="white")
else:
draw.text((75, 2),"X",font=FONT_HIGHTOWER, fill="white")
sleep(displaytime)
oldvol = volume
if (playing == "[playing]") or (playing == "[paused]"):
if playing == "[playing]":
#timer = set_characters(get_mpc("mpc -f %time% current"))
elapsed = mpcstatus.split("\n")[1].replace(" "," ").replace(" "," ").split(" ")[2]
if currmpc != oldmpc:
track = mpcstatus.split("\n")[1].replace(" "," ").replace(" "," ").split(" ")[1].replace("#","") #set_characters(get_mpc("mpc -f %track% current"))
if len(track.split("/")[1]) > 2:
track = track.split("/")[0]
if track == "\n":
track = mpcstatus.split("\n")[1].replace(" ", " ").replace(" ", " ").split(" ")[1].replace("#","") #.split("/")[0]
file = set_characters(get_mpc("mpc -f %file% current")) # Get the current title
if initvars['GENERAL']['mode'] == "full" :
if file.startswith("http"): # if it is a http stream!
txtline1 = set_characters(get_mpc("mpc -f %name% current"))
txtline2 = set_characters(get_mpc("mpc -f %title% current"))
txtline3 = ""
track = "--/--"
else: # if it is not a stream
lenline1 = -1
lenline2 = -1
lenline3 = -1
subline1 = 0
subline2 = 0
subline3 = 0
linepos = 1
text_album = get_mpc("mpc -f %album% current")
text_title = get_mpc("mpc -f %title% current")
text_artist = get_mpc("mpc -f %artist% current")
# test if ID3-tags are available
if len(text_album) < 2:
# if album is not available, use directory
text_album_tmp = get_mpc("mpc -f %file% current")
text_album_tmp = text_album_tmp.split("/")
text_album = text_album_tmp[0]
if len(text_artist) < 2:
text_artist = ""
if len(text_title) < 2:
# if title is not available, use filename without directory
text_title_tmp = get_mpc("mpc -f %file% current")
text_title_tmp = text_title_tmp.split("/")
text_title = text_title_tmp[len(text_title_tmp)-1]
txtline1 = set_characters(text_album)
txtline2 = set_characters(text_artist)
txtline3 = set_characters(text_title)
if initvars['GENERAL']['mode'] == "lite" :
if playing != "[paused]":
timeline = elapsed.split("/")
if not file.startswith("http"):
if timeline[1] != "0:00":
elapsed = timeline[1]
if not file.startswith("http"):
timelinep = int(mpcstatus.split("\n")[1].replace(" "," ").replace(" "," ").split(" ")[3].replace("(","").replace("%)",""))
timelinep = device.width * timelinep / 100
else:
timelinep = device.width
track = "X"
xpos_w = device.width/2-38
track = track.split("/")[0]
if len(track) == 1:
xpos = device.width/2-15
if len(track) == 2:
xpos = device.width/2-30
if len(track) == 3:
xpos = device.width/2-45
if len(track) == 4:
xpos = device.width/2-60
with canvas(device) as draw:
if not file.startswith("http"):
draw.text((xpos, 4),track,font=FONT_HIGHTOWER, fill="white")
else:
draw.text((xpos_w, 4),track,font=FONT_WIFI, fill="white")
draw.rectangle((0,0,timelinep,1), outline="white", fill="white")
draw.rectangle((109, line4+8,111,line4+10), outline=wificonn[0], fill=wificonn[0])
draw.rectangle((114, line4+6,116,line4+10), outline=wificonn[1], fill=wificonn[1])
draw.rectangle((119, line4+4,121,line4+10), outline=wificonn[2], fill=wificonn[2])
draw.rectangle((124, line4+2,126,line4+10), outline=wificonn[3], fill=wificonn[3])
if initvars['GENERAL']['mode'] == "mix" :
if playing != "[paused]":
timeline = elapsed.split("/")
elapsed = timeline[0]
if len(elapsed) == 4:
elapsed = " "+elapsed
if len(elapsed) == 5:
elapsed = " "+elapsed
else:
elapsed = "PAUSE"
if not file.startswith("http"):
timelinep = int(mpcstatus.split("\n")[1].replace(" "," ").replace(" "," ").split(" ")[3].replace("(","").replace("%)",""))
timelinep = device.width * timelinep / 100
else:
timelinep = device.width
xpos_w = device.width/2-28
track = "X"
tracki = track.replace("\n","")
if len(tracki) == 1:
tracki = " "+tracki
if len(tracki) == 2:
tracki = " "+tracki
if len(tracki) == 3:
tracki = " "+tracki
if len(tracki) == 4:
tracki = " "+tracki
if len(tracki) == 5:
tracki = tracki
track = track.split("/")[0]
if len(track) == 1:
xpos = device.width/2-13
if len(track) == 2:
xpos = device.width/2-26
if len(track) == 3:
xpos = device.width/2-39
if len(track) == 4:
xpos = device.width/2-52
with canvas(device) as draw:
if not file.startswith("http"):
draw.line((39, line4-2, 39, device.height), fill="white")
draw.text((0, line4),elapsed,font=FONT_SMALL, fill="white")
draw.text((42, line4),tracki,font=FONT_SMALL, fill="white")
draw.line((0, line4-2, device.width, line4-2), fill="white")
draw.text((xpos, 4),track,font=FONT_MIDTOWER, fill="white")
else:
draw.line((75, line4-2, device.width, line4-2), fill="white")
draw.text((xpos_w, 4),track,font=FONT_WIFI_MIX, fill="white")
draw.rectangle((0,0,timelinep,1), outline="white", fill="white")
draw.rectangle((109, line4+8,111,line4+10), outline=wificonn[0], fill=wificonn[0])
draw.rectangle((114, line4+6,116,line4+10), outline=wificonn[1], fill=wificonn[1])
draw.rectangle((119, line4+4,121,line4+10), outline=wificonn[2], fill=wificonn[2])
draw.rectangle((124, line4+2,126,line4+10), outline=wificonn[3], fill=wificonn[3])
draw.line((75, line4-2, 75, device.height), fill="white")
draw.line((105, line4-2, 105, device.height), fill="white")
draw.text((78, line4),vol,font=FONT_SMALL, fill="white")
draw.text((108, line4),"---",font=FONT_SMALL, fill=wificonn[4])
#draw.line((device.width/2, 0, device.width/2, device.height), fill="white")
if initvars['GENERAL']['mode'] == "full" :
if lenline1 == -1:
lenline1 = (len(txtline1)*widthletter)-device.width
if lenline1 > 0 and lenline1 < spacejump:
lenline1 = spacejump
if lenline1 < 1:
lenline1 = 0
lenline2 = (len(txtline2)*widthletter)-device.width
if lenline2 > 0 and lenline2 < spacejump:
lenline2 = spacejump
if lenline2 < 1:
lenline2 = 0
lenline3 = (len(txtline3)*widthletter)-device.width
if lenline3 > 0 and lenline3 < spacejump:
lenline3 = spacejump
if lenline3 < 1:
lenline3 = 0
cnt = 0
if linepos == 1:
if (cnt <= lenline1+spacejump*3) and (lenline1 != 0):
subline1 = cnt
subline2 = 0
subline3 = 0
else:
linepos = 2
cnt = 0-spacejump
elif linepos == 2:
if (cnt <= lenline2+spacejump*3) and (lenline2 != 0):
subline1 = 0
subline2 = cnt
subline3 = 0
else:
linepos = 3
cnt = 0-spacejump
elif linepos == 3:
if (cnt <= lenline3+spacejump*3) and (lenline3 != 0):
subline1 = 0
subline2 = 0
subline3 = cnt
else:
linepos = 1
cnt = 0-spacejump
if initvars['GENERAL']['mode'] == "full" :
if playing != "[paused]":
timeline = elapsed.split("/")
elapsed = timeline[0]
if len(elapsed) == 4:
elapsed = " "+elapsed
if len(elapsed) == 5:
elapsed = " "+elapsed
else:
elapsed = "PAUSE"
if not file.startswith("http"):
timelinep = int(mpcstatus.split("\n")[1].replace(" "," ").replace(" "," ").split(" ")[3].replace("(","").replace("%)",""))
timelinep = device.width * timelinep / 100
else:
timelinep = device.width
track = track.replace("\n","")
if len(track) == 1:
track = " "+track
if len(track) == 2:
track = " "+track
if len(track) == 3:
track = " "+track
if len(track) == 4:
track = " "+track
if len(track) == 5:
track = track
with canvas(device) as draw:
if not file.startswith("http"):
draw.line((39, line4-2, 39, device.height), fill="white")
draw.text((0, line4),elapsed,font=FONT_SMALL, fill="white")
draw.text((42, line4),track,font=FONT_SMALL, fill="white")
draw.line((0, line4-2, device.width, line4-2), fill="white")
else:
draw.line((75, line4-2, device.width, line4-2), fill="white")
draw.rectangle((0,0,timelinep,1), outline="white", fill="white")
draw.rectangle((109, line4+8,111,line4+10), outline=wificonn[0], fill=wificonn[0])
draw.rectangle((114, line4+6,116,line4+10), outline=wificonn[1], fill=wificonn[1])
draw.rectangle((119, line4+4,121,line4+10), outline=wificonn[2], fill=wificonn[2])
draw.rectangle((124, line4+2,126,line4+10), outline=wificonn[3], fill=wificonn[3])
draw.line((75, line4-2, 75, device.height), fill="white")
draw.line((105, line4-2, 105, device.height), fill="white")
draw.text((0-subline1, line1),txtline1,font=FONT_STANDARD, fill="white")
draw.text((0-subline2, line2),txtline2,font=FONT_STANDARD, fill="white")
draw.text((0-subline3, line3),txtline3,font=FONT_STANDARD, fill="white")
draw.text((78, line4),vol,font=FONT_SMALL, fill="white")
draw.text((108, line4),"---",font=FONT_SMALL, fill=wificonn[4])
oldmpc = currmpc
cnt = cnt + spacejump
else:
oldmpc = currmpc
if tmpcard < 3:
sleep(0.5)
tmpcard = tmpcard + 1
else:
showimage("cardhand")
tmpcard = 0
except:
sleep(0.5)
showimage("music")
if __name__ == "__main__":
signal.signal(signal.SIGTERM, sigterm_handler)
initvars = init_config(CONFFILE)
device = get_device(initvars['GENERAL']['controller'])
try:
main()
except KeyboardInterrupt:
pass