From e6be28f28b70ab7243dfece6db6a3e868806e21b Mon Sep 17 00:00:00 2001 From: Lucas Oskorep Date: Thu, 7 Nov 2019 13:14:04 -0600 Subject: [PATCH] finished adding 48 bit int support --- byteDecodingPlayground.py | 40 ++++++++++++------------- converters/BinToInt.py | 37 +++++++++++++++-------- subscribe_to_notification.py | 11 ++----- test2.py | 58 ------------------------------------ 4 files changed, 45 insertions(+), 101 deletions(-) delete mode 100644 test2.py diff --git a/byteDecodingPlayground.py b/byteDecodingPlayground.py index f930081..eccf973 100644 --- a/byteDecodingPlayground.py +++ b/byteDecodingPlayground.py @@ -13,9 +13,6 @@ print(BitArray(test).bin[:1]) def chunker(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(): val = 100 test = struct.pack(">f", val) @@ -27,22 +24,28 @@ def test_bin_float_converstion(): print(struct.unpack("f", test)) def test_bin_int_converstion(): + print() val = 100 - test = struct.pack(">i", val) + print("VAL IS ", val) + test = struct.pack("i", val) binary = float_to_bin(val) print(binary) print(BinToInt().process(test)) - print(struct.unpack(">i", test)) - print(BinToInt().process(test, True)) print(struct.unpack("i", test)) + print(BinToInt().process(test, True)) + print(struct.unpack(">i", test)) + + print() + val = -100 - test = struct.pack(">i", val) + print("VAL IS ", val) + test = struct.pack("i", val) binary = float_to_bin(val) print(binary) print(BinToInt().process(test)) - print(struct.unpack(">i", test)) - print(BinToInt().process(test, True)) print(struct.unpack("i", test)) + print(BinToInt().process(test, True)) + print(struct.unpack(">i", test)) def float_to_bin(num): @@ -51,23 +54,18 @@ def float_to_bin(num): def convert_row_to_bytes(row): row = b"".join([struct.pack("h", x) for x in row]) - b2f = BinToFloat(15, 32) - print([b2f.process(x) 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:])) + # b2f = BinToFloat(15, 32) + b2i = BinToInt(48) + print([b2i.process(x, True) for x in chunker(row, 6)]) data = pd.read_csv("accelerometer.data") print(data) -test_bin_float_converstion() -test_bin_int_converstion() +# test_bin_float_converstion() +# test_bin_int_converstion() -# for index, row in data.iterrows(): -# convert_row_to_bytes(row) +for index, row in data.iterrows(): + convert_row_to_bytes(row) diff --git a/converters/BinToInt.py b/converters/BinToInt.py index 8c514bd..10f349d 100644 --- a/converters/BinToInt.py +++ b/converters/BinToInt.py @@ -12,30 +12,41 @@ class BinToInt(object): def process(self, bin_string, reverse_marshalling=False): var = BitArray(bin_string) - print(bin_string) chunked_bytes = [x for x in chunker(var, 8)] 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 = [not 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 + ordered_bytes = [x for x in reversed(ordered_bytes)] - print([1 if x else 0 for x in ordered_bytes[:-1]]) 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: return self.convert_pos_int(ordered_bytes) - def convert_pos_int(self, val): + def convert_unsigned_int(self, val): total_val = 0 digit_val = 1 for i in val: total_val += digit_val if i else 0 digit_val *= 2 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 diff --git a/subscribe_to_notification.py b/subscribe_to_notification.py index f306e99..eab704e 100644 --- a/subscribe_to_notification.py +++ b/subscribe_to_notification.py @@ -20,6 +20,8 @@ ACCELEROMETER = 3 BATTERY = 4 TEMPERATURE = 5 +INT_DECODER = + CHARACTERISTIC_UUIDS = { # ("64a7000d-f691-4b93-a6f4-0968f5b648f8"):BUTTON,#Button # ("64a7000a-f691-4b93-a6f4-0968f5b648f8"):GYROSCOPE,#9 axis @@ -60,18 +62,10 @@ def notification_handler(sender, data): print(f"BUTTON PRESSED {data}") decode_button(data) elif sender == GYROSCOPE: - # print(f"GYRO CHANGED - {data}") print(BitArray(data).bin[:1]) elif sender == ACCELEROMETER: - # print(f"ACCEL CHANGED - {data}") - # print(BitArray(data).bin[:1]) - # print(struct.unpack("h",data[:2])) s = Struct("h") 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: decode_battery(data) elif sender == TEMPERATURE: @@ -108,7 +102,6 @@ async def run(address, loop, debug=False): if __name__ == "__main__": import os - os.environ["PYTHONASYNCIODEBUG"] = str(1) address = ( device_address # <--- Change to your device's address here if you are using Windows or Linux diff --git a/test2.py b/test2.py deleted file mode 100644 index c85ebb6..0000000 --- a/test2.py +++ /dev/null @@ -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)) \ No newline at end of file