Skip to content

Commit

Permalink
More example script improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
scaryghost committed Dec 17, 2018
1 parent d48c30e commit 9854274
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 268 deletions.
52 changes: 8 additions & 44 deletions examples/acc_threshold_detector.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
# usage: python acc_threshold_detector [mac]
# usage: python acc_threshold_detector.py [mac]
from __future__ import print_function
from mbientlab.metawear import MetaWear, libmetawear, parse_value
from mbientlab.metawear import MetaWear, libmetawear, parse_value, create_voidp_int, create_voidp
from mbientlab.metawear.cbindings import *
from time import sleep
from threading import Event

# CURRENT:
# Sample accelerometer at 50Hz
# Use the rms to combine X,Y,Z
# Use the average to downsample to about 4Hz (averaging 8 samples)
# Use the threshold detector to detect a bump
# Also log all the event and download it later


print("Searching for device...")
d = MetaWear(sys.argv[1])
d.connect()
Expand All @@ -21,35 +14,6 @@
sleep(1.0)

e = Event()
def create_voidp(create, resource):
result = [None]
def handler(ctx, pointer):
result[0] = RuntimeError("Could not create " + resource) if pointer == None else pointer
e.set()

callback_wrapper = FnVoid_VoidP_VoidP(handler)
create(callback_wrapper)
e.wait()

e.clear()
if (result[0] is RuntimeError):
raise result[0]
return result[0]

def end_event_record(evt):
result = [None]
def handler(ctx, pointer, status):
if (status != Const.STATUS_OK):
result[0] = RuntimeError("Event recording returned a non-zero status (%d)" % (status))
e.set()

callback_wrapper = FnVoid_VoidP_VoidP_Int(handler)
libmetawear.mbl_mw_event_end_record(evt, None, callback_wrapper)
e.wait()

e.clear()
if (result[0] is RuntimeError):
raise result[0]

try:
# setup accelerometer (odr 50Hz and 2Gs)
Expand All @@ -61,24 +25,24 @@ def handler(ctx, pointer, status):
acc_signal = libmetawear.mbl_mw_acc_get_acceleration_data_signal(d.board)

# create RMS - root mean square of acc X,Y,Z
rms = create_voidp(lambda fn: libmetawear.mbl_mw_dataprocessor_rms_create(acc_signal, None, fn), "RMS")
rms = create_voidp(lambda fn: libmetawear.mbl_mw_dataprocessor_rms_create(acc_signal, None, fn), resource = "RMS", event = e)
print("RMS created")

# setup averager - averages over 8 RMS samples @ 50Hz
avg = create_voidp(lambda fn: libmetawear.mbl_mw_dataprocessor_average_create(rms, 8, None, fn), "averager")
avg = create_voidp(lambda fn: libmetawear.mbl_mw_dataprocessor_average_create(rms, 8, None, fn), resource = "averager", event = e)
print("Averager created")

# setup event on avg - reset averager
libmetawear.mbl_mw_event_record_commands(avg)
libmetawear.mbl_mw_dataprocessor_average_reset(avg)
end_event_record(avg)
create_voidp_int(lambda fn: libmetawear.mbl_mw_event_end_record(avg, None, fn), event = e)

# setup threshold detector - detect anything above 1
ths = create_voidp(lambda fn: libmetawear.mbl_mw_dataprocessor_threshold_create(avg, ThresholdMode.BINARY, 1.0, 0.0, None, fn), "threshold detector")
ths = create_voidp(lambda fn: libmetawear.mbl_mw_dataprocessor_threshold_create(avg, ThresholdMode.BINARY, 1.0, 0.0, None, fn), resource = "threshold detector", event = e)
print("Threshold detector created")

# setup logger - log the final signal of the averaged data
ths_logger = create_voidp(lambda fn: libmetawear.mbl_mw_datasignal_log(ths, None, fn), "threshold logger")
ths_logger = create_voidp(lambda fn: libmetawear.mbl_mw_datasignal_log(ths, None, fn), resource = "threshold logger", event = e)
print("Threshold logger created")

# start accelerometer and event logging
Expand Down
2 changes: 1 addition & 1 deletion examples/calibrate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# usage: python led.py [mac]
# usage: python calibrate.py [mac]
from mbientlab.metawear import MetaWear, libmetawear, parse_value
from mbientlab.metawear.cbindings import *
from time import sleep
Expand Down
99 changes: 36 additions & 63 deletions examples/log_acc.py
Original file line number Diff line number Diff line change
@@ -1,87 +1,60 @@
# usage: python log_acc [mac]
# usage: python log_acc.py [mac]
from __future__ import print_function
from mbientlab.metawear import MetaWear, libmetawear, parse_value
from mbientlab.metawear import MetaWear, libmetawear, parse_value, create_voidp, create_voidp_int
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:
def __init__(self, device):
self.device = device

def setup_logger(self):
e = Event()
result = [None]

def logger_handler(ctx, pointer):
result[0] = RuntimeError("Could not create logger") if pointer == None else pointer
e.set()
fn_wrapper = FnVoid_VoidP_VoidP(logger_handler)

signal = libmetawear.mbl_mw_acc_get_acceleration_data_signal(self.device.board)
libmetawear.mbl_mw_datasignal_log(signal, None, fn_wrapper)
e.wait()

if (result[0] is RuntimeError):
raise result[0]

self.logger = result[0]

libmetawear.mbl_mw_acc_enable_acceleration_sampling(self.device.board)
libmetawear.mbl_mw_acc_start(self.device.board)
libmetawear.mbl_mw_logging_start(self.device.board, 0);

def download_data(self):
libmetawear.mbl_mw_acc_stop(self.device.board)
libmetawear.mbl_mw_acc_disable_acceleration_sampling(self.device.board)
libmetawear.mbl_mw_logging_stop(self.device.board)

e = Event()
def progress_update_handler(context, entries_left, total_entries):
if (entries_left == 0):
e.set()

fn_wrapper = FnVoid_VoidP_UInt_UInt(progress_update_handler)
download_handler= LogDownloadHandler(context = None, \
received_progress_update = fn_wrapper, \
received_unknown_entry = cast(None, FnVoid_VoidP_UByte_Long_UByteP_UByte), \
received_unhandled_entry = cast(None, FnVoid_VoidP_DataP))

callback = FnVoid_VoidP_DataP(lambda ctx, p: print("%s -> %s" % (self.device.address, parse_value(p))))
libmetawear.mbl_mw_logger_subscribe(self.logger, None, callback)
libmetawear.mbl_mw_logging_download(self.device.board, 0, byref(download_handler))
e.wait()

print("Searching for device...")
d = MetaWear(sys.argv[1])
d.connect()
print("Connected to " + d.address)
s = State(d)

print("Configuring device")
libmetawear.mbl_mw_settings_set_connection_parameters(s.device.board, 7.5, 7.5, 0, 6000)
sleep(1.0)

try:
s.setup_logger()
signal = libmetawear.mbl_mw_acc_get_acceleration_data_signal(d.board)
logger = create_voidp(lambda fn: libmetawear.mbl_mw_datasignal_log(signal, None, fn), resource = "acc_logger")

libmetawear.mbl_mw_logging_start(d.board, 0)
libmetawear.mbl_mw_acc_enable_acceleration_sampling(d.board)
libmetawear.mbl_mw_acc_start(d.board)

print("Logging data for 15s")
sleep(15.0)

s.download_data()

except RuntimeError as e:
print(e)
libmetawear.mbl_mw_acc_stop(d.board)
libmetawear.mbl_mw_acc_disable_acceleration_sampling(d.board)
libmetawear.mbl_mw_logging_stop(d.board)

print("Downloading data")
libmetawear.mbl_mw_settings_set_connection_parameters(d.board, 7.5, 7.5, 0, 6000)
sleep(1.0)

e = Event()
def progress_update_handler(context, entries_left, total_entries):
if (entries_left == 0):
e.set()

fn_wrapper = FnVoid_VoidP_UInt_UInt(progress_update_handler)
download_handler = LogDownloadHandler(context = None, \
received_progress_update = fn_wrapper, \
received_unknown_entry = cast(None, FnVoid_VoidP_UByte_Long_UByteP_UByte), \
received_unhandled_entry = cast(None, FnVoid_VoidP_DataP))

callback = FnVoid_VoidP_DataP(lambda ctx, p: print("{epoch: %d, value: %s}" % (p.contents.epoch, parse_value(p))))
libmetawear.mbl_mw_logger_subscribe(logger, None, callback)
libmetawear.mbl_mw_logging_download(d.board, 0, byref(download_handler))
e.wait()

except RuntimeError as err:
print(err)
finally:
print("Resetting device")

e = Event()
s.device.on_disconnect = lambda status: e.set()
libmetawear.mbl_mw_debug_reset(s.device.board)
d.on_disconnect = lambda status: e.set()
libmetawear.mbl_mw_debug_reset(d.board)
e.wait()
142 changes: 40 additions & 102 deletions examples/log_temp.py
Original file line number Diff line number Diff line change
@@ -1,124 +1,62 @@
# usage: python log_temp [mac]
# usage: python log_temp.py [mac]
from __future__ import print_function
from mbientlab.metawear import MetaWear, libmetawear, parse_value
from mbientlab.metawear import MetaWear, libmetawear, parse_value, create_voidp, create_voidp_int
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:
def __init__(self, device):
self.device = device

def setup_logger(self):
e = Event()
result = [None]

def logger_handler(ctx, pointer):
result[0] = RuntimeError("Could not create logger") if pointer == None else pointer
e.set()
fn_wrapper = FnVoid_VoidP_VoidP(logger_handler)

#streaming and logging
signal = libmetawear.mbl_mw_multi_chnl_temp_get_temperature_data_signal(self.device.board, \
MetaWearRProChannel.ON_BOARD_THERMISTOR)
libmetawear.mbl_mw_datasignal_log(signal, None, fn_wrapper)
e.wait()

if (result[0] is RuntimeError):
raise result[0]

self.logger = result[0]
self.timer = self._setup_timer()
self._setup_read_event(self.timer, signal)

libmetawear.mbl_mw_logging_start(self.device.board, 0);
libmetawear.mbl_mw_timer_start(self.timer)

def _setup_timer(self):
e = Event()
result = [None]

def timer_handler(ctx, pointer):
result[0] = RuntimeError("Could not create timer") if pointer == None else pointer
e.set()
fn_wrapper = FnVoid_VoidP_VoidP(timer_handler)

libmetawear.mbl_mw_timer_create_indefinite(self.device.board, 1000, 0, None, fn_wrapper)
e.wait()

if (result[0] is RuntimeError):
raise result[0]

return result[0]

def _setup_read_event(self, timer, signal):
e = Event()
result = [None]

def commands_recorded(ctx, event, status):
result[0] = RuntimeError("Could not create read event") if status != Const.STATUS_OK else None
e.set()
fn_wrapper = FnVoid_VoidP_VoidP_Int(commands_recorded)

libmetawear.mbl_mw_event_record_commands(timer)
libmetawear.mbl_mw_datasignal_read(signal)
libmetawear.mbl_mw_event_end_record(timer, None, fn_wrapper)
e.wait()

if (result[0] is RuntimeError):
raise result[0]

def download_data(self):
libmetawear.mbl_mw_timer_remove(self.timer)
libmetawear.mbl_mw_logging_stop(self.device.board)

e = Event()
def progress_update_handler(context, entries_left, total_entries):
if (entries_left == 0):
e.set()

fn_wrapper = FnVoid_VoidP_UInt_UInt(progress_update_handler)
download_handler= LogDownloadHandler(context = None, \
received_progress_update = fn_wrapper, \
received_unknown_entry = cast(None, FnVoid_VoidP_UByte_Long_UByteP_UByte), \
received_unhandled_entry = cast(None, FnVoid_VoidP_DataP))

callback = FnVoid_VoidP_DataP(lambda ctx, p: print("%f" % (parse_value(p))))
libmetawear.mbl_mw_logger_subscribe(self.logger, None, callback)
libmetawear.mbl_mw_logging_download(self.device.board, 0, byref(download_handler))
e.wait()



print("Searching for device...")
d = MetaWear(sys.argv[1])
d.connect()
print("Connected to " + d.address)
s = State(d)

print("Configuring device")
libmetawear.mbl_mw_settings_set_connection_parameters(s.device.board, 7.5, 7.5, 0, 6000)
sleep(1.0)

try:
e = Event()

print("Configuring device")
#logging
s.setup_logger()
signal = libmetawear.mbl_mw_multi_chnl_temp_get_temperature_data_signal(d.board, MetaWearRProChannel.ON_BOARD_THERMISTOR)
logger = create_voidp(lambda fn: libmetawear.mbl_mw_datasignal_log(signal, None, fn), resource = "temp_logger", event = e)

timer = create_voidp(lambda fn: libmetawear.mbl_mw_timer_create_indefinite(d.board, 1000, 0, None, fn), resource = "timer", event = e)
libmetawear.mbl_mw_event_record_commands(timer)
libmetawear.mbl_mw_datasignal_read(signal)
create_voidp_int(lambda fn: libmetawear.mbl_mw_event_end_record(timer, None, fn), event = e)

libmetawear.mbl_mw_logging_start(d.board, 0)
libmetawear.mbl_mw_timer_start(timer)

print("Logging data for 15s")
sleep(15.0)

s.download_data()
except RuntimeError as e:
print(e)
libmetawear.mbl_mw_timer_remove(timer)
libmetawear.mbl_mw_logging_stop(d.board)

print("Downloading data")
libmetawear.mbl_mw_settings_set_connection_parameters(d.board, 7.5, 7.5, 0, 6000)
sleep(1.0)

def progress_update_handler(context, entries_left, total_entries):
if (entries_left == 0):
e.set()

fn_wrapper = FnVoid_VoidP_UInt_UInt(progress_update_handler)
download_handler = LogDownloadHandler(context = None, \
received_progress_update = fn_wrapper, \
received_unknown_entry = cast(None, FnVoid_VoidP_UByte_Long_UByteP_UByte), \
received_unhandled_entry = cast(None, FnVoid_VoidP_DataP))

callback = FnVoid_VoidP_DataP(lambda ctx, p: print("{epoch: %d, value: %s}" % (p.contents.epoch, parse_value(p))))
libmetawear.mbl_mw_logger_subscribe(logger, None, callback)
libmetawear.mbl_mw_logging_download(d.board, 0, byref(download_handler))
e.wait()
except RuntimeError as err:
print(err)
finally:
print("Resetting device")
e = Event()
s.device.on_disconnect = lambda status: e.set()
libmetawear.mbl_mw_debug_reset(s.device.board)
d.on_disconnect = lambda status: e.set()
libmetawear.mbl_mw_debug_reset(d.board)
e.wait()
Loading

0 comments on commit 9854274

Please sign in to comment.