Drop address from command packing functions

This commit is contained in:
Andrew Kay
2020-03-13 08:13:42 -05:00
parent 711d3ed1c8
commit debe68c4c3
2 changed files with 9 additions and 22 deletions

View File

@@ -313,9 +313,9 @@ def diagnostic_reset(interface):
"""Execute a DIAGNOSTIC_RESET command."""
raise NotImplementedError
def pack_command_word(command, address=0):
"""Pack a command and address into a 10-bit command word."""
return (address << 7) | (command.value << 2) | 0x1
def pack_command_word(command):
"""Pack a command into a 10-bit command word."""
return (command.value << 2) | 0x1
def is_command_word(word):
"""Is command word bit set?"""
@@ -326,10 +326,9 @@ def unpack_command_word(word):
if not is_command_word(word):
raise ProtocolError('Word does not have command bit set')
address = (word >> 7) & 0x7
command = (word >> 2) & 0x1f
return (address, Command(command))
return Command(command)
def is_data_word(word):
"""Is data word bit set?"""
@@ -362,7 +361,7 @@ def _execute_read_command(interface, command_word, response_length=1,
return trta_value
if validate_response_length and len(response) != response_length:
(_, command) = unpack_command_word(command_word)
command = unpack_command_word(command_word)
raise ProtocolError(f'Expected {response_length} word {command.name} response')
@@ -373,7 +372,7 @@ def _execute_write_command(interface, command_word, data=None, **kwargs):
response = interface.execute(command_word, data, **kwargs)
if len(response) != 1:
(_, command) = unpack_command_word(command_word)
command = unpack_command_word(command_word)
raise ProtocolError(f'Expected 1 word {command.name} response')

View File

@@ -96,27 +96,15 @@ class SecondaryControlTestCase(unittest.TestCase):
self.assertEqual(control.value, 0b00000001)
class PackCommandWordTestCase(unittest.TestCase):
def test_without_address(self):
def test(self):
self.assertEqual(pack_command_word(Command.POLL_ACK), 0b0001000101)
def test_with_address(self):
self.assertEqual(pack_command_word(Command.POLL_ACK, address=7), 0b1111000101)
class UnpackCommandWordTestCase(unittest.TestCase):
def test_without_address(self):
def test(self):
# Act
(address, command) = unpack_command_word(0b0001000101)
command = unpack_command_word(0b0001000101)
# Assert
self.assertEqual(address, 0)
self.assertEqual(command, Command.POLL_ACK)
def test_with_address(self):
# Act
(address, command) = unpack_command_word(0b1111000101)
# Assert
self.assertEqual(address, 7)
self.assertEqual(command, Command.POLL_ACK)
def test_command_bit_not_set_error(self):