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:
parent
69dcda1696
commit
d881171739
|
@ -80,13 +80,17 @@ class CommandQueryWrapper:
|
||||||
class CommandWrapper:
|
class CommandWrapper:
|
||||||
def __init__(self, serial, msgformat, cmd_queue=None):
|
def __init__(self, serial, msgformat, cmd_queue=None):
|
||||||
self._serial = serial
|
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:
|
if cmd_queue is None:
|
||||||
cmd_queue = serial.get_default_command_queue()
|
cmd_queue = serial.get_default_command_queue()
|
||||||
self._cmd_queue = cmd_queue
|
self._cmd_queue = cmd_queue
|
||||||
|
self._msgtag = msgparser.lookup_msgtag(msgformat) & 0xffffffff
|
||||||
def send(self, data=(), minclock=0, reqclock=0):
|
def send(self, data=(), minclock=0, reqclock=0):
|
||||||
cmd = self._cmd.encode(data)
|
cmd = self._cmd.encode(data)
|
||||||
self._serial.raw_send(cmd, minclock, reqclock, self._cmd_queue)
|
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(
|
self._stepper_stop_cmd = mcu.lookup_command(
|
||||||
"stepper_stop_on_trigger oid=%c trsync_oid=%c", cq=self._cmd_queue)
|
"stepper_stop_on_trigger oid=%c trsync_oid=%c", cq=self._cmd_queue)
|
||||||
# Create trdispatch_mcu object
|
# Create trdispatch_mcu object
|
||||||
set_timeout_tag = mcu.lookup_command_tag(
|
set_timeout_tag = mcu.lookup_command(
|
||||||
"trsync_set_timeout oid=%c clock=%u")
|
"trsync_set_timeout oid=%c clock=%u").get_command_tag()
|
||||||
trigger_tag = mcu.lookup_command_tag("trsync_trigger oid=%c reason=%c")
|
trigger_cmd = mcu.lookup_command("trsync_trigger oid=%c reason=%c")
|
||||||
state_tag = mcu.lookup_command_tag(
|
trigger_tag = trigger_cmd.get_command_tag()
|
||||||
|
state_cmd = mcu.lookup_command(
|
||||||
"trsync_state oid=%c can_trigger=%c trigger_reason=%c clock=%u")
|
"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()
|
ffi_main, ffi_lib = chelper.get_ffi()
|
||||||
self._trdispatch_mcu = ffi_main.gc(ffi_lib.trdispatch_mcu_alloc(
|
self._trdispatch_mcu = ffi_main.gc(ffi_lib.trdispatch_mcu_alloc(
|
||||||
self._trdispatch, mcu._serial.get_serialqueue(), # XXX
|
self._trdispatch, mcu._serial.get_serialqueue(), # XXX
|
||||||
|
@ -874,9 +880,6 @@ class MCU:
|
||||||
return self.lookup_command(msgformat)
|
return self.lookup_command(msgformat)
|
||||||
except self._serial.get_msgparser().error as e:
|
except self._serial.get_msgparser().error as e:
|
||||||
return None
|
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):
|
def get_enumerations(self):
|
||||||
return self._serial.get_msgparser().get_enumerations()
|
return self._serial.get_msgparser().get_enumerations()
|
||||||
def get_constants(self):
|
def get_constants(self):
|
||||||
|
|
|
@ -234,6 +234,7 @@ class MessageParser:
|
||||||
self.messages = []
|
self.messages = []
|
||||||
self.messages_by_id = {}
|
self.messages_by_id = {}
|
||||||
self.messages_by_name = {}
|
self.messages_by_name = {}
|
||||||
|
self.msgtag_by_format = {}
|
||||||
self.config = {}
|
self.config = {}
|
||||||
self.version = self.build_versions = ""
|
self.version = self.build_versions = ""
|
||||||
self.raw_identify_data = ""
|
self.raw_identify_data = ""
|
||||||
|
@ -316,6 +317,11 @@ class MessageParser:
|
||||||
self._error("Command format mismatch: %s vs %s",
|
self._error("Command format mismatch: %s vs %s",
|
||||||
msgformat, mp.msgformat)
|
msgformat, mp.msgformat)
|
||||||
return mp
|
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):
|
def create_command(self, msg):
|
||||||
parts = msg.strip().split()
|
parts = msg.strip().split()
|
||||||
if not parts:
|
if not parts:
|
||||||
|
@ -376,6 +382,7 @@ class MessageParser:
|
||||||
self.messages.append((msgtag, msgtype, msgformat))
|
self.messages.append((msgtag, msgtype, msgformat))
|
||||||
if msgtag < -32 or msgtag > 95:
|
if msgtag < -32 or msgtag > 95:
|
||||||
self._error("Multi-byte msgtag not supported")
|
self._error("Multi-byte msgtag not supported")
|
||||||
|
self.msgtag_by_format[msgformat] = msgtag
|
||||||
msgid = msgtag & 0x7f
|
msgid = msgtag & 0x7f
|
||||||
if msgtype == 'output':
|
if msgtype == 'output':
|
||||||
self.messages_by_id[msgid] = OutputFormat(msgid, msgformat)
|
self.messages_by_id[msgid] = OutputFormat(msgid, msgformat)
|
||||||
|
|
|
@ -89,12 +89,12 @@ class MCU_stepper:
|
||||||
invert_step, step_pulse_ticks))
|
invert_step, step_pulse_ticks))
|
||||||
self._mcu.add_config_cmd("reset_step_clock oid=%d clock=0"
|
self._mcu.add_config_cmd("reset_step_clock oid=%d clock=0"
|
||||||
% (self._oid,), on_restart=True)
|
% (self._oid,), on_restart=True)
|
||||||
step_cmd_tag = self._mcu.lookup_command_tag(
|
step_cmd_tag = self._mcu.lookup_command(
|
||||||
"queue_step oid=%c interval=%u count=%hu add=%hi")
|
"queue_step oid=%c interval=%u count=%hu add=%hi").get_command_tag()
|
||||||
dir_cmd_tag = self._mcu.lookup_command_tag(
|
dir_cmd_tag = self._mcu.lookup_command(
|
||||||
"set_next_step_dir oid=%c dir=%c")
|
"set_next_step_dir oid=%c dir=%c").get_command_tag()
|
||||||
self._reset_cmd_tag = self._mcu.lookup_command_tag(
|
self._reset_cmd_tag = self._mcu.lookup_command(
|
||||||
"reset_step_clock oid=%c clock=%u")
|
"reset_step_clock oid=%c clock=%u").get_command_tag()
|
||||||
self._get_position_cmd = self._mcu.lookup_query_command(
|
self._get_position_cmd = self._mcu.lookup_query_command(
|
||||||
"stepper_get_position oid=%c",
|
"stepper_get_position oid=%c",
|
||||||
"stepper_position oid=%c pos=%i", oid=self._oid)
|
"stepper_position oid=%c pos=%i", oid=self._oid)
|
||||||
|
|
Loading…
Reference in New Issue