Example #1
0
    def notifications(self, callback=None):
        """Toggle notifications/subscriptions to data signals
        on the MetaWear board.

        :param callable callback: The function to call when
            data signal notification arrives. If ``None``, an
            unsubscription to notifications is sent.

        """
        data_signal = self.data_signal
        if callback is not None:
            if self._debug:
                log.debug("Subscribing to {0} changes. (Sig#: {1})".format(
                    self.module_name, data_signal))
            if self.callback is not None:
                raise PyMetaWearException(
                    "Subscription to {0} signal already in place!")
            self.callback = (callback, Fn_DataPtr(callback))
            libmetawear.mbl_mw_datasignal_subscribe(data_signal,
                                                    self.callback[1])
        else:
            if self._debug:
                log.debug("Unsubscribing to {0} changes. (Sig#: {1})".format(
                    self.module_name, data_signal))
            if self.callback is None:
                return
            libmetawear.mbl_mw_datasignal_unsubscribe(data_signal)
            self.callback = None
Example #2
0
 def check_and_change_callback(self, data_signal, callback):
     if callback is not None:
         if self._debug:
             log.debug("Subscribing to {0} changes. (Sig#: {1})".format(
                 self.module_name, data_signal))
         if data_signal in self._callbacks:
             raise PyMetaWearException(
                 "Subscription to {0} signal already in place!")
         self._callbacks[data_signal] = (callback, Fn_DataPtr(callback))
         libmetawear.mbl_mw_datasignal_subscribe(
             data_signal, self._callbacks[data_signal][1])
     else:
         if data_signal not in self._callbacks:
             return
         if self._debug:
             log.debug("Unsubscribing to {0} changes. (Sig#: {1})".format(
                 self.module_name, data_signal))
         libmetawear.mbl_mw_datasignal_unsubscribe(data_signal)
         del self._callbacks[data_signal]
Example #3
0
print("New client created: {0}".format(c))


def switch_callback(data):
    if (data.contents.type_id == DataTypeId.UINT32):
        data_ptr = cast(data.contents.value, POINTER(c_uint))
        if data_ptr.contents.value == 1:
            print("Switch pressed!")
        elif data_ptr.contents.value == 0:
            print("Switch released!")
        else:
            raise ValueError("Incorrect data returned.")
    else:
        raise RuntimeError('Incorrect data type id: ' +
                           str(data.contents.type_id))


# Subscribe
data_signal = libmetawear.mbl_mw_switch_get_state_data_signal(c.board)
fcn_dptr = Fn_DataPtr(switch_callback)
libmetawear.mbl_mw_datasignal_subscribe(data_signal, fcn_dptr)

time.sleep(10.0)

# Unsubscribe:
libmetawear.mbl_mw_datasignal_unsubscribe(data_signal)

time.sleep(1.0)

c.disconnect()
Example #4
0
address = select_device()
c = MetaWearClient(str(address), 'pygatt', debug=True)
print("New client created: {0}".format(c))


def _callback(data):
    """Handle a (x,y,z) gyroscope tuple."""
    if data.contents.type_id == DataTypeId.CARTESIAN_FLOAT:
        data_ptr = cast(data.contents.value, POINTER(CartesianFloat))
        print("X: {0}, Y: {1}, Z: {2}".format(*(data_ptr.contents.x,
                                     data_ptr.contents.y,
                                     data_ptr.contents.z)))
    else:
        raise PyMetaWearException('Incorrect data type id: ' +
            str(data.contents.type_id))
the_callback = Fn_DataPtr(_callback)


print("Set gyroscope settings...")
libmetawear.mbl_mw_gyro_bmi160_set_odr(c.board, 9)
libmetawear.mbl_mw_gyro_bmi160_set_range(c.board, 1)
print("Write gyroscope config....")
libmetawear.mbl_mw_gyro_bmi160_write_config(c.board)
print("Subscribing to gyroscope signal...")
data_signal = libmetawear.mbl_mw_gyro_bmi160_get_rotation_data_signal(c.board)
libmetawear.mbl_mw_datasignal_subscribe(data_signal, the_callback)
print("Enable gyro sampling on board...")
libmetawear.mbl_mw_gyro_bmi160_enable_rotation_sampling(c.board)
print("Start gyro on board...")
libmetawear.mbl_mw_gyro_bmi160_start(c.board)
Example #5
0

def temperature_callback(data):
    if data.contents.type_id == DataTypeId.FLOAT:
        data_ptr = cast(data.contents.value, POINTER(c_float))
        print("Temperature read: {0:.2f} C".format(data_ptr.contents.value))
    else:
        raise RuntimeError('Incorrect data type id: ' +
                           str(data.contents.type_id))


# On-Die temperature (channel 0) is always present.
channel = 0

# Subscribe to notifications
data_signal = libmetawear.mbl_mw_multi_chnl_temp_get_temperature_data_signal(
    c.board, channel)
fcn_dptr = Fn_DataPtr(temperature_callback)
libmetawear.mbl_mw_datasignal_subscribe(data_signal, fcn_dptr)

for i in range(5):
    libmetawear.mbl_mw_datasignal_read(data_signal)
    time.sleep(1.0)

# Unsubscribe to notifications
libmetawear.mbl_mw_datasignal_unsubscribe(data_signal)

time.sleep(1.0)

c.disconnect()
Example #6
0
address = select_device()
c = MetaWearClient(str(address), 'pygatt', debug=True)
print("New client created: {0}".format(c))


def acc_callback(data):
    """Handle a (x,y,z) accelerometer tuple."""
    if data.contents.type_id == DataTypeId.CARTESIAN_FLOAT:
        data_ptr = cast(data.contents.value, POINTER(CartesianFloat))
        print("X: {0}, Y: {1}, Z: {2}".format(*(data_ptr.contents.x,
                                     data_ptr.contents.y,
                                     data_ptr.contents.z)))
    else:
        raise PyMetaWearException('Incorrect data type id: ' +
            str(data.contents.type_id))
_acc_callback = Fn_DataPtr(acc_callback)


print("Write accelerometer settings...")
libmetawear.mbl_mw_acc_set_odr(c.board, c_float(50.0))
libmetawear.mbl_mw_acc_set_range(c.board, c_float(4.0))
print("Write accelerometer config....")
libmetawear.mbl_mw_acc_write_acceleration_config(c.board)

print("Subscribing to accelerometer signal...")
data_signal = libmetawear.mbl_mw_acc_get_acceleration_data_signal(c.board)
libmetawear.mbl_mw_datasignal_subscribe(data_signal, _acc_callback)
print("Enable acc sampling on board...")
libmetawear.mbl_mw_acc_enable_acceleration_sampling(c.board)
print("Start acc on board...")
libmetawear.mbl_mw_acc_start(c.board)