"""Parameter protocol helpers: PX4 int32 union encoding, read/set/echo. PX4 transports an INT32 parameter as the raw bit pattern of the int placed into the float param_value field. To send: pack the int as ' prints one line: # x + l SDLOG_UTC_OFFSET [used,idx] : 777 # three flag columns then the name; the SECOND flag is the save state: # '*' unsaved, '+' saved, ' ' unmodified from default # (src/systemcmds/param/param.cpp:822-823). _PARAM_SHOW_RE = re.compile( r'^\s*(?P[x ])\s?(?P[*+ ])\s?(?P[l ])\s+' r'(?P[A-Z0-9_]+)\s+\[[^\]]*\]\s*:\s*(?P-?\d+)') def parse_param_show(output, name): """Parse `param show ` output for one parameter. Returns (saved, value) where saved is True/False/None (None when the line is not found or the save state is 'default'/unknown) and value is the int32 value or None. Tolerant of ANSI escapes and the prompt. """ for raw in output.splitlines(): line = re.sub(r'\x1b\[[0-9;]*[A-Za-z]', '', raw).replace('nsh>', '') m = _PARAM_SHOW_RE.search(line) if not m or m.group('name') != name: continue flag = m.group('saved') saved = True if flag == '+' else (False if flag == '*' else None) try: value = int(m.group('value')) except ValueError: value = None return saved, value return None, None def param_is_saved(shell, name, timeout=10): """Run `param show ` over an open MavlinkShell and report save state. Returns (saved, value, timed_out): saved True/False/None as parsed by parse_param_show, value the int32 shown, timed_out True if the shell command did not complete. Caller owns the shell (open and close it). """ out, timed_out = shell.run('param show {}'.format(name), timeout=timeout) if timed_out: return None, None, True saved, value = parse_param_show(out, name) return saved, value, False