Correspondingly, the minimum version requirement for sinqMotor has been bumped to 0.8.0.
125 lines
3.9 KiB
Python
125 lines
3.9 KiB
Python
"""
|
|
Code shared by "decodeError.py" and "decodeStatus.py"
|
|
"""
|
|
|
|
import struct
|
|
|
|
def decode(value: int, interpretation):
|
|
|
|
# Pack the input as a short and unpack it as an unsigned short
|
|
value_uint16 = format(value, '016b') # Format as 16-bit unsigned integer
|
|
|
|
bit_list = [int(char) for char in value_uint16]
|
|
bit_list.reverse()
|
|
|
|
interpreted = []
|
|
for (bit, interpretations) in zip(bit_list, interpretation):
|
|
interpreted.append(interpretations[bit])
|
|
return (bit_list, interpreted)
|
|
|
|
def print_decoded(bit_list, interpreted):
|
|
for (idx, (bit_value, msg)) in enumerate(zip(bit_list, interpreted)):
|
|
print(f"Bit {idx} = {bit_value}: {msg}")
|
|
|
|
def interactive():
|
|
|
|
# Imported here, because curses is not available in Windows. Using the
|
|
# interactive mode therefore fails on Windows, but at least the single
|
|
# command mode can be used (which would not be possible if we would import
|
|
# curses at the top level)
|
|
import curses
|
|
|
|
stdscr = curses.initscr()
|
|
curses.noecho()
|
|
curses.cbreak()
|
|
stdscr.keypad(True)
|
|
stdscr.scrollok(True)
|
|
|
|
stdscr.addstr(">> ")
|
|
stdscr.refresh()
|
|
|
|
history = [""]
|
|
ptr = len(history) - 1
|
|
|
|
while True:
|
|
c = stdscr.getch()
|
|
if c == curses.KEY_RIGHT:
|
|
(y, x) = stdscr.getyx()
|
|
if x < len(history[ptr]) + 3:
|
|
stdscr.move(y, x+1)
|
|
stdscr.refresh()
|
|
elif c == curses.KEY_LEFT:
|
|
(y, x) = stdscr.getyx()
|
|
if x > 3:
|
|
stdscr.move(y, x-1)
|
|
stdscr.refresh()
|
|
elif c == curses.KEY_UP:
|
|
if ptr > 0:
|
|
ptr -= 1
|
|
stdscr.addch("\r")
|
|
stdscr.clrtoeol()
|
|
stdscr.addstr(">> " + history[ptr])
|
|
elif c == curses.KEY_DOWN:
|
|
if ptr < len(history) - 1:
|
|
ptr += 1
|
|
stdscr.addch("\r")
|
|
stdscr.clrtoeol()
|
|
stdscr.addstr(">> " + history[ptr])
|
|
elif c == curses.KEY_ENTER or c == ord('\n') or c == ord('\r'):
|
|
if history[ptr] == 'quit':
|
|
break
|
|
|
|
# because of arrow keys move back to the end of the line
|
|
(y, x) = stdscr.getyx()
|
|
stdscr.move(y, 3+len(history[ptr]))
|
|
|
|
if history[ptr]:
|
|
(bit_list, interpreted) = decode(history[ptr])
|
|
for (idx, (bit_value, msg)) in enumerate(zip(bit_list, interpreted)):
|
|
stdscr.addstr(f"\nBit {idx} = {bit_value}: {msg}")
|
|
stdscr.refresh()
|
|
|
|
if ptr == len(history) - 1 and history[ptr] != "":
|
|
history += [""]
|
|
else:
|
|
history[-1] = ""
|
|
ptr = len(history) - 1
|
|
|
|
stdscr.addstr("\n>> ")
|
|
stdscr.refresh()
|
|
|
|
else:
|
|
if ptr < len(history) - 1: # Modifying previous input
|
|
if len(history[-1]) == 0:
|
|
history[-1] = history[ptr]
|
|
ptr = len(history) - 1
|
|
|
|
else:
|
|
history += [history[ptr]]
|
|
ptr = len(history) - 1
|
|
|
|
if c == curses.KEY_BACKSPACE:
|
|
if len(history[ptr]) == 0:
|
|
continue
|
|
(y, x) = stdscr.getyx()
|
|
history[ptr] = history[ptr][0:x-4] + history[ptr][x-3:]
|
|
stdscr.addch("\r")
|
|
stdscr.clrtoeol()
|
|
stdscr.addstr(">> " + history[ptr])
|
|
stdscr.move(y, x-1)
|
|
stdscr.refresh()
|
|
|
|
else:
|
|
(y, x) = stdscr.getyx()
|
|
history[ptr] = history[ptr][0:x-3] + chr(c) + history[ptr][x-3:]
|
|
stdscr.addch("\r")
|
|
stdscr.clrtoeol()
|
|
stdscr.addstr(">> " + history[ptr])
|
|
stdscr.move(y, x+1)
|
|
stdscr.refresh()
|
|
|
|
# to quit
|
|
curses.nocbreak()
|
|
stdscr.keypad(False)
|
|
curses.echo()
|
|
curses.endwin() |