REPL
One of the main advantages of using MicroPython is the interactive REPL. REPL (read-evaluate-print loop) stands for the read-evaluate-print loop. The REPL is very helpful for learning a new programming language, because it can respond immediately to the programs written by beginners. This means you execute the code and can immediately see the results, without the tedious steps of compiling and uploading first. For the mPython Board to make the REPL work on Windows, you need to install the cp2104 serial port driver first.
Serial Port Connection
To access it via USB-serial, you need to use serial port terminal software. On Windows, kitty and xshell are good choices. Set the serial port baud rate to 115200, and you can start playing with MicroPython.
After establishing a connection through the serial port, you can test whether it works properly by pressing the Enter key a few times. If it works properly, you will see the Python REPL prompt, represented as >>>.
Using the REPL
Once you have the prompt, you can start experimenting! After pressing the Enter key, you can type anything at the prompt. MicroPython will run the code you enter and print the result (if any); if the entered text has an error, it will print an error message.
Try entering the following at the prompt:
>>> print('hello mPython')
hello mPython
Note that you do not need to type the >>> arrows. They indicate that you should type text after this prompt, and the next line is the response.
If you already know some python, you can now try some basic commands. For example:
>>> 1+2
3
>>> 1/2
0.5
>>> 12*34
408
You can try displaying characters on the LCD screen:
>>> from lv_gui import *
>>> gui.draw_label(text=str('Hello, world!'), row=1, color=0xffffff, wrap=False)
>>> gui.update()
>>>
Note
gui.draw_label(text, row, color=0xffffff, wrap=False) text is the string to be displayed.
Then use gui.update() to refresh the screen, and the string will be displayed on the OLED display. You can try displaying any string at other positions.
Line Editing
You can use the left and right arrow keys to move the cursor to edit the currently entered line; press the Home key or ctrl-A to move the cursor to the beginning of the line, and press End or ctrl-E to move to the end of the line; the Delete key or Backspace key is used to delete.
Input History
The REPL remembers a certain number of the previous lines of text you entered (up to 8 lines on the ESP32). To recall the previous line, use the up and down arrow keys.
Tab Key
The Tab key can view the list of all members in a module. This is very useful for finding out the functions and methods that a module or object has.
Suppose you imported machine in the above example, then type . and press the Tab key to view the list of all members of the machine module:
>>> machine.
__class__ __name__ ADC DAC
DEEPSLEEP DEEPSLEEP_RESET EXT0_WAKE
EXT1_WAKE HARD_RESET I2C PIN_WAKE
PWM PWRON_RESET Pin RTC
SLEEP SOFT_RESET SPI Signal
TIMER_WAKE TOUCHPAD_WAKE Timer TouchPad
UART ULP_WAKE WDT WDT_RESET
deepsleep disable_irq enable_irq freq
idle mem16 mem32 mem8
reset reset_cause sleep time_pulse_us
unique_id wake_reason
>>> machine.
Line Continuation and Auto-Indent
Some of what you type will need to be “continued”, that is, more lines of text are needed to generate a correct Python statement. In this case,
the prompt changes to ... and the cursor is automatically indented by the correct amount, so you can immediately start typing the next line.
Try this by defining the following function:
>>> def toggle(p):
... p.value(not p.value())
...
...
...
>>>
Above, you need to press the Enter key three times in a row to complete the compound statement (that is, only dots on the three lines). Another way to complete the compound statement is to press the Backspace key to reach the beginning of the line, and then press the Enter key. (If you type something wrong and want to exit, press ctrl-C, and all lines will be ignored.)
The function you just defined toggles the pin level. The pin object you created earlier should still exist (if not, you need to recreate it). You can use the following command to toggle the LED:
>>> toggle(pin)
Now let’s toggle the LED in a loop (if you don’t have an LED, you can print some text instead of calling toggle to see the effect):
>>> import time
>>> while True:
... toggle(pin)
... time.sleep_ms(500)
...
...
...
>>>
This will toggle the LED at 1Hz (half a second on, half a second off). To stop toggling, press ctrl-C, which will raise a keyboard interrupt exception and exit the loop.
Paste Mode
Pressing ctrl-E will enter the special paste mode, where you can copy and paste a large block of text into the REPL. If you press ctrl-E, you will see the paste mode prompt:
paste mode; Ctrl-C to cancel, Ctrl-D to finish
===
Then, you can paste (or type) your text. Note that no special keys or commands work in paste mode (such as Tab or Backspace);
they are just accepted as is. Press ctrl-D to finish entering the text and execute it.
Other Control Commands
There are four other control commands:
Ctrl-A on a blank line will enter raw REPL mode. This is similar to permanent paste mode, except that characters are not echoed.
Ctrl-B on a blank line goes to normal REPL mode.
Ctrl-Ccancels any input, or interrupts the currently running code.Ctrl-Don a blank line will perform a soft reboot.