File tree Expand file tree Collapse file tree 3 files changed +42
-5
lines changed Expand file tree Collapse file tree 3 files changed +42
-5
lines changed Original file line number Diff line number Diff line change @@ -61,7 +61,7 @@ async def read(self, n):
61
61
"""
62
62
63
63
core ._io_queue .queue_read (self .s )
64
- await core . sleep ( 0 )
64
+ yield
65
65
return self .s .read (n )
66
66
67
67
async def readinto (self , buf ):
@@ -73,7 +73,7 @@ async def readinto(self, buf):
73
73
"""
74
74
75
75
core ._io_queue .queue_read (self .s )
76
- await core . sleep ( 0 )
76
+ yield
77
77
return self .s .readinto (buf )
78
78
79
79
async def readexactly (self , n ):
@@ -88,7 +88,7 @@ async def readexactly(self, n):
88
88
r = b""
89
89
while n :
90
90
core ._io_queue .queue_read (self .s )
91
- await core . sleep ( 0 )
91
+ yield
92
92
r2 = self .s .read (n )
93
93
if r2 is not None :
94
94
if not len (r2 ):
@@ -106,7 +106,7 @@ async def readline(self):
106
106
l = b""
107
107
while True :
108
108
core ._io_queue .queue_read (self .s )
109
- await core . sleep ( 0 )
109
+ yield
110
110
l2 = self .s .readline () # may do multiple reads but won't block
111
111
l += l2
112
112
if not l2 or l [- 1 ] == 10 : # \n (check l in case l2 is str)
@@ -129,7 +129,8 @@ async def drain(self):
129
129
mv = memoryview (self .out_buf )
130
130
off = 0
131
131
while off < len (mv ):
132
- yield core ._io_queue .queue_write (self .s )
132
+ core ._io_queue .queue_write (self .s )
133
+ yield
133
134
ret = self .s .write (mv [off :])
134
135
if ret is not None :
135
136
off += ret
Original file line number Diff line number Diff line change
1
+ import usb_cdc
2
+ usb_cdc .enable (data = True , console = True )
Original file line number Diff line number Diff line change
1
+ """
2
+ example that reads from the cdc data serial port in groups of four and prints
3
+ to the console. The USB CDC data serial port will need enabling. This can be done
4
+ by copying examples/usb_cdc_boot.py to boot.py in the CIRCUITPY directory
5
+
6
+ Meanwhile a simple counter counts up every second and also prints
7
+ to the console.
8
+ """
9
+
10
+
11
+ import asyncio
12
+ import usb_cdc
13
+
14
+ async def client ():
15
+ usb_cdc .data .timeout = 0
16
+ s = asyncio .StreamReader (usb_cdc .data )
17
+ while True :
18
+ text = await s .readline ()
19
+ print (text )
20
+ await asyncio .sleep (0 )
21
+
22
+ async def counter ():
23
+ i = 0
24
+ while True :
25
+ print (i )
26
+ i += 1
27
+ await asyncio .sleep (1 )
28
+
29
+ async def main ():
30
+ client_task = asyncio .create_task (client ())
31
+ count_task = asyncio .create_task (counter ())
32
+ await asyncio .gather (client_task , count_task )
33
+
34
+ asyncio .run (main ())
You can’t perform that action at this time.
0 commit comments