# Chapter 11: TinyPython — Python on a Microcontroller ## Sample code - Directory: `docs/os-on-arduino/code/11_tiny_python` ```bash uv run pio run -d 11_tiny_python -t upload uv run pio device monitor -b 115200 ``` ## Source code The sketch for this chapter. Click a file name to expand. The code is mirrored automatically from the manuscript repository, so what you see here is always current. :::{dropdown} `src/main.cpp` — 262 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/11_tiny_python/src/main.cpp) ```{literalinclude} ../../../../code/11_tiny_python/src/main.cpp :language: cpp :linenos: ``` ::: :::{dropdown} `include/tp_config.h` — 43 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/11_tiny_python/include/tp_config.h) ```{literalinclude} ../../../../code/11_tiny_python/include/tp_config.h :language: cpp :linenos: ``` ::: :::{dropdown} `include/tp_env.h` — 60 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/11_tiny_python/include/tp_env.h) ```{literalinclude} ../../../../code/11_tiny_python/include/tp_env.h :language: cpp :linenos: ``` ::: :::{dropdown} `include/tp_eval.h` — 58 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/11_tiny_python/include/tp_eval.h) ```{literalinclude} ../../../../code/11_tiny_python/include/tp_eval.h :language: cpp :linenos: ``` ::: :::{dropdown} `include/tp_module.h` — 69 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/11_tiny_python/include/tp_module.h) ```{literalinclude} ../../../../code/11_tiny_python/include/tp_module.h :language: cpp :linenos: ``` ::: :::{dropdown} `include/tp_value.h` — 72 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/11_tiny_python/include/tp_value.h) ```{literalinclude} ../../../../code/11_tiny_python/include/tp_value.h :language: cpp :linenos: ``` ::: :::{dropdown} `src/tp_env.cpp` — 161 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/11_tiny_python/src/tp_env.cpp) ```{literalinclude} ../../../../code/11_tiny_python/src/tp_env.cpp :language: cpp :linenos: ``` ::: :::{dropdown} `src/tp_eval.cpp` — 774 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/11_tiny_python/src/tp_eval.cpp) ```{literalinclude} ../../../../code/11_tiny_python/src/tp_eval.cpp :language: cpp :linenos: ``` ::: :::{dropdown} `src/tp_mod_gpio.cpp` — 77 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/11_tiny_python/src/tp_mod_gpio.cpp) ```{literalinclude} ../../../../code/11_tiny_python/src/tp_mod_gpio.cpp :language: cpp :linenos: ``` ::: :::{dropdown} `src/tp_mod_led.cpp` — 59 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/11_tiny_python/src/tp_mod_led.cpp) ```{literalinclude} ../../../../code/11_tiny_python/src/tp_mod_led.cpp :language: cpp :linenos: ``` ::: :::{dropdown} `src/tp_mod_matrix.cpp` — 88 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/11_tiny_python/src/tp_mod_matrix.cpp) ```{literalinclude} ../../../../code/11_tiny_python/src/tp_mod_matrix.cpp :language: cpp :linenos: ``` ::: :::{dropdown} `src/tp_module.cpp` — 72 lines :icon: code [Open on GitHub](https://github.com/iory/learning-os-from-arduino/blob/main/docs/os-on-arduino/code/11_tiny_python/src/tp_module.cpp) ```{literalinclude} ../../../../code/11_tiny_python/src/tp_module.cpp :language: cpp :linenos: ``` ::: ## On the board ```{figure} ../_static/ch11_repl.gif :name: fig-ch11-repl :width: 100% Defining and calling a function in the REPL on the board. `fact(7)` returns 5040, and `fact(30)` stops with `RecursionError`. ``` ```{figure} ../_static/ch11_led_matrix.gif :name: fig-ch11-board :width: 70% `led.on()` / `led.off()` turn the L LED on and off, `matrix.fill(1)` lights all 96 LEDs of the matrix, and `matrix.clear()` turns them off. (Filmed with a webcam.) ``` ## In the simulator Without the board, the same firmware running in the {doc}`simulator <../getting-started/simulator>` looks like this. ```{figure} ../_static/sim_ch11_repl.gif :name: fig-ch11-sim :width: 80% In the TinyPython REPL, `led.on()` turns L on, `matrix.fill(1)` lights the whole matrix and `led.off()` turns L off. (Recorded in the simulator.) ``` ## Pitfalls (Collected from reader questions — ask via [GitHub Issues](https://github.com/iory/build-your-own-arduino-rtos/issues).)