Skip to content

Commit d565f7f

Browse files
committed
Add two simple examples: device discovery and bitstream loading
1 parent e8bfdb2 commit d565f7f

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

examples/bitstream_loading.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python3
2+
"""
3+
zbnt/python-client
4+
Copyright (C) 2020 Oscar R.
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
"""
19+
20+
import asyncio
21+
from zbnt import ZbntClient, discover_devices
22+
23+
async def main():
24+
devices = await discover_devices(2)
25+
26+
if len(devices) == 0:
27+
print("Error: No devices found")
28+
exit(1)
29+
30+
if len(devices) > 1:
31+
for i in range(len(devices)):
32+
print("{0}) {1} ({2})".format(i + 1, devices[i]["name"], ", ".join(devices[i]["address"])))
33+
34+
req_dev = int(input("Select a device [1-{0}]: ".format(len(devices)))) - 1
35+
36+
if req_dev < 0 or req_dev >= len(devices):
37+
print("Error: Invalid device selected")
38+
exit(1)
39+
else:
40+
req_dev = 0
41+
42+
req_dev = devices[req_dev]
43+
44+
print("- Connecting to {0} ({1})".format(req_dev["name"], req_dev["address"][0]))
45+
46+
client = await ZbntClient.connect(req_dev["address"][1], req_dev["port"])
47+
48+
if client == None:
49+
print("Error: Failed to connect to device")
50+
exit(1)
51+
52+
print("- Connected!")
53+
54+
while True:
55+
print("\nAvailable bitstreams:\n")
56+
57+
for i in range(len(client.bitstreams)):
58+
print("{0}) {1} {2}".format(i + 1, client.bitstreams[i], "*" if client.bitstreams[i] == client.active_bitstream else ""))
59+
60+
req_bit = int(input("\nSelect a bitstream [1-{0}]: ".format(len(client.bitstreams)))) - 1
61+
62+
if req_bit < 0 or req_bit >= len(client.bitstreams):
63+
print("Error: Invalid bitstream selected")
64+
continue
65+
66+
await client.load_bitstream(client.bitstreams[req_bit])
67+
68+
asyncio.run(main())

examples/discover_devices.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
"""
3+
zbnt/python-client
4+
Copyright (C) 2020 Oscar R.
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
"""
19+
20+
import asyncio
21+
from zbnt import discover_devices
22+
23+
async def main():
24+
devices = await discover_devices(3)
25+
26+
for d in devices:
27+
print(d["name"])
28+
print(" - Address:", ", ".join(d["address"]))
29+
print(" - Version:", d["versionstr"])
30+
print(" - Port:", str(d["port"]))
31+
print("")
32+
33+
asyncio.run(main())

0 commit comments

Comments
 (0)