From 5bb0b759410c380aa67cae6e65b70b1e4a5dda67 Mon Sep 17 00:00:00 2001 From: Lucas Oskorep Date: Thu, 7 Nov 2019 16:42:22 -0600 Subject: [PATCH] code cleanup --- byteDecodingPlayground.py | 6 ++---- converters/BinToFloat.py | 9 --------- subscribe_to_notification.py | 34 ++++++++++++++++++++++------------ 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/byteDecodingPlayground.py b/byteDecodingPlayground.py index eccf973..0e23cec 100644 --- a/byteDecodingPlayground.py +++ b/byteDecodingPlayground.py @@ -54,14 +54,12 @@ 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) b2i = BinToInt(48) - print([b2i.process(x, True) for x in chunker(row, 6)]) + converted = [b2i.process(x, True)/10**14 for x in chunker(row, 6)] + print(converted,sum(converted)) data = pd.read_csv("accelerometer.data") -print(data) - # test_bin_float_converstion() # test_bin_int_converstion() diff --git a/converters/BinToFloat.py b/converters/BinToFloat.py index 2cc70fd..fc21634 100644 --- a/converters/BinToFloat.py +++ b/converters/BinToFloat.py @@ -8,7 +8,6 @@ def chunker(seq, size): class BinToFloat(object): def __init__(self, exponent=8, mantissa=23): - # self.signed = signed self.signed = True self.exponent = exponent self.mantissa = mantissa @@ -20,12 +19,6 @@ class BinToFloat(object): ordered_bytes = [] for group in reversed(chunked_bytes) if reverse_marshalling else chunked_bytes: ordered_bytes.extend(group) - # 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]]) - # ) mant = [x for x in reversed(ordered_bytes[:self.mantissa])] exp = ordered_bytes[self.mantissa:self.mantissa + self.exponent] sign = ordered_bytes[self.mantissa + self.exponent] if self.signed else True @@ -41,8 +34,6 @@ class BinToFloat(object): total_val += digit_val if i else 0 digit_val *= 2 tot = (2 ** (self.exponent-1))-1 - # print(tot) - # print(total_val) return total_val - (tot) def convert_mantissa(self, mant): diff --git a/subscribe_to_notification.py b/subscribe_to_notification.py index eab704e..0919ad4 100644 --- a/subscribe_to_notification.py +++ b/subscribe_to_notification.py @@ -12,15 +12,15 @@ import struct from bleak import BleakClient from bleak import _logger as logger -from bitstring import BitArray -from struct import Struct +from converters import BinToInt + BUTTON = 1 GYROSCOPE = 2 ACCELEROMETER = 3 BATTERY = 4 TEMPERATURE = 5 -INT_DECODER = +INT_DECODER = BinToInt(48) CHARACTERISTIC_UUIDS = { # ("64a7000d-f691-4b93-a6f4-0968f5b648f8"):BUTTON,#Button @@ -28,21 +28,30 @@ CHARACTERISTIC_UUIDS = { ("64a7000c-f691-4b93-a6f4-0968f5b648f8"): ACCELEROMETER, # Accel # ("64a70007-f691-4b93-a6f4-0968f5b648f8"):BATTERY, # ("64a70014-f691-4b93-a6f4-0968f5b648f8"):TEMPERATURE -# Accel } # <--- Change to the characteristic you want to enable notifications from. +# TODO: RUMBLE +# TODO: RGB +# TODO: MAGNETOMETER?? +# + + # device_address = "e3:ae:cd:af:28:e2" device_address = "D8:9B:12:D1:08:80" +def chunker(seq, size): + return (seq[pos:pos + size] for pos in range(0, len(seq), size)) + def decode_button(data): print(data) print(int.from_bytes(data, byteorder='big')) +def decode_gyroscope(data): + decode_accelerometer(data) + def decode_accelerometer(data): - # print(data) - # print(len(data)) - print([x for x in data]) - print(int.from_bytes()) + converted = [INT_DECODER.process(x, True)/10**14 for x in chunker(data, 6)] + print(converted,sum(converted)) def decode_battery(data): print(len(data)) @@ -57,15 +66,14 @@ def decode_temp(data): def notification_handler(sender, data): """Simple notification handler which prints the data received.""" + #TODO: Convert to a dictionary and a single decode command sender = CHARACTERISTIC_UUIDS[sender] if sender == BUTTON: - print(f"BUTTON PRESSED {data}") decode_button(data) elif sender == GYROSCOPE: - print(BitArray(data).bin[:1]) + decode_gyroscope(data) elif sender == ACCELEROMETER: - s = Struct("h") - print([x for x in s.iter_unpack(data)]) + decode_accelerometer(data) elif sender == BATTERY: decode_battery(data) elif sender == TEMPERATURE: @@ -96,7 +104,9 @@ async def run(address, loop, debug=False): x = await client.is_connected() logger.info("Connected: {0}".format(x)) await start_notify(client) + await asyncio.sleep(60.0, loop=loop) + await stop_notify(client)