2014-06-28 00:03:47 +00:00
|
|
|
import uctypes
|
|
|
|
|
|
|
|
desc = {
|
|
|
|
"s0": uctypes.UINT16 | 0,
|
|
|
|
"sub": (0, {
|
|
|
|
"b0": uctypes.UINT8 | 0,
|
|
|
|
"b1": uctypes.UINT8 | 1,
|
|
|
|
}),
|
|
|
|
"arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2),
|
|
|
|
"arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}),
|
2014-07-10 21:06:36 +00:00
|
|
|
"bitf0": uctypes.BFUINT16 | 0 | 0 << uctypes.BF_POS | 8 << uctypes.BF_LEN,
|
|
|
|
"bitf1": uctypes.BFUINT16 | 0 | 8 << uctypes.BF_POS | 8 << uctypes.BF_LEN,
|
2014-06-28 00:03:47 +00:00
|
|
|
|
2014-07-10 21:06:36 +00:00
|
|
|
"bf0": uctypes.BFUINT16 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
|
|
|
|
"bf1": uctypes.BFUINT16 | 0 | 4 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
|
|
|
|
"bf2": uctypes.BFUINT16 | 0 | 8 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
|
|
|
|
"bf3": uctypes.BFUINT16 | 0 | 12 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
|
2014-06-28 00:03:47 +00:00
|
|
|
|
|
|
|
"ptr": (uctypes.PTR | 0, uctypes.UINT8),
|
|
|
|
"ptr2": (uctypes.PTR | 0, {"b": uctypes.UINT8 | 0}),
|
|
|
|
}
|
|
|
|
|
|
|
|
data = bytearray(b"01")
|
|
|
|
|
2015-06-06 19:57:31 +00:00
|
|
|
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)
|
2014-06-28 00:03:47 +00:00
|
|
|
|
|
|
|
#print(S)
|
|
|
|
print(hex(S.s0))
|
|
|
|
assert hex(S.s0) == "0x3130"
|
|
|
|
#print(S.sub.b0)
|
|
|
|
print(S.sub.b0, S.sub.b1)
|
|
|
|
assert S.sub.b0, S.sub.b1 == (0x30, 0x31)
|
|
|
|
|
|
|
|
try:
|
|
|
|
S[0]
|
|
|
|
assert False, "Can't index struct"
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
|
|
|
print("arr:", S.arr[0], S.arr[1])
|
|
|
|
assert (S.arr[0], S.arr[1]) == (0x30, 0x31)
|
|
|
|
|
|
|
|
print("arr of struct:", S.arr2[0].b, S.arr2[1].b)
|
|
|
|
assert (S.arr2[0].b, S.arr2[1].b) == (0x30, 0x31)
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
S.arr[2]
|
|
|
|
assert False, "Out of bounds index"
|
|
|
|
except IndexError:
|
|
|
|
print("IndexError")
|
|
|
|
|
|
|
|
print("bf:", S.bitf0, S.bitf1)
|
|
|
|
assert (S.bitf0, S.bitf1) == (0x30, 0x31)
|
|
|
|
|
|
|
|
print("bf 4bit:", S.bf3, S.bf2, S.bf1, S.bf0)
|
|
|
|
assert (S.bf3, S.bf2, S.bf1, S.bf0) == (3, 1, 3, 0)
|
|
|
|
|
|
|
|
|
|
|
|
# Write access
|
|
|
|
|
|
|
|
S.sub.b0 = ord("2")
|
|
|
|
print(data)
|
|
|
|
assert bytes(data) == b"21"
|
|
|
|
|
|
|
|
S.bf3 = 5
|
|
|
|
print(data)
|
|
|
|
assert bytes(data) == b"2Q"
|