Commit bc2c5146 authored by zing's avatar zing

Implement lcdd.py (status daemon)

parent bbbff565
......@@ -21,3 +21,19 @@ lcd.py
Commandline interface for the LCD plate.
lcdd.py
-------
Status display "daemon" program.
1) Start a server:
./lcdd.py --socket comm.sock --server --title "First line"
2) Talk to it using the client:
./lcdd.py --socket comm.sock --client --message "p10"
./lcdd.py --socket comm.sock --client --message "quit"
Available messages:
* `quit`: shut down server
* `pXXX`: display a percentage bar between 0% and 100%
#!/usr/bin/python
import argparse
import sys
import os
import atexit
import socket
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
# character that resembles a block on the display:
BLOCKCHAR='\xFF'
def info(msg):
'''Print a message string to stderr.'''
print >> sys.stderr, msg
def opensock(sockaddress, verbose):
'''Open a unix domain socket and return its handle.
Also registers a cleanup callback that ensures that the socket will be removed at exit.
'''
def removesock(sockaddress, verbose):
'''Remove the socket file'''
if verbose >= 2:
info("Removing socket file: "+sockaddress)
os.unlink(sockaddress)
if verbose >= 2:
info("Using socket: "+sockaddress)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(sockaddress)
# make sure we delete the socket file:
atexit.register(removesock,sockaddress,verbose)
return sock
def connectsock(sockaddress, verbose):
'''Connect to an existing unix domain socket and return its handle.'''
def closesock(sock, verbose):
if verbose >= 2:
info("Closing socket...")
sock.close
if verbose >= 2:
info("Using socket: "+sockaddress)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(sockaddress)
atexit.register(closesock,sock,verbose)
return sock
def setup_display(title,verbose):
'''Set up the initial display parameters and text.
Also registers a cleanup callback that clears the display and turns off the backlight at exit.'''
def cleanup_display(verbose):
if verbose >= 1:
info("Closing display...")
lcd.clear()
lcd.backlight(lcd.OFF)
if verbose >= 1:
info("Setting up display...")
# Initialize the LCD plate.
lcd = Adafruit_CharLCDPlate()
atexit.register(cleanup_display,verbose)
lcd.message(title)
return lcd
def display(title, percentage, verbose):
'''Set the first line to title, and print a bar graph in the second line.'''
if percentage < 0:
percentage = 0
elif percentage > 100:
percentage = 100
if verbose >= 2:
info("Display: {0}\\n{1}%".format(title, percentage))
nfill = percentage//10
# message:
# |title |
# |XXXXXXXXXX 100%| (X's are left aligned, percentage right aligned)
lcd.message(title + '\n{0:<10} {1:>3}%'.format((BLOCKCHAR*nfill), percentage))
### assemble command line syntax:
cmdparser = argparse.ArgumentParser(description='Commandline interface to the LCD plate.')
cmdparser.add_argument('--verbose','-v',action='count')
cmdparser.add_argument('--backlight','-b'
,help='Set backlight to color.\nPossible colors: red, yellow, green, teal, blue, violet, white, on, off'
,nargs='?'
,const='on'
)
cmdparser.add_argument('--socket','-S'
,help='Use socket for communication. Default: %(default)s'
,default='/var/run/lcdd.sock'
,nargs='?'
)
cmdparser.add_argument('--message'
,help='Set the message to send to the server. Default: %(default)s (ignored in server mode)'
,nargs='?'
,default='quit'
)
cmdparser.add_argument('--title'
,help='Set the title displayed in the first line (only used in server mode).'
,nargs='?'
,default='+x+x+x+x+x+x+x+x'
)
group = cmdparser.add_mutually_exclusive_group(required=True)
group.add_argument('--server','-s'
,help='Start up in server mode.'
,action='store_true'
)
group.add_argument('--client','-c'
,help='Connect to server as client.'
,action='store_true'
)
args = cmdparser.parse_args()
if args.server:
if args.verbose >= 1:
info("Server mode")
lcd = setup_display(args.title,verbose=args.verbose)
# bind to server socket:
sock = opensock( args.socket, verbose=args.verbose)
quit = False
sock.listen(1)
while not quit:
connection, addr = sock.accept()
data = connection.recv(4)
if args.verbose >= 2:
info("Message received: "+data)
if data == 'quit':
if args.verbose >= 1:
info("Quit requested.")
quit = True
elif data[0:1] == 'p':
value = int(data[1:])
if 0 <= value <= 100:
display(args.title,value,verbose=args.verbose)
else:
if args.verbose >= 1:
info("Client mode")
try:
sock = connectsock( args.socket, verbose=args.verbose)
except socket.error, msg:
info("Could not connect to socket: " + str(msg))
sys.exit(1)
try:
if args.verbose >= 1:
info("Sending message: "+args.message)
sock.sendall(args.message)
except socket.error, msg:
info("Could not send message: " + str(msg))
sys.exit(1)
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