|
Edited by mswiniuch at 2016-4-16 14:49
Main code:
/root/radio.py
- #!/usr/bin/env python
- import os
- import sys
- import subprocess
- import time
- import textwrap
- from time import sleep, strftime
- from datetime import datetime
- from subprocess import *
- from pyA20.gpio import gpio
- from pyA20.gpio import connector
- from pyA20.gpio import port
- if not os.getegid() == 0:
- sys.exit('Script must be run as root')
- #Defina GPIO to buttons mapping
- button_menu = port.PA2 #PIN 22
- button_prev = port.PA13 #PIN 8
- button_next = port.PA14 #PIN 10
- # Define GPIO to LCD mapping
- LCD_RS = port.PA21 #PIN 26
- LCD_E = port.PC3 #PIN 24
- LCD_D4 = port.PA0 #PIN 13
- LCD_D5 = port.PA3 #PIN 15
- LCD_D6 = port.PC4 #PIN 16
- LCD_D7 = port.PC7 #PIN 18
- # Define some device constants
- LCD_WIDTH = 16 # Maximum characters per line
- LCD_CHR = True
- LCD_CMD = False
- LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
- LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
- # Timing constants
- E_PULSE = 0.0005
- E_DELAY = 0.0005
- #Init gpio module
- gpio.init()
- gpio.setcfg(LCD_E, gpio.OUTPUT) # E
- gpio.setcfg(LCD_RS, gpio.OUTPUT) # RS
- gpio.setcfg(LCD_D4, gpio.OUTPUT) # DB4
- gpio.setcfg(LCD_D5, gpio.OUTPUT) # DB5
- gpio.setcfg(LCD_D6, gpio.OUTPUT) # DB6
- gpio.setcfg(LCD_D7, gpio.OUTPUT) # DB7
- #Set buttons
- gpio.setcfg(button_menu, gpio.INPUT)
- gpio.setcfg(button_prev, gpio.INPUT)
- gpio.setcfg(button_next, gpio.INPUT)
- #Enable pullup resistor
- gpio.pullup(button_menu, gpio.PULLUP)
- gpio.pullup(button_prev, gpio.PULLUP)
- gpio.pullup(button_next, gpio.PULLUP)
- def main():
- # Initialise display
- lcd_init()
- counter = 0
- while True:
- # Get current status and playtime
- process = subprocess.Popen('mpc', shell=True, stdout=subprocess.PIPE)
- status = process.communicate()[0]
- if status == "":
- radioName = " No radio "
- listN = " or no network "
- songName = " or no network "
- if counter == 10:
- run_cmd("service mpd restart")
- else:
- statusLines = status.split('\n')
- # Extract the songName (first line)
- songN = statusLines[0]
- listN = statusLines[1]
- if ":" in songN:
- radioName = statusLines[0].split(':',1)[0].strip()
- songName = statusLines[0].split(':',1)[1].strip()
- if ":" not in songN:
- radioName = statusLines[0]
- songName = " "
- if radioName == "volume":
- if counter == 10:
- run_cmd("mpc play")
- lcd_string(radioName,LCD_LINE_1)
- if counter < 10:
- lcd_string(listN,LCD_LINE_2)
- else:
- lcd_string(songName,LCD_LINE_2)
- press = read_switches()
- if (press == 3):
- menu()
- counter = 0
- if (press == 2):
- run_cmd("mpc next")
- counter = 0
- if (press == 1):
- run_cmd("mpc prev")
- counter = 0
- sleep(0.1)
- counter = counter + 1
- if counter == 20:
- counter = 0
- def read_switches():
- sw_prev = 1
- sw_next = 1
- sw_menu = 1
- # Initialize
- got_prev = False
- got_next = False
- got_menu = False
- # Read switches
- sw_prev = gpio.input(button_prev)
- sw_next = gpio.input(button_next)
- sw_menu = gpio.input(button_menu)
- if sw_prev == 0:
- got_prev = True
- if sw_next == 0:
- got_next = True
- if sw_menu == 0:
- got_menu = True
- if(got_menu):
- return 3
- if(got_next):
- return 2
- if(got_prev):
- return 1
- return 0
- sleep(0.01)
- MENU_LIST = [
- '0. Time & IP ',
- '1. Play / Pause ',
- '2. Reload Radio ',
- '3. Reboot System',
- '4. Halt System ',
- '5. Connect 3G ',
- '6. Exit Menu ']
- def menu ():
- global MENU_LIST
- item = 0
- lcd_string("[ << MENU >> ]",LCD_LINE_1)
- lcd_string(MENU_LIST[item],LCD_LINE_2)
- counter = 0
- keep_looping = True
- while (keep_looping):
- # Wait for a key press
- press = read_switches()
- # PREV button
- if(press == 1):
- counter = 0
- item -= 1
- if(item < 0):
- item = len(MENU_LIST) - 1
- lcd_string("[ << MENU >> ]",LCD_LINE_1)
- lcd_string(MENU_LIST[item],LCD_LINE_2)
- # NEXT button
- if(press == 2):
- counter = 0
- item += 1
- if(item >= len(MENU_LIST)):
- item = 0
- lcd_string("[ << MENU >> ]",LCD_LINE_1)
- lcd_string(MENU_LIST[item],LCD_LINE_2)
- # MENU button
- if(press == 3):
- # Take action
- if(item == 0):
- # display time and IP address
- display_ipaddr()
- if(item == 1):
- # Play/Pause
- output = run_cmd("mpc toggle")
- if(item == 2):
- # reload radio
- lcd_string(" WAIT ",LCD_LINE_1)
- lcd_string("Restarting radio",LCD_LINE_2)
- output = run_cmd("service mpd restart")
- output = run_cmd("mpc clear")
- output = run_cmd("mpc load radio.pls")
- output = run_cmd("mpc play")
- if(item == 3):
- # output reboot system
- lcd_string(" WAIT ",LCD_LINE_1)
- lcd_string("Rebooting system",LCD_LINE_2)
- output = run_cmd("sudo service mpd stop && sudo reboot")
- if(item == 4):
- #shutdown the system
- lcd_string(" GOODBYE! ",LCD_LINE_1)
- lcd_string(" Halting system ",LCD_LINE_2)
- output = run_cmd("sudo service mpd stop && sudo shutdown now -h")
- if(item == 5):
- #connect 3G mobile internet
- lcd_string(" WAIT ",LCD_LINE_1)
- lcd_string(" connecting 3G ",LCD_LINE_2)
- output = run_cmd("sudo /usr/bin/sakis3g connect --console")
- if(item == 6):
- #exit Menu - return to radio
- keep_looping = False
- keep_looping = False
- counter = counter + 1
- if counter == 40:
- keep_looping = False
- sleep(0.1)
- def display_ipaddr():
- show_eth0 = "/sbin/ifconfig eth0|grep inet|head -1|sed 's/\:/ /'|awk '{print $3}'"
- ipaddr = run_cmd(show_eth0)
- lcd_string(ipaddr,LCD_LINE_2)
- i = 0
- keep_looping_1 = True
- while (keep_looping_1):
- # Every second, update the time display
- i = i + 1
- if(i % 10 == 0):
- show_time = datetime.now().strftime('%b %d %H:%M:%S\n')
- lcd_string(show_time,LCD_LINE_1)
- lcd_string(ipaddr,LCD_LINE_2)
- # Every 100 milliseconds, read the switches
- press = read_switches()
- if(press == 3):
- keep_looping_1 = False
- sleep(0.1)
- def run_cmd(cmd):
- p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)
- output = p.communicate()[0]
- return output
- def lcd_init():
- # Initialise display
- lcd_byte(0x33,LCD_CMD) # 110011 Initialise
- lcd_byte(0x32,LCD_CMD) # 110010 Initialise
- lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
- lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
- lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
- lcd_byte(0x01,LCD_CMD) # 000001 Clear display
- time.sleep(E_DELAY)
- def lcd_byte(bits, mode):
- # Send byte to data pins
- # bits = data
- # mode = True for character
- # False for command
- gpio.output(LCD_RS, mode)
- # High bits
- gpio.output(LCD_D4, False)
- gpio.output(LCD_D5, False)
- gpio.output(LCD_D6, False)
- gpio.output(LCD_D7, False)
- if bits&0x10==0x10:
- gpio.output(LCD_D4, True)
- if bits&0x20==0x20:
- gpio.output(LCD_D5, True)
- if bits&0x40==0x40:
- gpio.output(LCD_D6, True)
- if bits&0x80==0x80:
- gpio.output(LCD_D7, True)
- # Toggle 'Enable' pin
- lcd_toggle_enable()
- # Low bits
- gpio.output(LCD_D4, False)
- gpio.output(LCD_D5, False)
- gpio.output(LCD_D6, False)
- gpio.output(LCD_D7, False)
- if bits&0x01==0x01:
- gpio.output(LCD_D4, True)
- if bits&0x02==0x02:
- gpio.output(LCD_D5, True)
- if bits&0x04==0x04:
- gpio.output(LCD_D6, True)
- if bits&0x08==0x08:
- gpio.output(LCD_D7, True)
- # Toggle 'Enable' pin
- lcd_toggle_enable()
- def lcd_toggle_enable():
- # Toggle enable
- time.sleep(E_DELAY)
- gpio.output(LCD_E, True)
- time.sleep(E_PULSE)
- gpio.output(LCD_E, False)
- time.sleep(E_DELAY)
- def lcd_string(message,line):
- # Send string to display
- message = message.ljust(LCD_WIDTH," ")
- lcd_byte(line, LCD_CMD)
- for i in range(LCD_WIDTH):
- lcd_byte(ord(message[i]),LCD_CHR)
- if __name__ == '__main__':
- try:
- main()
- except KeyboardInterrupt:
- pass
- finally:
- lcd_byte(0x01, LCD_CMD)
- lcd_string(" Goodbye! ",LCD_LINE_1)
- lcd_string(" ",LCD_LINE_2)
Copy code
If you want to have access to playlist and main code by Samba, add this to the end of samba config fie /etc/samba/smb.conf
- [MPD-Music]
- guest account = root
- writeable = yes
- delete readonly = yes
- path = /var/lib/mpd/music
- force group = root
- force user = root
- create mode = 777
- public = yes
- directory mode = 777
- [root]
- guest account = root
- writeable = yes
- delete readonly = yes
- path = /root
- force group = root
- force user = root
- create mode = 777
- public = yes
- directory mode = 777
Copy code |
|