The first part of this repo contains assorted code ideas for MicroPython
 
 
Go to file
Peter Hinch 605ba410d9
Update PICOWEB.md
2020-04-19 09:33:32 +01:00
DS3231 DS3231 Support Pyboard D. Various updates and fixes. 2020-01-27 09:58:35 +00:00
ESP8266 Samples adapted for changing ticks_diff semantics 2016-11-01 10:16:59 +00:00
PicoWeb PicoWeb guide now has no changes to Paul Sokolovsky's picoweb code. 2019-05-16 11:57:49 +01:00
bitmap Add bitmap directory. 2019-02-20 16:51:19 +00:00
buildcheck Build check added 2015-12-26 06:57:32 +00:00
data_to_py SSD1306/writer.py uses blit method 2017-03-21 11:40:12 +00:00
encoders encoder_portable.py added. 2017-07-07 07:00:56 +01:00
fastbuild Update build scripts to use manifests. 2019-11-01 15:42:50 +00:00
functor_singleton Update build scripts. Add functor/singleton doc. 2019-10-18 10:39:20 +01:00
micropip micropip: remove upip_m.py. Update README.md to reflect changes in upip.py 2019-06-19 17:03:48 +01:00
mutex Errors in mutex README fixed 2017-02-07 07:48:32 +00:00
phase phase/README.md: fix code sample and clarify doc. 2019-09-29 16:06:20 +01:00
power Fix document backlinks. 2018-02-21 06:10:37 +00:00
pyboard_d Deprecate unofficial Pyboard D guide now docs exist. 2019-04-02 18:08:11 +01:00
random Fix to pseudorandom number generator 2016-12-19 08:12:12 +00:00
resilient Add picoweb installation guide. 2019-05-14 16:48:38 +01:00
reverse Python Bit Reversal code added. 2017-03-17 16:25:40 +00:00
soft_wdt Add software watchdog timer. 2019-01-27 10:24:21 +00:00
timed_function Add timeout sample. Remove make axtls from buildnew script. 2018-11-25 10:56:15 +00:00
uasyncio_iostream Queue now uses a list because of dodgy deque. 2020-04-08 18:06:32 +01:00
watchdog Radio and watchdog added 2015-12-26 07:00:08 +00:00
LICENSE Initial commit 2015-11-29 17:37:41 +00:00
PICOWEB.md Update PICOWEB.md 2020-04-19 09:33:32 +01:00
README.md Add Serialisation doc. 2020-02-10 17:52:22 +00:00
SERIALISATION.md Add Serialisation doc. 2020-02-10 18:17:37 +00:00

README.md

micropython-samples

A place for assorted code ideas for MicroPython. Most are targeted at the Pyboard variants.

Installing MicroPython libraries

This is more involved since the advent of the pycopy fork of MicroPython. This doc describes the issues and provides a utility to simplify installation for users of official MicroPython firmware.

Fastbuild

Scripts for building MicroPython for various target hardware types and for updating your local source. Now detects and builds for Pyboard D. See docs

PicoWeb

Easy installation guide. Simplify installing this on MicroPython hardware platforms under official MicroPython firmware.

Serialisation

A discussion of the need for serialisation and of the relative characteristics of four libraries available to MicroPython. Includes a tutorial on a Protocol Buffer library.

SSD1306

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.

mutex

A class providing mutual exclusion enabling interrupt handlers and the main program to access shared data in a manner which ensures data integrity.

watchdog

Access the simpler of the Pyboard's watchdog timers.

software watchdog (soft_wdt)

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 swdt_tests.py.

reverse

Fast reverse a bytearray in Arm Thumb assembler.
Python code to bit-reverse (fast-ish) 8, 16 and 32 bit words.

DS3231

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.

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.

Buildcheck

Raise an exception if a firmware build is earlier than a given date.

timed_function

Time a function's execution using a decorator. Also a way to implement timeouts using a closure.

ESP8266 (MQTT benchmark)

benchmark.py Tests the performance of MQTT by periodically publishing while subscribed to the same topic. Measures the round-trip delay. 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.

conn.py Connect in station mode using saved connection details where possible.

resilient

A guide to writing reliable ESP8266 networking code. Probably applies to other WiFi connected MicroPython devices, but reliable ones are thin on the ground.

Rotary Incremental Encoder

Classes for handling incremental rotary position encoders. Note that the Pyboard timers can do this in hardware. These samples cater for cases where that solution can't be used. The encoder_timed.py sample provides rate information by timing successive edges. In practice this is likely to need filtering to reduce jitter caused by imperfections in the encoder geometry.

There are other algorithms but this is the simplest and fastest I've encountered.

These were written for encoders producing TTL outputs. For switches, adapt the pull definition to provide a pull up or pull down as required.

The encoder.portable.py version should work on all MicroPython platforms. Tested on ESP8266. Note that interrupt latency on the ESP8266 limits performance. ESP32 has similar limitations.

A pseudo random number generator

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 two use cases for the pseudo random number generator. Firstly on platforms lacking a hardware generator (e.g. the Pyboard Lite). And secondly where repeatable results are required, for example in testing. A pseudo random number generator is seeded with an arbitrary 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 the code for usage and timing documentation.

Measurement of relative timing and phase of fast analog signals

This describes ways of using the Pyboard to perform precision measurements of analog signals of up to around 50KHz. It is documented here.

bitmap: bool arrays and sets of integers

Classes for non-allocating handling of sets of small integers and small arrays of booleans. Legal member values for a set and array index values are constrained to 0 <= value <= max_value where max_value is a constructor arg. The set and the array are different ways of viewing a bitmap implemented as a bytearray: e.g. if max_value is 255 the bytearray occupies 32 bytes allocated by the constructor.

Functors and singletons

Two simple class decorators for objects useful in hardware interfacing. Documented here.

A design for a hardware power meter

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.

License

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.