Skip to content

Commit edaf06b

Browse files
committed
connections: Restore write method.
This got removed when we upgraded the download and run protocol. This can be used to send data to the hub, until we support full hub-to-hub communication. pybricks/support#376 Also update the notebook demo while we are at it. This removes the USB example for SPIKE.
1 parent 2919d8a commit edaf06b

File tree

2 files changed

+81
-51
lines changed

2 files changed

+81
-51
lines changed

demo/runner.ipynb

Lines changed: 78 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,113 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": null,
5+
"execution_count": 1,
66
"metadata": {},
77
"outputs": [],
88
"source": [
9-
"from pybricksdev.connections import EV3Connection, BLEPUPConnection, USBPUPConnection\n",
10-
"from pybricksdev.ble import find_device\n",
11-
"from asyncio import gather\n",
12-
"import logging\n",
13-
"\n",
14-
"logging.basicConfig(format='%(asctime)s: %(levelname)7s: %(message)s', level=logging.DEBUG)"
9+
"from pybricksdev.connections import PybricksHub\n",
10+
"from pybricksdev.ble import find_device, nus\n",
11+
"from asyncio import gather, sleep\n",
12+
"import logging"
1513
]
1614
},
1715
{
1816
"cell_type": "code",
19-
"execution_count": null,
17+
"execution_count": 2,
2018
"metadata": {},
21-
"outputs": [],
19+
"outputs": [
20+
{
21+
"name": "stdout",
22+
"output_type": "stream",
23+
"text": [
24+
"Searching for Pybricks Hub\n"
25+
]
26+
}
27+
],
2228
"source": [
23-
"# Go to help > Edit Keyboard Shortcuts to bind \"run all cells\" to your favorite key.\n",
24-
"# This way you can easily run all cells again without resetting the kernel.\n",
25-
"# That way, your hubs stay connected so that programs runs quickly.\n",
26-
"\n",
27-
"try:\n",
28-
" ev3, hub, spike\n",
29-
"except:\n",
30-
" spike = USBPUPConnection()\n",
31-
" await spike.connect('Pybricks Hub')\n",
32-
" \n",
33-
" ev3 = EV3Connection()\n",
34-
" await ev3.connect('192.168.133.101')\n",
35-
" \n",
36-
" hub = BLEPUPConnection()\n",
37-
" address = await find_device('Pybricks Hub')\n",
38-
" await hub.connect(address)\n",
39-
" \n"
29+
"hub = PybricksHub()\n",
30+
"address = await find_device('Pybricks Hub')\n",
31+
"await hub.connect(address) \n"
4032
]
4133
},
4234
{
4335
"cell_type": "code",
44-
"execution_count": null,
36+
"execution_count": 3,
4537
"metadata": {},
46-
"outputs": [],
47-
"source": [
48-
"%%file _ev3.py\n",
49-
"\n",
50-
"print(\"hi from ev3\")"
51-
]
52-
},
53-
{
54-
"cell_type": "code",
55-
"execution_count": null,
56-
"metadata": {},
57-
"outputs": [],
38+
"outputs": [
39+
{
40+
"name": "stdout",
41+
"output_type": "stream",
42+
"text": [
43+
"Overwriting _pup.py\n"
44+
]
45+
}
46+
],
5847
"source": [
5948
"%%file _pup.py\n",
6049
"\n",
61-
"print(\"hi from control+\")\n"
50+
"from usys import stdin\n",
51+
"from uselect import poll\n",
52+
"\n",
53+
"# Register the standard input so we can read keyboard presses.\n",
54+
"keyboard = poll()\n",
55+
"keyboard.register(stdin)\n",
56+
"\n",
57+
"while True:\n",
58+
" # Check if a key has been pressed.\n",
59+
" if keyboard.poll(0):\n",
60+
"\n",
61+
" # Read the key and print it.\n",
62+
" key = stdin.read(1)\n",
63+
" print(\"You pressed:\", key)\n"
6264
]
6365
},
6466
{
6567
"cell_type": "code",
66-
"execution_count": null,
68+
"execution_count": 4,
6769
"metadata": {},
6870
"outputs": [],
6971
"source": [
70-
"%%file _spike.py\n",
71-
"\n",
72-
"print(\"hi from spike\")\n"
72+
"async def send_data(hub):\n",
73+
" \n",
74+
" await sleep(1)\n",
75+
" await hub.client.write_gatt_char(nus.NUS_RX_UUID, bytearray(b\"HELLO!\"), True)"
7376
]
7477
},
7578
{
7679
"cell_type": "code",
7780
"execution_count": null,
7881
"metadata": {},
79-
"outputs": [],
82+
"outputs": [
83+
{
84+
"data": {
85+
"application/vnd.jupyter.widget-view+json": {
86+
"model_id": "c2d6d75f92d34d28a718b35ae4d08a29",
87+
"version_major": 2,
88+
"version_minor": 0
89+
},
90+
"text/plain": [
91+
" 0%| | 0.00/156 [00:00<?, ?B/s]"
92+
]
93+
},
94+
"metadata": {},
95+
"output_type": "display_data"
96+
},
97+
{
98+
"name": "stdout",
99+
"output_type": "stream",
100+
"text": [
101+
"You pressed: H\n",
102+
"You pressed: E\n",
103+
"You pressed: L\n",
104+
"You pressed: L\n",
105+
"You pressed: O\n",
106+
"You pressed: !\n"
107+
]
108+
}
109+
],
80110
"source": [
81-
"await gather(hub.run('_pup.py'), spike.run('_spike.py'), ev3.run('_ev3.py'))"
111+
"result = await gather(hub.run('_pup.py'), send_data(hub))"
82112
]
83113
},
84114
{
@@ -87,10 +117,7 @@
87117
"metadata": {},
88118
"outputs": [],
89119
"source": [
90-
"# await ev3.get('_ev3.py')\n",
91-
"# await hub.disconnect()\n",
92-
"# await spike.disconnect()\n",
93-
"# await ev3.disconnect()"
120+
"await hub.disconnect()"
94121
]
95122
}
96123
],
@@ -115,4 +142,4 @@
115142
},
116143
"nbformat": 4,
117144
"nbformat_minor": 4
118-
}
145+
}

pybricksdev/connections.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,9 @@ async def send_block(self, data):
757757
self.expected_checksum = -1
758758
raise
759759

760+
async def write(self, data, with_response=False):
761+
await self.client.write_gatt_char(NUS_RX_UUID, bytearray(data), with_response)
762+
760763
async def run(self, py_path, wait=True, print_output=True):
761764

762765
# Reset output buffer

0 commit comments

Comments
 (0)