|
Thank you nopnop2002 - it works
Belllow some code from me. This is verry simle internet radio control - mpd+mpc
I'm planing to do some menu sterred by three buttons to control not only radio but system.
I'm not programist and don't know pytton verry much so this code isn't so clear ;)
- #!/usr/bin/env python
- import os
- import sys
- import subprocess
- import time
- from time import sleep
- 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')
- button_pause = connector.gpio1p36 #PIN 36
- button_prev = connector.gpio1p38 #PIN 38
- button_next = connector.gpio1p40 #PIN 40
- # Define GPIO to LCD mapping
- LCD_RS = port.PA21 #PIN 26
- LCD_E = port.PC3 #PIN 24
- LCD_D4 = port.PA2 #PIN 22
- LCD_D5 = port.PC7 #PIN 18
- LCD_D6 = port.PC4 #PIN 16
- LCD_D7 = port.PD14 #PIN 12
- # 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_pause, gpio.INPUT)
- gpio.setcfg(button_prev, gpio.INPUT)
- gpio.setcfg(button_next, gpio.INPUT)
- #Enable pullup resistor
- gpio.pullup(button_pause, gpio.PULLUP)
- gpio.pullup(button_prev, gpio.PULLUP)
- gpio.pullup(button_next, gpio.PULLUP)
- def main():
- reset_pressed = 1
- prev_pressed = 1
- next_pressed = 1
- # Initialise display
- lcd_init()
- lcd_string("[ INTERNET ]",LCD_LINE_1)
- lcd_string("[ RADIO ]",LCD_LINE_2)
- while True:
- value_out = 0
- pause_pressed = gpio.input(button_pause)
- prev_pressed = gpio.input(button_prev)
- next_pressed = gpio.input(button_next)
- if (pause_pressed == 0):
- value_out = 1
- lcd_string("[ PRESSED ]",LCD_LINE_1)
- lcd_string("[ PAUSE ]",LCD_LINE_2)
- run_cmd("mpc pause")
- if (prev_pressed == 0):
- value_out = 1
- lcd_string("[ PRESSED ]",LCD_LINE_1)
- lcd_string("[ PREVIOUS]",LCD_LINE_2)
- run_cmd("mpc prev")
- if (next_pressed == 0):
- value_out = 1
- lcd_string("[ PRESSED ]",LCD_LINE_1)
- lcd_string("[ NEXT]",LCD_LINE_2)
- run_cmd("mpc next")
- 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)
- gpio.cleanup()
Copy code |
|