finished adding 48 bit int support
This commit is contained in:
+19
-21
@@ -13,9 +13,6 @@ print(BitArray(test).bin[:1])
|
|||||||
def chunker(seq, size):
|
def chunker(seq, size):
|
||||||
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
|
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
|
||||||
|
|
||||||
test = [16, -12, 1, 0, 3, 2, 999,0, -1277, -1, 1, 0]
|
|
||||||
# accelerom_string = b"".join([struct.pack("h", x) for x in test])
|
|
||||||
# print(b"".join([struct.pack("h", x) for x in test]))
|
|
||||||
def test_bin_float_converstion():
|
def test_bin_float_converstion():
|
||||||
val = 100
|
val = 100
|
||||||
test = struct.pack(">f", val)
|
test = struct.pack(">f", val)
|
||||||
@@ -27,22 +24,28 @@ def test_bin_float_converstion():
|
|||||||
print(struct.unpack("f", test))
|
print(struct.unpack("f", test))
|
||||||
|
|
||||||
def test_bin_int_converstion():
|
def test_bin_int_converstion():
|
||||||
|
print()
|
||||||
val = 100
|
val = 100
|
||||||
test = struct.pack(">i", val)
|
print("VAL IS ", val)
|
||||||
|
test = struct.pack("i", val)
|
||||||
binary = float_to_bin(val)
|
binary = float_to_bin(val)
|
||||||
print(binary)
|
print(binary)
|
||||||
print(BinToInt().process(test))
|
print(BinToInt().process(test))
|
||||||
print(struct.unpack(">i", test))
|
|
||||||
print(BinToInt().process(test, True))
|
|
||||||
print(struct.unpack("i", test))
|
print(struct.unpack("i", test))
|
||||||
|
print(BinToInt().process(test, True))
|
||||||
|
print(struct.unpack(">i", test))
|
||||||
|
|
||||||
|
print()
|
||||||
|
|
||||||
val = -100
|
val = -100
|
||||||
test = struct.pack(">i", val)
|
print("VAL IS ", val)
|
||||||
|
test = struct.pack("i", val)
|
||||||
binary = float_to_bin(val)
|
binary = float_to_bin(val)
|
||||||
print(binary)
|
print(binary)
|
||||||
print(BinToInt().process(test))
|
print(BinToInt().process(test))
|
||||||
print(struct.unpack(">i", test))
|
|
||||||
print(BinToInt().process(test, True))
|
|
||||||
print(struct.unpack("i", test))
|
print(struct.unpack("i", test))
|
||||||
|
print(BinToInt().process(test, True))
|
||||||
|
print(struct.unpack(">i", test))
|
||||||
|
|
||||||
|
|
||||||
def float_to_bin(num):
|
def float_to_bin(num):
|
||||||
@@ -51,23 +54,18 @@ def float_to_bin(num):
|
|||||||
|
|
||||||
def convert_row_to_bytes(row):
|
def convert_row_to_bytes(row):
|
||||||
row = b"".join([struct.pack("h", x) for x in row])
|
row = b"".join([struct.pack("h", x) for x in row])
|
||||||
b2f = BinToFloat(15, 32)
|
# b2f = BinToFloat(15, 32)
|
||||||
print([b2f.process(x) for x in chunker(row, 6)])
|
b2i = BinToInt(48)
|
||||||
|
print([b2i.process(x, True) for x in chunker(row, 6)])
|
||||||
# structure = "q"
|
|
||||||
# i = 8
|
|
||||||
# print(len(accelerom_string))
|
|
||||||
# print(struct.unpack(structure, accelerom_string[:i]), struct.unpack(structure, accelerom_string[i:2*i]),
|
|
||||||
# struct.unpack(structure, accelerom_string[2*i:]))
|
|
||||||
|
|
||||||
|
|
||||||
data = pd.read_csv("accelerometer.data")
|
data = pd.read_csv("accelerometer.data")
|
||||||
print(data)
|
print(data)
|
||||||
|
|
||||||
test_bin_float_converstion()
|
# test_bin_float_converstion()
|
||||||
test_bin_int_converstion()
|
# test_bin_int_converstion()
|
||||||
|
|
||||||
# for index, row in data.iterrows():
|
for index, row in data.iterrows():
|
||||||
# convert_row_to_bytes(row)
|
convert_row_to_bytes(row)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+24
-13
@@ -12,30 +12,41 @@ class BinToInt(object):
|
|||||||
|
|
||||||
def process(self, bin_string, reverse_marshalling=False):
|
def process(self, bin_string, reverse_marshalling=False):
|
||||||
var = BitArray(bin_string)
|
var = BitArray(bin_string)
|
||||||
print(bin_string)
|
|
||||||
chunked_bytes = [x for x in chunker(var, 8)]
|
chunked_bytes = [x for x in chunker(var, 8)]
|
||||||
ordered_bytes = []
|
ordered_bytes = []
|
||||||
for group in reversed(chunked_bytes) if reverse_marshalling else chunked_bytes:
|
for group in chunked_bytes if reverse_marshalling else reversed(chunked_bytes):
|
||||||
ordered_bytes.extend(group)
|
ordered_bytes.extend(group)
|
||||||
ordered_bytes = [not x for x in reversed(ordered_bytes)]
|
ordered_bytes = [x for x in reversed(ordered_bytes)]
|
||||||
# print(
|
|
||||||
# "".join(["1" if x else "0" for x in ordered_bytes[0:1]]),
|
|
||||||
# "".join(["1" if x else "0" for x in ordered_bytes[1:1+self.exponent]]),
|
|
||||||
# "".join(["1" if x else "0" for x in ordered_bytes[1+self.exponent:1+self.exponent+self.mantissa]])
|
|
||||||
# )
|
|
||||||
# sign =
|
|
||||||
# return -val if sign else val
|
|
||||||
|
|
||||||
print([1 if x else 0 for x in ordered_bytes[:-1]])
|
|
||||||
if self.signed:
|
if self.signed:
|
||||||
return self.convert_pos_int(ordered_bytes[:-1]) * (1 if False else -1)
|
negative = ordered_bytes[-1]
|
||||||
|
else:
|
||||||
|
negative = False
|
||||||
|
|
||||||
|
# print([1 if x else 0 for x in ordered_bytes[:]])
|
||||||
|
if self.signed:
|
||||||
|
return self.convert_pos_int(ordered_bytes[:], negative)
|
||||||
else:
|
else:
|
||||||
return self.convert_pos_int(ordered_bytes)
|
return self.convert_pos_int(ordered_bytes)
|
||||||
|
|
||||||
def convert_pos_int(self, val):
|
def convert_unsigned_int(self, val):
|
||||||
total_val = 0
|
total_val = 0
|
||||||
digit_val = 1
|
digit_val = 1
|
||||||
for i in val:
|
for i in val:
|
||||||
total_val += digit_val if i else 0
|
total_val += digit_val if i else 0
|
||||||
digit_val *= 2
|
digit_val *= 2
|
||||||
return total_val
|
return total_val
|
||||||
|
|
||||||
|
def convert_pos_int(self, val, negative):
|
||||||
|
if negative:
|
||||||
|
val = [not x for x in val]
|
||||||
|
self.add_one_to_binary_int(val)
|
||||||
|
return self.convert_unsigned_int(val) * (-1 if negative else 1)
|
||||||
|
|
||||||
|
def add_one_to_binary_int(self, binary_list):
|
||||||
|
for i in range(len(binary_list)):
|
||||||
|
if binary_list[i]:
|
||||||
|
binary_list[i] = 0
|
||||||
|
else:
|
||||||
|
binary_list[i] = 1
|
||||||
|
break
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ ACCELEROMETER = 3
|
|||||||
BATTERY = 4
|
BATTERY = 4
|
||||||
TEMPERATURE = 5
|
TEMPERATURE = 5
|
||||||
|
|
||||||
|
INT_DECODER =
|
||||||
|
|
||||||
CHARACTERISTIC_UUIDS = {
|
CHARACTERISTIC_UUIDS = {
|
||||||
# ("64a7000d-f691-4b93-a6f4-0968f5b648f8"):BUTTON,#Button
|
# ("64a7000d-f691-4b93-a6f4-0968f5b648f8"):BUTTON,#Button
|
||||||
# ("64a7000a-f691-4b93-a6f4-0968f5b648f8"):GYROSCOPE,#9 axis
|
# ("64a7000a-f691-4b93-a6f4-0968f5b648f8"):GYROSCOPE,#9 axis
|
||||||
@@ -60,18 +62,10 @@ def notification_handler(sender, data):
|
|||||||
print(f"BUTTON PRESSED {data}")
|
print(f"BUTTON PRESSED {data}")
|
||||||
decode_button(data)
|
decode_button(data)
|
||||||
elif sender == GYROSCOPE:
|
elif sender == GYROSCOPE:
|
||||||
# print(f"GYRO CHANGED - {data}")
|
|
||||||
print(BitArray(data).bin[:1])
|
print(BitArray(data).bin[:1])
|
||||||
elif sender == ACCELEROMETER:
|
elif sender == ACCELEROMETER:
|
||||||
# print(f"ACCEL CHANGED - {data}")
|
|
||||||
# print(BitArray(data).bin[:1])
|
|
||||||
# print(struct.unpack("h",data[:2]))
|
|
||||||
s = Struct("h")
|
s = Struct("h")
|
||||||
print([x for x in s.iter_unpack(data)])
|
print([x for x in s.iter_unpack(data)])
|
||||||
# print(struct.unpack("e",data[:2]))
|
|
||||||
# print(struct.unpack("f", data[:4]))
|
|
||||||
|
|
||||||
# decode_accelerometer(data)
|
|
||||||
elif sender == BATTERY:
|
elif sender == BATTERY:
|
||||||
decode_battery(data)
|
decode_battery(data)
|
||||||
elif sender == TEMPERATURE:
|
elif sender == TEMPERATURE:
|
||||||
@@ -108,7 +102,6 @@ async def run(address, loop, debug=False):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import os
|
import os
|
||||||
|
|
||||||
os.environ["PYTHONASYNCIODEBUG"] = str(1)
|
os.environ["PYTHONASYNCIODEBUG"] = str(1)
|
||||||
address = (
|
address = (
|
||||||
device_address # <--- Change to your device's address here if you are using Windows or Linux
|
device_address # <--- Change to your device's address here if you are using Windows or Linux
|
||||||
|
|||||||
@@ -1,58 +0,0 @@
|
|||||||
import asyncio
|
|
||||||
from bleak import discover
|
|
||||||
|
|
||||||
from pprint import pprint
|
|
||||||
|
|
||||||
|
|
||||||
async def run():
|
|
||||||
devices = await discover()
|
|
||||||
for d in devices:
|
|
||||||
print(d)
|
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
loop.run_until_complete(run())
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
from bleak import BleakClient
|
|
||||||
|
|
||||||
address = "e3:ae:cd:af:28:e2"
|
|
||||||
MODEL_NBR_UUID = "64a7000f-f691-4b93-a6f4-0968f5b648f8"
|
|
||||||
|
|
||||||
async def run(address, loop):
|
|
||||||
async with BleakClient(address, loop=loop) as client:
|
|
||||||
# await client.connect()
|
|
||||||
print(await client.is_connected())
|
|
||||||
print(await client.get_services())
|
|
||||||
services = await client.get_services()
|
|
||||||
pprint(services.descriptors)
|
|
||||||
pprint(services.characteristics)
|
|
||||||
pprint(services.services)
|
|
||||||
# print(services.descriptors)
|
|
||||||
# for key, val in services.descriptors.items():
|
|
||||||
# print(f"{key} + {val}")
|
|
||||||
#
|
|
||||||
# print(services.characteristics)
|
|
||||||
# for key, val in services.characteristics.items():
|
|
||||||
# print(f"{key} + {val}")
|
|
||||||
|
|
||||||
print(services)
|
|
||||||
for x in services:
|
|
||||||
print(x)
|
|
||||||
for characteristic in x.characteristics:
|
|
||||||
print("")
|
|
||||||
print(characteristic)
|
|
||||||
print(characteristic.properties)
|
|
||||||
for descriptor in characteristic.descriptors:
|
|
||||||
print(descriptor)
|
|
||||||
print(x.description)
|
|
||||||
# for i in range(10):
|
|
||||||
# x = await client.read_gatt_descriptor(i)
|
|
||||||
# print(x)
|
|
||||||
|
|
||||||
# model_number = await client.read_gatt_char()
|
|
||||||
# print(model_number)
|
|
||||||
# print("Model Number: {0}".format("".join(map(chr, model_number))))
|
|
||||||
|
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
loop.run_until_complete(run(address, loop))
|
|
||||||
Reference in New Issue
Block a user