code cleanup
This commit is contained in:
@@ -54,14 +54,12 @@ 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)
|
|
||||||
b2i = BinToInt(48)
|
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")
|
data = pd.read_csv("accelerometer.data")
|
||||||
print(data)
|
|
||||||
|
|
||||||
# test_bin_float_converstion()
|
# test_bin_float_converstion()
|
||||||
# test_bin_int_converstion()
|
# test_bin_int_converstion()
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ def chunker(seq, size):
|
|||||||
class BinToFloat(object):
|
class BinToFloat(object):
|
||||||
|
|
||||||
def __init__(self, exponent=8, mantissa=23):
|
def __init__(self, exponent=8, mantissa=23):
|
||||||
# self.signed = signed
|
|
||||||
self.signed = True
|
self.signed = True
|
||||||
self.exponent = exponent
|
self.exponent = exponent
|
||||||
self.mantissa = mantissa
|
self.mantissa = mantissa
|
||||||
@@ -20,12 +19,6 @@ class BinToFloat(object):
|
|||||||
ordered_bytes = []
|
ordered_bytes = []
|
||||||
for group in reversed(chunked_bytes) if reverse_marshalling else chunked_bytes:
|
for group in reversed(chunked_bytes) if reverse_marshalling else chunked_bytes:
|
||||||
ordered_bytes.extend(group)
|
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])]
|
mant = [x for x in reversed(ordered_bytes[:self.mantissa])]
|
||||||
exp = ordered_bytes[self.mantissa:self.mantissa + self.exponent]
|
exp = ordered_bytes[self.mantissa:self.mantissa + self.exponent]
|
||||||
sign = ordered_bytes[self.mantissa + self.exponent] if self.signed else True
|
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
|
total_val += digit_val if i else 0
|
||||||
digit_val *= 2
|
digit_val *= 2
|
||||||
tot = (2 ** (self.exponent-1))-1
|
tot = (2 ** (self.exponent-1))-1
|
||||||
# print(tot)
|
|
||||||
# print(total_val)
|
|
||||||
return total_val - (tot)
|
return total_val - (tot)
|
||||||
|
|
||||||
def convert_mantissa(self, mant):
|
def convert_mantissa(self, mant):
|
||||||
|
|||||||
@@ -12,15 +12,15 @@ import struct
|
|||||||
|
|
||||||
from bleak import BleakClient
|
from bleak import BleakClient
|
||||||
from bleak import _logger as logger
|
from bleak import _logger as logger
|
||||||
from bitstring import BitArray
|
from converters import BinToInt
|
||||||
from struct import Struct
|
|
||||||
BUTTON = 1
|
BUTTON = 1
|
||||||
GYROSCOPE = 2
|
GYROSCOPE = 2
|
||||||
ACCELEROMETER = 3
|
ACCELEROMETER = 3
|
||||||
BATTERY = 4
|
BATTERY = 4
|
||||||
TEMPERATURE = 5
|
TEMPERATURE = 5
|
||||||
|
|
||||||
INT_DECODER =
|
INT_DECODER = BinToInt(48)
|
||||||
|
|
||||||
CHARACTERISTIC_UUIDS = {
|
CHARACTERISTIC_UUIDS = {
|
||||||
# ("64a7000d-f691-4b93-a6f4-0968f5b648f8"):BUTTON,#Button
|
# ("64a7000d-f691-4b93-a6f4-0968f5b648f8"):BUTTON,#Button
|
||||||
@@ -28,21 +28,30 @@ CHARACTERISTIC_UUIDS = {
|
|||||||
("64a7000c-f691-4b93-a6f4-0968f5b648f8"): ACCELEROMETER, # Accel
|
("64a7000c-f691-4b93-a6f4-0968f5b648f8"): ACCELEROMETER, # Accel
|
||||||
# ("64a70007-f691-4b93-a6f4-0968f5b648f8"):BATTERY,
|
# ("64a70007-f691-4b93-a6f4-0968f5b648f8"):BATTERY,
|
||||||
# ("64a70014-f691-4b93-a6f4-0968f5b648f8"):TEMPERATURE
|
# ("64a70014-f691-4b93-a6f4-0968f5b648f8"):TEMPERATURE
|
||||||
# Accel
|
|
||||||
} # <--- Change to the characteristic you want to enable notifications from.
|
} # <--- 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 = "e3:ae:cd:af:28:e2"
|
||||||
device_address = "D8:9B:12:D1:08:80"
|
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):
|
def decode_button(data):
|
||||||
print(data)
|
print(data)
|
||||||
print(int.from_bytes(data, byteorder='big'))
|
print(int.from_bytes(data, byteorder='big'))
|
||||||
|
|
||||||
|
def decode_gyroscope(data):
|
||||||
|
decode_accelerometer(data)
|
||||||
|
|
||||||
def decode_accelerometer(data):
|
def decode_accelerometer(data):
|
||||||
# print(data)
|
converted = [INT_DECODER.process(x, True)/10**14 for x in chunker(data, 6)]
|
||||||
# print(len(data))
|
print(converted,sum(converted))
|
||||||
print([x for x in data])
|
|
||||||
print(int.from_bytes())
|
|
||||||
|
|
||||||
def decode_battery(data):
|
def decode_battery(data):
|
||||||
print(len(data))
|
print(len(data))
|
||||||
@@ -57,15 +66,14 @@ def decode_temp(data):
|
|||||||
|
|
||||||
def notification_handler(sender, data):
|
def notification_handler(sender, data):
|
||||||
"""Simple notification handler which prints the data received."""
|
"""Simple notification handler which prints the data received."""
|
||||||
|
#TODO: Convert to a dictionary and a single decode command
|
||||||
sender = CHARACTERISTIC_UUIDS[sender]
|
sender = CHARACTERISTIC_UUIDS[sender]
|
||||||
if sender == BUTTON:
|
if sender == BUTTON:
|
||||||
print(f"BUTTON PRESSED {data}")
|
|
||||||
decode_button(data)
|
decode_button(data)
|
||||||
elif sender == GYROSCOPE:
|
elif sender == GYROSCOPE:
|
||||||
print(BitArray(data).bin[:1])
|
decode_gyroscope(data)
|
||||||
elif sender == ACCELEROMETER:
|
elif sender == ACCELEROMETER:
|
||||||
s = Struct("h")
|
decode_accelerometer(data)
|
||||||
print([x for x in s.iter_unpack(data)])
|
|
||||||
elif sender == BATTERY:
|
elif sender == BATTERY:
|
||||||
decode_battery(data)
|
decode_battery(data)
|
||||||
elif sender == TEMPERATURE:
|
elif sender == TEMPERATURE:
|
||||||
@@ -96,7 +104,9 @@ async def run(address, loop, debug=False):
|
|||||||
x = await client.is_connected()
|
x = await client.is_connected()
|
||||||
logger.info("Connected: {0}".format(x))
|
logger.info("Connected: {0}".format(x))
|
||||||
await start_notify(client)
|
await start_notify(client)
|
||||||
|
|
||||||
await asyncio.sleep(60.0, loop=loop)
|
await asyncio.sleep(60.0, loop=loop)
|
||||||
|
|
||||||
await stop_notify(client)
|
await stop_notify(client)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user