Skip to content

Commit a9f3030

Browse files
dhylandsdpgeorge
authored andcommitted
docs: Add docs about REPL paste-mode and Control-C
1 parent 98fb0bf commit a9f3030

File tree

3 files changed

+212
-6
lines changed

3 files changed

+212
-6
lines changed

README.md

-6
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,3 @@ correct permissions. Try then:
151151

152152
$ sudo dfu-util -a 0 -d 0483:df11 -D build-PYBV10/firmware.dfu
153153

154-
Documentation
155-
-------------
156-
157-
You can find information about the documentation in the
158-
[docs/README.md](https://github.com/micropython/micropython/blob/master/docs/README.md) file.
159-

docs/reference/index.rst

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ documentation at
99
Differences to standard Python as well as additional features of
1010
MicroPython are described in the sections here.
1111

12+
.. toctree::
13+
:maxdepth: 1
14+
15+
repl.rst
16+
1217
.. only:: port_pyboard
1318

1419
.. toctree::

docs/reference/repl.rst

+207
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
The MicroPython Interactive Interpreter Mode (aka REPL)
2+
=======================================================
3+
4+
This section covers some characteristics of the MicroPython Interactive
5+
Interpreter Mode. A commonly used term for this is REPL (read-eval-print-loop)
6+
which will used to refer to this interactive prompt.
7+
8+
Auto-indent
9+
-----------
10+
11+
When typing python statements which end in a colon (for example if, for, while)
12+
then the prompt will change to three dots (...) and the cursor will be indented
13+
by 4 spaces. When you press return, the next line will continue at the same
14+
level of indentation for regular statements or an additional level of indentation
15+
where appropriate. If you press the backspace key then it will undo one
16+
level of indentation.
17+
18+
If your cursor is all the way back at the beginning, pressing RETURN will then
19+
execute the code that you've entered. The following shows what you'd see
20+
after entering a for statement (the underscore shows where the cursor winds up):
21+
22+
>>> for i in range(3):
23+
... _
24+
25+
If you then enter an if statement, an additional level of indentation will be
26+
provided:
27+
28+
>>> for i in range(30):
29+
... if i > 3:
30+
... _
31+
32+
Now enter ``break`` followed by RETURN and press BACKSPACE:
33+
34+
>>> for i in range(30):
35+
... if i > 3:
36+
... break
37+
... _
38+
39+
Finally type ``print(i)``, press RETURN, press BACKSPACE and press RETURN again:
40+
41+
>>> for i in range(30):
42+
... if i > 3:
43+
... break
44+
... print(i)
45+
...
46+
0
47+
1
48+
2
49+
3
50+
>>>
51+
52+
Auto-completion
53+
---------------
54+
55+
While typing a command at the REPL, if the line typed so far corresponds to
56+
the beginning of the name of something, then pressing TAB will show
57+
possible things that could be entered. For example type ``m`` and press TAB
58+
and it should expand to ``machine``. Enter a dot ``.`` and press TAB again. You
59+
should see something like:
60+
61+
>>> machine.
62+
__name__ info unique_id reset
63+
bootloader freq rng idle
64+
sleep deepsleep disable_irq enable_irq
65+
Pin
66+
67+
The word will be expanded as much as possible until multiple possibilities exist.
68+
For example, type ``machine.Pin.AF3`` and press TAB and it will expand to
69+
``machine.Pin.AF3_TIM``. Pressing TAB a second time will show the possible
70+
expansions:
71+
72+
>>> machine.Pin.AF3_TIM
73+
AF3_TIM10 AF3_TIM11 AF3_TIM8 AF3_TIM9
74+
>>> machine.Pin.AF3_TIM
75+
76+
Interrupting a running program
77+
------------------------------
78+
79+
You can interupt a running program by pressing Ctrl-C. This will raise a KeyboardInterrupt
80+
which will bring you back to the REPL, providing your program doesn't intercept the
81+
KeyboardInterrupt exception.
82+
83+
For example:
84+
85+
>>> for i in range(1000000):
86+
... print(i)
87+
...
88+
0
89+
1
90+
2
91+
3
92+
...
93+
6466
94+
6467
95+
6468
96+
Traceback (most recent call last):
97+
File "<stdin>", line 2, in <module>
98+
KeyboardInterrupt:
99+
>>>
100+
101+
Paste Mode
102+
----------
103+
104+
If you want to paste some code into your terminal window, the auto-indent feature
105+
will mess things up. For example, if you had the following python code: ::
106+
107+
def foo():
108+
print('This is a test to show paste mode')
109+
print('Here is a second line')
110+
foo()
111+
112+
and you try to paste this into the normal REPL, then you will see something like
113+
this:
114+
115+
>>> def foo():
116+
... print('This is a test to show paste mode')
117+
... print('Here is a second line')
118+
... foo()
119+
...
120+
Traceback (most recent call last):
121+
File "<stdin>", line 3
122+
IndentationError: unexpected indent
123+
124+
If you press Ctrl-E, then you will enter paste mode, which essentially turns off
125+
the auto-indent feature, and changes the prompt from ``>>>`` to ``===``. For example:
126+
127+
>>>
128+
paste mode; Ctrl-C to cancel, Ctrl-D to finish
129+
=== def foo():
130+
=== print('This is a test to show paste mode')
131+
=== print('Here is a second line')
132+
=== foo()
133+
===
134+
This is a test to show paste mode
135+
Here is a second line
136+
>>>
137+
138+
Paste Mode allows blank lines to be pasted. The pasted text is compiled as if
139+
it were a file. Pressing Ctrl-D exits paste mode and initiates the compilation.
140+
141+
Soft Reset
142+
----------
143+
144+
A soft reset will reset the python interpreter, but tries not to reset the
145+
method by which you're connected to the MicroPython board (USB-serial, or Wifi).
146+
147+
You can perform a soft reset from the REPL by pressing Ctrl-D, or from your python
148+
code by executing: ::
149+
150+
raise SystemExit
151+
152+
For example, if you reset your MicroPython board, and you execute a dir()
153+
command, you'd see something like this:
154+
155+
>>> dir()
156+
['__name__', 'pyb']
157+
158+
Now create some variables and repeat the dir() command:
159+
160+
>>> i = 1
161+
>>> j = 23
162+
>>> x = 'abc'
163+
>>> dir()
164+
['j', 'x', '__name__', 'pyb', 'i']
165+
>>>
166+
167+
Now if you enter Ctrl-D, and repeat the dir() command, you'll see that your
168+
variables no longer exist:
169+
170+
.. code-block:: python
171+
172+
PYB: sync filesystems
173+
PYB: soft reboot
174+
MicroPython v1.5-51-g6f70283-dirty on 2015-10-30; PYBv1.0 with STM32F405RG
175+
Type "help()" for more information.
176+
>>> dir()
177+
['__name__', 'pyb']
178+
>>>
179+
180+
The special variable _ (underscore)
181+
-----------------------------------
182+
183+
When you use the REPL, you may perfom computations and see the results.
184+
MicroPython stores the results of the previous statment in the variable _ (underscore).
185+
So you can use the underscore to save the result in a variable. For example:
186+
187+
>>> 1 + 2 + 3 + 4 + 5
188+
15
189+
>>> x = _
190+
>>> x
191+
15
192+
>>>
193+
194+
Raw Mode
195+
--------
196+
197+
Raw mode is not something that a person would normally use. It is intended for
198+
programmatic use. It essentially behaves like paste mode with echo turned off.
199+
200+
Raw mode is entered using Ctrl-A. You then send your python code, followed by
201+
a Ctrl-D. The Ctrl-D will be acknowledged by 'OK' and then the python code will
202+
be compiled and executed. Any output (or errors) will be sent back. Entering
203+
Ctrl-B will leave raw mode and return the the regular (aka friendly) REPL.
204+
205+
The ``tools/pyboard.py`` program uses the raw REPL to execute python files on the
206+
MicroPython board.
207+

0 commit comments

Comments
 (0)