mcu: Move lookup_command_tag() to CommandWrapper class

Use mcu.lookup_command().get_command_tag() instead of
mcu.lookup_command_tag().  This improves error reporting on a protocol
mismatch.  It also enables support for a msgtag that is negative.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2023-01-13 12:42:14 -05:00
parent 69dcda1696
commit d881171739
3 changed files with 24 additions and 14 deletions

View File

@ -80,13 +80,17 @@ class CommandQueryWrapper:
class CommandWrapper:
def __init__(self, serial, msgformat, cmd_queue=None):
self._serial = serial
self._cmd = serial.get_msgparser().lookup_command(msgformat)
msgparser = serial.get_msgparser()
self._cmd = msgparser.lookup_command(msgformat)
if cmd_queue is None:
cmd_queue = serial.get_default_command_queue()
self._cmd_queue = cmd_queue
self._msgtag = msgparser.lookup_msgtag(msgformat) & 0xffffffff
def send(self, data=(), minclock=0, reqclock=0):
cmd = self._cmd.encode(data)
self._serial.raw_send(cmd, minclock, reqclock, self._cmd_queue)
def get_command_tag(self):
return self._msgtag
######################################################################
@ -148,11 +152,13 @@ class MCU_trsync:
self._stepper_stop_cmd = mcu.lookup_command(
"stepper_stop_on_trigger oid=%c trsync_oid=%c", cq=self._cmd_queue)
# Create trdispatch_mcu object
set_timeout_tag = mcu.lookup_command_tag(
"trsync_set_timeout oid=%c clock=%u")
trigger_tag = mcu.lookup_command_tag("trsync_trigger oid=%c reason=%c")
state_tag = mcu.lookup_command_tag(
set_timeout_tag = mcu.lookup_command(
"trsync_set_timeout oid=%c clock=%u").get_command_tag()
trigger_cmd = mcu.lookup_command("trsync_trigger oid=%c reason=%c")
trigger_tag = trigger_cmd.get_command_tag()
state_cmd = mcu.lookup_command(
"trsync_state oid=%c can_trigger=%c trigger_reason=%c clock=%u")
state_tag = state_cmd.get_command_tag()
ffi_main, ffi_lib = chelper.get_ffi()
self._trdispatch_mcu = ffi_main.gc(ffi_lib.trdispatch_mcu_alloc(
self._trdispatch, mcu._serial.get_serialqueue(), # XXX
@ -874,9 +880,6 @@ class MCU:
return self.lookup_command(msgformat)
except self._serial.get_msgparser().error as e:
return None
def lookup_command_tag(self, msgformat):
all_msgs = self._serial.get_msgparser().get_messages()
return {fmt: msgtag for msgtag, msgtype, fmt in all_msgs}[msgformat]
def get_enumerations(self):
return self._serial.get_msgparser().get_enumerations()
def get_constants(self):

View File

@ -234,6 +234,7 @@ class MessageParser:
self.messages = []
self.messages_by_id = {}
self.messages_by_name = {}
self.msgtag_by_format = {}
self.config = {}
self.version = self.build_versions = ""
self.raw_identify_data = ""
@ -316,6 +317,11 @@ class MessageParser:
self._error("Command format mismatch: %s vs %s",
msgformat, mp.msgformat)
return mp
def lookup_msgtag(self, msgformat):
msgtag = self.msgtag_by_format.get(msgformat)
if msgtag is None:
self._error("Unknown command: %s", msgformat)
return msgtag
def create_command(self, msg):
parts = msg.strip().split()
if not parts:
@ -376,6 +382,7 @@ class MessageParser:
self.messages.append((msgtag, msgtype, msgformat))
if msgtag < -32 or msgtag > 95:
self._error("Multi-byte msgtag not supported")
self.msgtag_by_format[msgformat] = msgtag
msgid = msgtag & 0x7f
if msgtype == 'output':
self.messages_by_id[msgid] = OutputFormat(msgid, msgformat)

View File

@ -89,12 +89,12 @@ class MCU_stepper:
invert_step, step_pulse_ticks))
self._mcu.add_config_cmd("reset_step_clock oid=%d clock=0"
% (self._oid,), on_restart=True)
step_cmd_tag = self._mcu.lookup_command_tag(
"queue_step oid=%c interval=%u count=%hu add=%hi")
dir_cmd_tag = self._mcu.lookup_command_tag(
"set_next_step_dir oid=%c dir=%c")
self._reset_cmd_tag = self._mcu.lookup_command_tag(
"reset_step_clock oid=%c clock=%u")
step_cmd_tag = self._mcu.lookup_command(
"queue_step oid=%c interval=%u count=%hu add=%hi").get_command_tag()
dir_cmd_tag = self._mcu.lookup_command(
"set_next_step_dir oid=%c dir=%c").get_command_tag()
self._reset_cmd_tag = self._mcu.lookup_command(
"reset_step_clock oid=%c clock=%u").get_command_tag()
self._get_position_cmd = self._mcu.lookup_query_command(
"stepper_get_position oid=%c",
"stepper_position oid=%c pos=%i", oid=self._oid)