async def run_example(packet, callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) # Register the callback queue used by process_callbacks() # We can register the same queue for multiple callbacks. bricklet.register_event_queue(bricklet.CallbackID.MOTION_DETECTED, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.DETECTION_CYCLE_ENDED, callback_queue) # Query the value print('Motion detected?:', await bricklet.get_motion_detected()) print('Set sensitivity to maximum and wait for callbacks') await bricklet.set_sensitivity(100) print('Sensitivity set to {value}.'.format( value=await bricklet.get_sensitivity())) print('Enabling lights.') await bricklet.set_indicator(top_left=255, top_right=255, bottom=255) print('Indicator status:', await bricklet.get_indicator()) print('Waiting 10 seconds for callbacks') await asyncio.sleep(10) # Wait for callbacks print('Done waiting.') # Test the generic features of the bricklet. These are available with all # new bricklets that have a microcontroller await run_example_generic(bricklet) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) # Register the callback queue used by process_callbacks() # We can register the same queue for multiple callbacks. bricklet.register_event_queue(bricklet.CallbackID.HUMIDITY_REACHED, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.ANALOG_VALUE_REACHED, callback_queue) print('Set callback period to', 1000, 'ms') await bricklet.set_humidity_callback_period(1000) print('Get callback period:', await bricklet.get_humidity_callback_period()) print('Set bricklet debounce period to', 1000, 'ms') await bricklet.set_debounce_period(1000) print('Get bricklet debounce period:', await bricklet.get_debounce_period()) # Use a humidity callback print('Get humidity:', await bricklet.get_humidity()) print('Set threshold to >10 %rH and wait for callbacks') # We use a low humidity on purpose, so that the callback will be triggered await bricklet.set_humidity_callback_threshold( bricklet.ThresholdOption.GREATER_THAN, 10, 0) print('Humidity threshold:', await bricklet.get_humidity_callback_threshold()) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disabling threshold callback') await bricklet.set_humidity_callback_threshold() print('Humidity threshold:', await bricklet.get_humidity_callback_threshold()) # Use an analog value callback print('Get analog value:', await bricklet.get_analog_value()) print('Set threshold to >10 and wait for callbacks') await bricklet.set_analog_value_callback_threshold( bricklet.ThresholdOption.GREATER_THAN, 10, 0) print('Analog value threshold:', await bricklet.get_analog_value_callback_threshold()) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disabling threshold callback') await bricklet.set_analog_value_callback_threshold() print('Analog value threshold:', await bricklet.get_analog_value_callback_threshold()) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) # Register the callback queue used by process_callbacks() # We can register the same queue for multiple callbacks. bricklet.register_event_queue(bricklet.CallbackID.TEMPERATURE, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.RESISTANCE, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.SENSOR_CONNECTED, callback_queue) filter_settings = await bricklet.get_noise_rejection_filter() print('Noise rejection filter settings', filter_settings) await bricklet.set_noise_rejection_filter(filter_settings) moving_average_config = await bricklet.get_moving_average_configuration() print('Moving average config:', moving_average_config) await bricklet.set_moving_average_configuration(**moving_average_config._asdict()) print('Is a sensor connected?', await bricklet.is_sensor_connected()) wire_mode = await bricklet.get_wire_mode() print('Wire mode:', wire_mode) await bricklet.set_wire_mode(wire_mode) print('PTC temperature:', await bricklet.get_temperature(), '°C') print('PTC resistance:', await bricklet.get_resistance(), 'Ω') # Use a temperature and resistance value callback print('Set callback period to', 1000, 'ms and wait for callbacks') await asyncio.gather(bricklet.set_temperature_callback_configuration(period=1000), bricklet.set_resistance_callback_configuration(period=1000), bricklet.set_sensor_connected_callback_configuration(True)) print('Temperature callback configuration:', await bricklet.get_temperature_callback_configuration()) print('Resistance callback configuration:', await bricklet.get_resistance_callback_configuration()) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disable callbacks') await asyncio.gather(bricklet.set_temperature_callback_configuration(), bricklet.set_resistance_callback_configuration(), bricklet.set_sensor_connected_callback_configuration()) print('Temperature callback configuration:', await bricklet.get_temperature_callback_configuration()) print('Resistance callback configuration:', await bricklet.get_resistance_callback_configuration()) # Test the generic features of the bricklet. These are available with all # new bricklets that have a microcontroller await run_example_generic(bricklet) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) # Register the callback queue used by process_callbacks() # We can register the same queue for multiple callbacks. bricklet.register_event_queue(bricklet.CallbackID.TEMPERATURE, callback_queue) # Query the value print('Get temperature:', await bricklet.get_temperature()) print('Set callback period to', 1000, 'ms') print('Set threshold to >10 °C and wait for callbacks') # We use a low temperature value on purpose, so that the callback will be triggered await bricklet.set_temperature_callback_configuration( period=1000, value_has_to_change=False, option=bricklet.ThresholdOption.GREATER_THAN, minimum=10, maximum=0) print('Temperature callback configuration:', await bricklet.get_temperature_callback_configuration()) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disable threshold callback') await bricklet.set_temperature_callback_configuration() print('Temperature callback configuration:', await bricklet.get_temperature_callback_configuration()) print('Enabling heater') await bricklet.set_heater_configuration(bricklet.HeaterConfig.ENABLED) print('Heater config:', await bricklet.get_heater_configuration()) print('Disabling heater') await bricklet.set_heater_configuration() print('Heater config:', await bricklet.get_heater_configuration()) # Test the generic features of the bricklet. These are available with all # new bricklets that have a microcontroller await run_example_generic(bricklet) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) # Register the callback queue used by process_callbacks() # We can register the same queue for multiple callbacks. bricklet.register_event_queue(bricklet.CallbackID.ILLUMINANCE, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.ILLUMINANCE_REACHED, callback_queue) config = await bricklet.get_configuration() print('Configuration:', config) new_config = config._asdict() new_config['illuminance_range'] = bricklet.IlluminanceRange.RANGE_600LUX await bricklet.set_configuration(**new_config) print('Set callback period to', 1000, 'ms') await bricklet.set_illuminance_callback_period(1000) print('Get callback period:', await bricklet.get_illuminance_callback_period()) print('Set bricklet debounce period to', 1000, 'ms') await bricklet.set_debounce_period(1000) print('Get bricklet debounce period:', await bricklet.get_debounce_period()) print('Set threshold to >1 lux and wait for callbacks') # We use a low illuminance on purpose, so that the callback will be triggered await bricklet.set_illuminance_callback_threshold( bricklet.ThresholdOption.GREATER_THAN, 1, 0) print('Illuminance threshold:', await bricklet.get_illuminance_callback_threshold()) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disabling threshold callback') await bricklet.set_illuminance_callback_threshold() print('Illumiance threshold:', await bricklet.get_illuminance_callback_threshold()) print('Get illumiance:', await bricklet.get_illuminance()) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) # Register the callback queue used by process_callbacks() bricklet.register_event_queue(bricklet.CallbackID.MOISTURE, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.MOISTURE_REACHED, callback_queue) print('Setting moving average to 20 samples') await bricklet.set_moving_average(20) print('Moving average configuration:', await bricklet.get_moving_average()) print('Resetting moving average') await bricklet.set_moving_average() print('Set callback period to', 1000, 'ms') await bricklet.set_moisture_callback_period(1000) print('Get callback period:', await bricklet.get_moisture_callback_period()) print('Set bricklet debounce period to', 1000, 'ms') await bricklet.set_debounce_period(1000) print('Get bricklet debounce period:', await bricklet.get_debounce_period()) print('Set threshold to >10 and wait for callbacks') # We use a low 'moisture value' on purpose, so that the callback will be triggered await bricklet.set_moisture_callback_threshold( bricklet.ThresholdOption.GREATER_THAN, 10, 0) print('Moisture threshold:', await bricklet.get_moisture_callback_threshold()) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disabling threshold callback') await bricklet.set_moisture_callback_threshold() print('Moisture threshold:', await bricklet.get_moisture_callback_threshold()) print('Get Moisture:', await bricklet.get_moisture_value()) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) # Register the callback queue used by process_callbacks() # We can register the same queue for multiple callbacks. bricklet.register_event_queue(bricklet.CallbackID.ILLUMINANCE, callback_queue) await bricklet.set_configuration(bricklet.IlluminanceRange.LUX64000, bricklet.IntegrationTime.T150MS) print('Bricklet configuration:', await bricklet.get_configuration()) # Query the value print('Get illuminance:', await bricklet.get_illuminance()) print('Set callback period to', 1000, 'ms') await bricklet.set_illuminance_callback_configuration( period=1000, value_has_to_change=False, option=bricklet.ThresholdOption.OFF, minimum=0, maximum=0) print('Illuminance callback configuration:', await bricklet.get_illuminance_callback_configuration()) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disable threshold callback') await bricklet.set_illuminance_callback_configuration() print('Illuminance callback configuration:', await bricklet.get_illuminance_callback_configuration()) # Test the generic features of the bricklet. These are available with all # new bricklets that have a microcontroller await run_example_generic(bricklet) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) # Register the callback queue used by process_callbacks() bricklet.register_event_queue(bricklet.CallbackID.COUNTER_FINISHED, callback_queue) print('Setting segments to "00:00"') await bricklet.set_segments(segments=(DIGITS[0], DIGITS[0], DIGITS[0], DIGITS[0]), brightness=3, colon=True) print('Get segments:', await bricklet.get_segments()) print('Counting from 0 to 5 and back again') await bricklet.start_counter(value_from=0, value_to=5, increment=1, length=1000) for _ in range(6): print('Counter value:', await bricklet.get_counter_value()) await asyncio.sleep(1) # Wait for 1 second await bricklet.start_counter(value_from=5, value_to=0, increment=-1, length=1000) for _ in range(5): await asyncio.sleep(1) # Wait for 1 second print('Counter value:', await bricklet.get_counter_value()) print('Turning off segments') await bricklet.set_segments() # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) # Register the callback queue used by process_callbacks() # We can register the same queue for multiple callbacks. bricklet.register_event_queue(bricklet.CallbackID.TEMPERATURE, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.TEMPERATURE_REACHED, callback_queue) print('Set callback period to', 1000, 'ms') await bricklet.set_temperature_callback_period(1000) print('Get callback period:', await bricklet.get_temperature_callback_period()) print('Get I²C mode:', await bricklet.get_i2c_mode()) print('Set I²C mode to', bricklet.I2cOption.SLOW) await bricklet.set_i2c_mode(bricklet.I2cOption.SLOW) print('Get I²C mode:', await bricklet.get_i2c_mode()) print('Set I²C mode to default') await bricklet.set_i2c_mode() print('Set bricklet debounce period to', 1000, 'ms') await bricklet.set_debounce_period(1000) print('Get bricklet debounce period:', await bricklet.get_debounce_period()) print('Set threshold to >10 °C and wait for callbacks') # We use a low temperature on purpose, so that the callback will be triggered await bricklet.set_temperature_callback_threshold(bricklet.ThresholdOption.GREATER_THAN, 10, 0) print('Temperature threshold:', await bricklet.get_temperature_callback_threshold()) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disabling threshold callback') await bricklet.set_temperature_callback_threshold() print('Temperature threshold:', await bricklet.get_temperature_callback_threshold()) print('Get temperature:', await bricklet.get_temperature()) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) # Register the callback queue used by process_callbacks() # We can register the same queue for multiple callbacks. bricklet.register_event_queue(bricklet.CallbackID.AIR_PRESSURE, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.ALTITUDE, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.TEMPERATURE, callback_queue) print('Moving average configuration:', await bricklet.get_moving_average_configuration()) await bricklet.set_moving_average_configuration(100, 100) print('Moving average configuration:', await bricklet.get_moving_average_configuration()) # Query a value print('Get air pressure:', await bricklet.get_air_pressure()) # Use an air pressure callback print('Set callback period to', 1000, 'ms') print('Set threshold to >500 hPa and wait for callbacks') # We use a low pressure value on purpose, so that the callback will be triggered await bricklet.set_air_pressure_callback_configuration( period=1000, value_has_to_change=False, option=bricklet.ThresholdOption.GREATER_THAN, minimum=500, maximum=0) print('Air pressure callback configuration:', await bricklet.get_air_pressure_callback_configuration()) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disable threshold callback') await bricklet.set_air_pressure_callback_configuration() print('Air pressure callback configuration:', await bricklet.get_air_pressure_callback_configuration()) # Use a altitude value callback print('Get altitude:', await bricklet.get_altitude()) # Use an altitude callback print('Set callback period to', 1000, 'ms') print('Set threshold to >-100 m and < 5000 m and wait for callbacks') # We use a low altitude value on purpose, so that the callback will be triggered await bricklet.set_altitude_callback_configuration( period=1000, value_has_to_change=False, option=bricklet.ThresholdOption.GREATER_THAN, minimum=-1000, maximum=5000) print('Altitude callback configuration:', await bricklet.get_altitude_callback_configuration()) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disable threshold callback') await bricklet.set_altitude_callback_configuration() print('Altitude callback configuration:', await bricklet.get_altitude_callback_configuration()) # Use a temperature value callback print('Get temperature:', await bricklet.get_temperature()) print('Set callback period to', 1000, 'ms') print('Set threshold to >10 °C and wait for callbacks') await bricklet.set_temperature_callback_configuration( 1000, False, bricklet.ThresholdOption.GREATER_THAN, 10, 0) print('Temperature callback configuration:', await bricklet.get_temperature_callback_configuration()) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disable threshold callback') await bricklet.set_temperature_callback_configuration() print('Temperature callback configuration:', await bricklet.get_temperature_callback_configuration()) print('Enable two callbacks at once') await asyncio.gather( bricklet.set_temperature_callback_configuration(1000, False), bricklet.set_air_pressure_callback_configuration(1000, False)) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disable all callbacks') await asyncio.gather(bricklet.set_temperature_callback_configuration(), bricklet.set_air_pressure_callback_configuration()) pressure_ref = await bricklet.get_reference_air_pressure() print('Reference air pressure:', pressure_ref, 'hPa') await bricklet.set_reference_air_pressure(pressure_ref) print('Calibration constants:', await bricklet.get_reference_air_pressure()) sensor_config = await bricklet.get_sensor_configuration() print('Sensor configuration:', sensor_config) await bricklet.set_sensor_configuration( sensor_config.data_rate, sensor_config.air_pressure_low_pass_filter) # Test the generic features of the bricklet. These are available with all # new bricklets that have a microcontroller await run_example_generic(bricklet) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) # Register the callback queue used by process_callbacks() # We can register the same queue for multiple callbacks. bricklet.register_event_queue(bricklet.CallbackID.COUNTER_FINISHED, callback_queue) print('Setting segments to "00:00"') await bricklet.set_segments(segments=(DIGITS[0], DIGITS[0], DIGITS[0], DIGITS[0]), colon=(True, True), tick=False) print('Get segments:', await bricklet.get_segments()) #await asyncio.sleep(3) print('Setting segments to "10.00"') await bricklet.set_segments(segments=(DIGITS[1], DIGITS[0] | 128, DIGITS[0], DIGITS[0]), colon=(False, False), tick=False) print('Get segments:', await bricklet.get_segments()) #await asyncio.sleep(3) print('Setting segments to "10°C"') await bricklet.set_segments(segments=(0, DIGITS[1], DIGITS[0], DIGITS[12]), colon=(False, False), tick=True) print('Get segments:', await bricklet.get_segments()) #await asyncio.sleep(3) print('Flashing the display') await bricklet.set_brightness(0) print('Display brightness:', await bricklet.get_brightness()) await asyncio.sleep(0.5) await bricklet.set_brightness(7) print('Display brightness:', await bricklet.get_brightness()) print('Setting segments to "- 42"') await asyncio.gather(bricklet.set_segments(), bricklet.set_numeric_value((-2, -1, 4, 2))) #await asyncio.sleep(3) print("Toggle the tick") is_set = await bricklet.get_selected_segment(34) for _ in range(5): await bricklet.set_selected_segment(34, not is_set) await asyncio.sleep(0.5) await bricklet.set_selected_segment(34, is_set) await asyncio.sleep(0.5) print('Counting from 0 to 5 and back again') await bricklet.start_counter(value_from=0, value_to=5, increment=1, length=1000) for _ in range(6): print('Counter value:', await bricklet.get_counter_value()) await asyncio.sleep(1) # Wait for 1 second await bricklet.start_counter(value_from=5, value_to=0, increment=-1, length=1000) for _ in range(5): await asyncio.sleep(1) # Wait for 1 second print('Counter value:', await bricklet.get_counter_value()) # Test the generic features of the bricklet. These are available with all # new bricklets that have a microcontroller await run_example_generic(bricklet) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is the actual demo. If the brick is found, this code will be run. """ print('Registering master brick') master = device_factory.get(packet['device_id'], packet['uid'], ipcon) print('Identity:', await master.get_identity()) debounce_period = await master.get_debounce_period() print('Debounce period:', debounce_period, 'ms') await master.set_debounce_period(debounce_period) print('Connection type used to control Brick:', await master.get_connection_type()) print('USB voltage: ', await master.get_usb_voltage(), 'V') print('Stack voltage:', await master.get_stack_voltage(), 'V') print('Stack current:', await master.get_stack_current(), 'A') print('Chip temperature:', await master.get_chip_temperature(), '°C') print('##################\nCallbacks:') master.register_event_queue(master.CallbackID.STACK_CURRENT, callback_queue) master.register_event_queue(master.CallbackID.STACK_VOLTAGE, callback_queue) master.register_event_queue(master.CallbackID.USB_VOLTAGE, callback_queue) await asyncio.gather( master.set_usb_voltage_callback_period(100), master.set_stack_voltage_callback_period(110), master.set_stack_current_callback_period(120), master.set_usb_voltage_callback_threshold(master.ThresholdOption.OFF, 0, 0), master.set_stack_voltage_callback_threshold(master.ThresholdOption.OFF, 0, 0), master.set_stack_current_callback_threshold(master.ThresholdOption.OFF, 0, 0), ) print('USB voltage callback period: ', await master.get_usb_voltage_callback_period(), 'ms') print('Stack voltage callback period:', await master.get_stack_voltage_callback_period(), 'ms') print('Stack current callback period:', await master.get_stack_current_callback_period(), 'ms') print('USB voltage callback threshold: ', await master.get_usb_voltage_callback_threshold()) print('Stack voltage callback threshold:', await master.get_stack_voltage_callback_threshold()) print('Stack current callback threshold:', await master.get_stack_current_callback_threshold()) print('Waiting 1 second for callbacks...') await asyncio.sleep(1) await asyncio.gather(master.set_usb_voltage_callback_period(0), master.set_stack_voltage_callback_period(0), master.set_stack_current_callback_period(0)) print('##################\nExtensions:') if await master.is_chibi_present(): await run_master_extension_chibi(master) if await master.is_rs485_present(): await run_master_extension_rs485(master) if await master.is_wifi_present(): await run_master_extension_wifi(master) if await master.is_ethernet_present(): await run_master_extension_ethernet(master) if await master.is_wifi2_present(): await run_master_extension_wifi2(master) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue, frame_readable_callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) config = await bricklet.get_configuration() print('Configuration:', config) config = config._asdict() config['baudrate'] = 2000000 await bricklet.set_configuration(**config) config = await bricklet.get_buffer_config() print('Buffer configuration:', config) await bricklet.set_buffer_config(**config._asdict()) print('Buffer status:', await bricklet.get_buffer_status()) print('Errors:', await bricklet.get_error_count()) frame_size = await bricklet.get_frame_readable_callback_configuration() print('Trigger callback if number of bytes available:', frame_size) await bricklet.set_frame_readable_callback_configuration(frame_size) # Register the callback queue used by process_callbacks() # We can register the same queue for multiple callbacks. bricklet.register_event_queue(bricklet.CallbackID.READ, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.ERROR_COUNT, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.FRAME_READABLE, frame_readable_callback_queue) # Disable read callback to buffer a multi chunk message await bricklet.set_read_callback(False) await bricklet.set_frame_readable_callback_configuration(0) print('Sending message, then enable the read callback.') msg = b'foo' * 30 await bricklet.write(msg) await bricklet.set_read_callback(True) print('Read callback enabled?', await bricklet.is_read_callback_enabled()) await asyncio.sleep(0.1) print('Disabling read callback') await bricklet.set_read_callback() # Use a frame readable callback print('Enabling Frame readable callback') await bricklet.set_frame_readable_callback_configuration(frame_size=3) await bricklet.write(b'foo' * 3) await asyncio.sleep(0.1) print('Disabling Frame readable callback') await bricklet.set_frame_readable_callback_configuration() print('Polling the rs232 port') await bricklet.write(b'foo' * 3) result = await bricklet.read(len(msg)) print('Result:', result, 'number of bytes:', len(result)) # Test the generic features of the bricklet. These are available with all # new bricklets that have a microcontroller await run_example_generic(bricklet) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) # Register the callback queue used by process_callbacks() # We can register the same queue for multiple callbacks. bricklet.register_event_queue(bricklet.CallbackID.TEMPERATURE, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.TEMPERATURE_REACHED, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.RESISTANCE, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.RESISTANCE_REACHED, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.SENSOR_CONNECTED, callback_queue) filter_settings = await bricklet.get_noise_rejection_filter() print('Noise rejection filter settings:', filter_settings) await bricklet.set_noise_rejection_filter(filter_settings) print('Is a sensor connected?', await bricklet.is_sensor_connected()) print('Sensor type:', bricklet.sensor_type) bricklet.sensor_type = bricklet.SensorType.PT_100 wire_mode = await bricklet.get_wire_mode() print('Wire mode:', wire_mode) await bricklet.set_wire_mode(wire_mode) print('PTC temperature:', await bricklet.get_temperature(), '°C') print('PTC resistance:', await bricklet.get_resistance(), 'Ω') # Use a temperature and resistance value callback print('Set callback period to', 1000, 'ms and wait for callbacks') await asyncio.gather( bricklet.set_temperature_callback_period(1000), bricklet.set_resistance_callback_period(1000), bricklet.set_sensor_connected_callback_configuration(True), bricklet.set_temperature_callback_threshold( option=bricklet.ThresholdOption.GREATER_THAN, minimum=0, maximum=0), bricklet.set_resistance_callback_threshold( option=bricklet.ThresholdOption.GREATER_THAN, minimum=10, maximum=0)) print('Get temperature callback period:', await bricklet.get_temperature_callback_period()) print('Get resistance callback period:', await bricklet.get_resistance_callback_period()) print('Get sensor connected callback configuration:', await bricklet.get_sensor_connected_callback_configuration()) debounce_period = await bricklet.get_debounce_period() print('Set bricklet debounce period to', debounce_period, 'ms') await bricklet.set_debounce_period(debounce_period) print('Get bricklet debounce period:', await bricklet.get_debounce_period()) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disable callbacks') await asyncio.gather( bricklet.set_temperature_callback_period(), bricklet.set_resistance_callback_period(), bricklet.set_sensor_connected_callback_configuration(False), bricklet.set_temperature_callback_threshold(), bricklet.set_resistance_callback_threshold()) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is a demo of the generic features of the Tinkerforge bricklets with a microcontroller. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) # Register the callback queue used by process_callbacks() # We can register the same queue for multiple callbacks. bricklet.register_event_queue(bricklet.CallbackID.VOLTAGE, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.ALL_VOLTAGES, callback_queue) await bricklet.set_sample_rate(bricklet.SamplingRate.RATE_1_SPS) print('Sampling rate:', await bricklet.get_sample_rate()) cal_data = await bricklet.get_calibration() print('Calibration data:', cal_data) await bricklet.set_calibration(**cal_data._asdict()) print('ADC raw values (with offset subtracted):', await bricklet.get_adc_values()) # Query LEDs print('Channel 0 led configuration:', await bricklet.get_channel_led_config(0)) print('Channel 1 led config:', await bricklet.get_channel_led_config(1)) led_status_config = await bricklet.get_channel_led_status_config(0) print('Channel 0 led status config', led_status_config) await bricklet.set_channel_led_status_config(0, **led_status_config._asdict()) led_status_config = await bricklet.get_channel_led_status_config(1) print('Channel 1 led status config', led_status_config) led_status_config = led_status_config._asdict() led_status_config['config'] = led_status_config[ 'config'].value # convert to int. Both int and enum work await bricklet.set_channel_led_status_config(1, **led_status_config) print('Setting channel leds to heartbeat') await bricklet.set_channel_led_config(0, bricklet.ChannelLedConfig.HEARTBEAT) await bricklet.set_channel_led_config(1, bricklet.ChannelLedConfig.HEARTBEAT) # Query a value print('Get voltage, channel 0:', await bricklet.get_voltage(0), 'V') print('Get voltage, channel 1:', await bricklet.get_voltage(1), 'V') # Use a voltage value callback print('Set callback period to', 1000, 'ms and wait for callbacks') await bricklet.set_voltage_callback_configuration(channel=0, period=1000) await bricklet.set_voltage_callback_configuration(channel=1, period=500) print('Voltage callback configuration, channel 0:', await bricklet.get_voltage_callback_configuration(0)) print('Voltage callback configuration, channel 1:', await bricklet.get_voltage_callback_configuration(1)) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disable callbacks') await bricklet.set_voltage_callback_configuration(0) await bricklet.set_voltage_callback_configuration(1) print('Voltage callback configuration, channel 0:', await bricklet.get_voltage_callback_configuration(0)) print('Voltage callback configuration, channel 1:', await bricklet.get_voltage_callback_configuration(1)) # Use all voltages callback print('Get all voltages:', await bricklet.get_all_voltages()) await bricklet.set_all_voltages_callback_configuration(period=1000) print('All voltages callback configuration:', await bricklet.get_all_voltages_callback_configuration()) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disable callback') await bricklet.set_all_voltages_callback_configuration() print('All voltages callback configuration:', await bricklet.get_all_voltages_callback_configuration()) print('Resetting channel leds to status') await bricklet.set_channel_led_config( 0, bricklet.ChannelLedConfig.CHANNEL_STATUS) await bricklet.set_channel_led_config( 1, bricklet.ChannelLedConfig.CHANNEL_STATUS) # Test the generic features of the bricklet. These are available with all # new bricklets that have a microcontroller await run_example_generic(bricklet) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) # Register the callback queue used by process_callbacks() # We can register the same queue for multiple callbacks. bricklet.register_event_queue(bricklet.CallbackID.INPUT_VALUE, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.ALL_INPUT_VALUE, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.MONOFLOP_DONE, callback_queue) print('Setting all channels to INPUT and floating them') await asyncio.gather(*[ bricklet.set_configuration(channel=channel, direction=bricklet.Direction.IN, value=False) for channel in range(4) ]) # Set all pins to inputs with pull ups await asyncio.sleep(0.01) print('Input values:', await bricklet.get_value()) channel_configs = await asyncio.gather( *[bricklet.get_configuration(channel=channel) for channel in range(4)]) print('Channel configs:\n', *[f"{i}: {channel}\n" for i, channel in enumerate(channel_configs)]) print('Setting all channels to OUTPUT and driving them HIGH') await asyncio.gather(*[ bricklet.set_configuration(channel=channel, direction=bricklet.Direction.OUT, value=True) for channel in range(4) ]) # Set all pins to inputs with pull ups await asyncio.sleep(0.01) print('Output values:', await bricklet.get_value()) channel_configs = await asyncio.gather( *[bricklet.get_configuration(channel=channel) for channel in range(4)]) print('Channel configs:\n', *[f"{i}: {channel}\n" for i, channel in enumerate(channel_configs)]) print('Driving all channels LOW') await bricklet.set_value((False, False, False, False)) print('Output values:', await bricklet.get_value()) print('Driving all channel 1 HIGH') await bricklet.set_selected_value(1, True) print('Output values:', await bricklet.get_value()) print('Enabling monoflop, going HIGH for 1 second') await bricklet.set_monoflop(0, True, 1000) print('Monoflop status:', await bricklet.get_monoflop(0)) await asyncio.sleep(1) print('Triggering error, because we want to enable Edgecount on an output') try: await bricklet.set_edge_count_configuration( channel=2, edge_type=bricklet.EdgeType.RISING, debounce=1) except ValueError: print('Got a ValueError.') print('Enabling input callbacks on channel 2') await asyncio.gather(*[ bricklet.set_configuration(channel=channel, direction=bricklet.Direction.IN, value=False) for channel in range(4) ]) # Set all pins to inputs with pull ups await bricklet.set_input_value_callback_configuration( channel=2, period=100, value_has_to_change=False) print('Callback configuration for channel 2:', await bricklet.get_input_value_callback_configuration(2)) await bricklet.set_all_input_value_callback_configuration( period=100, value_has_to_change=False) print('Callback configuration for all inputs:', await bricklet.get_all_input_value_callback_configuration()) await asyncio.sleep(0.5) await bricklet.set_input_value_callback_configuration( channel=2, period=100, value_has_to_change=True) await bricklet.set_all_input_value_callback_configuration( period=100, value_has_to_change=True) print('Enabling edgecount on channel 2') await bricklet.set_edge_count_configuration( channel=2, edge_type=bricklet.EdgeType.BOTH, debounce=1) print('Edgecount config:', await bricklet.get_edge_count_configuration(2)) print('Toggling channel 2') await bricklet.set_configuration(channel=2, direction=bricklet.Direction.IN, value=True) await bricklet.set_configuration(channel=2, direction=bricklet.Direction.IN, value=False) await asyncio.sleep(0.1) print('Edges:', await bricklet.get_edge_count(2)) print('Enabling PWM on channel 3') await bricklet.set_configuration(channel=3, direction=bricklet.Direction.OUT, value=False) await bricklet.set_pwm_configuration(channel=3, frequency=10, duty_cycle=0.5) print('PWM configuration:', await bricklet.get_pwm_configuration(3)) await asyncio.sleep(1) # Test the generic features of the bricklet. These are available with all # new bricklets that have a microcontroller await run_example_generic(bricklet) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) # Register the callback queue used by process_callbacks() # We can register the same queue for multiple callbacks. bricklet.register_event_queue(bricklet.CallbackID.VOLTAGE, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.ANALOG_VALUE, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.VOLTAGE_REACHED, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.ANALOG_VALUE_REACHED, callback_queue) voltage_range = await bricklet.get_range() print('Range:', voltage_range) await bricklet.set_range(voltage_range) averaging = await bricklet.get_averaging() print('Averaging:', averaging) await bricklet.set_averaging(averaging) # Query a value print('Get voltage:', await bricklet.get_voltage(), 'V') print('Analog value:', await bricklet.get_analog_value()) print('Set bricklet debounce period to', 1000, 'ms') await bricklet.set_debounce_period(1000) print('Get bricklet debounce period:', await bricklet.get_debounce_period()) # Use a voltage callback print('Set voltage callback period to', 1000, 'ms') await bricklet.set_voltage_callback_period(1000) print('Voltage callback period:', await bricklet.get_voltage_callback_period()) print('Set voltage threshold to > 1 mV and wait for callbacks') # We use a low voltage on purpose, so that the callback will be triggered await bricklet.set_voltage_callback_threshold( bricklet.ThresholdOption.GREATER_THAN, 0.001, 0) print('Voltage threshold:', await bricklet.get_voltage_callback_threshold()) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disabling threshold callback') await asyncio.gather(bricklet.set_voltage_callback_threshold(), bricklet.set_voltage_callback_period()) print('Voltage threshold:', await bricklet.get_voltage_callback_threshold()) print('Voltage callback period:', await bricklet.get_voltage_callback_period()) # Use an analog value callback print('Set analog value callback period to', 1000, 'ms') await bricklet.set_analog_value_callback_period(1000) print('Analog value callback period:', await bricklet.get_analog_value_callback_period()) print('Set analog value threshold to > 0 and wait for callbacks') await bricklet.set_analog_value_callback_threshold( bricklet.ThresholdOption.GREATER_THAN, 0, 0) print('Analog value threshold:', await bricklet.get_analog_value_callback_threshold()) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disabling threshold callback') await bricklet.set_analog_value_callback_threshold() print('Analog value threshold:', await bricklet.get_analog_value_callback_threshold()) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) # Register the callback queue used by process_callbacks() # We can register the same queue for multiple callbacks. bricklet.register_event_queue(bricklet.CallbackID.INTERRUPT, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.MONOFLOP_DONE, callback_queue) port_a = await bricklet.get_port(bricklet.Port.A) # or 'a' or 'A' port_b = await bricklet.get_port('b') print('Port A:', '{0:08b}'.format(port_a)) print('Port B:', '{0:08b}'.format(port_b)) await asyncio.gather(bricklet.set_port('A', port_a), bricklet.set_port('B', port_b)) print('Port A configuration:', await bricklet.get_port_configuration('A')) print('Port B configuration:', await bricklet.get_port_configuration('B')) await bricklet.set_port_configuration('A', 0b11111111, bricklet.Direction.IN, bricklet.InputConfiguration.PULL_UP) # Set all pins to inputs with pull ups await bricklet.set_port_configuration('B', 0b11111111, bricklet.Direction.OUT, bricklet.OutputConfiguration.LOW) # Set all pins to outputs with LOW values print('Set bricklet debounce period to', 10, 'ms') await bricklet.set_debounce_period(10) print('Get bricklet debounce period:', await bricklet.get_debounce_period()) port_a = await bricklet.get_port_interrupt('A') port_b = await bricklet.get_port_interrupt('B') print('Port A interrupts:', '{0:08b}'.format(port_a)) print('Port B interrupts:', '{0:08b}'.format(port_b)) print('Enable interrupts on port A') await bricklet.set_port_interrupt('A', 0b11111111) print('Port interrupts:', '{0:08b}'.format(await bricklet.get_port_interrupt('A'))) print('Wait for interrupts. Connect a few outputs from port B to port A.') await asyncio.sleep(5) print('Enabling monoflop on B0 and B3. A0 will be high and B3 low until the time runs out, then B0 will go low and B3 high. Like a dead man\'s switch') await bricklet.set_port_monoflop('B', 0b00001001, 0b00000001, 3000) print('Monoflop state:') for i in range(3): print('B0: {0}, B3: {1}'.format(*(await asyncio.gather(bricklet.get_port_monoflop('B', 0), bricklet.get_port_monoflop('B', 3))))) await asyncio.sleep(1) print('Setting B3 low.') await bricklet.set_selected_values('B', 0b00001000, 0b00000000) await asyncio.sleep(0.1) print('Enabling edge counting on A0. Connect pin B7 to A0.') await bricklet.set_selected_values('B', 0b10000000, 0b10000000) # Set B7 to high, because toggling starts by going low. await bricklet.set_edge_count_config(0, bricklet.EdgeType.BOTH, debounce=5) print('Edge count config A0:', await bricklet.get_edge_count_config(0)) print('Edge count config A1:', await bricklet.get_edge_count_config(1)) print('Toggling pin B7.') for i in range(10): print('Setting pin', 'HIGH.' if (i % 2) else 'LOW.') await bricklet.set_selected_values('B', 0b10000000, 0b10000000 & ((i % 2) << 7)) print('Edge count of A0:', await bricklet.get_edge_count(0)) await asyncio.sleep(0.01) print('Disabling interrupts.') await bricklet.set_port_interrupt('A', 0b00000000) # Terminate the loop asyncio.create_task(shutdown())
async def run_example(packet, callback_queue): """ This is the actual demo. If the bricklet is found, this code will be run. """ print('Registering bricklet') bricklet = device_factory.get(packet['device_id'], packet['uid'], ipcon) # Create device object print('Identity:', await bricklet.get_identity()) print('Get chip temperature:', await bricklet.get_chip_temperature(), '°C') # Register the callback queue used by process_callbacks() # We can register the same queue for multiple callbacks. bricklet.register_event_queue(bricklet.CallbackID.AIR_PRESSURE, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.ALTITUDE, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.AIR_PRESSURE_REACHED, callback_queue) bricklet.register_event_queue(bricklet.CallbackID.ALTITUDE_REACHED, callback_queue) print('Air pressure:', await bricklet.get_air_pressure(), 'Pa') print('Altidtude:', await bricklet.get_altitude(), 'm') reference_pressure = await bricklet.get_reference_air_pressure() print('Reference air pressure:', reference_pressure, 'Pa') await bricklet.set_reference_air_pressure() averageing_config = await bricklet.get_averaging() print('Averageing config:', averageing_config) await bricklet.set_averaging(**averageing_config._asdict()) print('Set callback period to', 1000, 'ms') await asyncio.gather(bricklet.set_air_pressure_callback_period(1000), bricklet.set_altitude_callback_period(1000)) print('Air pressure callback period:', await bricklet.get_air_pressure_callback_period()) print('Altidtude callback period:', await bricklet.get_altitude_callback_period()) print('Set bricklet debounce period to', 1000, 'ms') await bricklet.set_debounce_period(1000) print('Get bricklet debounce period:', await bricklet.get_debounce_period()) # Use an air pressure and altitude callback print( 'Set air pressure threshold to >10 Pa and altitude to > 1m and wait for callbacks' ) # We use a low humidity on purpose, so that the callback will be triggered await asyncio.gather( bricklet.set_air_pressure_callback_threshold( bricklet.ThresholdOption.GREATER_THAN, 10, 0), bricklet.set_altitude_callback_threshold( bricklet.ThresholdOption.GREATER_THAN, 1, 0)) print('Air pressure threshold:', await bricklet.get_air_pressure_callback_threshold()) print('Altidtude threshold:', await bricklet.get_altitude_callback_threshold()) await asyncio.sleep(2.1) # Wait for 2-3 callbacks print('Disabling threshold callback') await asyncio.gather(bricklet.set_air_pressure_callback_threshold(), bricklet.set_altitude_callback_threshold()) print('Air pressure threshold:', await bricklet.get_air_pressure_callback_threshold()) print('Altidtude threshold:', await bricklet.get_altitude_callback_threshold()) # Terminate the loop asyncio.create_task(shutdown())