@@ -33,50 +33,65 @@ def get_bits_from_int(val_int, val_size=16):
3333int2bits = get_bits_from_int
3434
3535
36- #########################
37- # floating-point function
38- #########################
39- def decode_ieee (val_int , double = False ):
40- """Decode Python int (32 bits integer) as an IEEE single or double precision format
36+ def test_bit (value , offset ):
37+ """Test a bit at offset position
4138
42- Support NaN.
39+ :param value: value of integer to test
40+ :type value: int
41+ :param offset: bit offset (0 is lsb)
42+ :type offset: int
43+ :returns: value of bit at offset position
44+ :rtype: bool
45+ """
46+ mask = 1 << offset
47+ return bool (value & mask )
4348
44- :param val_int: a 32 or 64 bits integer as an int Python value
45- :type val_int: int
46- :param double: set to decode as a 64 bits double precision,
47- default is 32 bits single (optional)
48- :type double: bool
49- :returns: float result
50- :rtype: float
49+
50+ def set_bit (value , offset ):
51+ """Set a bit at offset position
52+
53+ :param value: value of integer where set the bit
54+ :type value: int
55+ :param offset: bit offset (0 is lsb)
56+ :type offset: int
57+ :returns: value of integer with bit set
58+ :rtype: int
5159 """
52- if double :
53- return struct .unpack ("d" , struct .pack ("Q" , val_int ))[0 ]
54- else :
55- return struct .unpack ("f" , struct .pack ("I" , val_int ))[0 ]
60+ mask = 1 << offset
61+ return int (value | mask )
5662
5763
58- def encode_ieee ( val_float , double = False ):
59- """Encode Python float to int (32 bits integer) as an IEEE single or double precision format
64+ def reset_bit ( value , offset ):
65+ """Reset a bit at offset position
6066
61- Support NaN.
67+ :param value: value of integer where reset the bit
68+ :type value: int
69+ :param offset: bit offset (0 is lsb)
70+ :type offset: int
71+ :returns: value of integer with bit reset
72+ :rtype: int
73+ """
74+ mask = ~ (1 << offset )
75+ return int (value & mask )
6276
63- :param val_float: float value to convert
64- :type val_float: float
65- :param double: set to encode as a 64 bits double precision,
66- default is 32 bits single (optional)
67- :type double: bool
68- :returns: IEEE 32 bits (single precision) as Python int
69- :rtype: int
77+
78+ def toggle_bit (value , offset ):
79+ """Return an integer with the bit at offset position inverted
80+
81+ :param value: value of integer where invert the bit
82+ :type value: int
83+ :param offset: bit offset (0 is lsb)
84+ :type offset: int
85+ :returns: value of integer with bit inverted
86+ :rtype: int
7087 """
71- if double :
72- return struct .unpack ("Q" , struct .pack ("d" , val_float ))[0 ]
73- else :
74- return struct .unpack ("I" , struct .pack ("f" , val_float ))[0 ]
88+ mask = 1 << offset
89+ return int (value ^ mask )
7590
7691
77- ################################
78- # long format (32 bits) function
79- ################################
92+ ########################
93+ # Word convert functions
94+ ########################
8095def word_list_to_long (val_list , big_endian = True ):
8196 """Word list (16 bits int) to long list (32 bits int)
8297
@@ -135,9 +150,9 @@ def long_list_to_word(val_list, big_endian=True):
135150longs2words = long_list_to_word
136151
137152
138- #########################################################
139- # 2's complement of int value (scalar and list) functions
140- #########################################################
153+ ##########################
154+ # 2's complement functions
155+ ##########################
141156def get_2comp (val_int , val_size = 16 ):
142157 """Get the 2's complement of Python int val_int
143158
@@ -184,9 +199,50 @@ def get_list_2comp(val_list, val_size=16):
184199twos_c_l = get_list_2comp
185200
186201
187- ######################
188- # compute CRC of frame
189- ######################
202+ ###############################
203+ # IEEE floating-point functions
204+ ###############################
205+ def decode_ieee (val_int , double = False ):
206+ """Decode Python int (32 bits integer) as an IEEE single or double precision format
207+
208+ Support NaN.
209+
210+ :param val_int: a 32 or 64 bits integer as an int Python value
211+ :type val_int: int
212+ :param double: set to decode as a 64 bits double precision,
213+ default is 32 bits single (optional)
214+ :type double: bool
215+ :returns: float result
216+ :rtype: float
217+ """
218+ if double :
219+ return struct .unpack ("d" , struct .pack ("Q" , val_int ))[0 ]
220+ else :
221+ return struct .unpack ("f" , struct .pack ("I" , val_int ))[0 ]
222+
223+
224+ def encode_ieee (val_float , double = False ):
225+ """Encode Python float to int (32 bits integer) as an IEEE single or double precision format
226+
227+ Support NaN.
228+
229+ :param val_float: float value to convert
230+ :type val_float: float
231+ :param double: set to encode as a 64 bits double precision,
232+ default is 32 bits single (optional)
233+ :type double: bool
234+ :returns: IEEE 32 bits (single precision) as Python int
235+ :rtype: int
236+ """
237+ if double :
238+ return struct .unpack ("Q" , struct .pack ("d" , val_float ))[0 ]
239+ else :
240+ return struct .unpack ("I" , struct .pack ("f" , val_float ))[0 ]
241+
242+
243+ ################
244+ # misc functions
245+ ################
190246def crc16 (frame ):
191247 """Compute CRC16
192248
@@ -205,62 +261,3 @@ def crc16(frame):
205261 if lsb :
206262 crc ^= 0xA001
207263 return crc
208-
209-
210- ####################
211- # misc bit functions
212- ####################
213- def test_bit (value , offset ):
214- """Test a bit at offset position
215-
216- :param value: value of integer to test
217- :type value: int
218- :param offset: bit offset (0 is lsb)
219- :type offset: int
220- :returns: value of bit at offset position
221- :rtype: bool
222- """
223- mask = 1 << offset
224- return bool (value & mask )
225-
226-
227- def set_bit (value , offset ):
228- """Set a bit at offset position
229-
230- :param value: value of integer where set the bit
231- :type value: int
232- :param offset: bit offset (0 is lsb)
233- :type offset: int
234- :returns: value of integer with bit set
235- :rtype: int
236- """
237- mask = 1 << offset
238- return int (value | mask )
239-
240-
241- def reset_bit (value , offset ):
242- """Reset a bit at offset position
243-
244- :param value: value of integer where reset the bit
245- :type value: int
246- :param offset: bit offset (0 is lsb)
247- :type offset: int
248- :returns: value of integer with bit reset
249- :rtype: int
250- """
251- mask = ~ (1 << offset )
252- return int (value & mask )
253-
254-
255- def toggle_bit (value , offset ):
256- """Return an integer with the bit at offset position inverted
257-
258- :param value: value of integer where invert the bit
259- :type value: int
260- :param offset: bit offset (0 is lsb)
261- :type offset: int
262- :returns: value of integer with bit inverted
263- :rtype: int
264- """
265- mask = 1 << offset
266- return int (value ^ mask )
0 commit comments