Commit 73302578 authored by zing's avatar zing

Initial commit.

parents
LCDTools for Raspberry Pi
=========================
Random stuff for working with the Adafruit i2x 16x2 RGB LCD Pi Plate.
Prerequisites
-------------
Get the python package for interfacing to the LCD plate from the Adafruit github
repository:
* https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
Don't forget to add the '''Adafruit_CharLCDPlate'''' directory to your '''PYTHONPATH'''.
lcd.py
------
Commandline interface for the LCD plate.
#!/bin/sh
echo "Checking required Adafruit packages:"
echo -n "Adafruit_CharLCDPlate "
if python -c 'import Adafruit_CharLCDPlate' 2>/dev/null
then
echo "found."
else
echo "not found!"
echo " -> If you have already installed it, set the PYTHONPATH environment variable accordingly!"
echo " -> You may want to run the following commands:"
echo " git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git"
echo " export PYTHONPATH=\"\$PYTHONPATH:`pwd`/Adafruit-Raspberry-Pi-Python-Code/Adafruit_CharLCDPlate\""
fi
#!/usr/bin/python
import argparse
import sys
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
def info(msg):
'''Print a message string to stderr.'''
sys.stdout.write(msg+'\n')
def backlight_off(args):
if args.clear:
clear_lcd(args)
if args.verbose >= 1:
info("Turning off the backlight...")
lcd.backlight(lcd.OFF)
def clear_lcd(args):
if args.verbose >= 1:
info("Clearing contents...")
lcd.clear()
def set_backlight(args):
backlight = args.backlight.lower()
if backlight == 'red':
color = lcd.RED
elif backlight == 'yellow':
color = lcd.YELLOW
elif backlight == 'green':
color = lcd.GREEN
elif backlight == 'teal':
color = lcd.TEAL
elif backlight == 'blue':
color = lcd.BLUE
elif backlight == 'violet':
color = lcd.VIOLET
elif backlight in {'white','on'}:
color = lcd.ON
elif backlight == 'off':
color = lcd.OFF
else:
info('Invalid color: '+backlight)
sys.exit(1)
if args.verbose >= 1:
info("Setting backlight to "+backlight+"...")
lcd.backlight(color)
def set_lcd(args):
if args.backlight:
set_backlight(args)
if args.verbose >= 1:
info("Setting text...")
args.text = args.text.replace('\\n','\n')
do_autowrap = len(args.text) > 16 and not '\n' in args.text
if do_autowrap:
args.text = args.text[0:16]+'\n'+args.text[16:32]
lcd.message(args.text)
# Initialize the LCD plate. Should auto-detect correct I2C bus. If not,
# pass '0' for early 256 MB Model B boards or '1' for all later versions
lcd = Adafruit_CharLCDPlate()
### assemble command line syntax:
cmdparser = argparse.ArgumentParser(description='Commandline interface to the LCD plate.')
cmdparser.add_argument('--verbose','-v',action='count')
subparsers = cmdparser.add_subparsers()
parser_clear = subparsers.add_parser('clear',help='Clear LCD contents')
parser_clear.set_defaults(func=clear_lcd)
parser_set = subparsers.add_parser('set',help='Set LCD contents')
parser_set.add_argument('--backlight','-b'
,help='Set backlight to color.\nPossible colors: red, yellow, green, teal, blue, violet, white, on, off'
,nargs='?'
,const='on'
)
parser_set.add_argument('text',help='Send string to LCD. Newline wraps to second line')
parser_set.set_defaults(func=set_lcd)
parser_bl = subparsers.add_parser('off',help='Turn off backlight')
parser_bl.add_argument('--clear','-c',help='Also clear contents',action='store_true')
parser_bl.set_defaults(func=backlight_off)
args = cmdparser.parse_args()
args.func(args)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment