Beispiel #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
Beispiel #2
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:
            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!")
            # Handle the context argument added in MetaWear C++ API 0.12.
            callback = context_callback(callback)
            self.callback = (callback, FnVoid_VoidP_DataP(callback))
            libmetawear.mbl_mw_datasignal_subscribe(
                data_signal, None, self.callback[1])
        else:
            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
Beispiel #3
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]
Beispiel #4
0
    def check_and_change_callback(self, data_signal, callback):
        if callback is not None:

            log.debug("Subscribing to {0} changes. (Sig#: {1})".format(
                self.module_name, data_signal))
            if data_signal in self._callbacks:
                log.debug('Replacing callback for datasignal {0}...'.format(
                    data_signal))
                libmetawear.mbl_mw_datasignal_unsubscribe(data_signal)
                self._callbacks.pop(data_signal)
            callback = context_callback(callback)
            self._callbacks[data_signal] = (callback, FnVoid_VoidP_DataP(callback))
            libmetawear.mbl_mw_datasignal_subscribe(
                data_signal, None, self._callbacks[data_signal][1])
        else:
            if data_signal not in self._callbacks:
                return
            log.debug("Unsubscribing to {0} changes. (Sig#: {1})".format(
                self.module_name, data_signal))
            libmetawear.mbl_mw_datasignal_unsubscribe(data_signal)
            self._callbacks.pop(data_signal)
Beispiel #5
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:
             log.debug('Replacing callback for datasignal {0}...'.format(
                 data_signal))
             libmetawear.mbl_mw_datasignal_unsubscribe(data_signal)
             self._callbacks.pop(data_signal)
         self._callbacks[data_signal] = (callback, FnVoid_DataP(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)
         self._callbacks.pop(data_signal)
Beispiel #6
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()