-
Notifications
You must be signed in to change notification settings - Fork 6
Emit runtime device interface as a module #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ea2cae5
Emit runtime device interface as a module
glopesdev 3dfd6b1
Declare the emitted device module type
glopesdev 6a28f8d
Type-check the examples and the public API
glopesdev db1d62e
Name the schema emitter after what it returns
glopesdev 96504ae
Type the dataset reader by module contract
glopesdev e3cd769
Scope a device module to its own registers
glopesdev edeb2f9
Take schema text rather than a parsed model
glopesdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
35 changes: 22 additions & 13 deletions
35
docs/examples/create_device/create_device.py → ...ate_device_module/create_device_module.py
This file contains hidden or 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 |
|---|---|---|
| @@ -1,38 +1,47 @@ | ||
| from pathlib import Path | ||
|
|
||
| from harp.data import parse_to_dataframe | ||
| from harp.device import create_device | ||
| from harp.device import Device, create_device_module | ||
| from harp.serial import open_serial_device | ||
|
|
||
| SERIAL_PORT = "/dev/ttyUSB0" # or "COMx" in Windows ("x" is the number of the serial port) | ||
|
|
||
| # `create_device` compiles a Harp `device.yml` into a typed `Device` subclass at | ||
| # `create_device_module` compiles a Harp `device.yml` into a module of register classes at | ||
| # runtime — no code-generation step. This is the quickest way to work with a device | ||
| # when you don't have a pre-generated package for it: point it at the schema and you | ||
| # get the device's registers (keyed by address) plus its identity. | ||
| Behavior = create_device(Path("device.yml").read_text()) | ||
|
|
||
| print("WhoAmI:", Behavior.__whoami__) # device identity, taken from the schema | ||
| AnalogData = Behavior.REGISTER_MAP[44] # registers are reached by address | ||
|
|
||
| # The generated device behaves like any other `Device` class. Talk to hardware over | ||
| # a transport — `read`/`write` take a register class: | ||
| with open_serial_device(Behavior, port=SERIAL_PORT) as device: | ||
| # get the same shape a generated package has, registers at module level beside a | ||
| # `REGISTER_MAP`. | ||
| behavior = create_device_module(Path("device.yml").read_text()) | ||
|
|
||
| print("WhoAmI:", behavior.WHO_AM_I) # device identity, taken from the schema | ||
| AnalogData = behavior.AnalogData # registers are reached by name... | ||
| assert behavior.REGISTER_MAP[44] is AnalogData # ...or by address | ||
|
|
||
| # Registers are ordinary register classes, so they drive `read`/`write` on any | ||
| # `Device` over a transport. The schema carries no Python code, so there is no | ||
| # generated device class here: use `Device` itself. | ||
| with open_serial_device(Device, port=SERIAL_PORT) as device: | ||
| print("AnalogData:", device.read(AnalogData).parsed) | ||
|
|
||
| # ...or use the same register classes to decode a recorded binary dump into a | ||
| # pandas DataFrame (see the "Reading Data into a DataFrame" example for more): | ||
| df = parse_to_dataframe(AnalogData, "Behavior_44.bin") | ||
| print(df.head()) | ||
|
|
||
| # To have the identity checked on connect, subclass `Device` with the schema's | ||
| # WhoAmI — the same one-liner a generated package ships: | ||
| # | ||
| # class Behavior(Device): | ||
| # __whoami__ = behavior.WHO_AM_I | ||
|
|
||
|
|
||
| # --- Custom interface types -------------------------------------------------- | ||
| # A register with a custom `interfaceType` needs a converter so its field decodes | ||
| # to the right Python type. Pass it via `converters=`, keyed by "<Name>Converter": | ||
| # | ||
| # Behavior = create_device(yml_text, converters={"DataConverter": DataConverter()}) | ||
| # behavior = create_device_module(yml_text, converters={"DataConverter": DataConverter()}) | ||
| # | ||
| # An unresolved custom type raises `UnknownConverterError`; pass `strict=False` to | ||
| # decode it natively instead. `exclude_private=True` (the default) drops registers | ||
| # marked `private` in the schema. If you only want the parsed schema model rather | ||
| # than a device, `parse_device_schema(yml_text)` returns that directly. | ||
| # than a module, `parse_device_schema(yml_text)` returns that directly. |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.