|  | 
|  | 1 | +# Porting to a new device | 
|  | 2 | + | 
|  | 3 | +Porting MicroPythonOS to a new device can be fairly easy, if it's a common device that can already run MicroPython. | 
|  | 4 | + | 
|  | 5 | +Ideally, the hardware drivers (for your display) are also already available in the [lvgl_micropython](https://github.com/lvgl-micropython/lvgl_micropython) project, which MicroPythonOS heavily relies upon. | 
|  | 6 | + | 
|  | 7 | +Of course, porting is always quite a technical undertaking, so if you want to make your life easy, just purchase one of the already supported devices - they're fairly cheap. | 
|  | 8 | + | 
|  | 9 | +## What to write | 
|  | 10 | + | 
|  | 11 | +By design, the only device-specific code for MicroPythonOS is found in the ```internal_filesystem/boot*.py``` files. | 
|  | 12 | + | 
|  | 13 | +## Steps to port to a new device | 
|  | 14 | + | 
|  | 15 | +1. Compile [lvgl_micropython](https://github.com/lvgl-micropython/lvgl_micropython) for the new device. | 
|  | 16 | + | 
|  | 17 | +	The goal is to have it boot and show a MicroPython REPL shell on the serial line. | 
|  | 18 | + | 
|  | 19 | +	Take a look at our [build_lvgl_micropython.sh](https://github.com/MicroPythonOS/MicroPythonOS/blob/main/scripts/build_lvgl_micropython.sh) script. A "dev" build (without any "frozen" filesystem) is preferred as this will still change a lot. | 
|  | 20 | + | 
|  | 21 | +	Also go over the [official lvgl_micropython documentation](https://github.com/lvgl-micropython/lvgl_micropython/blob/main/README.md) for porting instructions. If you're in luck, your device is already listed in the esp32 BOARD list. Otherwise use a generic one like `BOARD=ESP32_GENERIC` with `BOARD_VARIANT=SPIRAM` or `BOARD=ESP32_GENERIC_S3` with `BOARD_VARIANT=SPIRAM_OCT` if it has an SPIRAM. | 
|  | 22 | + | 
|  | 23 | +2. Figure out how to initialize the display for the new device | 
|  | 24 | + | 
|  | 25 | +    Use the MicroPython REPL shell on the serial port to type or paste (CTRL-E) MicroPython code. | 
|  | 26 | +     | 
|  | 27 | +    Check out how it's done for the [Waveshare 2 inch Touch Screen](https://github.com/MicroPythonOS/MicroPythonOS/blob/main/internal_filesystem/boot.py) and for the [Fri3d Camp 2024 Badge](https://github.com/MicroPythonOS/MicroPythonOS/blob/main/internal_filesystem/boot_fri3d-2024.py). You essentially need to set the correct pins to which the display is connected (like `LCD_SCLK`, `LCD_MOSI`, `LCD_MOSI` etc.) and also set the resolution of the display (`TFT_HOR_RES`, `TFT_VER_RE`S). | 
|  | 28 | +     | 
|  | 29 | +    After a failed attempt, reset the device to make sure the hardware is in a known initial state again. | 
|  | 30 | +     | 
|  | 31 | +    If the display stays black, and your pins are correct, you may need to change between reset_state=0 and reset_state=1. | 
|  | 32 | +     | 
|  | 33 | +    If the colors are off, play around with `color_space=lv.COLOR_FORMAT.RGB565`, `color_byte_order=st7789.BYTE_ORDER_BGR` and `rgb565_byte_swap=True`. | 
|  | 34 | + | 
|  | 35 | +    If you're successful, the display will still be black because LVGL isn't drawing anything to it yet. | 
|  | 36 | +    So use this code (based on main.py and helloworld.py) at the end to display something: | 
|  | 37 | +    <pre> | 
|  | 38 | +    ``` | 
|  | 39 | +    import lvgl as lv | 
|  | 40 | +    import task_handler | 
|  | 41 | +    task_handler.TaskHandler(duration=5) | 
|  | 42 | +    label = lv.label(lv.screen_active()) | 
|  | 43 | +    label.set_text('Hello World!') | 
|  | 44 | +    label.center() | 
|  | 45 | +    ``` | 
|  | 46 | +    </pre> | 
|  | 47 | + | 
|  | 48 | +3. Put the initialization code in a custom boot_...py file for your device | 
|  | 49 | + | 
|  | 50 | +4. Copy the custom boot_...py file and the generic MicroPythonOS files to your device (see [install.sh](https://github.com/MicroPythonOS/MicroPythonOS/blob/main/scripts/install.sh) | 
|  | 51 | + | 
|  | 52 | +    After reset, your custom boot_...py file should initialize the display and then MicroPythonOS should start, run the launcher, which shows the icons etc. | 
|  | 53 | + | 
|  | 54 | +5. Add more hardware support | 
|  | 55 | + | 
|  | 56 | +    If your device has a touch screen, check out how it's initialized for the [Waveshare 2 inch Touch Screen](https://github.com/MicroPythonOS/MicroPythonOS/blob/main/internal_filesystem/boot.py). | 
|  | 57 | +    If it has buttons for input, check out the KeyPad code for the [Fri3d Camp 2024 Badge](https://github.com/MicroPythonOS/MicroPythonOS/blob/main/internal_filesystem/boot_fri3d-2024.py). | 
|  | 58 | +     | 
|  | 59 | +    Now you should be able to control the device, connect to WiFi and install more apps from the AppStore. | 
|  | 60 | +     | 
|  | 61 | +    This would be a good time to create a pull-request to merge your boot_...py file into the main codebase so the support becomes official! | 
0 commit comments