def set_pin(self, pin_number, pin_input, pin_analogue): """ For a given pin, set the direction and type for the microbit pin. :param pin_number: Pin number of the microbit :param pin_input: False for output, True for input :param pin_analogue: False for digital, True for analogue :return: """ pin_bit = tools.int_to_uint32(2 ** pin_number) current_io_setting = self._pin_config current_ad_setting = self._pin_ad_config if pin_input: new_setting = tools.bitwise_or_2lists(pin_bit, current_io_setting) self._pin_config = new_setting else: pin_mask = tools.bitwise_xor_2lists(pin_bit, [0xff, 0xff, 0xff, 0xff]) new_setting = tools.bitwise_and_2lists(pin_mask, current_io_setting) self._pin_config = new_setting if pin_analogue: new_setting = tools.bitwise_or_2lists(pin_bit, current_ad_setting) self._pin_ad_config = new_setting else: pin_mask = tools.bitwise_xor_2lists(pin_bit, [0xff, 0xff, 0xff, 0xff]) new_setting = tools.bitwise_and_2lists(pin_mask, current_ad_setting) self._pin_ad_config = new_setting
def set_pin(self, pin_number, pin_input, pin_analogue): """ For a given pin, set the direction and type for the microbit pin. :param pin_number: Pin number of the microbit :param pin_input: False for output, True for input :param pin_analogue: False for digital, True for analogue :return: """ pin_bit = tools.int_to_uint32(2**pin_number) current_io_setting = self._pin_config current_ad_setting = self._pin_ad_config if pin_input: new_setting = tools.bitwise_or_2lists(pin_bit, current_io_setting) self._pin_config = new_setting else: pin_mask = tools.bitwise_xor_2lists(pin_bit, [0xff, 0xff, 0xff, 0xff]) new_setting = tools.bitwise_and_2lists(pin_mask, current_io_setting) self._pin_config = new_setting if pin_analogue: new_setting = tools.bitwise_or_2lists(pin_bit, current_ad_setting) self._pin_ad_config = new_setting else: pin_mask = tools.bitwise_xor_2lists(pin_bit, [0xff, 0xff, 0xff, 0xff]) new_setting = tools.bitwise_and_2lists(pin_mask, current_ad_setting) self._pin_ad_config = new_setting
def _update_motors(self, left_val, right_val, left_rev, right_rev, pwm_period=20000): period = tools.int_to_uint32(pwm_period) left_pwm = tools.int_to_uint16(left_val) right_pwm = tools.int_to_uint16(right_val) self.ubit._io_pin_pwm.value = [ 0, left_pwm[0], left_pwm[1], period[0], period[1], period[2], period[3], 1, right_pwm[0], right_pwm[1], period[0], period[1], period[2], period[3] ] self.ubit._pin_states = [0x08, left_rev, 0x0C, right_rev]
def _update_motors(self, left_val, right_val, left_rev, right_rev, pwm_period=20000): period = tools.int_to_uint32(pwm_period) left_pwm = tools.int_to_uint16(left_val) right_pwm = tools.int_to_uint16(right_val) self.pin_pwm_iface.WriteValue([ 0, left_pwm[0], left_pwm[1], period[0], period[1], period[2], period[3], 1, right_pwm[0], right_pwm[1], period[0], period[1], period[2], period[3] ], ()) self._pin_states([0x08, left_rev, 0x0C, right_rev])
def _pin_pwm_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: """ byte_value = tools.int_to_uint16(value) byte_period = tools.int_to_uint32(period) self.pin_pwm_iface.WriteValue([ pin, byte_value[0], byte_value[1], byte_period[0], byte_period[1], byte_period[2], byte_period[3] ], ())
def _pin_pwm_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) byte_value = tools.int_to_uint16(value) byte_period = tools.int_to_uint32(period) pin_pwm_iface.WriteValue([ pin, byte_value[0], byte_value[1], byte_period[0], byte_period[1], byte_period[2], byte_period[3] ], ())
def _update_motors(self, left_val, right_val, left_rev, right_rev, pwm_period=20000): period = tools.int_to_uint32(pwm_period) left_pwm = tools.int_to_uint16(left_val) right_pwm = tools.int_to_uint16(right_val) self.ubit._io_pin_pwm.value = [0, left_pwm[0], left_pwm[1], period[0], period[1], period[2], period[3], 1, right_pwm[0], right_pwm[1], period[0], period[1], period[2], period[3] ] self.ubit._pin_states = [0x08, left_rev, 0x0C, right_rev]
def _pin_pwm_control(self, data): """ 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 = data[0] value = data[1] period = data[2] byte_value = tools.int_to_uint16(value) byte_period = tools.int_to_uint32(period) self._io_pin_pwm.value = [pin, byte_value[0], byte_value[1], byte_period[0], byte_period[1], byte_period[2], byte_period[3] ]
def test_all_full(self): little_endian = tools.int_to_uint32(305419896) self.assertListEqual(little_endian, [0x78, 0x56, 0x34, 0x12])
def test_with_zeros(self): little_endian = tools.int_to_uint32(2094) self.assertListEqual(little_endian, [0x2E, 0x08, 0x00, 0x00])