Skip to content

Commit

Permalink
Merge pull request #26 from mbientlab/usb_perf_rebase
Browse files Browse the repository at this point in the history
usb: performance enhancement.
  • Loading branch information
mattthebaker committed Apr 5, 2022
2 parents 5586b69 + 1b685d3 commit 84baa96
Show file tree
Hide file tree
Showing 31 changed files with 112 additions and 37 deletions.
2 changes: 1 addition & 1 deletion examples/acc_threshold_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
print("Searching for device...")
d = MetaWear(sys.argv[1])
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))
print("Configuring device")
libmetawear.mbl_mw_settings_set_connection_parameters(d.board, 7.5, 7.5, 0, 6000)
sleep(1.0)
Expand Down
2 changes: 1 addition & 1 deletion examples/anonymous_datasignals.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# connect
metawear = MetaWear(sys.argv[1])
metawear.connect()
print("Connected")
print("Connected to " + metawear.address + " over " + ("USB" if metawear.usb.is_connected else "BLE"))

# setup
e = Event()
Expand Down
2 changes: 1 addition & 1 deletion examples/calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# connect
device = MetaWear(sys.argv[1])
device.connect()
print("Connected")
print("Connected to " + device.address + " over " + ("USB" if device.usb.is_connected else "BLE"))

# event
e = Event()
Expand Down
2 changes: 1 addition & 1 deletion examples/data_fuser.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def start(self):
for i in range(len(argv) - 1):
d = MetaWear(argv[i + 1])
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))
states.append(State(d))

# configure
Expand Down
2 changes: 1 addition & 1 deletion examples/data_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def start(self):
for i in range(len(argv) - 1):
d = MetaWear(argv[i + 1])
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))
states.append(State(d))

# configure
Expand Down
2 changes: 1 addition & 1 deletion examples/firmware_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# connect
m = MetaWear(sys.argv[1])
m.connect()
print("Connected")
print("Connected to " + m.address + " over " + ("USB" if m.usb.is_connected else "BLE"))

# get board info
size = c_uint(0)
Expand Down
2 changes: 1 addition & 1 deletion examples/full_reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# connect
device = MetaWear(sys.argv[1])
device.connect()
print("Connected")
print("Connected to " + device.address + " over " + ("USB" if device.usb.is_connected else "BLE"))

# stop logging
libmetawear.mbl_mw_logging_stop(device.board)
Expand Down
2 changes: 1 addition & 1 deletion examples/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# connect
device = MetaWear(sys.argv[1])
device.connect()
print("Connected")
print("Connected to " + device.address + " over " + ("USB" if device.usb.is_connected else "BLE"))

# create led pattern
pattern= LedPattern(repeat_count= Const.LED_REPEAT_INDEFINITELY)
Expand Down
2 changes: 1 addition & 1 deletion examples/led_dongle.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# Add hci_mac to the setup to specify which dongle to use
device = MetaWear(sys.argv[1], hci_mac="B8:27:EB:F2:61:2E")
device.connect()
print("Connected")
print("Connected to " + device.address + " over " + ("USB" if device.usb.is_connected else "BLE"))

# create led pattern
pattern= LedPattern(repeat_count= Const.LED_REPEAT_INDEFINITELY)
Expand Down
2 changes: 1 addition & 1 deletion examples/led_temp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# connect
device = MetaWear(sys.argv[1])
device.connect()
print("Connected")
print("Connected to " + device.address + " over " + ("USB" if device.usb.is_connected else "BLE"))

# create led pattern
pattern= LedPattern(repeat_count= Const.LED_REPEAT_INDEFINITELY)
Expand Down
2 changes: 1 addition & 1 deletion examples/log_acc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
print("Searching for device...")
d = MetaWear(sys.argv[1])
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))

print("Configuring device")

Expand Down
2 changes: 1 addition & 1 deletion examples/log_acc_perf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
print("Searching for device...")
d = MetaWear(sys.argv[1])
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))

print("Configuring device")

Expand Down
2 changes: 1 addition & 1 deletion examples/log_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# connect
metawear = MetaWear(sys.argv[1])
metawear.connect()
print("Connected")
print("Connected to " + metawear.address + " over " + ("USB" if metawear.usb.is_connected else "BLE"))

# setup
e = Event()
Expand Down
2 changes: 1 addition & 1 deletion examples/log_temp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
print("Searching for device...")
d = MetaWear(sys.argv[1])
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))

try:
# setup events
Expand Down
2 changes: 1 addition & 1 deletion examples/macro_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# connect
device = MetaWear(sys.argv[1])
device.connect()
print("Connected to " + device.address)
print("Connected to " + device.address + " over " + ("USB" if device.usb.is_connected else "BLE"))
sleep(1.0)

# remove macros
Expand Down
2 changes: 1 addition & 1 deletion examples/macro_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# connect
device.connect()
print("Connected to " + device.address)
print("Connected to " + device.address + " over " + ("USB" if device.usb.is_connected else "BLE"))

# setup events
e = Event()
Expand Down
2 changes: 1 addition & 1 deletion examples/motion_record_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def wont_be_used():
for i in range(len(sys.argv) - 1):
d = MetaWear(sys.argv[i + 1])
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))
states.append(State(d))

# configure
Expand Down
2 changes: 1 addition & 1 deletion examples/motion_record_log_no_reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def wont_be_used():
for i in range(len(sys.argv) - 1):
d = MetaWear(sys.argv[i + 1])
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))
states.append(State(d))

for s in states:
Expand Down
2 changes: 1 addition & 1 deletion examples/motion_record_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def passthrough_created(self, context, signal):
for i in range(len(sys.argv) - 1):
d = MetaWear(sys.argv[i + 1])
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))
states.append(State(d))

# configure
Expand Down
2 changes: 1 addition & 1 deletion examples/scan_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def handler(result):
device = MetaWear(address)
device.connect()

print("Connected")
print("Connected to " + device.address + " over " + ("USB" if device.usb.is_connected else "BLE"))
print("Device information: " + str(device.info))
sleep(5.0)

Expand Down
2 changes: 1 addition & 1 deletion examples/stream_acc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def data_handler(self, ctx, data):
for i in range(len(sys.argv) - 1):
d = MetaWear(sys.argv[i + 1])
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))
states.append(State(d))

# configure
Expand Down
2 changes: 1 addition & 1 deletion examples/stream_acc_gyro_bmi160.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def gyro_data_handler(self, ctx, data):
for i in range(len(sys.argv) - 1):
d = MetaWear(sys.argv[i + 1])
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))
states.append(State(d))

# configure all metawears
Expand Down
2 changes: 1 addition & 1 deletion examples/stream_acc_gyro_bmi270.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def gyro_data_handler(self, ctx, data):
for i in range(len(sys.argv) - 1):
d = MetaWear(sys.argv[i + 1])
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))
states.append(State(d))

# configure
Expand Down
2 changes: 1 addition & 1 deletion examples/stream_acc_packed.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def acc_data_handler(self, ctx, data):
for i in range(len(sys.argv) - 1):
d = MetaWear(sys.argv[i + 1])
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))
states.append(State(d))

# configure
Expand Down
2 changes: 1 addition & 1 deletion examples/stream_acc_x.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def data_handler(self, ctx, data):
for i in range(len(sys.argv) - 1):
d = MetaWear(sys.argv[i + 1])
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))
states.append(State(d))

# configure
Expand Down
2 changes: 1 addition & 1 deletion examples/stream_gyro_packed.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def gyro_data_handler(self, ctx, data):
for i in range(len(sys.argv) - 1):
d = MetaWear(sys.argv[i + 1])
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))
states.append(State(d))

# configure
Expand Down
69 changes: 69 additions & 0 deletions examples/stream_mag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# usage: python3 stream_mag.py [mac1] [mac2] ... [mac(n)]
from __future__ import print_function
from mbientlab.metawear import MetaWear, libmetawear, parse_value
from mbientlab.metawear.cbindings import *
from time import sleep
from threading import Event

import platform
import sys

if sys.version_info[0] == 2:
range = xrange

class State:
# init
def __init__(self, device):
self.device = device
self.samples = 0
self.magCallback = FnVoid_VoidP_DataP(self.mag_data_handler)

# mag callback
def mag_data_handler(self, ctx, data):
print("MAG: %s -> %s" % (self.device.address, parse_value(data)))
self.samples+= 1

states = []

# connect
for i in range(len(sys.argv) - 1):
d = MetaWear(sys.argv[i + 1])
d.connect()
print("Connected to %s over %s" % (d.address, "USB" if d.usb.is_connected else "BLE"))
states.append(State(d))

# configure
for s in states:
print("Configuring device")
libmetawear.mbl_mw_settings_set_connection_parameters(s.device.board, 7.5, 7.5, 0, 6000)
sleep(1.5)

# setup mag
libmetawear.mbl_mw_mag_bmm150_stop(s.device.board)
libmetawear.mbl_mw_mag_bmm150_set_preset(s.device.board, MagBmm150Preset.REGULAR)

# get mag and subscribe
mag = libmetawear.mbl_mw_mag_bmm150_get_b_field_data_signal(s.device.board)
libmetawear.mbl_mw_datasignal_subscribe(mag, None, s.magCallback)

# start mag
libmetawear.mbl_mw_mag_bmm150_enable_b_field_sampling(s.device.board)
libmetawear.mbl_mw_mag_bmm150_start(s.device.board)

# sleep
sleep(10.0)

# stop
for s in states:
libmetawear.mbl_mw_mag_bmm150_stop(s.device.board)
libmetawear.mbl_mw_mag_bmm150_disable_b_field_sampling(s.device.board)

mag = libmetawear.mbl_mw_mag_bmm150_get_b_field_data_signal(s.device.board)
libmetawear.mbl_mw_datasignal_unsubscribe(mag)

libmetawear.mbl_mw_debug_disconnect(s.device.board)

# recap
print("Total Samples Received")
for s in states:
print("%s -> %d" % (s.device.address, s.samples))
2 changes: 1 addition & 1 deletion examples/stream_quat.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def data_handler(self, ctx, data):
for i in range(len(sys.argv) - 1):
d = MetaWear(sys.argv[i + 1])
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))
states.append(State(d))

# configure
Expand Down
2 changes: 1 addition & 1 deletion examples/stream_temp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# connect to macc
d.connect()
print("Connected to " + d.address)
print("Connected to " + d.address + " over " + ("USB" if d.usb.is_connected else "BLE"))
e = Event()

# create callback
Expand Down
22 changes: 14 additions & 8 deletions mbientlab/metawear/metawear.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class MetaWearUSB(object):
GATT_MW_CHAR_COMMAND = '326a9001-85cb-9195-d9dd-464cfbbae75a'
GATT_MW_CHAR_NOTIFICATION = '326a9006-85cb-9195-d9dd-464cfbbae75a'

SERIAL_XFER_SIZE = 1024
SERIAL_BYTE_START = b'\x1f'
SERIAL_BYTE_STOP = b'\n'

@staticmethod
def scan():
"""List MetaWear devices attached to USB"""
Expand Down Expand Up @@ -202,10 +206,10 @@ def _bin_cmd_decode(self, c):
elif self._cmd_recv_len < self._cmd_len:
self._cmd_recv_len += 1
self._cmd_buffer += c
elif c == b'\n':
elif c == MetaWearUSB.SERIAL_BYTE_STOP:
self._cmd_started = False
return self._cmd_buffer
elif c == b'\x1f':
elif c == MetaWearUSB.SERIAL_BYTE_START:
self._cmd_started = True
self._cmd_len = 0
self._cmd_recv_len = 0
Expand All @@ -217,18 +221,20 @@ def _read_poller(self):
self._cmd_started = False
while self._read_poll:
try:
c = self.ser.read()
read_len = max(1, min(MetaWearUSB.SERIAL_XFER_SIZE, self.ser.in_waiting))
line_bytes = self.ser.read(read_len)
except serial.SerialException:
self._read_poll = False
self.disconnect()
return

if len(c) < 1:
if len(line_bytes) < 1:
continue
cmd = self._bin_cmd_decode(c)
if len(cmd) > 0:
if self._notify_handler is not None:
self._notify_handler(cmd)
for i in range(len(line_bytes)):
cmd = self._bin_cmd_decode(line_bytes[i:i+1])
if len(cmd) > 0:
if self._notify_handler is not None:
self._notify_handler(cmd)

def _write_poller(self):
"""Write poller enabling async writes and write response callbacks."""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def run(self):
setup(
name='metawear',
packages=['mbientlab', 'mbientlab.metawear'],
version='1.0.6',
version='1.0.7',
description='Python bindings for the MetaWear C++ SDK by MbientLab',
long_description=open(os.path.join(os.path.dirname(__file__), "README.rst")).read(),
package_data={'mbientlab.metawear': so_pkg_data},
Expand Down

0 comments on commit 84baa96

Please sign in to comment.