Modifying the scan response according to events

edited August 2019 in Python

Hello again!

I have been working with several sensors using the MMR+ (firmware 1.4.4), the application in which I'm working uses macros for generating events after some Data Proccesors operations.

I posted a discussion before, about how to set the scan response. I want to change it according to events generated by the processed data. I just post here a the simple button signal, but I had achieved a lot of progress with the sensors that our application will use.

The code is the following, the device starts advertising, then the scan response is set with 0x01 0x01 as Manufacturer Specific Data, I want to modify it when I press the button, in this case replacing it by 0x11 0x11, so I included it some lines with that purpose in the section libmetawear.mbl_mw_event_record_commands(pressed) .

I know that the event is generated after pressing the button because the motor vibrates and the led is on, but the scan response have not changed. Any suggestion?

from __future__ import print_function
from mbientlab.metawear import MetaWear, libmetawear, parse_value, create_voidp_int, create_voidp
from mbientlab.metawear.cbindings import *
from threading import Event
from ctypes import *

import platform
import sys

device = MetaWear(sys.argv[1])
print("Connected to " + device.address)

e = Event()

print("Configuring device")

signal= libmetawear.mbl_mw_switch_get_state_data_signal(device.board)

# Start macro recording
libmetawear.mbl_mw_macro_record(device.board, 1)

device_name = create_string_buffer(b'Bracelet', 8)
bytes = cast(device_name, POINTER(c_ubyte))
libmetawear.mbl_mw_settings_set_device_name(device.board, bytes, len(device_name.raw))

scan_response= create_string_buffer(b'\x05\xff\xff\xff\x01\x01', 6)
bytes_scan = cast(scan_response, POINTER(c_ubyte))
libmetawear.mbl_mw_settings_set_scan_response(device.board, bytes_scan, len(scan_response.raw))

# Setup comparators - the first when the button is not pressed and the second when it's pressed
not_pressed = create_voidp(lambda fn: libmetawear.mbl_mw_dataprocessor_comparator_create(signal, ComparatorOperation.EQ, 0.0, None, fn), resource = "not_pressed", event = e)
pressed = create_voidp(lambda fn: libmetawear.mbl_mw_dataprocessor_comparator_create(signal, ComparatorOperation.EQ, 1.0, None, fn), resource = "pressed", event = e)

# LED is OFF when the button is not pressed
create_voidp_int(lambda fn: libmetawear.mbl_mw_event_end_record(not_pressed, None, fn), event = e)

# LED is ON when the button is pressed and the motor runs
pattern= LedPattern(pulse_duration_ms=1000, high_time_ms=500, high_intensity=16, low_intensity=16, repeat_count=Const.LED_REPEAT_INDEFINITELY)
libmetawear.mbl_mw_led_write_pattern(device.board, byref(pattern), LedColor.BLUE)
# Run motor at 50% strength for 500 ms when the button is pressed
libmetawear.mbl_mw_haptic_start_motor(device.board, 50.0, 500);

modified_scan_response= create_string_buffer(b'\x05\xff\xff\xff\x11\x11', 6)
bytes_modified_scan_response = cast(modified_scan_response, POINTER(c_ubyte))
libmetawear.mbl_mw_settings_set_scan_response(device.board, bytes_modified_scan_response, len(modified_scan_response.raw))

create_voidp_int(lambda fn: libmetawear.mbl_mw_event_end_record(pressed, None, fn), event = e)

# End macro recording
create_voidp_int(lambda fn: libmetawear.mbl_mw_macro_end_record(device.board, None, fn), event = e)

print("Resetting device")
device.on_disconnect = lambda status: e.set()


Sign In or Register to comment.