console: Add support for connecting via CAN bus

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2021-02-07 17:13:15 -05:00
parent a20f4a8759
commit babde17c4c
1 changed files with 13 additions and 3 deletions

View File

@ -30,9 +30,11 @@ help_txt = """
re_eval = re.compile(r'\{(?P<eval>[^}]*)\}') re_eval = re.compile(r'\{(?P<eval>[^}]*)\}')
class KeyboardReader: class KeyboardReader:
def __init__(self, reactor, serialport, baud): def __init__(self, reactor, serialport, baud, canbus_iface, canbus_nodeid):
self.serialport = serialport self.serialport = serialport
self.baud = baud self.baud = baud
self.canbus_iface = canbus_iface
self.canbus_nodeid = canbus_nodeid
self.ser = serialhdl.SerialReader(reactor) self.ser = serialhdl.SerialReader(reactor)
self.reactor = reactor self.reactor = reactor
self.start_time = reactor.monotonic() self.start_time = reactor.monotonic()
@ -54,7 +56,10 @@ class KeyboardReader:
def connect(self, eventtime): def connect(self, eventtime):
self.output(help_txt) self.output(help_txt)
self.output("="*20 + " attempting to connect " + "="*20) self.output("="*20 + " attempting to connect " + "="*20)
if self.baud: if self.canbus_iface is not None:
self.ser.connect_canbus(self.serialport, self.canbus_nodeid,
self.canbus_iface)
elif self.baud:
self.ser.connect_uart(self.serialport, self.baud) self.ser.connect_uart(self.serialport, self.baud)
else: else:
self.ser.connect_pipe(self.serialport) self.ser.connect_pipe(self.serialport)
@ -207,6 +212,10 @@ def main():
opts.add_option("-v", action="store_true", dest="verbose", opts.add_option("-v", action="store_true", dest="verbose",
help="enable debug messages") help="enable debug messages")
opts.add_option("-b", "--baud", type="int", dest="baud", help="baud rate") opts.add_option("-b", "--baud", type="int", dest="baud", help="baud rate")
opts.add_option("-c", "--canbus_iface", dest="canbus_iface",
help="Use CAN bus interface; serialdevice is the chip UUID")
opts.add_option("-i", "--canbus_nodeid", type="int", dest="canbus_nodeid",
default=64, help="The CAN nodeid to use (default 64)")
options, args = opts.parse_args() options, args = opts.parse_args()
if len(args) != 1: if len(args) != 1:
opts.error("Incorrect number of arguments") opts.error("Incorrect number of arguments")
@ -223,7 +232,8 @@ def main():
logging.basicConfig(level=debuglevel) logging.basicConfig(level=debuglevel)
r = reactor.Reactor() r = reactor.Reactor()
kbd = KeyboardReader(r, serialport, baud) kbd = KeyboardReader(r, serialport, baud, options.canbus_iface,
options.canbus_nodeid)
try: try:
r.run() r.run()
except KeyboardInterrupt: except KeyboardInterrupt: