Repository navigation
Expand file tree
/
Copy pathFun.py
More file actions
56 lines (46 loc) · 1.38 KB
/
Copy pathFun.py
File metadata and controls
56 lines (46 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import struct
def get_bool(_bytearray, byte_index, bool_index):
"""
Get the boolean value from location in bytearray
"""
index_value = 1 << bool_index
byte_value = _bytearray[byte_index]
current_value = byte_value & index_value
return current_value == index_value
def set_real(_bytearray, byte_index, real):
"""
Set Real value
make 4 byte data from real
"""
real = float(real)
real = struct.pack('>f', real)
_bytes = struct.unpack('4B', real)
for i, b in enumerate(_bytes):
_bytearray[byte_index + i] = b
def get_real(_bytearray, byte_index):
"""
Get real value. create float from 4 bytes
"""
x = _bytearray[byte_index:byte_index + 4]
real = struct.unpack('>f', struct.pack('4B', *x))[0]
return real
def get_int(bytearray_, byte_index):
"""
Get int value from bytearray.
int are represented in two bytes
"""
data = bytearray_[byte_index:byte_index + 2]
data[1] = data[1] & 0xff
data[0] = data[0] & 0xff
packed = struct.pack('2B', *data)
value = struct.unpack('>h', packed)[0]
return value
def set_int(bytearray_, byte_index, _int):
"""
Set value in bytearray to int
"""
# make sure were dealing with an int
_int = int(_int)
_bytes = struct.unpack('2B', struct.pack('>h', _int))
bytearray_[byte_index:byte_index + 2] = _bytes
return bytearray_