README.md: Add note about callback execution speed.

main
Peter Hinch 2024-01-27 17:35:02 +00:00
rodzic ca5e2b2a20
commit 923caaa6df
1 zmienionych plików z 50 dodań i 21 usunięć

Wyświetl plik

@ -155,7 +155,7 @@ under development so check for updates.
9. [Realtime applications](./README.md#9-realtime-applications) Accommodating tasks requiring fast RT performance.
10. [ePaper displays](./README.md#10-epaper-displays) Guidance on using ePaper displays.
[Appendix 1 Application design](./README.md#appendix-1-application-design) Tab order, button layout, encoder interface, use of graphics primitives
[Appendix 1 Application design](./README.md#appendix-1-application-design) Tab order, button layout, encoder interface, use of graphics primitives, more on callbacks.
[Appendix 2 Freezing bytecode](./README.md#appendix-2-freezing-bytecode) Optional way to save RAM.
[Appendix 3 Cross compiling](./README.md#appendix-3-cross-compiling) Another way to save RAM.
@ -809,6 +809,10 @@ are passed, bearing in mind the first arg described above. An incorrect
argument count results in puzzling tracebacks which appear to implicate the GUI
code. This is because it is the GUI which actually executes the callbacks.
Callbacks should complete quickly. See
[Appendix 1 Application design](./README.md#appendix-1-application-design) for
discussion of this.
###### [Contents](./README.md#0-contents)
## 2.3 Colors
@ -3339,6 +3343,31 @@ micro-gui.
The `primitives.py` demo provides a simple example.
## Callbacks
Callback functions should execute quickly, otherwise screen refresh will be
delayed until the callback is complete. Where a time consuming task is to be
triggered by a callback an `asyncio` task should be launched. In the following
sample an `LED` widget is to be cycled through various colors in response to
a callback.
```python
def callback(self, button, val):
asyncio.create_task(self.flash_led())
async def flash_led(self):
self.led.color(RED)
self.led.value(True) # Turn on LED
await asyncio.sleep_ms(500)
self.led.color(YELLOW)
await asyncio.sleep_ms(500)
self.led.color(GREEN)
await asyncio.sleep_ms(500)
self.led.value(False) # Turn it off. Task is complete.
```
The `callback()` executes fast, with the `flash_led()` running as a background
task. For more information on asyncio, see
[the tutorial](https://github.com/peterhinch/micropython-async/blob/master/v3/docs/TUTORIAL.md).
###### [Contents](./README.md#0-contents)
## Appendix 2 Freezing bytecode