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:
169
README.md
169
README.md
@@ -25,6 +25,14 @@ this.authors()
|
||||
love.badaboom()
|
||||
```
|
||||
|
||||
## 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.
|
||||
|
||||
## 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.
|
||||
@@ -35,13 +43,9 @@ micro:bit's MicroPython is based on Python 3.4. Which means many built-in Python
|
||||
|
||||
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
|
||||
## 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.
|
||||
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.
|
||||
|
||||
## Why You Shouldn't Use * For Import
|
||||
|
||||
@@ -76,25 +80,45 @@ gc.enable() # auto memory recycle
|
||||
gc.collect() # force memory recycle
|
||||
```
|
||||
|
||||
## Recursion is Not Welcomed
|
||||
|
||||
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.
|
||||
|
||||
## Classic Blinky
|
||||
|
||||
```python
|
||||
from microbit import display, sleep
|
||||
from microbit import display, Image, sleep
|
||||
|
||||
while True:
|
||||
display.set_pixel(0, 0, 9)
|
||||
display.show(Image.HEART)
|
||||
sleep(1000)
|
||||
display.set_pixel(0, 0, 0)
|
||||
display.clear()
|
||||
sleep(1000)
|
||||
```
|
||||
|
||||
## Blinky LEDs Without Using Sleep
|
||||
|
||||
The two LEDs would blink at different intervals.
|
||||
|
||||
```python
|
||||
from microbit import display
|
||||
import utime
|
||||
|
||||
delay1, delay2 = 1000, 300
|
||||
since1, since2 = utime.ticks_ms(), utime.ticks_ms()
|
||||
|
||||
while True:
|
||||
|
||||
now = utime.ticks_ms()
|
||||
|
||||
if utime.ticks_diff(now, since1) >= delay1:
|
||||
display.set_pixel(0, 0, 9 if display.get_pixel(0, 0) == 0 else 0)
|
||||
since1 = utime.ticks_ms()
|
||||
|
||||
if utime.ticks_diff(now, since2) >= delay2:
|
||||
display.set_pixel(4, 4, 9 if display.get_pixel(4, 4) == 0 else 0)
|
||||
since2 = utime.ticks_ms()
|
||||
```
|
||||
|
||||
## Fill LED Display
|
||||
|
||||
Light up every LEDs. Use fillScreen() as default.
|
||||
Light up every LEDs. You can use fillScreen() as default.
|
||||
|
||||
```python
|
||||
from microbit import display, Image, sleep
|
||||
@@ -121,6 +145,34 @@ while True:
|
||||
sleep(50)
|
||||
```
|
||||
|
||||
## LED Bar Graph
|
||||
|
||||
A 25-level LED progress bar.
|
||||
|
||||
```python
|
||||
from microbit import display, sleep
|
||||
|
||||
def plotBarGraph(value, maxValue, brightness=9):
|
||||
bar = value / maxValue
|
||||
valueArray = ((0.96, 0.88, 0.84, 0.92, 1.00),
|
||||
(0.76, 0.68, 0.64, 0.72, 0.80),
|
||||
(0.56, 0.48, 0.44, 0.52, 0.60),
|
||||
(0.36, 0.28, 0.24, 0.32, 0.40),
|
||||
(0.16, 0.08, 0.04, 0.12, 0.20))
|
||||
for y in range(5):
|
||||
for x in range(5):
|
||||
display.set_pixel(x, y,
|
||||
brightness if bar >= valueArray[y][x] else 0)
|
||||
|
||||
|
||||
while True:
|
||||
lightLevel = display.read_light_level()
|
||||
plotBarGraph(lightLevel, 255) # or plotBarGraph(lightLevel, 255, 9)
|
||||
sleep(50)
|
||||
```
|
||||
|
||||
Since read_light_level() uses LEDs themselves as light sensors (see [this video](https://www.youtube.com/watch?v=TKhCr-dQMBY)), The LED screen would flicker a bit.
|
||||
|
||||
## A More Convenient Pin Class
|
||||
|
||||
Make a Pin class to "rename" existing pin methods.
|
||||
@@ -163,7 +215,7 @@ while True:
|
||||
|
||||
## Another Version of Pin Class
|
||||
|
||||
Use **namedtuple** as a simple Pin class. Save more memory than regular class.
|
||||
Use **namedtuple** as a simple Pin class. Might save more memory than regular class.
|
||||
|
||||
```python
|
||||
from microbit import pin0, pin2, sleep
|
||||
@@ -184,74 +236,18 @@ while True:
|
||||
sleep(50)
|
||||
```
|
||||
|
||||
## Blinky Without Using Sleep
|
||||
## Value Mapping
|
||||
|
||||
Translate a value in a range to its corresponding value in anoher range. Borrowed from [here](https://stackoverflow.com/questions/1969240/mapping-a-range-of-values-to-another).
|
||||
|
||||
```python
|
||||
from microbit import display
|
||||
import utime
|
||||
|
||||
delay = 1000
|
||||
since = utime.ticks_ms()
|
||||
|
||||
while True:
|
||||
|
||||
now = utime.ticks_ms()
|
||||
|
||||
if utime.ticks_diff(now, since) >= delay:
|
||||
display.set_pixel(0, 0, 9 if display.get_pixel(0, 0) == 0 else 0)
|
||||
since = utime.ticks_ms()
|
||||
def translate(value, leftMin, leftMax, rightMin, rightMax):
|
||||
leftSpan = leftMax - leftMin
|
||||
rightSpan = rightMax - rightMin
|
||||
valueScaled = float(value - leftMin) / float(leftSpan)
|
||||
return rightMin + (valueScaled * rightSpan)
|
||||
```
|
||||
|
||||
This method would be useful if you want to do severl things at different intervals:
|
||||
|
||||
```python
|
||||
from microbit import display
|
||||
import utime
|
||||
|
||||
delay1, delay2 = 1000, 300
|
||||
since1, since2 = utime.ticks_ms(), utime.ticks_ms()
|
||||
|
||||
while True:
|
||||
|
||||
now = utime.ticks_ms()
|
||||
|
||||
if utime.ticks_diff(now, since1) >= delay1:
|
||||
display.set_pixel(0, 0, 9 if display.get_pixel(0, 0) == 0 else 0)
|
||||
since1 = utime.ticks_ms()
|
||||
|
||||
if utime.ticks_diff(now, since2) >= delay2:
|
||||
display.set_pixel(4, 4, 9 if display.get_pixel(4, 4) == 0 else 0)
|
||||
since2 = utime.ticks_ms()
|
||||
```
|
||||
|
||||
## LED Bar Graph
|
||||
|
||||
A 25-level LED progress bar.
|
||||
|
||||
```python
|
||||
from microbit import display, sleep
|
||||
|
||||
def plotBarGraph(value, maxValue, brightness=9):
|
||||
bar = value / maxValue
|
||||
valueArray = ((0.96, 0.88, 0.84, 0.92, 1.00),
|
||||
(0.76, 0.68, 0.64, 0.72, 0.80),
|
||||
(0.56, 0.48, 0.44, 0.52, 0.60),
|
||||
(0.36, 0.28, 0.24, 0.32, 0.40),
|
||||
(0.16, 0.08, 0.04, 0.12, 0.20))
|
||||
for y in range(5):
|
||||
for x in range(5):
|
||||
display.set_pixel(x, y,
|
||||
brightness if bar >= valueArray[y][x] else 0)
|
||||
|
||||
|
||||
while True:
|
||||
lightLevel = display.read_light_level()
|
||||
plotBarGraph(lightLevel, 255) # or plotBarGraph(lightLevel, 255, 9)
|
||||
sleep(50)
|
||||
```
|
||||
|
||||
Since read_light_level() uses LEDs themselves as light sensors (see [this video](https://www.youtube.com/watch?v=TKhCr-dQMBY)), in this example a short delay is added, but the LED screen would still flicker a bit.
|
||||
|
||||
## Servo Control
|
||||
|
||||
```python
|
||||
@@ -273,19 +269,8 @@ while True:
|
||||
|
||||
Do not use servos and buzzers at the same time. They require different PWM frequencies and most microcontrollers can only set one frequency accross all pins at a time.
|
||||
|
||||
Also: micro:bit's power output may just (barely) enough to power a single SG90 mini servo. External power supply recommended.
|
||||
Also: micro:bit's power output may just (barely) enough to power a single SG90 mini servo. External power supply is recommended.
|
||||
|
||||
## Value Mapping
|
||||
|
||||
Translate a value in a range to its corresponding value in anoher range. Borrowed from [here](https://stackoverflow.com/questions/1969240/mapping-a-range-of-values-to-another).
|
||||
|
||||
```python
|
||||
def translate(value, leftMin, leftMax, rightMin, rightMax):
|
||||
leftSpan = leftMax - leftMin
|
||||
rightSpan = rightMax - rightMin
|
||||
valueScaled = float(value - leftMin) / float(leftSpan)
|
||||
return rightMin + (valueScaled * rightSpan)
|
||||
```
|
||||
|
||||
## Get Pitch and Roll Degrees
|
||||
|
||||
@@ -314,7 +299,7 @@ while True:
|
||||
print('Pitch:', rotationPitch(), ' / roll:', rotationRoll())
|
||||
sleep(100)
|
||||
```
|
||||
## NeoPixel Rainbow/Rotation
|
||||
## NeoPixel Rainbow/Rotation Effect
|
||||
|
||||
This code needs at least 3 LEDs in the NeoPixel chain. Of course, you can set a number (much) higher than actual LEDs to get smooth rainbow effects.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user