The first part of this repo comprises assorted code ideas for MicroPython and also serves as an unofficial MicroPython FAQ. Some samples are targeted at Pyboard variants. Some are intended as pointers for programmers rather than being complete solutions. Egregious bugs will be fixed but I may not accept feature requests.
Section 5 is an index to complete applications and modules which are documented and supported.
Please also see the official examples.
[1:3, 20]
syntax to address a 2D array.Please read the official docs. This doc describes a simple way to override modules which have been incorporated in firmware as frozen bytecode.
Scripts for building MicroPython for various target hardware types and for updating your local source. Now detects and builds for Pyboard D. See docs
Paul Sokolovsk's PicoWeb requires his fork of MicroPython and requires some modification to run under official MicroPython.
The solution preferred by MicroPython maintainers is Microdot.
Raise an exception if a firmware build is earlier than a given date.
By default the Pyboard's /flash/boot.py
enables MSC (mass storage) mode. This
makes the Pyboard look like a USB stick, making its filesystem visible to the
PC. This helpful feature ignores a fundamental flaw which leads to filesystem
corruption. This is because the USB standard requires mass storage devices to
behave like disks with static content. By contrast a Pyboard can independently
modify the "disk" contents causing chaos.
To fix this, edit /flash/boot.py
so that the usb_mode
line reads:
pyb.usb_mode('VCP')
On reboot the Pyboard will no longer appear as a mass storage device on the PC. Various tools are available to manage the device's storage via USB. I use rshell.
Pinout diagram for the reference board with notes and warnings about reserved pins etc. See this doc. See also this excellent resource.
A means of rendering multiple larger fonts to the SSD1306 OLED display. The
Writer
class which performs this has been substantially improved and may now
be found as part of this repository.
Assorted information not yet in the official docs or hard to find.
This is a low cost precision battery backed real time clock (RTC) accurate to +-2 minutes/year. Two drivers are provided, one portable across platforms and one which is Pyboard specific.
This driver also supports the MAX31328 which is a 100% compatible chip from the same manufacturer.
The Pyboard-specific driver provides a facility to calibrate the Pyboard's RTC from the DS3231. Calibration to high precision may be achieved in five minutes.
The drivers are documented here.
A guide to writing reliable ESP8266 networking code. Probably applies to other WiFi connected MicroPython devices.
A discussion of the need for serialisation and of the relative characteristics of five libraries available to MicroPython. Includes a tutorial on a Protocol Buffer library.
This describes ways of using the Pyboard to perform precision measurements of analog signals of up to around 50KHz. It is documented here.
This doc describes a way to save RAM with Damien's lazy
loader, a reload
function, and ways to use wildcard imports.
A class providing mutual exclusion enabling hard interrupt handlers and the main program to access shared data in a manner which ensures data integrity.
Access the simpler of the Pyboard's watchdog timers.
A software watchdog timer with a fixed or variable timeout. Supports temporary suspension and permanent cancellation. The latter can be useful when debugging code to prevent a machine reboot when the application fails, terminates or is interrupted with ctrl-c. See code and comments in the test script.
Fast reverse a bytearray in Arm Thumb assembler: this
reverses the byte order of the array so [1,2,3,4]
becomes [4,3,2,1]
.
Also includes cross-platform Python code to bit-reverse (fast-ish) 8, 16 and 32 bit words.
Time a function's execution using a decorator and implement timeouts using a closure.
This benchmark tests the performance of MQTT by
periodically publishing while subscribed to the same topic. Measures the
round-trip delay. Uses the official umqtt.simple
library. Adapt to suit your
server address and desired QOS (quality of service, 0 and 1 are supported).
After 100 messages reports maximum and minimum delays.
This connect utility connects in station mode using saved connection details where possible.
These devices produce digital signals from a shaft's rotary motion in such a way that the absolute angle may be deduced. Specifically they measure incremental change: it is up to the code to keep track of absolute position, a task which has some pitfalls. This doc discusses this and points to some solutions in MicroPython code.
On the Pyboard V1.1, true random numbers may be generated rapidly with
pyb.rng()
which uses a hardware random number generator on the
microcontroller.
There are a few use-cases for pseudo random number generators. Some platforms
lack a hardware generator (e.g. the Pyboard Lite) and some ports don't support
uos.urandom
. There is also a case for running a RNG in an interrupt service
routine.
Pseudo random number generators provide repeatable sequences of numbers which can be an advantage, for example in testing. The RNG is seeded with an initial value. On each call to the function it will return a random number, but (given the same seed) the sequence of numbers following initialisation will always be the same.
See random.py for usage and timing documentation. The yasmarang generator is also included, along with my own cheap random. The latter constrains calculations to 30 bits, allowing its use in an ISR. It comes with no guarantees of random quality and the only statistical test is that the mean converges on the right value. None of these generators are suitable for cryptography.
When testing communications applications it is often necessary to check for missing, duplicated, or out-of-order messages. To do this, the transmitter test script ensures that messages include an incrementing message number. The receiver script verifies the sequence. The CheckMid class does this, also detecting transmitter reboots
A bitmap stored in a pre-allocated, fixed size bytearray may be viewed in two ways:
These views provide a Pythonic interface while retaining the non-allocating performance advantage relative to native sets and lists.
The file bitmap.py offers classes supporting these views.
The constraint 0 <= value <= max_value
applies where max_value
is a
constructor arg. The max_value
arg defines the size of the underlying
bytearray. For example if max_value
is 255, the bytearray will use 32 bytes.
The constraint applies to member values of a set, and to index values of a
boolean array. So the set will be capable of storing integers from 0 to 255,
and the array will accept indices in the same range.
These classes are lightweight. For example the IntSet
class does not include
all the dunder (magic) methods required to match the native set
class. These
may readily be added as required.
Two simple class decorators for objects useful in hardware interfacing. Documented here.
Singletons denote classes for which only a single instance can ever occur. They can be useful for hardware interface classes. Their use avoids the need for a global instance: the sole instance may be retrieved efficiently from anywhere in the code.
A functor is a class which is accessed via function call syntax. There is only
one instance, like a singleton. Initial access calls the constructor, with
subsequent accesses being via __call__
. As an object it can retain state. As
an example, a functor might have a continuously running task: successive calls
modify the behaviour of the task.
Quaternions have a reputation for being mathematically difficult. Surely true if you intend using them to write Maxwell's equations (as per Maxwell). If your ambitions are limited to manipulating three dimensional objects or processing IMU data they can be remarkably simple.
The Quaternion
class lets you create, move, transform and rotate 3D objects.
They assume no maths beyond familiarity with an xyz
coordinate system.
Includes a demo where a wireframe cube rotates in response to movements of a
BNo055 IMU, plus a demo of moving wireframe graphics. Neither demo uses trig
functions. See the docs.
This uses a Pyboard to measure the power consumption of mains powered devices. Unlike simple commercial devices it performs a true vector (phasor) measurement enabling it to provide information on power factor and to work with devices which generate as well as consume power. It uses the official LCD160CR display as a touch GUI interface. It is documented here.
The official code has a number of drawbacks, mainly a lack of portability.
Code is here.
It seems impractical to write a portable version of settime
as the
machine.RTC
class is not yet fully portable.
It's worth noting that NTP queries do time out. The default timeout is 1s: on
many WANs this is quite demanding, yet for obvious reasons a longer value does
not benefit precision. Another issue is that issuing NTP queries too frequently
causes the time server to send a KoD (Kiss of Death) packet. The official
version issues a large negative number which causes .settime
to throw an
exception. The version here returns 0: the caller should test for this. I
suspect that this also occurs occasionally in non-KoD circumstances.
Finally, an option is to run the NTP daemon on a local server which can be as simple as a Raspberry Pi. This gives high accuracy and should eliminate timeouts. Install with:
$ sudo apt install -y ntp ntpdate
and point the MicroPython device at the local server with:
ntptime.host="192.168.0.10" # Server address.
ntptime.time()
The official datetime module
is fully featured but substantial. This Date
class has no concept of time,
but is very compact. Dates are stored as a small int. Contrary to normal MP
practice, properties are used. This allows basic arithmetic syntax while
ensuring automatic rollover. The speed penalty of properties is unlikely to be
a factor in date operations.
The Date
class provides basic arithmetic and comparison methods. The
DateCal
subclass adds pretty printing and methods to assist in creating
calendars.
The classes are documented here
This was found here:
def gcd(a, b) :
while b:
a, b = b, a % b
return a
This enables a class to be written that maps a 2D address onto an underlying 1D addressable object such as an array, list or random access file. An instance can then be accessed with syntax such as
s = sum(obj[5, 0:20]) # Sum row 5, cols 0..19 (or x = 5, y = 0..19)
obj[10, 10] = 42
obj[0:5, 3] = iter(range(100, 105))
See the docs.
This module enables Sun and Moon rise and set times to be determined for timing applications or for lunar clocks. Moon phase can also be accessed. Designed to work at global locations and timezones. See docs.
This index references applications and device drivers that I have developed, in some cases as collaborations. This is acknowledged in their respective docs.
Unlike the code samples these are fully documented and supported.
Tutorial
Main focus: using asyncio to drive and communicate with hardware devices.
Drivers
Asynchronous device drivers for switches, pushbuttons and ADC's. Also has
information on interfacing interrupts to asyncio.
Asynchronous monitor Use a
raspberry Pico and a logic analyser or scope to monitor asynchronous code.
Schedule
Schedule events at specified times and dates.
HTU21D
Asynchronous driver for this temperature and humidity sensor.
I2C Slave
Uses the Pyboard's I2C slave mode to implement a full duplex asynchronous
link. Principal use case is for ESP8266 which has only one UART.
GPS
See section 5.4.
EEPROM Support for EEPROM, FRAM and Flash chips and modules. In all cases devices or sets of devices can be configured as a single memory array supporting access either as an array of bytes or as a filesystem.
BNo055 In my view the
best IMU as it performs internal sensor fusion.
MPU9x50 Driver for
the popular InvenSense MPU9250, MPU9150, MPU6050 devices.
BMP180 Driver for the
Bosch BMP180 pressure/temperature sensor.
Fusion Sensor fusion:
combine IMU readings to produce heading, pitch and roll or Quaternion data.
Quaternions The proper way to deal with 3D
rotations. Amazingly requires less maths than Euler angles.
Asynchronous GPS driver
Thermal IR Support for the
Adafruit 3538 thermal camera. Includes optional bicubic interpolation.
Audio High quality audio
I/O with the Adafruit VS1053 board. Synchronous and asynchronous options.
IR remotes Support for various
infra red protocols in receiver or transmitter ('blaster') applications.
433MHz remote Supports
433MHz remote controlled wall sockets. Captures the signal enabling replay so
that mains devices may safely be controlled at low cost.
BME280 A bugfix fork of an
abandoned project. Supports the BME280 combined temperature, pressure and
humidity sensor on ESP8266.
Asynchronous MQTT
This improves on official MQTT drivers by recovering from WiFi outages and
offering asynchronous operation. Now also supports micropower applications on
ESP32 using ESPNow (via an MQTT subset).
IOT An IOT solution. Provides
a socket-like interface between WiFi clients and a server with wired ethernet.
The interface is resilient in the presence of WiFi outages. The server (e.g.
Raspberry Pi) does any internet work, improving security.
MessagePack Serialisation
with usage almost identical to ujson
but with message compression and other
advantages.
Radio Simplify use of the
officially supported NRF24l01 radio.
Fonts, graphics, GUIs and display drivers.
All GUIs are based on asyncio. Where input is supported a callback-based interface is provided: knowledge of asyncio is not required for their use.
font-to-py Converts
industry standard font files to Python source which may be frozen as bytecode.
Files use minimal RAM when frozen.
writer
A simple way to render text to displays where the driver is subclassed from
framebuf
.
nano-gui Simple output
only GUI for displays where the driver is subclassed from framebuf
. Repo
includes display drivers for various displays including TFT, OLED, ePaper and
Sharp. Supports a range of hosts with low RAM usage.
micro-gui Derived from
nano-gui and supporting the same displays and hosts, this provides for user
input via pushbuttons or a navigation joystick.
micropython-touch Similar to
micro-gui but uses touch input. Supports multiple displays, hosts and touch
controllers.
LCD160CR Touch GUI
for the official display module.
TFT-GUI A fast touch GUI
for large displays based on SSD1963 controller with XPT2046 touch controller.
RA8875-GUI Touch GUI for
large displays based on the RA8875 controller (e.g. from Adafruit).
micropower Support for low power applications on Pyboard 1.x and Pyboard D.
fourier DFT using Arm
Thumb assembler. Primarily for processing data received on an ADC.
Filters FIR filters
using ARM Thumb assembler. Using an online utility you can go from a graph
of required frequency response to a filter implementation.
rshell is a fork of Dave Hylands' excellent utility. My fork adds text macros to improve its power and usability, notably when maintaining complex Python packages. The fork includes documentation, but these notes provide a usage example where each project has its own set of automatically loaded macros.__
data to py Convert arbitrary objects to Python files which may be frozen as bytecode. Can be used to freeze images for display.
Any code placed here is released under the MIT License (MIT).
The MIT License (MIT)
Copyright (c) 2016 Peter Hinch
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.