Skip to content

Commit cdbd551

Browse files
Added "File System"
1 parent 917e07f commit cdbd551

File tree

1 file changed

+134
-0
lines changed
  • content/micropython/03.micropython/02.enviroment/02.file-system

1 file changed

+134
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
title: 'File System'
3+
description: 'Learn how to use the File Sytem in MicroPython.'
4+
author: 'Pedro Lima'
5+
tags: [MicroPython, REPL]
6+
---
7+
8+
9+
When working with MicroPython, we’re not limited to a single program like in traditional Arduino sketches. Instead, MicroPython provides a file system, enabling us to store and manage multiple files on our microcontroller. This opens up powerful capabilities for organizing code, managing assets, and creating modular projects.
10+
11+
In this article, we’ll explore how the MicroPython file system works, how to organize files effectively, and the typical structure of MicroPython projects.
12+
13+
## The MicroPython File System: Key Differences
14+
15+
In traditional Arduino programming, we upload a single compiled file directly to the microcontroller, where it runs immediately. With MicroPython, however, we work within a file system that can store multiple files. This file system allows us to:
16+
17+
- **Upload and Download Files**: We can save individual scripts, libraries, and assets directly on the device.
18+
- **Organize Project Files**: Create folders, save multiple scripts, and organize files for a modular approach.
19+
- **Edit Files Directly on the Device**: Modify files without needing to overwrite everything, making adjustments faster and more flexible.
20+
21+
## Accessing the MicroPython File System
22+
23+
To interact with the MicroPython file system, we’ll use Arduino Labs for MicroPython, which provides tools to manage and view files directly on our device. Here’s how to get started:
24+
25+
1. **Connect to Your Device**: Open Arduino Labs for MicroPython and establish a connection to our microcontroller.
26+
2. **Upload and Download Files**: Use the file manager to upload files from our computer to the microcontroller or download files back to our computer.
27+
3. **Organize Files**: We can create folders and store multiple files, making it easy to organize our project.
28+
29+
## Basic MicroPython File Structure
30+
31+
A typical MicroPython project includes a main file, boot script, libraries, and any supporting files our project needs. Here’s a standard layout:
32+
33+
```
34+
/ (Root Directory)
35+
├── boot.py
36+
├── main.py
37+
```
38+
39+
### Key Files
40+
41+
- **`boot.py`**: Runs once at startup, before `main.py`, and is typically used for system configurations that need to be set when the device first powers on.
42+
- **`main.py`**: This is the primary script, similar to the `setup()` and `loop()` structure in Arduino. It runs automatically after `boot.py` finishes.
43+
44+
## Example: Switching Execution from `main.py` to `favorite_things.py`
45+
46+
Let’s say we’re creating a project where `main.py` runs and, at a certain point, hands off control to `favorite_things.py` to perform a specific task. Here’s how we might organize it.
47+
48+
1. **Write the Main Script** (`main.py`): This script runs some initial code and then switches to executing `favorite_things.py`.
49+
2.Create the file "favourite_things.py".
50+
3. **Add Content to `favorite_things.py`**: In this case, we’ll simply print a message from `favorite_things.py`.
51+
52+
### Sample `main.py` Code
53+
54+
Here’s how `main.py` might look, printing an introductory message and then executing `favorite_things.py`:
55+
56+
```python
57+
def main():
58+
print("Getting the list of my favourite things...")
59+
60+
# Now execute favorite_things.py
61+
print("Switch to favorite_things.py...")
62+
try:
63+
with open("favorite_things.py") as f:
64+
exec(f.read())
65+
except OSError:
66+
print("Error: Could not open favorite_things.py")
67+
68+
if __name__ == "__main__":
69+
main()
70+
```
71+
72+
### Sample `favorite_things.py` Code
73+
74+
In `favorite_things.py`, we’ll keep it simple with a message:
75+
76+
```python
77+
# favorite_things.py
78+
print("Bears. Beets. Battlestar Galactica.")
79+
```
80+
81+
### Explanation
82+
83+
- **Switching Execution**: `main.py` starts by printing an introductory message, and then uses `exec(f.read())` to read and run the content of `favorite_things.py`.
84+
- **Running Another Script**: The `exec()` function allows `main.py` to execute `favorite_things.py` as if it were part of `main.py`, printing “Bears. Beets. Battlestar Galactica.” directly.
85+
86+
### Expected Output
87+
88+
When you run `main.py`, the output should look like this:
89+
90+
```
91+
Getting the list of my favourite things...
92+
Switch to favorite_things.py...
93+
Bears. Beets. Battlestar Galactica.
94+
```
95+
96+
This setup demonstrates how to use MicroPython’s file system to organize and execute multiple scripts, allowing for modular and readable code.
97+
98+
## Organizing Code with Modules and Libraries
99+
100+
When you start importing custom modules or libraries, MicroPython will automatically create a `/lib` folder to store them. This helps keep external libraries or reusable functions separate from your main code. You can use this structure to further organize your project, making it easy to manage larger codebases.
101+
102+
For example:
103+
104+
```
105+
/ (Root Directory)
106+
├── boot.py
107+
├── main.py
108+
├── /lib
109+
│ ├── my_custom_library.py
110+
```
111+
112+
### Using Libraries in `main.py`
113+
114+
After placing reusable code in `/lib`, you can import it directly in `main.py`:
115+
116+
```python
117+
from lib.my_custom_library import some_function
118+
119+
some_function()
120+
```
121+
122+
This structure allows for cleaner, more modular code that’s easy to scale as your project grows.
123+
124+
## Conclusion
125+
126+
MicroPython’s file system brings flexibility and structure to embedded programming, enabling a more organized and modular approach. By understanding the typical file structure and how to manage files, we’ll be able to create more complex and maintainable projects.
127+
128+
**Tips for Organizing Our MicroPython Projects**
129+
130+
- **Modularize Code with Additional Scripts**: Store reusable or specific tasks in separate files, making `main.py` clean and focused on high-level logic.
131+
- **Leverage the `/lib` Folder**: Use the automatically created `/lib` folder to keep libraries organized and separate from your main application code.
132+
- **Edit and Manage Files Easily**: Arduino Labs for MicroPython’s file management tools make it easy to upload, download, and modify files on the device.
133+
134+
With a well-organized file system, we can streamline our development process and unlock new possibilities with MicroPython!

0 commit comments

Comments
 (0)