mirror of
https://github.com/alankrantas/microbit-micropython-cookbook.git
synced 2026-01-05 03:40:46 +01:00
Update README.md
This commit is contained in:
93
README.md
93
README.md
@@ -1,6 +1,6 @@
|
||||
# micro:bit MicroPython Cookbook (Updating)
|
||||
|
||||
A note for some Python tricks and experiments on BBC micro:bit.
|
||||
My notes for some Python tricks and experiments on BBC micro:bit, mostly written by me.
|
||||
|
||||
## Easer Eggs
|
||||
|
||||
@@ -19,10 +19,45 @@ this.authors()
|
||||
love.badaboom()
|
||||
```
|
||||
|
||||
## Why You Shouldn't Use *
|
||||
|
||||
The following code
|
||||
|
||||
```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.
|
||||
|
||||
Instead, you should only import sub-modules you actually use:
|
||||
|
||||
```python
|
||||
from microbit import pin0, display, sleep
|
||||
```
|
||||
|
||||
## How Much Memory Left?
|
||||
|
||||
```python
|
||||
from micropython import mem_info
|
||||
|
||||
print(mem_info(1))
|
||||
```
|
||||
|
||||
You can also try to turn on garbage collection:
|
||||
|
||||
```python
|
||||
import gc
|
||||
|
||||
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
|
||||
@@ -50,23 +85,65 @@ while True:
|
||||
|
||||
## A More Convenient Pin Class
|
||||
|
||||
Use **namedtuple** to "rename" pin methods as set() and get().
|
||||
Make a Pin class to "rename" existing pin methods.
|
||||
|
||||
```python
|
||||
from microbit import pin0, sleep
|
||||
from microbit import pin0, pin2, sleep
|
||||
|
||||
class Pin:
|
||||
|
||||
__slot__ = ["pin"]
|
||||
|
||||
def __init__(self, pin):
|
||||
self.pin = pin
|
||||
|
||||
def set(self, value):
|
||||
self.pin.write_digital(value)
|
||||
|
||||
def setPWM(self, value):
|
||||
self.pin.write_analog(value)
|
||||
|
||||
def get(self):
|
||||
self.pin.set_pull(self.pin.PULL_DOWN)
|
||||
return self.pin.read_digital()
|
||||
|
||||
def pressed(self):
|
||||
self.pin.set_pull(self.pin.PULL_UP)
|
||||
return not self.pin.read_digital()
|
||||
|
||||
def getADC(self):
|
||||
return self.pin.read_analog()
|
||||
|
||||
|
||||
led = Pin(pin0)
|
||||
button = Pin(pin2)
|
||||
|
||||
while True:
|
||||
led.set(button.pressed())
|
||||
sleep(50)
|
||||
```
|
||||
|
||||
## Another Version of Pin Class
|
||||
|
||||
Use **namedtuple** as a simple Pin class. Save more memory than regular class.
|
||||
|
||||
```python
|
||||
from microbit import pin0, pin2, sleep
|
||||
from ucollections import namedtuple
|
||||
|
||||
Pin = namedtuple('Pin', ['set', 'get'])
|
||||
setPin = lambda p: Pin(p.write_digital, p.read_digital)
|
||||
|
||||
def setPin(pin, pull_up=False):
|
||||
pin.set_pull(pin.PULL_UP if pull_up else pin.PULL_DOWN)
|
||||
return Pin(pin.write_digital, pin.read_digital)
|
||||
|
||||
|
||||
led = setPin(pin0)
|
||||
button = setPin(pin2, pull_up=True)
|
||||
|
||||
while True:
|
||||
led.set(1)
|
||||
sleep(500)
|
||||
led.set(0)
|
||||
sleep(500)
|
||||
led.set(not button.get())
|
||||
sleep(50)
|
||||
```
|
||||
|
||||
## LED Bar Graph
|
||||
|
||||
Reference in New Issue
Block a user