-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d48c30e
commit 9854274
Showing
9 changed files
with
209 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.