fix play state loop

This commit is contained in:
ABelliqueux 2024-03-16 19:21:02 +01:00
parent af1a0d6a5f
commit c0c0d4053a

View File

@ -4,6 +4,7 @@
#
# MPD client
import musicpd
from math import floor
from os import environ
import sys
from time import sleep
@ -15,7 +16,7 @@ from luma.core.render import canvas
from luma.oled.device import ssd1306
# MPD config
off_delay = 10
off_delay = 3
mpd_host='localhost'
mpd_port=6600
mpd_passwd = None
@ -45,6 +46,11 @@ mpd_client_status = {'volume': 'N/A',
'state': 'N/A',
'song': 'N/A',
'songid': 'N/A',
'time': '0',
'elapsed': '0',
'bitrate': 'N/A',
'duration': '0',
'audio': 'N/A',
'nextsong': 'N/A',
'nextsongid': 'N/A'
}
@ -74,16 +80,24 @@ if 'MPD_HOST' in environ:
if 'MPD_PORT' in environ:
mpd_port = environ['MPD_PORT']
# ~ client.status() # duration, elapsed, volume, repeat, random, single
# ~ client.currentsong() # artist, title, album
def sectomin(sec:str):
minute = 0
minute = floor(float(sec)/60)
second = round(float(sec)-minute*60)
return "{}:{}".format(str(minute), str(second))
def update_display(device):
def update_display(device, currentsong, status):
# ~ print(currentsong['artist'])
# ~ print(currentsong['title'])
# ~ print(status['time'])
# ~ print("{}/{}".format(status['time'], sectomin(status['duration'])))
with canvas(device) as draw:
draw.rectangle(device.bounding_box, outline="white", fill="black")
draw.text((10, 5), mpd_client_currentsong['artist'], fill="white")
draw.text((10, 25), mpd_client_currentsong['title'], fill="white")
draw.text((10, 40), "{}/{}".format(mpd_client_status['elapsed'], mpd_client_status['duration']), fill="white")
draw.text((10, 5), currentsong['artist'], fill="white")
draw.text((10, 18), currentsong['title'], fill="white")
draw.text((10, 31), currentsong['album'], fill="white")
draw.text((10, 44), "{}/{}".format(status['time'], sectomin(status['duration'])), fill="white")
draw.regular_polygon(bounding_circle=(14, 55, 8), n_sides=3, rotate=90, fill="white")
def main(args):
previous_sond_id = None
@ -101,29 +115,40 @@ def main(args):
except musicpd.ConnectionError as errorMessage:
print(repr(errorMessage))
print("Check host and port are correct.")
# ~ print(client.status()) # duration, elapsed, volume, repeat, random, single
# ~ print(client.currentsong()) # artist, title, album
while ctrlc_pressed is False:
mpd_status = client.status()
if len(mpd_status):
mpd_status = client.status()
if len(mpd_status):
mpd_client_status = mpd_status
mpd_client_currentsong = client.currentsong()
play_state = client.status()['state']
current_song_id = client.status()['songid']
if play_state in mpd_states:
if play_state == 'play':
paused_since_seconds = 0
if (current_song_id != previous_sond_id) and (previous_state != play_state):
print("Play")
# Relay on
GPIO.output(RELAIS_1_GPIO, GPIO.HIGH)
if play_state == 'pause':
paused_since_seconds += 1
if paused_since_seconds > off_delay :
if paused_since_seconds < off_delay:
paused_since_seconds += 1
print("Paused for {}".format(str(paused_since_seconds)))
if (paused_since_seconds >= off_delay) and GPIO.input(RELAIS_1_GPIO):
print("Off")
# Relay off
GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
if play_state == 'stop' or play_state == 'unknown':
previous_sond_id = None
# Relay off
GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
paused_since_seconds = 0
if previous_state != play_state:
print("Stopped")
# Relay off
GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
previous_state = play_state
sleep(2)
sleep(1)
update_display(device, mpd_client_currentsong, mpd_client_status)
client.disconnect()
return 0