-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tutorial on using JSON with Trigger objects (#9)
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Saving and Loading Trigger Settings from a JSON file | ||
|
||
This tutorial will provide an example of saving and subsequently loading a `Trigger` object from a JSON file. | ||
|
||
## Initialize Runtime | ||
|
||
To start, we'll import `Acquire` and create a `Runtime` object, which coordinates the streaming process. | ||
|
||
```python | ||
import acquire | ||
runtime = acquire.Runtime() | ||
``` | ||
|
||
## Create a Trigger Object | ||
|
||
`Trigger` objects have 4 attributes: edge, enable, line, and kind. In this example, will only adjust the edge attribute. | ||
|
||
```python | ||
# Instantiate a Trigger object | ||
trig = acquire.Trigger() | ||
|
||
# change the edge attribute from the default Rising to Falling | ||
trig.edge = acquire.TriggerEdge.Falling | ||
``` | ||
|
||
## Save Properties to a JSON file | ||
We'll utilize the [json library](https://docs.python.org/3/library/json.html#) to write our `Trigger` to a JSON file to save for subsequent acquisition. | ||
|
||
```python | ||
import json | ||
|
||
# cast the properties to a dictionary | ||
trig = trig.dict() | ||
|
||
# convert the dictionary to json with "human-readable" formatting | ||
trig = json.dumps(trig, indent=4, sort_keys=True) | ||
|
||
# save the trigger to file "sample_trig.json" in the current directory | ||
with open("sample_trig.json", "w") as outfile: | ||
outfile.write(trig) | ||
``` | ||
|
||
## Example JSON file | ||
The resulting sample_trig.json file is below: | ||
|
||
```json | ||
{ | ||
"edge": "Falling", | ||
"enable": false, | ||
"kind": "Input", | ||
"line": 0 | ||
} | ||
``` | ||
|
||
## Load Properties from a JSON file | ||
You can load the trigger attributes in the JSON file to a `Trigger` object as shown below: | ||
|
||
```python | ||
# Instantiate a `Trigger` object from the settings in sample_trig.json | ||
trig = acquire.Trigger(**json.load(open('sample_trig.json'))) | ||
``` | ||
|