def subscribe_button_b(self, user_callback): btn_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.btn_b_state_path) btn_iface = tools.get_dbus_iface(constants.DBUS_PROP_IFACE, btn_obj) btn_iface.connect_to_signal('PropertiesChanged', user_callback) btn_obj.StartNotify(reply_handler=self._button_notify_cb, error_handler=tools.generic_error_cb, dbus_interface=constants.GATT_CHRC_IFACE)
def _write_pixels(self, data): """ Utility function for the different display functions :param data: list of 5 numbers in the range 0 to 255 (e.g. [0xff, 0x00, 0, 255, 0b10101] """ pixels_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.led_state_path) pixels_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, pixels_obj) pixels_iface.WriteValue(data, ())
def read_pixels(self): """ Returns a list of 5 binary numbers. Each number represents a row from top to bottom :return: Example [0b1110, 0b10000, 0b10000, 0b10000, 0b1110] """ pixels_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.led_state_path) pixels_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, pixels_obj) rows = pixels_iface.ReadValue(()) return [bin(i) for i in rows]
def _pin_states(self): """ :return: """ pin_states_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.io_pin_data_path) pin_states_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, pin_states_obj) return pin_states_iface.ReadValue(())
def _pin_config(self): """ A bit mask (32 bit) which defines which inputs will be read. A value of 0 means configured for output and 1 means configured for input. """ pin_conf_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.io_pin_config_path) pin_conf_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, pin_conf_obj) return pin_conf_iface.ReadValue(())
def read_temperature(self): """ Temperature from sensors in micro:bit processors :return: Integer of temperature in Celsius """ temp_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.temp_data_path) temp_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, temp_obj) # Read button value tmp_val = temp_iface.ReadValue(()) return int.from_bytes(tmp_val, byteorder='little', signed=True)
def read_bearing(self): """ Compass bearing in degrees from North. :return: degrees in integer """ mag_bear_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.magneto_bearing_path) mag_bear_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, mag_bear_obj) mag_bear_val = mag_bear_iface.ReadValue(()) return int.from_bytes(mag_bear_val, byteorder='little', signed=False)
def _pin_ad_config(self): """ A bit mask (32 bit) which allows each pin to be configured for analogue or digital use. A value of 0 means digital and 1 means analogue. """ pin_ad_conf_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.io_ad_config_path) pin_ad_conf_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, pin_ad_conf_obj) return pin_ad_conf_iface.ReadValue(())
def read_magnetometer(self): """ Exposes magnetometer data. A magnetometer measures a magnetic field such as the earth's magnetic field in 3 axes. :return: List of x, y & z value """ mag_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.magneto_data_path) mag_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, mag_obj) bytes = mag_iface.ReadValue(()) return tools.bytes_to_xyz(bytes)
def _get_dbus_paths(self): """ Utility function to get the paths for UUIDs :return: """ self.blinkt_srv_path = tools.uuid_dbus_path( constants.GATT_SERVICE_IFACE, BLINKT_SRV)[0] self.blinkt_chrc_path = tools.uuid_dbus_path(constants.GATT_CHRC_IFACE, BLINKT_CHRC)[0] self.blinkt_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.blinkt_chrc_path) self.blinkt_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, self.blinkt_obj)
def _pin_states(self, pin_value_pairs): """ Contains data relating to zero or more pins. Structured as a variable length list of up to 19 Pin Number / Value pairs. """ pin_states_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.io_pin_data_path) pin_states_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, pin_states_obj) if states is None: return pin_states_iface.ReadValue(()) else: pin_states_iface.WriteValue([pin_value_pairs], ())
def _pin_ad_config(self, states=None): """ A bit mask (32 bit) which allows each pin to be configured for analogue or digital use. A value of 0 means digital and 1 means analogue. If no states are specified then the current state is returned """ pin_ad_conf_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.io_ad_config_path) pin_ad_conf_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, pin_ad_conf_obj) if states is None: return pin_ad_conf_iface.ReadValue(()) else: pin_ad_conf_iface.WriteValue([states], ())
def read_accelerometer(self): """ Read the values of the accelerometer on the microbit :return: return a list in the order of x, y & z """ # [16, 0, 64, 0, 32, 252] # x=0.16, y=0.024, z=-0.992 accel_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.accel_data_path) accel_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, accel_obj) # Read button value bytes = accel_iface.ReadValue(()) return tools.bytes_to_xyz(bytes)
def _pin_pwn_control(self, pin, value, period): """ Write only method to set the PWM control data :param pin: pin number [range 0-19] :param value: Value is in the range 0 to 1024, per the current DAL API (e.g. setAnalogValue). 0 means OFF. :param period: Period is in microseconds and is an unsigned integer :return: """ pin_pwm_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.io_pin_pwm_path) pin_pwm_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, pin_pwm_obj) return pin_pwm_iface.WriteValue([pin, value, period], ())
def _read_button(self, btn_path): """ Read the button characteristic on the micro:bit and return value 3 button states are defined and represented by a simple numeric enumeration: 0 = not pressed, 1 = pressed, 2 = long press. :param btn_path: The Bluez path to the button characteristic :return: integer representing button value """ # Get characteristic interface for data btn_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, btn_path) btn_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, btn_obj) # Read button value btn_val = btn_iface.ReadValue(()) answer = int.from_bytes(btn_val, byteorder='little', signed=False) return answer
def display_text(self, words): """ Specify text to be displayed. Limit of 20 characters. The content will be restricted to that number of characters. :param words: """ led_txt_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.led_text_path) led_txt_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, led_txt_obj) data = [] text = '' if len(words) > 20: text = words[:19] else: text = words for letter in text: data.append(ord(letter)) led_txt_iface.WriteValue(data, ())
def display_scroll_delay(self, delay=None): """ Specifies a millisecond delay to wait for in between showing each character on the display. """ scroll_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.led_scroll_path) scroll_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, scroll_obj) if delay is None: return int.from_bytes(scroll_iface.ReadValue(()), byteorder='little', signed=False) else: if delay < 0: delay = 0 elif delay > 2**16: delay = 2**16 scroll_iface.WriteValue(tools.int_to_uint16(delay), ())
def _config_temp_iface(self): temp_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.temp_data_path) self.temp_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, temp_obj)
def _config_btn_b_iface(self): btn_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.btn_b_state_path) self.btn_a_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, btn_obj)
def _config_accel_iface(self): accel_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.accel_data_path) self.accel_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, accel_obj)
def _config_pixels_iface(self): pixels_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.led_state_path) self.pixels_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, pixels_obj)
def _config_mag_bear_iface(self): mag_bear_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.magneto_bearing_path) self.mag_bear_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, mag_bear_obj)
def _config_pin_pwm_iface(self): pin_pwm_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.io_pin_pwm_path) self.pin_pwm_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, pin_pwm_obj)
def _config_scroll_iface(self): scroll_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.led_scroll_path) self.scroll_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, scroll_obj)
def _config_led_txt_iface(self): led_txt_obj = tools.get_dbus_obj(constants.BLUEZ_SERVICE_NAME, self.led_text_path) self.led_txt_iface = tools.get_dbus_iface(constants.GATT_CHRC_IFACE, led_txt_obj)