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