C++20 coroutines for ESPHome core #3212
Unanswered
scaiper
asked this question in
Core functionality
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I've recently made a component utilizing an UART bus to communicate with a device. A device itself is not very fast to respond, a communication session can take up to hundreds of milliseconds. The Contributing Guide recommends making a state machine in this case.
So I've tried to make a state machine using C++ coroutines and I'm quite happy with the result. The coroutines are convenient to use, and are quite transparent.
Inside a coroutine you can
co_await
another coroutine, they will form a stack as one would expect. Or you canco_await
an instance ofstd::suspend_always
to suspend until a nextloop
iteration.An example of a coroutine reading from UART.
There is also a sequential scheduler to queue up invocations so they will run strictly sequentially and not interleaved. This queue in intended to be per component, so that synchronization inside a component is not needed.
As C++20 support has landed recently, I think, this coroutines would be useful addition to the ESPHome core, as this kind of async execution can be often needed. I'd like to hear some opinions.
If there are no objections, I can work on the PR with changes.
Beta Was this translation helpful? Give feedback.
All reactions