Skip to content

Commit d6ae022

Browse files
committed
v3/primitives/irq_event.py simplify.
1 parent 931dc75 commit d6ae022

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

v3/docs/TUTORIAL.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,15 +1612,20 @@ The behaviour is "correct": CPython `asyncio` behaves identically. Ref
16121612
At heart all interfaces between `uasyncio` and external asynchronous events
16131613
rely on polling. Hardware requiring a fast response may use an interrupt. But
16141614
the interface between the interrupt service routine (ISR) and a user task will
1615-
be polled. For example the ISR might trigger an `Event` or set a global flag,
1616-
while a task awaiting the outcome polls the object each time it is
1617-
scheduled.
1615+
be polled. For example the ISR might set a global flag with the task awaiting
1616+
the outcome polling the flag each time it is scheduled. This is explicit
1617+
polling.
16181618

1619-
Polling may be effected in two ways, explicitly or implicitly. The latter is
1620-
performed by using the `stream I/O` mechanism which is a system designed for
1621-
stream devices such as UARTs and sockets. At its simplest explicit polling may
1622-
consist of code like this:
1619+
Polling may also be effected implicitly. This is performed by using the
1620+
`stream I/O` mechanism which is a system designed for stream devices such as
1621+
UARTs and sockets.
16231622

1623+
There are hazards involved with approaches to interfacing ISR's which appear to
1624+
avoid polling. See [the IRQ_EVENT class](./DRIVERS.md#6-irq_event) for details.
1625+
This class is a thread-safe way to implement this interface with efficient
1626+
implicit polling.
1627+
1628+
At its simplest explicit polling may consist of code like this:
16241629
```python
16251630
async def poll_my_device():
16261631
global my_flag # Set by device ISR
@@ -1631,17 +1636,17 @@ async def poll_my_device():
16311636
await asyncio.sleep(0)
16321637
```
16331638

1634-
In place of a global, an instance variable, an `Event` object or an instance of
1635-
an awaitable class might be used. Explicit polling is discussed
1636-
further [below](./TUTORIAL.md#62-polling-hardware-with-a-task).
1639+
In place of a global, an instance variable or an instance of an awaitable class
1640+
might be used. Explicit polling is discussed further
1641+
[below](./TUTORIAL.md#62-polling-hardware-with-a-task).
16371642

16381643
Implicit polling consists of designing the driver to behave like a stream I/O
16391644
device such as a socket or UART, using `stream I/O`. This polls devices using
16401645
Python's `select.poll` system: because the polling is done in C it is faster
16411646
and more efficient than explicit polling. The use of `stream I/O` is discussed
16421647
[here](./TUTORIAL.md#63-using-the-stream-mechanism).
16431648

1644-
Owing to its efficiency implicit polling benefits most fast I/O device drivers:
1649+
Owing to its efficiency implicit polling most benefits fast I/O device drivers:
16451650
streaming drivers can be written for many devices not normally considered as
16461651
streaming devices [section 6.4](./TUTORIAL.md#64-writing-streaming-device-drivers).
16471652

@@ -1678,10 +1683,6 @@ and `sleep_ms()` functions. The worst-case value for this overrun may be
16781683
calculated by summing, for every other task, the worst-case execution time
16791684
between yielding to the scheduler.
16801685

1681-
The [fast_io](./FASTPOLL.md) version of `uasyncio` in this repo provides a way
1682-
to ensure that stream I/O is polled on every iteration of the scheduler. It is
1683-
hoped that official `uasyncio` will adopt code to this effect in due course.
1684-
16851686
###### [Contents](./TUTORIAL.md#contents)
16861687

16871688
## 6.2 Polling hardware with a task
@@ -1915,14 +1916,14 @@ class MillisecTimer(io.IOBase):
19151916
self.sreader = asyncio.StreamReader(self)
19161917

19171918
def __iter__(self):
1918-
await self.sreader.readline()
1919+
await self.sreader.read(1)
19191920

19201921
def __call__(self, ms):
19211922
self.end = utime.ticks_add(utime.ticks_ms(), ms)
19221923
return self
19231924

1924-
def readline(self):
1925-
return b'\n'
1925+
def read(self, _):
1926+
pass
19261927

19271928
def ioctl(self, req, arg):
19281929
ret = MP_STREAM_ERROR
@@ -1979,10 +1980,9 @@ class PinCall(io.IOBase):
19791980
v = self.pinval
19801981
if v and self.cb_rise is not None:
19811982
self.cb_rise(*self.cbr_args)
1982-
return b'\n'
1983+
return
19831984
if not v and self.cb_fall is not None:
19841985
self.cb_fall(*self.cbf_args)
1985-
return b'\n'
19861986

19871987
def ioctl(self, req, arg):
19881988
ret = MP_STREAM_ERROR

v3/primitives/irq_event.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@ def __init__(self):
1717
self.sreader = asyncio.StreamReader(self)
1818

1919
def wait(self):
20-
await self.sreader.readline()
20+
await self.sreader.read(1)
2121
self.state = False
2222

2323
def set(self):
2424
self.state = True
25-
return self
2625

2726
def is_set(self):
2827
return self.state
2928

30-
def readline(self):
31-
return b'\n'
29+
def read(self, _):
30+
pass
3231

3332
def clear(self):
3433
pass # See docs

0 commit comments

Comments
 (0)