diff --git a/pycoax/coax/protocol.py b/pycoax/coax/protocol.py index 5705830..919760c 100644 --- a/pycoax/coax/protocol.py +++ b/pycoax/coax/protocol.py @@ -73,6 +73,22 @@ class KeystrokePollResponse(PollResponse): self.scan_code = (value >> 2) & 0xff +class Status: + """Terminal status.""" + + def __init__(self, value): + self.value = value + + self.monocase = bool(value & 0x80) + self.busy = not bool(value & 0x20) + self.feature_error = bool(value & 0x04) + self.operation_complete = bool(value & 0x02) + + def __repr__(self): + return (f'') + class TerminalId: """Terminal model and keyboard.""" @@ -122,9 +138,11 @@ def poll_ack(interface, **kwargs): """Execute a POLL_ACK command.""" _execute_write_command(interface, Command.POLL_ACK, **kwargs) -def read_status(interface): +def read_status(interface, **kwargs): """Execute a READ_STATUS command.""" - raise NotImplementedError + response = _execute_read_command(interface, Command.READ_STATUS, **kwargs) + + return Status(response[0]) def read_terminal_id(interface, **kwargs): """Execute a READ_TERMINAL_ID command.""" diff --git a/pycoax/examples/07_read_status.py b/pycoax/examples/07_read_status.py new file mode 100755 index 0000000..9c0784e --- /dev/null +++ b/pycoax/examples/07_read_status.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +import sys +import time +from serial import Serial + +sys.path.append('..') + +from coax import Interface1, read_status + +print('Opening serial port...') + +with Serial('/dev/ttyUSB0', 115200) as serial: + print('Sleeping to allow interface time to wake up...') + + time.sleep(3) + + interface = Interface1(serial) + + print('Resetting interface...') + + version = interface.reset() + + print(f'Firmware version is {version}') + + print('READ_STATUS...') + + status = read_status(interface) + + print(status) diff --git a/pycoax/tests/test_protocol.py b/pycoax/tests/test_protocol.py index ecb0c36..fa51745 100644 --- a/pycoax/tests/test_protocol.py +++ b/pycoax/tests/test_protocol.py @@ -4,7 +4,7 @@ from unittest.mock import Mock import context from coax import PollResponse, KeystrokePollResponse, ProtocolError -from coax.protocol import Command, TerminalId, _execute_read_command, _execute_write_command, _pack_command_word, _unpack_data_words, _unpack_data_word +from coax.protocol import Command, Status, TerminalId, _execute_read_command, _execute_write_command, _pack_command_word, _unpack_data_words, _unpack_data_word class PollResponseTestCase(unittest.TestCase): def test_is_power_on_reset_complete(self): @@ -25,6 +25,15 @@ class KeystrokePollResponseTestCase(unittest.TestCase): with self.assertRaisesRegex(ValueError, 'Invalid keystroke poll response'): response = KeystrokePollResponse(0b0000001000) +class StatusTestCase(unittest.TestCase): + def test(self): + status = Status(0b10000110) + + self.assertTrue(status.monocase) + self.assertTrue(status.busy) + self.assertTrue(status.feature_error) + self.assertTrue(status.operation_complete) + class TerminalIdTestCase(unittest.TestCase): def test_model_2(self): terminal_id = TerminalId(0b00000100)