Skip to content

Instantly share code, notes, and snippets.

@manderso7
Created August 19, 2020 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manderso7/5dfce90b0ad47fb82643cb3c898e191d to your computer and use it in GitHub Desktop.
Save manderso7/5dfce90b0ad47fb82643cb3c898e191d to your computer and use it in GitHub Desktop.
Used to display currently playing audio on RGB LCD from moode audio
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import time
import sys
import board
import busio
import adafruit_character_lcd.character_lcd_rgb_i2c as character_lcd
lcd_columns = 16
lcd_rows = 2
# init I2C bus
i2c = busio.I2C(board.SCL, board.SDA)
# init the LCD class
lcd = character_lcd.Character_LCD_RGB_I2C(i2c, lcd_columns, lcd_rows)
lcd.color = [60, 0, 0]
PLAYING_STATE = 'play'
STOPPED_STATE = 'stop'
MOODE_FILE = '/var/local/www/currentsong.txt'
# Delay this long to allow file to populate
READ_DELAY = 2
while True:
try:
with open(MOODE_FILE) as f:
file = f.read()
# print(file)
artist = re.search('(?<=artist=).*\S', file)
song = re.search('(?<=title=).*\S', file)
state = re.search('(?<=state=).*\S', file)
# Check if state exists and is play
if state and state.group() == PLAYING_STATE:
# Check if artist and song exists and write to lcd.
if artist and song:
display_txt = artist.group() + '\n' + song.group()
#print(display_txt)
if len(display_txt)>32:
lcd.message = display_txt
for i in range(len(lcd.message)):
time.sleep(0.5)
lcd.move_left()
else:
lcd.message = display_txt
else:
# lcd.clear()
# Sleep to give time for file to populate. If timeout is reached lcd is cleared and will try again
# next loop pass
time.sleep(READ_DELAY)
if artist and song:
pass
else:
print('artist or song missing')
elif state and state.group() == STOPPED_STATE:
# Check if state exists and if is stop clear lcd.
lcd.clear()
print('clear lcd')
else:
# Sleep to give time for file to populate. If timeout is reached lcd is cleared and will try again
# next loop pass
time.sleep(READ_DELAY)
if state:
pass
else:
print('state missing')
lcd.clear()
except:
# If script errors out write to screen.
lcd.message = 'Error in script!'
print('Error in script!')
time.sleep(.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment