digi.xbee.util.utils module¶
-
digi.xbee.util.utils.
is_bit_enabled
(number, position)[source]¶ Returns whether the bit located at position within number is enabled.
Parameters: - number (Integer) – the number to check if a bit is enabled.
- position (Integer) – the position of the bit to check if is enabled in number.
Returns: - True if the bit located at position within number is
enabled, False otherwise.
Return type: Boolean
-
digi.xbee.util.utils.
get_int_from_byte
(number, offset, length)[source]¶ Reads an integer value from the given byte using the provived bit offset and length.
Parameters: - number (Integer) – Byte to read the integer from.
- offset (Integer) – Bit offset inside the byte to start reading (LSB = 0, MSB = 7).
- length (Integer) – Number of bits to read.
Returns: The integer value read.
Return type: Integer
Raises: ValueError
– If number is lower than 0 or higher than 255. If `offset is lower than 0 or higher than 7. If length is lower than 0 or higher than 8. If offset + length is higher than 8.
-
digi.xbee.util.utils.
hex_string_to_bytes
(hex_string)[source]¶ Converts a String (composed by hex. digits) into a bytearray with same digits.
Parameters: hex_string (String) – String (made by hex. digits) with “0x” header or not. Returns: bytearray containing the numeric value of the hexadecimal digits. Return type: Bytearray Raises: ValueError
– if invalid literal for int() with base 16 is provided.Example
>>> a = "0xFFFE" >>> for i in hex_string_to_bytes(a): print(i) 255 254 >>> print(type(hex_string_to_bytes(a))) <type 'bytearray'>
>>> b = "FFFE" >>> for i in hex_string_to_bytes(b): print(i) 255 254 >>> print(type(hex_string_to_bytes(b))) <type 'bytearray'>
-
digi.xbee.util.utils.
int_to_bytes
(number, num_bytes=None)[source]¶ Converts the provided integer into a bytearray.
If number has less bytes than num_bytes, the resultant bytearray is filled with zeros (0x00) starting at the beginning.
If number has more bytes than num_bytes, the resultant bytearray is returned without changes.
Parameters: - number (Integer) – the number to convert to a bytearray.
- num_bytes (Integer) – the number of bytes that the resultant bytearray will have.
Returns: the bytearray corresponding to the provided number.
Return type: Bytearray
Example
>>> a=0xFFFE >>> print([i for i in int_to_bytes(a)]) [255,254] >>> print(type(int_to_bytes(a))) <type 'bytearray'>
-
digi.xbee.util.utils.
length_to_int
(byte_array)[source]¶ Calculates the length value for the given length field of a packet. Length field are bytes 1 and 2 of any packet.
Parameters: byte_array (Bytearray) – length field of a packet. Returns: the length value. Return type: Integer Raises: ValueError
– if byte_array is not a valid length field (it has length distinct than 0).Example
>>> b = bytearray([13,14]) >>> c = length_to_int(b) >>> print("0x%02X" % c) 0x1314 >>> print(c) 4884
-
digi.xbee.util.utils.
bytes_to_int
(byte_array)[source]¶ Converts the provided bytearray in an Integer. This integer is result of concatenate all components of byte_array and convert that hex number to a decimal number.
Parameters: byte_array (Bytearray) – bytearray to convert in integer. Returns: the integer corresponding to the provided bytearray. Return type: Integer Example
>>> x = bytearray([0xA,0x0A,0x0A]) #this is 0xA0A0A >>> print(bytes_to_int(x)) 657930 >>> b = bytearray([0x0A,0xAA]) #this is 0xAAA >>> print(bytes_to_int(b)) 2730
-
digi.xbee.util.utils.
ascii_to_int
(array)[source]¶ Converts a bytearray containing the ASCII code of each number digit in an Integer. This integer is result of the number formed by all ASCII codes of the bytearray.
Parameters: array (Bytearray) – bytearray to convert in integer. Example
>>> x = bytearray( [0x31,0x30,0x30] ) #0x31 => ASCII code for number 1. #0x31,0x30,0x30 <==> 1,0,0 >>> print(ascii_to_int(x)) 100
-
digi.xbee.util.utils.
int_to_ascii
(number)[source]¶ Converts an integer number to a bytearray. Each element of the bytearray is the ASCII code that corresponds to the digit of its position.
Parameters: number (Integer) – the number to convert to an ASCII bytearray. Returns: the bytearray containing the ASCII value of each digit of the number. Return type: Bytearray Example
>>> x = int_to_ascii(100) >>> print(x) 100 >>> print([i for i in x]) [49, 48, 48]
-
digi.xbee.util.utils.
int_to_length
(number)[source]¶ Converts an integer into a bytearray of 2 bytes corresponding to the length field of a packet. If this bytearray has length 1, a byte with value 0 is added at the beginning.
Parameters: number (Integer) – the number to convert to a length field. Returns: The bytearray. Return type: Bytearray Raises: ValueError
– if number is less than 0 or greater than 0xFFFF.Example
>>> a = 0 >>> print(hex_to_string(int_to_length(a))) 00 00
>>> a = 8 >>> print(hex_to_string(int_to_length(a))) 00 08
>>> a = 200 >>> print(hex_to_string(int_to_length(a))) 00 C8
>>> a = 0xFF00 >>> print(hex_to_string(int_to_length(a))) FF 00
>>> a = 0xFF >>> print(hex_to_string(int_to_length(a))) 00 FF
-
digi.xbee.util.utils.
hex_to_string
(byte_array, pretty=True)[source]¶ Returns the provided bytearray in a pretty string format. All bytes are separated by blank spaces and printed in hex format.
Parameters: - byte_array (Bytearray) – the bytearray to print in pretty string.
- pretty (Boolean, optional) – True for pretty string format, False for plain string format. Default to True.
Returns: the bytearray formatted in a string format.
Return type: String
-
digi.xbee.util.utils.
doc_enum
(enum_class, descriptions=None)[source]¶ Returns a string with the description of each value of an enumeration.
Parameters: - enum_class (Enumeration) – the Enumeration to get its values documentation.
- descriptions (dictionary) – each enumeration’s item description. The key is the enumeration element name and the value is the description.
Returns: the string listing all the enumeration values and their descriptions.
Return type: String
-
digi.xbee.util.utils.
enable_logger
(name, level=10)[source]¶ Enables a logger with the given name and level.
Parameters: - name (String) – name of the logger to enable.
- level (Integer) – logging level value.
Assigns a default formatter and a default handler (for console).
-
digi.xbee.util.utils.
disable_logger
(name)[source]¶ Disables the logger with the give name.
Parameters: name (String) – the name of the logger to disable.
-
digi.xbee.util.utils.
deprecated
(version, details='None')[source]¶ Decorates a method to mark as deprecated. This adds a deprecation note to the method docstring and also raises a
warning.DeprecationWarning
.Parameters: - version (String) – Version that deprecates this feature.
- details (String, optional, default=`None`) – Extra details to be added to the method docstring and warning.