Python example for Multi-Value Comparison data processors for GPIO pin monitoring.

Hi All:

I am trying to implement monitoring the GPIO pin using Multi-Value Comparison data processors in Python by following the example in https://mbientlab.com/cppdocs/latest/dataprocessor.html#multi-value-comparison and https://mbientlab.com/cppdocs/latest/dataprocessor.html#threshold.

I am struggling with how to pass pointer for the Python array of floats to libmetawear.mbl_mw_dataprocessor_multi_comparator_modify().

Any example or guidance would be greatly appreciated.

Thanks.

Below is the code snippet ( incomplete):

**ReferenceValues = arr.array('f', [0.5, 1.0, 1.5, 2.0, 2.5, 3.0] )
Nrefs = len(ReferenceValues)
ptr = cast(ReferenceValues, POINTER(c_float * ReferenceValues))
**
result = {}
def handler(ctx, processor):
result['processor'] = cast(processor, POINTER(c_void_p * processor)) if processor is not None else None
print("Comparison processor created\n")
handler_fn = FnVoid_VoidP_VoidP(handler)

device = MetaWear(sys.argv[1])
device.connect()
print("Connected (1)")
sleep(1.0)

int_signal0 = libmetawear.mbl_mw_gpio_get_analog_input_data_signal(device.board, DIO0, GpioAnalogReadMode.ABS_REF)

libmetawear.mbl_mw_gpio_set_pull_mode(int_signal0, device.board, DIO0, GpioPullMode.DOWN)

libmetawear.mbl_mw_dataprocessor_threshold_create(int_signal0, ThresholdMode.BINARY, 1.5, 0, None, handler_fn)

libmetawear.mbl_mw_dataprocessor_comparator_create(int_signal0, ComparatorOperation.GT, 1.5, None, handler_fn)

libmetawear.mbl_mw_dataprocessor_multi_comparator_modify.argtypes = [c_void_p, c_int, POINTER(c_float), c_ubyte]

libmetawear.mbl_mw_dataprocessor_multi_comparator_modify(int_signal0, ComparatorMode.ZONE, ReferenceValues, Nrefs, None, handler_fn)

.....
.....

sleep(1.0)
print("Disconnecting...")
device.disconnect()

Comments

  • edited November 2018

    Wrap your code with the appropriate Markdown code tags so your post renders correctly.

  • My apology for badly formatting the code above.

    My question is how to correctly cast a pointer to ReferenceValues array in order to pass it as argument to the libmetawear.mbl_mw_dataprocessor_multi_comparator_modify(int_signal0, ComparatorMode.ZONE, ptrToRefArray, RefCount, None, handler_fn)

    Here's the error I get:
    python testgpiopin.py F9:4E:B7:A1:F7:CE
    File "testgpiopin.py", line 25
    ptrToRefArray = POINTER(c_float ReferenceValues)
    ^
    SyntaxError: invalid syntax

    Below is the Python2.7 code.

    Thanks for your help.
    ---------------X---------------------
    DIO0=1
    ReferenceValues = arr.array('f', [0.5, 1.0, 1.5, 2.0, 2.5, 3.0] )
    RefCount = len(ReferenceValues)

    ptrToRefArray = POINTER(c_float ReferenceValues)

    ptr = cast(ReferenceValues, POINTER(c_float * ReferenceValues))

    lpcf = cast(ReferenceValues, POINTER(ctypes.c_float * ReferenceValues))

    result = {}
    def handler(ctx, processor):
    result['processor'] = cast(processor, POINTER(c_void_p * processor)) if processor is not None else None
    print("Comparison processor created\n")

    handler_fn = FnVoid_VoidP_VoidP(handler)

    def state_handler(data):
    print("Edge detected...")
    print("%s" % (parse_value(data)))

    state_handler_fn = FnVoid_VoidP_DataP(state_handler)

    device = MetaWear(sys.argv[1])
    device.connect()
    print("Connected (1)")
    sleep(1.0)

    int_signal0 = libmetawear.mbl_mw_gpio_get_analog_input_data_signal(device.board, DIO0, GpioAnalogReadMode.ABS_REF)

    libmetawear.mbl_mw_dataprocessor_multi_comparator_modify(int_signal0, ComparatorMode.ZONE, ptrToRefArray, RefCount, None, handler_fn)

    libmetawear.mbl_mw_gpio_set_pin_change_type(device.board, DIO0, GpioPinChangeType.ANY)

    libmetawear.mbl_mw_datasignal_subscribe(int_signal0, None, state_handler_fn)

    libmetawear.mbl_mw_gpio_start_pin_monitoring(device.board, DIO0)

    for x in range (0,10,1):
    print("Looping...")
    sleep(2.0)

    sleep(1.0)
    print("Disconnecting...")
    device.disconnect()

  • Please ignore inadvertent highlighting. The commented python code gets automatically highlighted.

  • Yes...that's why I told you to wrap your code with Markdown code tags, which you still haven't done.

    Please fix your posts.

  • Hi Eric: I don't know what are the markdown code tags but put the code inpython .

    My question is how to correctly cast a pointer to ReferenceValues array in order to pass it as argument to the libmetawear.mbl_mw_dataprocessor_multi_comparator_modify(int_signal0, ComparatorMode.ZONE, ptrToRefArray, RefCount, None, handler_fn)

    Here's the error I get:
    python testgpiopin.py F9:4E:B7:A1:F7:CE
    File "testgpiopin.py", line 25
    ptrToRefArray = POINTER(c_float ReferenceValues)
    ^
    SyntaxError: invalid syntax

    Below is the Python2.7 code.
    Thanks for your help.

    from mbientlab.metawear import MetaWear, libmetawear, parse_value
    from mbientlab.metawear.cbindings import *
    from time import sleep
    from threading import Event
    import array as arr
    import thread
    import time
    import sys
    import ctypes
    
    HCD=0
    DIO0=1
    DIO1=2
    DIO2=3
    Vgnd=4
    V3v=5
    DIO3=6
    DIO4=7
    SDA=8
    SCL=9
    
    ReferenceValues = arr.array('f', [0.5, 1.0, 1.5, 2.0, 2.5, 3.0] )
    Nrefs = len(ReferenceValues)
    ptrToRefArray = POINTER(c_float ReferenceValues)
    
    # ptr = POINTER(c_float * ReferenceValues)
    # ptr = cast(ReferenceValues, POINTER(c_float * ReferenceValues))
    # lpcf = cast(ReferenceValues, POINTER(ctypes.c_float * ReferenceValues))
    
    result = {}
    def proc_handler(ctx, processor):
        result['processor'] = cast(processor, POINTER(c_void_p * processor)) if processor is not None else None
        print("Comparison processor created\n")
    
    handler_fn = FnVoid_VoidP_VoidP(proc_handler)
    
    def state_handler(ctx, data):
            #value = cast(data, POINTER(MblMwData * data))
            #print("Edge detected...")
            print("GPIO Pin 0:  %s" % (parse_value(data)))
            #print("GPIO 0: {0}".format(cast(data, POINTER(c_void_p * data))))
    
    state_handler_fn = FnVoid_VoidP_DataP(state_handler)
    
    device = MetaWear(sys.argv[1])
    device.connect()
    print("Connected (1)")
    sleep(1.0)
    
    int_signal0 = libmetawear.mbl_mw_gpio_get_analog_input_data_signal(device.board, DIO0, GpioAnalogReadMode.ABS_REF)
    
    libmetawear.mbl_mw_dataprocessor_multi_comparator_modify(int_signal0, ComparatorMode.ZONE, ReferenceValues,  Nrefs, None, handler_fn)
    
    libmetawear.mbl_mw_gpio_set_pin_change_type(device.board, DIO0, GpioPinChangeType.ANY)
    libmetawear.mbl_mw_datasignal_subscribe(int_signal0, None, state_handler_fn)
    libmetawear.mbl_mw_gpio_start_pin_monitoring(device.board, DIO0)
    
    for x in range (0,10,1):
        libmetawear.mbl_mw_datasignal_read(int_signal0)
        sleep(2.0)
    
    sleep(1.0)
    print("Disconnecting...")
    libmetawear.mbl_mw_datasignal_subscribe(int_signal0, None, None)
    libmetawear.mbl_mw_gpio_stop_pin_monitoring(device.board, DIO0)
    device.disconnect()
    
  • edited November 2018

    Well, I can't tell what you are are trying to accomplish in that line of code so all I can do is point you to the pointer documentation:
    https://docs.python.org/2/library/ctypes.html#pointers

    I don't see you creating a multi comparator in your script, so your call to "modify" doesn't make any sense. Check out the multi comparator unit tests:
    https://github.com/mbientlab/MetaWear-SDK-Cpp/blob/0.16.3/test/test_dataprocessor.py#L746#L824

  • Thanks Eric. Following earlier threads I found test scripts are helpful.
    I want to monitor threshold crossing of voltage drop between 3V line and pin DIO0 by changing resistance in between.

Sign In or Register to comment.