[SOLVED]Garbled Text on LCD only when running Pyton script.
Posted: Tue Jan 13, 2015 4:38 am
Hello, I'm running into a strange issue where when I add an additional function or elseif (elif), upon running the python script, and scrolling through the menu, it garbles the 20X4 lcd (no rgb). However, I have another python script that works perfectly. Scratching my head against this one. I have tried adjusting the epulse and edelay as I read from another post. What I would like it to do is output the artist and title information by running, mpc current | tail -c25 on lcd. Here is the attached python script: Thanks!
Code: Select all
# Define GPIO output pins for Radio Controls
PIN_SW1_PREV = 4
PIN_SW2_NEXT = 18
# The Adafruit_CharLCD class controls the LCD display
# using six GPIO output pins:
# 7 = RS (register select)
# 8 = E (enable/strobe)
# 25 = Data bit 4
# 24 = Data bit 5
# 23 = Data bit 6
# 17 = Data bit 7
#dependancies
from Adafruit_CharLCD import Adafruit_CharLCD
from datetime import datetime
from subprocess import *
from time import sleep, strftime
import RPi.GPIO as GPIO
# globals
LCD = Adafruit_CharLCD()
PLAYLIST_MSG = []
STATION = 1
NUM_STATIONS = 0
E_PULSE = 0.000005
E_DELAY = 0.000005
def main():
global STATION, NUM_STATIONS, PLAYLIST_MSG
# Stop music player
output = run_cmd("mpc stop" )
# Setup GPIO
GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
GPIO.setup(PIN_SW1_PREV, GPIO.IN) # Previous Channel button
GPIO.setup(PIN_SW2_NEXT, GPIO.IN) # Next Channel button
# Setup AdaFruit LCD
LCD.begin(20,4)
LCD.clear()
# ----------------------------
# LOAD PLAYLIST OF STATIONS
# ----------------------------
# Startup banner
LCD.message('Welcome to\nPi Radio!')
# Run shell script to add all stations
# to the MPC/MPD music player playlist
output = run_cmd ("mpc clear")
output = run_cmd("/home/pi/projects/radio/radio_playlist.sh")
# Load PLAYLIST_MSG list
with open ("/home/pi/projects/radio/radio_playlist.sh", "r") as playlist:
# Skip leading hash-bang line
for line in playlist:
if line[0:1] != '#!':
break
# Remaining comment lines are loaded
for line in playlist:
if line[0] == "#" :
PLAYLIST_MSG.append(line.replace(r'\n','\n')[1:-1] + " ")
playlist.close()
sleep(1.5)
NUM_STATIONS = len(PLAYLIST_MSG)
# ----------------------------
# START THE MUSIC!
# ----------------------------
# Start music player
LCD.clear()
LCD.message(PLAYLIST_MSG[STATION - 1] )
mpc_play(STATION)
countdown_to_play = 0
# Main loop
while True:
press = read_switches()
# PREV button pressed
if(press == 1):
STATION -= 1
if(STATION < 1):
STATION = NUM_STATIONS
LCD.setCursor(0,0)
LCD.message(PLAYLIST_MSG[STATION - 1] )
# start play in 300msec unless another key pressed
countdown_to_play = 3
# NEXT button pressed
if(press == 2):
STATION += 1
if(STATION > NUM_STATIONS):
STATION = 1
LCD.setCursor(0,0)
LCD.message(PLAYLIST_MSG[STATION - 1] )
# start play in 300msec unless another key pressed
countdown_to_play = 3
# BOTH buttons pressed together
if(press == 3):
menu_pressed()
# If we haven't had a key press in 300 msec
# go ahead and issue the MPC command
if(countdown_to_play > 0):
countdown_to_play -= 1
if(countdown_to_play == 0):
# Play requested station
mpc_play(STATION)
delay_milliseconds(100)
def read_switches():
# Initialize
got_prev = False
got_next = False
# Read switches
sw1_prev = GPIO.input(PIN_SW1_PREV)
sw2_next = GPIO.input(PIN_SW2_NEXT)
# Debounce switches and look for two-button combo
while(sw1_prev or sw2_next):
if(sw1_prev):
got_prev = True
if(sw2_next):
got_next = True
delay_milliseconds(1)
sw1_prev = GPIO.input(PIN_SW1_PREV)
sw2_next = GPIO.input(PIN_SW2_NEXT)
if(got_prev and got_next):
return 3
if(got_next):
return 2
if(got_prev):
return 1
return 0
def delay_milliseconds(milliseconds):
seconds = milliseconds / float(1000) # divide milliseconds by 1000 for seconds
sleep(seconds)
# ----------------------------
# RADIO SETUP MENU
# ----------------------------
MENU_LIST = [
'1. Display Time \n & IP Address ',
'2. Output Audio \n to HDMI ',
'3. Output Audio \n to Headphones',
'4. Auto Select \n Audio Output ',
'5. Song \n Track ',
'6. Reboot \n Reboot ',
'7. System \n Shutdown! ',
'8. Exit \n ']
def menu_pressed():
global MENU_LIST
item = 0
LCD.clear()
LCD.message(MENU_LIST[item] )
keep_looping = True
while (keep_looping):
# Wait for a key press
press = read_switches()
# PREV button
if(press == 1):
item -= 1
if(item < 0):
item = len(MENU_LIST) - 1
LCD.clear()
LCD.message(MENU_LIST[item] )
# NEXT button
elif(press == 2):
item += 1
if(item >= len(MENU_LIST)):
item = 0
LCD.clear()
LCD.message(MENU_LIST[item] )
# BOTH buttons together, This is "Select"
elif(press == 3):
keep_looping = False
# Take action
if( item == 0):
# display time and IP address
display_ipaddr()
elif(item == 1):
# output to HDMI
output = run_cmd("amixer -q cset numid=3 2")
elif(item == 2):
# output to headphone jack
output = run_cmd("amixer -q cset numid=3 1")
elif(item == 3):
# output auto-select
output = run_cmd("amixer -q cset numid=3 0")
elif(item == 4):
# Check artist/title info
output = run_cmd("mpc current | tail -c25")
elif(item == 5):
#reboot the system
output = run_cmd("mpc clear")
output = run_cmd("sudo reboot")
elif(item == 6):
#shutdown the system
output = run_cmd("mpc clear")
output = run_cmd("sudo halt -p")
else:
delay_milliseconds(100)
# Restore display
LCD.clear()
LCD.message(PLAYLIST_MSG[STATION - 1] )
#def get_track():
# show_track=run_cmd ("mpc current | tail -c25")
# if get_track == "":
# get_track = run_cmd(show_track)
# i = 0
# keep_looping = True
def display_ipaddr():
show_wlan0 = "ip addr show wlan0 | cut -d/ -f1 | awk '/inet/ {printf \"w%15.15s\", $2}'"
show_eth0 = "ip addr show eth0 | cut -d/ -f1 | awk '/inet/ {printf \"e%15.15s\", $2}'"
ipaddr = run_cmd(show_eth0)
if ipaddr == "":
ipaddr = run_cmd(show_wlan0)
i = 0
keep_looping = True
while (keep_looping):
# Every second, update the time display
i += 1
if(i % 10 == 0):
LCD.setCursor(0,0)
LCD.message(datetime.now().strftime('%b %d %H:%M:%S\n'))
LCD.message(ipaddr)
#LCD.message('e%15.15s' % ( ipaddr ) )
# Every 3 seconds, update ethernet or wi-fi IP address
if(i == 60):
ipaddr = run_cmd(show_eth0)
i = 0
elif(i == 30):
ipaddr = run_cmd(show_wlan0)
# Every 100 milliseconds, read the switches
press = read_switches()
if(press == 3):
keep_looping = False
delay_milliseconds(100)
def run_cmd(cmd):
p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)
output = p.communicate()[0]
return output
def run_cmd_nowait(cmd):
pid = Popen(cmd, shell=True, stdout=NONE, stderr=STDOUT).pid
def mpc_play(STATION):
pid = Popen(["/usr/bin/mpc", "play", '%d' % ( STATION )]).pid
if __name__ == '__main__':
main()