1 #!/usr/bin/env python 2 3 """ 4 A client for updating the display of the ElecFreaks alphanumeric display brick. 5 6 Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT ANY 14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 15 PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License along 18 with this program. If not, see <http://www.gnu.org/licenses/>. 19 """ 20 21 from serial import Serial 22 from time import sleep 23 import sys 24 25 port = "/dev/ttyUSB0" 26 rate = 115200 27 28 def openPort(): 29 global s 30 s = Serial(port, rate) 31 s.setTimeout(5) 32 s.readline() 33 34 def closePort(): 35 global s 36 s.close() 37 s = None 38 39 def writeToPort(data): 40 s.write(data) 41 s.flush() 42 print >>sys.stderr, data.rstrip() 43 44 def readFromPort(): 45 resp = s.readline().rstrip("\r\n") 46 if resp == "?": 47 resp = s.readline().rstrip("\r\n") 48 return resp 49 50 def console(prompt=True): 51 try: 52 while 1: 53 if prompt: 54 cmd = raw_input("> ") 55 if cmd: 56 s.write(cmd + "\n") 57 s.flush() 58 print s.readline().rstrip("\r\n") 59 except EOFError: 60 print "Session closed." 61 62 if __name__ == "__main__": 63 openPort() 64 65 try: 66 console(not "--read-only" in sys.argv) 67 finally: 68 closePort() 69 70 # vim: tabstop=4 expandtab shiftwidth=4