mirror of
https://github.com/alankrantas/microbit-micropython-cookbook.git
synced 2025-12-16 10:00:37 +01:00
Update README.md
This commit is contained in:
52
README.md
52
README.md
@@ -1,10 +1,14 @@
|
||||
# micro:bit MicroPython Cookbook (Updating)
|
||||
|
||||
My notes for some Python tricks and experiments on BBC micro:bit, mostly written by me.
|
||||

|
||||
|
||||
[BBC micro:bit MicroPython documentation](https://microbit-micropython.readthedocs.io/en/latest/index.html#)
|
||||
|
||||
This is my notes, tricks and experiments for BBC micro:bit with MicroPython.
|
||||
|
||||
## Easer Eggs
|
||||
|
||||
Enter the following codes into REPL:
|
||||
Enter the following codes in [REPL](https://microbit-micropython.readthedocs.io/en/latest/devguide/repl.html):
|
||||
|
||||
```python
|
||||
import this
|
||||
@@ -12,22 +16,40 @@ import love
|
||||
import antigravity
|
||||
```
|
||||
|
||||
Also
|
||||
The result from <b>import this</b> is a version of [Zen of Python](https://www.python.org/dev/peps/pep-0020/) and <b>import antigravity</b> is from [original Python easter egg](https://xkcd.com/353/).
|
||||
|
||||
Also you can try
|
||||
|
||||
```python
|
||||
this.authors()
|
||||
love.badaboom()
|
||||
```
|
||||
|
||||
## Why You Shouldn't Use *
|
||||
## Some Lesser Known Facts
|
||||
|
||||
The following code
|
||||
Since both Python and MicroPython are interpreted languages, they eat a lot of memory. Also, the hex file generated by micro:bit Python editors are consisted of 2 parts: the MicroPython firmware (up to 248 KB) and user's script (up to only 8 KB). See [Firmware Hex File](https://microbit-micropython.readthedocs.io/en/latest/devguide/hexformat.html). Which means it's not possible to build big projects with micro:bit's MicroPython.
|
||||
|
||||
One way to "minimize" your script size is to use one-space indents instead of 4.
|
||||
|
||||
Also, about how micro:bit get its own version of MicroPython: [The Story of MicroPython on the BBC micro:bit](http://ntoll.org/article/story-micropython-on-microbit) by Nicholas H. Tollervey, who also created the [Mu editor](https://codewith.mu/).
|
||||
|
||||
## Editor of Choice
|
||||
|
||||
The official [Python online editor](https://python.microbit.org/v/2.0) does not need installation and can be used anywhere with Internet and Chrome web browser. Support Web-USB. It's ok to use, really.
|
||||
|
||||
Personally, I would perfer [Mu editor](https://codewith.mu/) for any beginners. It has code check, (limited) auto-complete and can automatically detect/upload code to your micro:bit.
|
||||
|
||||
If you have experiences with MicroPython with ESP8266/ESP32 or CircuitPython, you can consider [Thonny](https://thonny.org/) which allows you to access micro:bit's REPL directly without having to upload hex file.
|
||||
|
||||
## Why You Shouldn't Use * For Import
|
||||
|
||||
The following import statement
|
||||
|
||||
```python
|
||||
from microbit import *
|
||||
```
|
||||
|
||||
is a bad idea. This way imports everything of the microbit module even you don't need many of the features and wastes extra memory.
|
||||
is a bad idea. This imports everything of the microbit module even you don't need many of the features and wastes extra memory.
|
||||
|
||||
Instead, you should only import sub-modules you are going to use:
|
||||
|
||||
@@ -43,7 +65,7 @@ from micropython import mem_info
|
||||
print(mem_info(1))
|
||||
```
|
||||
|
||||
You can also try to turn on garbage collection:
|
||||
You can also try to turn on garbage collection if the memory is almost full:
|
||||
|
||||
```python
|
||||
import gc
|
||||
@@ -52,14 +74,6 @@ gc.enable() # auto memory recycle
|
||||
gc.collect() # force memory recycle
|
||||
```
|
||||
|
||||
## Some Lesser Known Facts
|
||||
|
||||
Since Python and MicroPython are interpreted languages, they eat a lot of memory. Also, the hex file generated by micro:bit Python editors are consisted of 2 parts: the MicroPython firmware (up to 248 KB) and user's script (up to only 8 KB). See [Firmware Hex File](https://microbit-micropython.readthedocs.io/en/latest/devguide/hexformat.html). Which means it's not possible to build big projects with micro:bit's MicroPython.
|
||||
|
||||
One way to "minimize" your script size is to use one-space indents instead of 4.
|
||||
|
||||
Also, how micro:bit get its own version of MicroPython anyway: [The Story of MicroPython on the BBC micro:bit](http://ntoll.org/article/story-micropython-on-microbit) by Nicholas H. Tollervey, who also created the [Mu editor](https://codewith.mu/), which is easier to use than the official online editor.
|
||||
|
||||
## Fill LED Display
|
||||
|
||||
Light up every LEDs. Use fillScreen() as default.
|
||||
@@ -74,10 +88,16 @@ def fillScreen(b = 9):
|
||||
|
||||
while True:
|
||||
|
||||
for _ in range(2):
|
||||
fillScreen()
|
||||
sleep(100)
|
||||
fillScreen(0)
|
||||
sleep(100)
|
||||
|
||||
for i in range(9):
|
||||
fillScreen(i)
|
||||
sleep(50)
|
||||
|
||||
|
||||
for i in reversed(range(9)):
|
||||
fillScreen(i)
|
||||
sleep(50)
|
||||
|
||||
Reference in New Issue
Block a user