Add terminal type to TerminalId (CUT and DFT)

This commit is contained in:
Andrew Kay
2020-05-11 20:19:06 -05:00
parent a8bbc2917e
commit 62a054ae12
3 changed files with 39 additions and 16 deletions

View File

@@ -7,6 +7,7 @@ from .protocol import (
PollResponse,
PowerOnResetCompletePollResponse,
KeystrokePollResponse,
TerminalType,
Control,
SecondaryControl,
poll,

View File

@@ -97,6 +97,12 @@ class Status:
f'feature_error={self.feature_error}, '
f'operation_complete={self.operation_complete}>')
class TerminalType(Enum):
"""Terminal type."""
CUT = 1
DFT = 2
class TerminalId:
"""Terminal model and keyboard."""
@@ -108,21 +114,28 @@ class TerminalId:
}
def __init__(self, value):
if (value & 0x1) != 0:
raise ValueError('Invalid terminal identifier')
self.value = value
model = (value & 0x0e) >> 1
if (value & 0x1) == 0:
self.type = TerminalType.CUT
if model not in TerminalId._MODEL_MAP:
raise ValueError('Invalid model')
model = (value & 0x0e) >> 1
self.model = TerminalId._MODEL_MAP[model]
self.keyboard = (value & 0xf0) >> 4
if model not in TerminalId._MODEL_MAP:
raise ValueError('Invalid model')
self.model = TerminalId._MODEL_MAP[model]
self.keyboard = (value & 0xf0) >> 4
elif value == 1:
self.type = TerminalType.DFT
self.model = None
self.keyboard = None
else:
raise ValueError('Invalid terminal identifier')
def __repr__(self):
return f'<TerminalId model={self.model}, keyboard={self.keyboard}>'
return (f'<TerminalId type={self.type.name}, model={self.model}, '
f'keyboard={self.keyboard}>')
class Control:
"""Terminal control register."""

View File

@@ -4,7 +4,7 @@ from unittest.mock import Mock
import context
from coax import PollResponse, KeystrokePollResponse, ProtocolError
from coax.protocol import Command, Status, TerminalId, Control, SecondaryControl, pack_command_word, unpack_command_word, pack_data_word, unpack_data_word, pack_data_words, unpack_data_words, _execute_read_command, _execute_write_command
from coax.protocol import Command, Status, TerminalType, TerminalId, Control, SecondaryControl, pack_command_word, unpack_command_word, pack_data_word, unpack_data_word, pack_data_words, unpack_data_words, _execute_read_command, _execute_write_command
class PollResponseTestCase(unittest.TestCase):
def test_is_power_on_reset_complete(self):
@@ -35,31 +35,40 @@ class StatusTestCase(unittest.TestCase):
self.assertTrue(status.operation_complete)
class TerminalIdTestCase(unittest.TestCase):
def test_model_2(self):
def test_cut_model_2(self):
terminal_id = TerminalId(0b00000100)
self.assertEqual(terminal_id.type, TerminalType.CUT)
self.assertEqual(terminal_id.model, 2)
def test_model_3(self):
def test_cut_model_3(self):
terminal_id = TerminalId(0b00000110)
self.assertEqual(terminal_id.type, TerminalType.CUT)
self.assertEqual(terminal_id.model, 3)
def test_model_4(self):
def test_cut_model_4(self):
terminal_id = TerminalId(0b00001110)
self.assertEqual(terminal_id.type, TerminalType.CUT)
self.assertEqual(terminal_id.model, 4)
def test_model_5(self):
def test_cut_model_5(self):
terminal_id = TerminalId(0b00001100)
self.assertEqual(terminal_id.type, TerminalType.CUT)
self.assertEqual(terminal_id.model, 5)
def test_dft(self):
terminal_id = TerminalId(0b00000001)
self.assertEqual(terminal_id.type, TerminalType.DFT)
def test_invalid_identifier(self):
with self.assertRaisesRegex(ValueError, 'Invalid terminal identifier'):
terminal_id = TerminalId(0b00000001)
terminal_id = TerminalId(0b00000011)
def test_invalid_model(self):
def test_invalid_cut_model(self):
with self.assertRaisesRegex(ValueError, 'Invalid model'):
terminal_id = TerminalId(0b00000000)