def test_output_objects(self): OUTPUT_SLEEP_DELAY = 0.01 # test there are no outputs self.assertEqual(0, pfio.read_output()) for led_num in range(1, 5): this_led = pfio.LED(led_num) this_led.turn_on() sleep(OUTPUT_SLEEP_DELAY) expected_output_bpat = 1 << (led_num-1) self.assertEqual(expected_output_bpat, pfio.read_output()) this_led.turn_off() sleep(OUTPUT_SLEEP_DELAY) self.assertEqual(0, pfio.read_output()) for relay_num in range(1, 3): this_relay = pfio.Relay(relay_num) this_relay.turn_on() sleep(OUTPUT_SLEEP_DELAY) expected_output_bpat = 1 << (relay_num-1) self.assertEqual(expected_output_bpat, pfio.read_output()) this_relay.turn_off() sleep(OUTPUT_SLEEP_DELAY) self.assertEqual(0, pfio.read_output())
def sensor_update(self, data): index_is_data = False # ignore the loop contents if not sensor zero_bit_mask = 0 # bit mask showing where zeros should be written one_bit_mask = 0 # bit mask showing where ones should be written we_should_update_piface = False # go through all of the sensors that have been updated for i in range(len(data)): if index_is_data: index_is_data = False continue sensor_name = data[i].strip('"') # if this sensor is a piface output then reflect # that update on the board if sensor_name in SCRATCH_SENSOR_NAME_OUTPUT: we_should_update_piface = True pin_index = SCRATCH_SENSOR_NAME_OUTPUT.index(sensor_name) sensor_value = int(data[i+1]) index_is_data = True # could this be made more efficient by sending a single write if sensor_value == 0: zero_bit_mask ^= (1 << pin_index) else: one_bit_mask ^= (1 << pin_index) if we_should_update_piface: old_pin_bitp = pfio.read_output() # grab the old values new_pin_bitp = old_pin_bitp & ~zero_bit_mask # set the zeros new_pin_bitp |= one_bit_mask # set the ones if new_pin_bitp != old_pin_bitp: pfio.write_output(new_pin_bitp) # write the new bit pattern
def sensor_update(self, data): index_is_data = False # ignore the loop contents if not sensor zero_bit_mask = 0 # bit mask showing where zeros should be written one_bit_mask = 0 # bit mask showing where ones should be written we_should_update_piface = False # go through all of the sensors that have been updated for i in range(len(data)): if index_is_data: index_is_data = False continue sensor_name = data[i].strip('"') # if this sensor is a piface output then reflect # that update on the board if sensor_name in SCRATCH_SENSOR_NAME_OUTPUT: we_should_update_piface = True pin_index = SCRATCH_SENSOR_NAME_OUTPUT.index(sensor_name) sensor_value = int(data[i + 1]) index_is_data = True # could this be made more efficient by sending a single write if sensor_value == 0: zero_bit_mask ^= (1 << pin_index) else: one_bit_mask ^= (1 << pin_index) if we_should_update_piface: old_pin_bitp = pfio.read_output() # grab the old values new_pin_bitp = old_pin_bitp & ~zero_bit_mask # set the zeros new_pin_bitp |= one_bit_mask # set the ones if new_pin_bitp != old_pin_bitp: pfio.write_output(new_pin_bitp) # write the new bit pattern