mcu: Move SerialCommand from serialhdl.py to mcu.py
Move the serial command wrapper class from serialhdl.py to mcu.py. This will allow that class to better support higher level functionality. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
cc3666aa78
commit
e202a8802b
|
@ -424,6 +424,25 @@ class MCU_adc:
|
||||||
if self._callback is not None:
|
if self._callback is not None:
|
||||||
self._callback(last_read_time, last_value)
|
self._callback(last_read_time, last_value)
|
||||||
|
|
||||||
|
# Wrapper around command sending
|
||||||
|
class CommandWrapper:
|
||||||
|
def __init__(self, mcu, serial, cmd, cmd_queue):
|
||||||
|
self._mcu = mcu
|
||||||
|
self._serial = serial
|
||||||
|
self._cmd = cmd
|
||||||
|
self._cmd_queue = cmd_queue
|
||||||
|
def send(self, data=(), minclock=0, reqclock=0):
|
||||||
|
cmd = self._cmd.encode(data)
|
||||||
|
self._serial.raw_send(cmd, minclock, reqclock, self._cmd_queue)
|
||||||
|
def send_with_response(self, data=(), response=None, response_oid=None):
|
||||||
|
cmd = self._cmd.encode(data)
|
||||||
|
try:
|
||||||
|
src = serialhdl.SerialRetryCommand(self._serial, cmd,
|
||||||
|
response, response_oid)
|
||||||
|
return src.get_response()
|
||||||
|
except serialhdl.error as e:
|
||||||
|
raise error(str(e))
|
||||||
|
|
||||||
class MCU:
|
class MCU:
|
||||||
error = error
|
error = error
|
||||||
def __init__(self, config, clocksync):
|
def __init__(self, config, clocksync):
|
||||||
|
@ -688,7 +707,10 @@ class MCU:
|
||||||
def alloc_command_queue(self):
|
def alloc_command_queue(self):
|
||||||
return self._serial.alloc_command_queue()
|
return self._serial.alloc_command_queue()
|
||||||
def lookup_command(self, msgformat, cq=None):
|
def lookup_command(self, msgformat, cq=None):
|
||||||
return self._serial.lookup_command(msgformat, cq)
|
if cq is None:
|
||||||
|
cq = self._serial.get_default_command_queue()
|
||||||
|
cmd = self._serial.get_msgparser().lookup_command(msgformat)
|
||||||
|
return CommandWrapper(self, self._serial, cmd, cq)
|
||||||
def try_lookup_command(self, msgformat):
|
def try_lookup_command(self, msgformat):
|
||||||
try:
|
try:
|
||||||
return self.lookup_command(msgformat)
|
return self.lookup_command(msgformat)
|
||||||
|
|
|
@ -134,6 +134,8 @@ class SerialReader:
|
||||||
return self.ffi_main.string(self.stats_buf)
|
return self.ffi_main.string(self.stats_buf)
|
||||||
def get_msgparser(self):
|
def get_msgparser(self):
|
||||||
return self.msgparser
|
return self.msgparser
|
||||||
|
def get_default_command_queue(self):
|
||||||
|
return self.default_cmd_queue
|
||||||
# Serial response callbacks
|
# Serial response callbacks
|
||||||
def register_response(self, callback, name, oid=None):
|
def register_response(self, callback, name, oid=None):
|
||||||
with self.lock:
|
with self.lock:
|
||||||
|
@ -152,11 +154,6 @@ class SerialReader:
|
||||||
cmd = self.msgparser.create_command(msg)
|
cmd = self.msgparser.create_command(msg)
|
||||||
src = SerialRetryCommand(self, cmd, response)
|
src = SerialRetryCommand(self, cmd, response)
|
||||||
return src.get_response()
|
return src.get_response()
|
||||||
def lookup_command(self, msgformat, cq=None):
|
|
||||||
if cq is None:
|
|
||||||
cq = self.default_cmd_queue
|
|
||||||
cmd = self.msgparser.lookup_command(msgformat)
|
|
||||||
return SerialCommand(self, cq, cmd)
|
|
||||||
def alloc_command_queue(self):
|
def alloc_command_queue(self):
|
||||||
return self.ffi_main.gc(self.ffi_lib.serialqueue_alloc_commandqueue(),
|
return self.ffi_main.gc(self.ffi_lib.serialqueue_alloc_commandqueue(),
|
||||||
self.ffi_lib.serialqueue_free_commandqueue)
|
self.ffi_lib.serialqueue_free_commandqueue)
|
||||||
|
@ -198,20 +195,6 @@ class SerialReader:
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
|
|
||||||
# Wrapper around command sending
|
|
||||||
class SerialCommand:
|
|
||||||
def __init__(self, serial, cmd_queue, cmd):
|
|
||||||
self.serial = serial
|
|
||||||
self.cmd_queue = cmd_queue
|
|
||||||
self.cmd = cmd
|
|
||||||
def send(self, data=(), minclock=0, reqclock=0):
|
|
||||||
cmd = self.cmd.encode(data)
|
|
||||||
self.serial.raw_send(cmd, minclock, reqclock, self.cmd_queue)
|
|
||||||
def send_with_response(self, data=(), response=None, response_oid=None):
|
|
||||||
cmd = self.cmd.encode(data)
|
|
||||||
src = SerialRetryCommand(self.serial, cmd, response, response_oid)
|
|
||||||
return src.get_response()
|
|
||||||
|
|
||||||
# Class to retry sending of a query command until a given response is received
|
# Class to retry sending of a query command until a given response is received
|
||||||
class SerialRetryCommand:
|
class SerialRetryCommand:
|
||||||
TIMEOUT_TIME = 5.0
|
TIMEOUT_TIME = 5.0
|
||||||
|
|
Loading…
Reference in New Issue