[Wiki]Introduction to MicroPython on Pokitto HW



It is easier to develop Python scripts first on Pokitto Simulator, as the development and testing cycle is faster on PC. An exception is the case when there are features the Simulator do not support, like GIOP-port usage. Also, simulator has a lot of memory and CPU power which makes it to behave different than the real HW. Anyway, your final target is Pokitto, so sooner or later you will try to run your scripts on HW.

Build the example application

1) Start the EmBitz application and select the “MicroPython” target in the default project.

All the MicroPython related files are in the folder Pokitto\POKITTO_LIBS\MicroPython. Most of the files in subfolders are copied from the actual MicroPython project, to be able to compile Python files (.py) to bytecode (.mpy ) and freezing them, i.e. wrapping the bytecode as C-code for compiling it as a part of the flash image. The src_py subfolder is the folder where your own codes will be stored. Everything in the src_py folder will be automatically included in the flash image.

2) Select the file Pokitto\POKITTO_LIBS\MicroPython\My_settings.h in the Project window and set the defines as below.

   #define PROJ_PYTHON_REPL 0      // Do not use the interactive prompt
   #define MICROPY_ENABLE_GC 1     // This just means micropython is in use
   #ifdef POK_SIM
   #define USE_USB_SERIAL_PRINT 0  // Never use the USB serial with the simulator. 
   #define USE_USB_SERIAL_PRINT 1  // Use USB serial with the HW.

Note: if you set the project to use the USB serial connection, you cannot start or use Pokitto without the USB cable connected. In the final flash image you should set USE_USB_SERIAL_PRINT to 0.

3) Rebuild the Micropython target

As a pre-build step, at the top of the build log window, you should see how the example Python files get compiled to the bytecode and froze. The combined resultin file is frozen_mpy.c, which will be linked to the Pokitto binary. Later in the log, the Python core library (libmicropython.a) is linked to the binary too.

4) Flash the binary file to Pokitto

If the building went without errors there should now be a flash image file: Pokitto\build\pythonex.bin. It contain all the python scripts freezed and it automatically starts the example_main.py script like already defined in Pokitto\POKITTO_LIBS\MicroPython\main.cpp

5) Restart Pokitto

Remember to keep the USB cable connected as the image will not start without it. After startup, you should see the Frogitto demo running on screen :slight_smile:

Setup serial connection over USB

A terminal connection via USB between Pokitto and PC is needed for observing the debug output, printing from a Python script, and also for interactive Python prompt (REPL).

1) Install the driver for the virtual serial port over USB

The driver for that can be found in the PokittoLib repository: PokittoLib/Pokitto/POKITTO_LIBS/USBDevice/working serial driver/serial.inf

2) You also need to install a terminal program.

One option is TeraTerm, which can be found here: https://osdn.net/projects/ttssh2/releases/

3) Start TeraTerm (or equivalent)

Select the virtual USB serial port for using. The serial connection settings can be as default.

If you see something like this on the terminal, congratulations, you have the Python serial connection output working:

display ready
all_frogittos len= 1

GC: total: 16064, used: 11568, free: 4496
 No. of 1-blocks: 330, 2-blocks: 15, max blk sz: 32, max free sz: 148

Using the Python interactive prompt (REPL)

1) Build and flash the binary image

Build the MicroPython target and flash the Pythonex.bin binary image to the Pokitto like you did with the Frogitto game, but set the define in the Pokitto\POKITTO_LIBS\MicroPython\My_settings.h file as follows:

#define PROJ_PYTHON_REPL 1     // Use the interactive prompt

2) Restart Pokitto

Remember to keep the USB cable connected as the image will not start without it. After restart, do not be confused even if the the volume settings screen stays visible on Pokitto. That is because Python has not drawn anything on the screen yet !

In the terminal application, you should see the REPL prompt as follows:

MicroPython v1.9.2-195-g51713075-dirty on 2017-10-30; Pokitto HW with LPC11U68

3) Give some Python commands

>>> print("hello world!")
>>> 123 + 456

The results will be seen on the terminal screen.

4) Draw text on the Pokitto screen

  • Copy the Python script below to the clipboard
  • Go to the terminal application and press ctrl-E to go to the paste mode
  • Paste the code from the clipboard
  • Press ctrl-D to execute the script
import upygame
import umachine
umachine.draw_text(20, 20, "hello world!", 3)

The text is drawn on the Pokitto screen.


“But wait a second”, you say, “Isn’t the Frogitto Demo still on the rom image?”

Yes, indeed. Just give a command in REPL:

import example_main

And the Demo starts.

MicroPython progress

Hello @Hanski

I was looking for some libraries on micropython to handle and display sprites and images and apparently you ported part of the pygame engine to do exactly this. But I was trying to find it on the PokittoLib and could not find the source code. The only definition that I could find was on mpconfigport linking it to the builtin module:

{ MP_ROM_QSTR(MP_QSTR_upygame), MP_ROM_PTR(&mp_module_pygame) }, 

Would you mind explaining for me a little bit where is the source code? I want to compile it on my pyboard.

Thank you very much,

Vitor Henrique


Here are all classes and methods of uPyGame listed

Here is my fork of the Micropython, which contains the Pokitto port and uPyGame