REPL
One major advantage 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 responds immediately to programs written by beginners, meaning you can execute code and see results right away without the tedious steps of compiling and uploading first. To make the REPL work on Windows with the mPython Board, you need to install the cp2104 serial port driver first.
Serial Port Connection
To access via USB-serial, you need to use serial terminal software. On Windows, kitty and xshell are good choices. Set the serial 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 by pressing Enter a few times. If it works, you will see the Python REPL prompt, shown as >>>.
Using the REPL
Once you see the prompt, you can start experimenting! After pressing Enter, you can type anything at the prompt. MicroPython will run the code you type and print the result (if any); if there is an error in the input text, it will print an error message.
Try typing 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 the prompt, and the next line shows 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
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 display.
Then use gui.update() to refresh the screen, and the string will appear 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 and edit the current input line; press Home or ctrl-A to move the cursor to the beginning of the line, and End or ctrl-E to move to the end; use the Delete key or Backspace to delete characters.
Input History
The REPL remembers a certain number of previously entered lines (up to 8 on ESP32). To recall the previous line, use the up and down arrow keys.
Tab Key
The Tab key shows a list of all members in a module. This is very useful for finding out the functions and methods that a module or object provides.
Suppose you imported machine in the example above, then type . and press Tab to see 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-Indentation
Some of what you type will require “continuation”, meaning more lines of text are needed to form a valid 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 Enter three times consecutively to complete the compound statement (i.e. three lines with only dots). Another way to complete a compound statement is to press Backspace until you reach the beginning of the line, then press Enter. (If you typed something wrong and want to exit, press ctrl-C and all lines will be discarded.)
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), and 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 1 Hz (half a second on, half a second off). To stop toggling, press ctrl-C, which raises a KeyboardInterrupt exception and exits the loop.
Paste Mode
Pressing ctrl-E enters a special paste mode that allows you to 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 text and execute it.
Other Control Commands
There are four other control commands:
Ctrl-A on a blank line enters raw REPL mode. This is similar to permanent paste mode, except characters are not echoed.
Ctrl-B at a blank prompt returns to normal REPL mode.
Ctrl-Ccancels any input or interrupts currently running code.Ctrl-Don a blank line performs a soft reboot.