-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from PatrickBaus/add_isolator_bricklet
Add isolator bricklet
- Loading branch information
Showing
13 changed files
with
497 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#!/usr/bin/env python3 | ||
# pylint: disable=duplicate-code | ||
""" | ||
An example to demonstrate most of the capabilities of the Tinkerforge Isolator Bricklet. | ||
""" | ||
import asyncio | ||
import warnings | ||
from decimal import Decimal | ||
|
||
from tinkerforge_async.bricklet_isolator import BrickletIsolator | ||
from tinkerforge_async.devices import BrickletWithMCU | ||
from tinkerforge_async.ip_connection import IPConnectionAsync | ||
|
||
|
||
async def process_callbacks(device: BrickletIsolator) -> None: | ||
"""Prints the callbacks (filtered by id) of the bricklet.""" | ||
async for packet in device.read_events(): | ||
print("Callback received", packet) | ||
|
||
|
||
async def run_example_generic(bricklet: BrickletWithMCU) -> None: | ||
"""This is a demo of the generic features of the Tinkerforge bricklets with a microcontroller.""" | ||
uid = await bricklet.read_uid() | ||
print("Device uid:", uid) | ||
await bricklet.write_uid(uid) | ||
|
||
print("SPI error count:", await bricklet.get_spitfp_error_count()) | ||
|
||
print("Current bootloader mode:", await bricklet.get_bootloader_mode()) | ||
bootloader_mode = bricklet.BootloaderMode.FIRMWARE | ||
print("Setting bootloader mode to", bootloader_mode, ":", await bricklet.set_bootloader_mode(bootloader_mode)) | ||
|
||
print("Disable status LED") | ||
await bricklet.set_status_led_config(bricklet.LedConfig.OFF) | ||
print("Current status:", await bricklet.get_status_led_config()) | ||
await asyncio.sleep(1) | ||
print("Enable status LED") | ||
await bricklet.set_status_led_config(bricklet.LedConfig.SHOW_STATUS) | ||
print("Current status:", await bricklet.get_status_led_config()) | ||
|
||
print("Get Chip temperature:", await bricklet.get_chip_temperature() - Decimal("273.15"), "°C") | ||
|
||
print("Reset Bricklet") | ||
await bricklet.reset() | ||
|
||
|
||
async def run_example(bricklet: BrickletIsolator) -> None: | ||
"""This is the actual demo. If the bricklet is found, this code will be run.""" | ||
callback_task = asyncio.create_task(process_callbacks(bricklet)) | ||
try: | ||
print("Identity:", await bricklet.get_identity()) | ||
|
||
print("Get isolator statistics:", await bricklet.get_statistics()) | ||
|
||
# Query a value | ||
baud_rate = await bricklet.get_spitfp_baudrate() | ||
print("Set SPITFP baud rate to 400000 baud.") | ||
await bricklet.set_spitfp_baudrate(400000) | ||
print("Get SPITFP baud rate:", await bricklet.get_spitfp_baudrate(), "baud") | ||
await bricklet.set_spitfp_baudrate(baud_rate) | ||
baud_rate_config = await bricklet.get_spitfp_baudrate_config() | ||
print("Get SPITFP baud rate config:", baud_rate_config) | ||
print("Set SPITFP baud rate config:", baud_rate_config) | ||
await bricklet.set_spitfp_baudrate_config(*baud_rate_config) | ||
|
||
# Use a configuration callback | ||
print("Set callback period to", 1000, "ms and wait for callbacks.") | ||
# We use a low humidity value on purpose, so that the callback will be triggered | ||
await bricklet.set_statistics_callback_configuration(period=1000, value_has_to_change=False) | ||
print("Configuration callback configuration:", await bricklet.get_statistics_callback_configuration()) | ||
await asyncio.sleep(2.1) # Wait for 2-3 callbacks | ||
print("Disable configuration callback") | ||
await bricklet.set_statistics_callback_configuration() | ||
print("Configuration callback configuration:", await bricklet.get_statistics_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) | ||
finally: | ||
callback_task.cancel() | ||
|
||
|
||
async def shutdown(tasks: set[asyncio.Task]) -> None: | ||
"""Clean up by stopping all consumers""" | ||
for task in tasks: | ||
task.cancel() | ||
await asyncio.gather(*tasks) | ||
|
||
|
||
async def main() -> None: | ||
""" | ||
The main loop, that will spawn all callback handlers and wait until they are done. There are two callback handlers, | ||
one waits for the bricklet to connect and runs the demo, the other handles messages sent by the bricklet. | ||
""" | ||
tasks = set() | ||
try: | ||
# Use the context manager of the ip connection. It will automatically do the cleanup. | ||
async with IPConnectionAsync(host="127.0.0.1", port=4223) as connection: | ||
await connection.enumerate() | ||
# Read all enumeration replies, then start the example if we find the correct device | ||
async for enumeration_type, device in connection.read_enumeration(): # pylint: disable=unused-variable | ||
if isinstance(device, BrickletIsolator): | ||
print(f"Found {device}, running example.") | ||
tasks.add(asyncio.create_task(run_example(device))) | ||
break | ||
print(f"Found {device}, but not interested.") | ||
|
||
# Wait for run_example() to finish | ||
await asyncio.gather(*tasks) | ||
except ConnectionRefusedError: | ||
print("Could not connect to server. Connection refused. Is the brick daemon up?") | ||
except asyncio.CancelledError: | ||
print("Stopped the main loop.") | ||
raise # It is good practice to re-raise CancelledErrors | ||
finally: | ||
await shutdown(tasks) | ||
|
||
|
||
# Report all mistakes managing asynchronous resources. | ||
warnings.simplefilter("always", ResourceWarning) | ||
|
||
# Start the main loop and run the async loop forever. Turn off the debug parameter for production code. | ||
try: | ||
asyncio.run(main(), debug=True) | ||
except KeyboardInterrupt: | ||
print("Shutting down gracefully.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.