-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathada-buttons.py
executable file
·78 lines (63 loc) · 1.91 KB
/
ada-buttons.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
#!/usr/bin/python2 -u
# -*- coding: utf-8 -*-
# A small MPD client to watch button events on AdaFruit CharLCD display
# for raspberry pi
import mpd,time,sys,os
sys.path.append(os.path.dirname(__file__)+'/Adafruit-Raspberry-Pi-Python-Code/Adafruit_CharLCDPlate')
import Adafruit_CharLCDPlate
lcd = Adafruit_CharLCDPlate.Adafruit_CharLCDPlate()
client = mpd.MPDClient()
client.connect("localhost", 6600)
# adafruit lib is not interrupt-driven so we have to set a loop
# https://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi/usage#using-the-library-code
# drawback is that sleeping .2s consumes cpu
# so we place a idletime counter. if idle (no button pressed) for more than 60s :
# if playing, keep .2s
# if not playing anymore, set nap to 5s
# drawbacks: - we may wait a minute before the buttons to be active if set playing by another client
# - you have to keep a button pressed some seconds to "wake up" the script
idletime = 3600
while True:
press = False
#print(repr(client.status()))
if lcd.buttonPressed(lcd.SELECT):
press = True
print("select")
if client.status()["state"] == "play":
client.pause(1)
else:
client.pause(0)
elif lcd.buttonPressed(lcd.LEFT):
press = True
print("left")
client.previous()
elif lcd.buttonPressed(lcd.RIGHT):
press = True
print("right")
client.next()
elif lcd.buttonPressed(lcd.UP):
press = True
print("up")
newvol = int(client.status()["volume"]) +10
if newvol > 100: newvol = 100
client.setvol(newvol)
elif lcd.buttonPressed(lcd.DOWN):
press = True
print("down")
newvol = int(client.status()["volume"]) -10
if newvol < 0: newvol = 0
client.setvol(newvol)
if press:
nap = .2
time.sleep(1)
idletime = 0
if idletime > 60:
idletime = 0
if client.status()["state"] == "play":
print("naptime to .2s...")
nap = .2
else:
print("naptime to 5s...")
nap = 5
idletime+= nap
time.sleep(nap)