#!/usr/bin/env python import time import os import thread, itertools from nanpy import Arduino from nanpy import (OneWire, Lcd) from subprocess import * # configure pins for Hobbytronics LCD shield Arduino.pinMode(0, input) lcd = Lcd([8, 9, 4, 5, 6, 7], [16, 2]) # threaded function to show time twice a minute def showTime(): while True: if pause == 0: now = time.localtime(time.time()) lcd.printString(time.strftime("%H:%M %d/%m/%y", now), 0, 1) time.sleep(30) def run_cmd(cmd): p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT) output = p.communicate()[0] return output lcd.printString("MyLittleRadio1.4", 0, 0) lcd.printString("by @blogmywiki ", 0, 1) time.sleep(5) 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) lcd.printString('IP Address: ',0,0) lcd.printString(ipaddr,0,1) time.sleep(2) def getKey(): val = Arduino.analogRead(0) if val == 1023: return "NONE" elif val < 100: return "RIGHT" elif val < 200: return "UP" elif val < 400: return "DOWN" elif val < 600: return "LEFT" elif val < 800: return "SEL" else: return "KBD_FAULT" def getTrack(): L= [S.strip('\n') for S in os.popen('mpc').readlines()] # Get the Track info from the stdout of the mpc command sttn = L[0][0:16] # Pick out the Station and Track info lcd.printString(16*" ", 0, 0) # Send it out to the LCD Display lcd.printString(sttn, 0, 0) print L print sttn station = 10 # play station 10 fip by default os.system("mpc play " + str(station)) # trying a different way to display initial station L = [S.strip('\n') for S in os.popen('mpc current').readlines()] if L == "": L = "audio not found" lcd.printString(" "*16, 0, 0) lcd.printString(L, 0, 0) print L # getTrack() global pause pause = 0 # start a new thread for displaying time thread.start_new_thread(showTime, ()) while True: #take a reading key=getKey() if key == "UP": if pause == 0: station += 1 if station > 11: station = 1 print(str(station)) os.system("mpc play " + str(station)) getTrack() elif key == "DOWN": if pause == 0: station -=1 if station < 1: station = 11 print(str(station)) os.system("mpc play " + str(station)) getTrack() elif key =="LEFT": if pause == 1: display_ipaddr() elif pause == 0: os.system("mpc volume -3") elif key =="RIGHT": os.system("mpc volume +3") elif key == "SEL": if pause == 0: pause = 1 elif pause == 1: pause = 0 os.system("mpc toggle") if pause == 1: lcd.printString(" PAUSE ", 0, 1) elif pause == 0: noo = time.localtime(time.time()) lcd.printString(time.strftime("%H:%M %d/%m/%y", noo), 0, 1) getTrack() # added for key press debounce time.sleep(0.1)