From 2425b703505a00f6193665bfc021370947588049 Mon Sep 17 00:00:00 2001 From: Alan Wang <44191076+alankrantas@users.noreply.github.com> Date: Sun, 14 Jun 2020 15:53:35 +0800 Subject: [PATCH] Update README.md --- README.md | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index bc1bebe..17e700a 100644 --- a/README.md +++ b/README.md @@ -43,19 +43,25 @@ dir(microbit) ## Some Lesser Known Facts -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 less likely to build bigger projects with micro:bit's MicroPython. +Since MicroPython, like standard Python, is a interpreted language, it eats a lot of memory. A slightly bigger program may cause MemoryError (out of memory). Also due to this reason, there is no room for the Bluetooth driver. + +Another limit is that the MicroPython hex file 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 less likely to build bigger projects with micro:bit's MicroPython. One way to "minimize" your script size is to use 1-space indents instead of 4. -micro:bit's MicroPython is based on Python 3.4. Which means many built-in Python advanced feaetures, like string.format(), list comprehension, list slice, variable unpacking, lambda function, decorators, generators, @classmethod, @staticmethod, etc. can be used as well. +micro:bit's MicroPython is based on Python 3.4. Which means many built-in Python advanced feaetures are supported. Although some features are dropped as well to save memory. -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/). +Nicholas H. Tollervey - also the creator of [Mu editor](https://codewith.mu/) - has an interesting account about [How BBC micro:bit get its MicroPython](http://ntoll.org/article/story-micropython-on-microbit). -## Recursion is Not Welcomed +## Editor of Choice -Since micro:bit has very limited memory, the recursion depth is severely limited. Only [8 nested function calls or so](https://mail.python.org/pipermail/microbit/2016-February/000896.html) can be used without triggering RuntimeError. +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. -## Why You Shouldn't Use * For Import +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 in standard Python or 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 switch tabs. + +## Why You Shouldn Aviud Import * The following import statement @@ -63,9 +69,9 @@ The following import statement from microbit import * ``` -is a bad idea. This 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 from the big microbit module even you don't need many of the features and thus waste memory. -Instead, you should only import sub-modules you are going to use: +Instead, you should import the only submodules you are going to use: ```python from microbit import pin0, display, sleep @@ -88,13 +94,9 @@ gc.enable() # auto memory recycle gc.collect() # force memory recycle ``` -## Editor of Choice +## Recursion is Not Welcomed -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. +Recursion depth (how many level can a function calls itself) is severely limited because of the memory constraint. Only [8 nested function calls or so](https://mail.python.org/pipermail/microbit/2016-February/000896.html) can be used without triggering RuntimeError. So sadly it's not possible to do some popular algorithms on micro:bit. ## Classic Blinky @@ -150,7 +152,7 @@ while True: for _ in range(2): fillScreen() sleep(100) - fillScreen(0) + display.clear() sleep(100) for i in range(9): @@ -214,7 +216,7 @@ while True: since2 = utime.ticks_ms() ``` -## A More Convenient Pin Class +## A More Convenient Pin Class? Make a Pin class to "rename" existing pin methods. @@ -254,7 +256,7 @@ while True: sleep(50) ``` -## Another Version of Pin Class +## Simpler Alternate Pin Class Use **namedtuple** as a simple Pin class. Might save more memory than regular class. @@ -432,6 +434,8 @@ while True: ## Calcualte Fibonacci Sequence +[Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number) + ```python from microbit import display @@ -448,7 +452,7 @@ display.scroll(b) ## Calcuate a List of Prime Numbers -Prime numbers (except 2, 3) are either 6n - 1 or 6n + 1. +Prime numbers (except 2, 3) are either 6n - 1 or 6n + 1. So we check if a number of 6n - 1/6n + 1 can be divided with any existing primes in the list. If not, it is a prime number and can be added to the list. ```python from microbit import display @@ -473,7 +477,7 @@ for prime in primes: ## Conway's Game of Life on 5x5 LED Display -The code would reset the micro:bit if there's no cell left or the cells are stable. +The code would reset the micro:bit if there's no cell left or the cells are stable. Although sometimes it may be locked into a state with the same alternating cell numbers and need manual reset. ```python from microbit import display @@ -548,7 +552,7 @@ while True: This allows you to enter your message and display it as Morse code on the LED screen. Go to the REPL mode and reset micro:bit to make it work. -If you attach a passive buzzer between pin 0 and ground you can hear it too. +If you attach a passive buzzer between pin 0 and ground you can hear the Morse code too. ```python from microbit import display, Image, sleep