def dp(number, min_value, max_value, default): if not isinstance(min_value, str): min_value = Sint(min_value, 16) if not isinstance(max_value, str): max_value = Sint(max_value, 16) return dummy_parameter(number=number, datatype=Sint, min_value=min_value, max_value=max_value, default=Sint(default, 16))
def test_default_int(self): parameter = dummy_parameter_from_line(default='10') self.assertEqual(parameter.default, Uint(10, 16)) parameter = dummy_parameter_from_line( default='-10', format_='s16') self.assertEqual(parameter.default, Sint(-10, 16))
def set_temperature(self, value: int): """Set the temperature. Note that *value* can also be negative. """ self._kwargs['temperature'] = Sint(value, bits=16) return self
def __init__(self, parameters=PARAMETERS): """ __init__(parameters=PARAMETERS) Initialize a new :class:`TelegramBuilder`. Args: parameters: The object to be assigned to :attr:`parameters`. """ self.parameters = parameters # Keyword arguments used to create a telegram. self._kwargs = { 'parameter_code': Bin(4 * '0', bits=4), 'parameter_number': Uint(0, bits=11), 'parameter_index': Uint(0, bits=8), 'parameter_value': Uint(0, bits=32), 'flag_bits': Bin(16 * '0', bits=16), 'frequency': Uint(0, bits=16), 'temperature': Sint(0, bits=16), 'current': Uint(0, bits=16,), 'voltage': Uint(0, bits=16), } # parameter_value and parameter_mode are special cases. # These variables store the values given by the user, and the final # values used as arguments are only determined when the telegram is # created. # __init__ and set_parameter_value set this to a int or a float, # and from_bytes to a bytes object. self._parameter_value = 0 # __init__ and set_parameter_mode set this to a string, and from_bytes # to a Bin object. self._parameter_mode = 'none'
def from_bytes(self, bytes_): """Read the contents of the telegram from a :class:`bytes` object. The type of :attr:`parameter_value <Telegram.parameter_value>` depends on :attr:`parameter_number <Telegram.parameter_number>`, and is assigned automatically. If *bytes_* contains a parameter number that doesn't exist, a :class:`ValueError` is raised. If the parameter isn't accessed (i.e. the parameter mode is set to ``'none'`` or code to ``'0000'``), invalid parameter numbers, such as the default value of 0, are permitted. In that case, the parameter type is set to :class:`~turboctl.telegram.datatypes.Uint`. Note that this isn't a class method; a :class:`TelegramBuilder` must first be initialized normally with :meth:`__init__`, after which this method may be called. Raises: ValueError: If *bytes_* doesn't represent a valid telegram. """ self._check_valid_telegram(bytes_) code_and_number_bits = Bin(bytes_[3:5]) self._kwargs = { 'parameter_number': Uint(code_and_number_bits[5:16]), 'parameter_index': Uint(bytes_[6]), 'flag_bits': Bin(bytes_[11:13])[::-1], 'frequency': Uint(bytes_[13:15]), 'temperature': Sint(bytes_[15:17]), 'current': Uint(bytes_[17:19]), 'voltage': Uint(bytes_[21:23]) } self._parameter_value = bytes_[7:11] self._parameter_mode = Bin(code_and_number_bits[0:4]) return self
def test_min_max_int(self): parameter = dummy_parameter_from_line( min_value='-10', max_value='100', format_='s16') self.assertEqual(parameter.min_value, Sint(-10, 16)) self.assertEqual(parameter.max_value, Sint(100, 16))
def test_max_value_as_reference(self): self.assertEqual(self.pc.parameters[5].max_value, Sint(1, 16)) self.write_test(self.upper.number, 2) self.assertEqual(self.pc.parameters[5].max_value, Sint(2, 16))
def test_min_value_as_reference(self): self.assertEqual(self.pc.parameters[5].min_value, Sint(-1, 16)) self.write_test(self.lower.number, -2) self.assertEqual(self.pc.parameters[5].min_value, Sint(-2, 16))
def test_bits_default_value(self): self.assertEqual(Uint(1).bits, 8) self.assertEqual(Sint(-1).bits, 8) self.assertEqual(Float(1.0).bits, 32) self.assertEqual(Float(1).bits, 32) self.assertEqual(Bin(1).bits, 8)
def test_sint_from_int_sets_attributes_correctly(self, i_and_bits): i, bits = i_and_bits sint = Sint(i, bits) self.assertEqual(sint.value, i) self.assertEqual(sint.bits, bits)