-
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.
Merge pull request #8 from Neradoc/add-vidpid-filter
Add devices_by_vidpid to filter devices by vid and pid.
- Loading branch information
Showing
5 changed files
with
92 additions
and
36 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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
devices_by_name, | ||
devices_by_drive, | ||
devices_by_serial, | ||
devices_by_vidpid, | ||
) | ||
|
||
try: | ||
|
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,43 @@ | ||
""" | ||
A demo host-side script that | ||
- finds a board by product name ("pico", "macropad", "featherS3"...) | ||
- sends some data to the board's data serial port | ||
python3 find_boards_with_filters.py --name FeatherS3 ---data "Hi there" | ||
""" | ||
import argparse | ||
import discotool | ||
import os | ||
import serial | ||
|
||
PRODUCT_NAME = "QT PY" # not case sensitive | ||
DATA_STRING = "Hello World" | ||
|
||
# This lets you specify the drive name and data to send in the command line | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--name", type=str, help="Product name", default="") | ||
parser.add_argument("--data", type=str, help="Data to send", default=DATA_STRING) | ||
parser.add_argument("--vid", type=int, help="Product name", default=0) | ||
args = parser.parse_args() | ||
|
||
# prepare the parameters | ||
product_name = args.name | ||
data_string = args.data.encode("utf8") | ||
vid = args.vid | ||
|
||
# use filters based on given parameters | ||
if vid != 0 and product_name == "": | ||
pad = discotool.devices_by_vidpid(vid) | ||
elif not product_name: | ||
pad = discotool.devices_by_name(PRODUCT_NAME) | ||
else: | ||
pad = discotool.devices_by_name(product_name) | ||
|
||
# send data to whatever is found | ||
if pad and (data_port := pad[0].data): | ||
print(f"Board found: {pad[0].name}") | ||
print(f"Send {data_string} to {data_port}") | ||
with serial.Serial(data_port) as pp: | ||
pp.write(data_string) | ||
else: | ||
print(f"Could not find a {product_name} with an open data port.") |