You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/micropython/03.micropython/02.environment/04.runtime/runtime.md
+5-6Lines changed: 5 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ A **runtime environment** is the infrastructure that allows code to execute on a
15
15
16
16
For MicroPython, the runtime is a lightweight version of the Python runtime, optimized to work with the limited resources of microcontrollers. This means it has to manage memory efficiently, provide quick access to hardware components, and interpret Python code in real-time.
17
17
18
-
### How MicroPython's Runtime Works
18
+
### MicroPython's Runtime
19
19
20
20
MicroPython's runtime environment consists of several key components:
21
21
@@ -27,7 +27,7 @@ MicroPython's runtime environment consists of several key components:
27
27
28
28
4.**File System**: The runtime includes a file system, typically mounted on the microcontroller’s flash storage. This allows MicroPython to load and execute scripts, save configuration files, or store data persistently.
29
29
30
-
### Key Features of the MicroPython Runtime
30
+
### MicroPython Runtime Features
31
31
32
32
1.**REPL (Read-Eval-Print Loop)**: One of the standout features of MicroPython's runtime is the REPL. The REPL is an interactive shell where we can type and execute Python code line by line, get immediate feedback, and test ideas quickly. It’s especially useful for debugging and learning how the hardware responds to different commands.
33
33
@@ -37,15 +37,15 @@ MicroPython's runtime environment consists of several key components:
37
37
38
38
3.**Concurrency with `uasyncio`**: MicroPython provides support for asynchronous programming using the `uasyncio` module. This allows us to handle multiple tasks concurrently, such as reading a sensor while controlling an LED, without blocking the main program.
39
39
40
-
### Comparison to Standard Python
40
+
### Micro vs Python
41
41
42
42
While MicroPython aims to be as compatible as possible with standard Python, there are some important differences due to the limited resources available on microcontrollers:
43
43
44
44
-**Memory Constraints**: Standard Python runs on desktops or servers with abundant RAM, but MicroPython runs on devices with as little as 16KB to 512KB of RAM. The runtime is optimized to be efficient, but we must be mindful of memory usage.
45
45
-**Limited Libraries**: Many of Python’s standard libraries are unavailable in MicroPython because they are too large or not relevant for embedded systems. However, MicroPython provides specialized libraries for hardware interaction.
46
46
-**Performance**: MicroPython is generally slower than compiled languages, but it’s fast enough for most embedded applications thanks to its lightweight runtime and efficient interpreter.
47
47
48
-
## How Code Execution Works in MicroPython
48
+
## Code Execution Works in MicroPython
49
49
50
50
When we upload a MicroPython script to our microcontroller, the runtime handles code execution as follows:
51
51
@@ -54,8 +54,7 @@ When we upload a MicroPython script to our microcontroller, the runtime handles
54
54
3.**Hardware Interaction**: The runtime communicates with the microcontroller’s hardware through built-in libraries. For example, we can control GPIO pins, read sensor data, or send data over I2C using simple Python commands.
55
55
4.**Error Handling**: MicroPython includes mechanisms for error handling and debugging. If the script encounters an error, the runtime will stop execution and display an error message in the REPL or console.
56
56
57
-
## Best Practices for Working with the MicroPython Runtime
58
-
57
+
## Best Practices
59
58
1.**Manage Memory Wisely**: Be mindful of memory usage, especially when working with large data structures or performing frequent allocations. Use the garbage collector [(`gc`)](https://docs.micropython.org/en/latest/library/gc.html) if you need to manually free up memory.
60
59
2.**Use REPL for Testing**: Take advantage of the REPL to experiment and debug code interactively. This can save time and help you understand how your code interacts with the hardware.
61
60
3.**Optimize Code for Speed and Efficiency**: Use efficient data structures and algorithms to keep your code fast and responsive. Avoid blocking operations when possible, and consider using `uasyncio` for concurrency.
0 commit comments