26 Packet class to be used as a base class for all packets acting as an interface.
27 it contains the static method from_bytearray to construct a packet
from a bytearray
28 and chooses the correct packet type depending on the header
37 """ method to construct a packet from a bytearray
38 depending on the header the correct packet type is returned
39 @param byte_array: (bytearray) the bytearray to construct the packet
from
40 @return: the constructed packet of the coresponing sub type
43 unpacked = struct.unpack(
"<BII", byte_array)
48 logging.debug(
"detected: Data32bit : "+ str(header))
49 return Data32bitPacket.from_bytearray(byte_array)
53 logging.debug(
"detected: DataI2C : "+ str(header))
54 return DataI2CPacket.from_bytearray(byte_array)
58 logging.debug(
"detected: Pin : "+ str(header))
59 return PinPacket.from_bytearray(byte_array)
63 logging.debug(
"detected: Config Main : "+ str(header))
64 return ConfigPacket.from_bytearray(byte_array)
68 logging.debug(
"detected: Error : "+ str(header))
69 return ErrorPacket.from_bytearray(byte_array)
71 logging.error(
"Header "+ str(unpacked[0]) +
" is not a valid header, Packet:" + str(byte_array) +
" "+ str(unpacked[0]) +
" "+ str(unpacked[1]) +
" "+ str(unpacked[2]))
72 raise ValueError(
"Header "+ str(unpacked[0]) +
" is not a valid header")
73 except Exception
as e:
76 """getter method for the header attribute
78 :return: the header of the packet
79 :rtype: EnumInt entry
or int
83 """getter method for the time attribute
85 :return: the time of execution of the packet
86 :rtype: EnumInt entry
or int
91 """setter method for the header attribute
92 @param header: (byte/uint_8) the header of the packet
94 if (header < 256
and header >= 0
and isinstance(header, int)):
97 logging.error(
"header "+str(header)+
" is not a valid header")
100 """setter method for the execution time attribute
101 @param time: (uint_32) the time of execution of the packet
103 if (time < 2**32
and time >= 0
and isinstance(time, int)):
106 logging.error(
"exec_time "+str(time)+
" is not a valid unsigned integer of 4 bytes")
109 """ placeholder for converting the package to a bytearray
110 @return: the packet
as a bytearray
112 logging.warning("the method to_bytearray needs to be implemented by the child class")
116 logs the packet if the header
is in the logging lists
118 if self.
_header in LOGGING_WARNING_LIST:
119 logging.warning(str(self))
120 if self.
_header in LOGGING_INFO_LIST:
121 logging.info(str(self))
126 """ The Data32bitPacket is used to send 32bit data instructions to the uC
127 all availible instructions are defined in the Data32bitHeader
131 """ constructor for the Data32bitPacket
132 @param header: (Data32bitHeader) the header of the packet
133 @param value: (uint_32) the value to be sent (optional, default = 0)
134 @param time: (uint_32) the time of execution of the packet (optional, default = 0)
142 """ getter method for the value attribute
143 @return: the value of the packet
148 if (header
in Data32bitHeader):
151 logging.error(
"header "+str(header)+
" is not a valid header")
154 if (value < 2**32
and value >= 0
and isinstance(value, int)):
157 logging.error(
"value "+str(value)+
" is not a valid unsigned integer of 4 bytes")
168 constructs a Data32bitPacket from a bytearray
169 @param byte_array: 9 byte bytearray to construct the Packet
from
170 @return: Data32bitPacket
171 @raise Exception:
if package construction fails
173 unpacked = struct.unpack("<BII", byte_array)
180 """ The DataI2CPacket is used for I2C communication
181 and all types which need 3x 8bit values instread of 1x 32bit value
182 all availible instructions are defined
in the DataI2CHeader
184 def __init__(self, header, device_address, register_address, read=False, value=0, time=0):
194 """ getter method for the value attribute
195 @return: the value of the packet
200 """ getter method for the 1bit read/write attribute
201 @return: the read flag of the i2c packet
206 """ getter method for the 7bit device_address attribute
207 @return: the device_address of the i2c packet
212 """ getter method for the 8bit register_address attribute
213 @return: the register_address of the i2c packet
218 """ setter method for the header attribute
219 @param header: (DataI2CHeader
or uint8) the header of the packet
221 if (header
in DataI2CHeader):
224 logging.error(
"header "+str(header)+
" is not a valid header")
227 """ setter method for the value attribute
228 @param value: (uint_16) the value to be sent
230 if (value < 2**16
and value >= 0
and isinstance(value, int)):
234 logging.error(
"value "+str(value)+
" is not a valid unsigned integer of 2 byte")
237 """ setter method for the 7bit device address attribute
238 @param value: (uint_8) the 7bit device address to be sent to
240 if (value < 2**7
and value >= 0
and isinstance(value, int)):
243 logging.error(
"device_address "+str(value)+
" is not a valid unsigned integer of 7 bit")
246 """ setter method for the 1bit read/write attribute
247 @param value: (bool) the read flag of the i2c packet
254 """ setter method for the 8bit register address attribute
255 @param value: (uint_8) the 8bit register address to be sent to
257 if (value < 2**8
and value >= 0
and isinstance(value, int)):
260 logging.error(
"register_address "+str(value)+
" is not a valid unsigned integer of 8 bit")
262 """ method to convert the packet to a bytearray in the correct format of <header><exec_time><addess+read><value ms/ls>
263 @return: the packet
as a bytearray
273 constructs a DataI2CPacket from a bytearray
274 @param byte_array: 9 byte bytearray to construct the Packet
from
275 @return: DataI2CPacket
276 @raise Exception:
if package construction fails
278 unpacked = struct.unpack("<BIBBBB", byte_array)
280 print((unpacked[4] << 8) | unpacked[5])
282 device_address=unpacked[2]>>1,
283 register_address=unpacked[3], read=unpacked[2] & 0x1, value=((unpacked[4] << 8) | unpacked[5]),
288value: (int) 0 to set pin LOW, 1 to set pin HIGH.
291 """ The PinPacket is used to comunicate pin instructions with the uC
292 all availible instructions are defined in the PinHeader
294 def __init__(self, header, pin_id, value=0, time=0):
295 """ constructor for the PinPacket
296 @param header: (PinHeader
or UINT8) the header of the packet
297 @param pin_id: (uint_8) the pin id to be adressed
298 @param value: (uint_8) the value to be sent (optional, default = 0) - 0 to set pin LOW, (>=) 1 to set pin HIGH.
299 @param time: (uint_32) the time of execution of the packet (optional, default = 0)
308 """ getter method for the pin_id attribute
309 @return: the pin_id of the pin
314 """ getter method for the value attribute
315 @return: the value of the pin
320 """ setter method for the header attribute
321 @param header: (PinHeader
or uint8) the header of the packet
323 if (header
in PinHeader):
326 logging.error(
"header "+str(header)+
" is not a valid header")
329 """ setter method for the pin_id attribute
330 @param pin_id: (uint_8) the pin id to be adressed
332 if (pin_id < 55
and pin_id >= 0
and isinstance(pin_id, int)):
335 logging.error(
"di "+str(pin_id)+
" is not a valid unsigned integer of 1 byte + id < 55")
339 """ setter method for the pin value attribute
340 @param value: (uint_8) the value
342 if (value < 2**8
and value >= 0
and isinstance(value, int)):
345 logging.error(
"value "+str(value)+
" is not a valid unsigned integer of 1 byte")
348 """ method to convert the packet to a bytearray in the correct format of <header><exec_time><pin_id><value>
349 @return: the packet
as a bytearray
359 constructs a PinPacket from a bytearray
360 @param byte_array: 9 byte bytearray to construct the Packet
from
362 @raise Exception:
if package construction fails
364 unpacked = struct.unpack("<BIBBBB", byte_array)
366 pin_id = unpacked[2],
372header: (ConfigMainHeader) what's to be configured (e.g. PIN, SPI,...).
373config_header: (ConfigSubHeader) configuration properties.
376 """ The ConfigPacket is used to cumunicate configuration instructions with the uC
377 all availible instructions are defined in the ConfigMainHeader
and ConfigSubHeader
379 def __init__(self, header, config_header, value=0, time=0):
387 """ getter method for the value attribute
393 """ getter method for the sub instruction config_header attribute
394 @return: the config_sub_header
399 """ setter method for the header attribute
400 @param header: (ConfigMainHeader
or uint8) the header of the packet
402 if (header
in ConfigMainHeader):
405 logging.error(
"header "+str(header)+
" is not a valid header")
408 """ setter method for the sub instruction config_header attribute
409 @param config_header: (ConfigSubHeader
or uint8) the config_sub_header
411 if (config_header
in ConfigSubHeader):
414 logging.error(
"config sub header "+str(header)+
" is not a valid header")
417 """ setter method for the value attribute
418 @param value: (uint_8) the value
420 if (value < 2**8
and value >= 0
and isinstance(value, int)):
423 logging.error(
"value "+str(value)+
" is not a valid unsigned integer of 1 byte")
426 """ method to convert the packet to a bytearray in the correct format of <header><exec_time><config_header><value>
427 @return: the packet
as a bytearray
437 constructs a ConfigPacket from a bytearray
438 @param byte_array: 9 byte bytearray to construct the Packet
from
439 @return: ConfigPacket
440 @raise Exception:
if package construction fails
442 unpacked = struct.unpack("<BIBBBB", byte_array)
451 """ The ErrorPacket is used by the uC to send errors to the API
452 all availible errors are defined in the ErrorHeader
454 def __init__(self, header, original_header, value=0, original_sub_header=0, skip_header_matching=False, print_errors=True):
455 """ constructor for the ErrorPacket
456 @param header: (ErrorHeader
or uint8) the header indicating the error
457 @param original_header: (uint_8) the original header of the packet causing the error
458 @param value: (uint_32) the value associated (optional, default = 0)
459 @param original_sub_header: (uint_8) the original sub header causeing the error (optional, default = 0)
in case of a config
460 @param skip_header_matching: (bool)
if the causing headers should be converted into human readable objects (optional, default =
False)
461 @param print_errors: (bool)
if the error should be logged (optional, default =
True)
477 """ getter method for the value attribute
483 """ getter method for the original header attribute
484 @return: the original header causing the error
489 """ getter method for the original sub header attribute
490 @return: the original sub header causing the error
495 """ setter method for the header attribute
496 @param header: (ErrorHeader
or uint8) the header indicating the error
498 if (header
in ErrorHeader):
501 logging.error(
"header "+str(header)+
" is not a valid header")
504 """ setter method for the original header attribute
505 @param header: (uint_8) the original header of the packet causing the error
506 @param skip_header_matching: (bool)
if the causing headers should be converted into human readable objects (optional, default =
False)
508 if skip_header_matching:
527 logging.error(
"source Header "+ str(header) +
" is not a valid header,")
531 """ setter method for the original sub header attribute
532 @param header: (uint_8) the original sub header causeing the error
533 @param skip_header_matching: (bool)
if the causing headers should be converted into human readable objects (optional, default =
False)
535 if skip_header_matching:
557 logging.error(
"source Sub Header "+ str(header) +
" is not a valid header,")
562 """ setter method for the value attribute
563 @param value: (uint_32) the value associated
565 if (value < 2**32
and value >= 0
and isinstance(value, int)):
568 logging.error(
"value "+str(value)+
" is not a valid unsigned integer of 1 byte")
573 """ method to convert the packet to a bytearray in the correct format of <header><exec_time><org_header><value><org_sub_header>
574 @return: the packet
as a bytearray
581 constructs a ErrorPacket from a bytearray
582 @param byte_array: 9 byte bytearray to construct the Packet
from
584 @raise Exception:
if package construction fails
586 unpacked = struct.unpack("<BBIBBB", byte_array)
588 original_header = unpacked[1],
590 original_sub_header = unpacked[3])
ConfigMainHeader are all command headers used in ConfigPacket in conjunction with ConfigSubHeader.
The ConfigPacket is used to cumunicate configuration instructions with the uC all availible instructi...
def config_header(self)
getter method for the sub instruction config_header attribute
def value(self)
getter method for the value attribute
def set_header(self, header)
setter method for the header attribute
def to_bytearray(self)
method to convert the packet to a bytearray in the correct format of <header><exec_time><config_heade...
def from_bytearray(self, byte_array)
constructs a ConfigPacket from a bytearray
def set_value(self, value)
setter method for the value attribute
def set_config_header(self, config_header)
setter method for the sub instruction config_header attribute
def __init__(self, header, config_header, value=0, time=0)
The Data32bitPacket is used to send 32bit data instructions to the uC all availible instructions are ...
def value(self)
getter method for the value attribute
def set_header(self, header)
setter method for the header attribute
def to_bytearray(self)
placeholder for converting the package to a bytearray
def from_bytearray(self, byte_array)
constructs a Data32bitPacket from a bytearray
def set_value(self, value)
def __init__(self, header, value=0, time=0)
constructor for the Data32bitPacket
The DataI2CPacket is used for I2C communication and all types which need 3x 8bit values instread of 1...
def set_read(self, value)
setter method for the 1bit read/write attribute
def value(self)
getter method for the value attribute
def set_header(self, header)
setter method for the header attribute
def set_device_address(self, value)
setter method for the 7bit device address attribute
def to_bytearray(self)
method to convert the packet to a bytearray in the correct format of <header><exec_time><addess+read>...
def device_address(self)
getter method for the 7bit device_address attribute
def read(self)
getter method for the 1bit read/write attribute
def __init__(self, header, device_address, register_address, read=False, value=0, time=0)
def from_bytearray(self, byte_array)
constructs a DataI2CPacket from a bytearray
def register_address(self)
getter method for the 8bit register_address attribute
def set_value(self, value)
setter method for the value attribute
def set_register_address(self, value)
setter method for the 8bit register address attribute
The ErrorPacket is used by the uC to send errors to the API all availible errors are defined in the E...
def original_sub_header(self)
getter method for the original sub header attribute
def __init__(self, header, original_header, value=0, original_sub_header=0, skip_header_matching=False, print_errors=True)
constructor for the ErrorPacket
def set_org_header(self, header, skip_header_matching=False)
setter method for the original header attribute
def value(self)
getter method for the value attribute
def set_header(self, header)
setter method for the header attribute
def set_org_sub_header(self, header, skip_header_matching=False)
setter method for the original sub header attribute
def to_bytearray(self)
method to convert the packet to a bytearray in the correct format of <header><exec_time><org_header><...
def from_bytearray(self, byte_array)
constructs a ErrorPacket from a bytearray
def original_header(self)
getter method for the original header attribute
def set_value(self, value)
setter method for the value attribute
Packet class to be used as a base class for all packets acting as an interface.
def set_header(self, header)
setter method for the header attribute
def to_bytearray(self)
placeholder for converting the package to a bytearray
def check_and_log(self)
check_and_log logs the packet if the header is in the logging lists
def time(self)
getter method for the time attribute
def from_bytearray(cls, byte_array)
method to construct a packet from a bytearray depending on the header the correct packet type is retu...
def set_exec_time(self, time)
setter method for the execution time attribute
The PinPacket is used to comunicate pin instructions with the uC all availible instructions are defin...
def pin_id(self)
getter method for the pin_id attribute
def set_pin_id(self, pin_id)
setter method for the pin_id attribute
def value(self)
getter method for the value attribute
def __init__(self, header, pin_id, value=0, time=0)
constructor for the PinPacket
def set_header(self, header)
setter method for the header attribute
def to_bytearray(self)
method to convert the packet to a bytearray in the correct format of <header><exec_time><pin_id>
def from_bytearray(self, byte_array)
constructs a PinPacket from a bytearray
def set_value(self, value)
setter method for the pin value attribute