diff --git a/README.md b/README.md index 0f6cef6..cbdffb2 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,39 @@ # micropython-samples -A place for assorted code ideas for MicroPython. +A place for assorted code ideas for MicroPython. Most are targeted at the Pyboard variants. -mutex: A class providing mutal exclusion enabling interrupt handlers and the main program to -access shared data in a manner which ensures data integrity. +## mutex +A class providing mutal exclusion enabling interrupt handlers and the main program to access shared +data in a manner which ensures data integrity. -watchdog: How to access the simpler of the Pyboard's watchdog timers. +## watchdog +Access the simpler of the Pyboard's watchdog timers. -reverse: Fast reverse a bytearray. +## reverse +Fast reverse a bytearray. -font: Convert a C file produced by GLCD Font Creator to Python for storage as persistent byte code. +## font +Convert a C file produced by GLCD Font Creator to Python for storage as persistent byte code. -ds3231_pb: Driver for the DS3231 low cost precison RTC, including a facility to calibrate the -Pyboard's RTC from the DS3231 +## ds3231_pb +Driver for the DS3231 low cost precison RTC, including a facility to calibrate the Pyboard's RTC +from the DS3231. -Buildcheck: Raise an exception if a firmware build is earlier than a given date. +## Buildcheck +Raise an exception if a firmware build is earlier than a given date. + +## timed_function +Time a function's execution using a decorator + +## fastbuild - Pyboard use under Linux +Build MicroPython with your frozen bytecode, put the Pyboard into DFU mode, and deploy the build to +the board with a single command. Takes just over 60 seconds on a fast PC. Note the use of make's j +argument to use mutliple cores. Empirically 8 gave the fastest build on my core i7 4/8 core laptop: +adjust to suit your PC. + +Includes udev rules to avoid jumps from /dev/ttyACM0 to /dev/ttyACM1: ensures Pyboards of all types +appear as /dev/pyboard. Also rules for USB connected WiPy and FTDI USB/serial adaptor. + +# License Any code placed here is released under the MIT License (MIT). The MIT License (MIT) diff --git a/fastbuild/47-ftdi.rules b/fastbuild/47-ftdi.rules new file mode 100644 index 0000000..b78fa38 --- /dev/null +++ b/fastbuild/47-ftdi.rules @@ -0,0 +1,5 @@ +# FTDI lead +ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ENV{MTP_NO_PROBE}="1" +SUBSYSTEMS=="usb", ATTRS{idVendor}=="0403", MODE:="0666" +KERNEL=="ttyUSB*", ATTRS{idVendor}=="0403", SYMLINK+="ftdi", MODE:="0666" + diff --git a/fastbuild/48-wipy.rules b/fastbuild/48-wipy.rules new file mode 100644 index 0000000..01311b1 --- /dev/null +++ b/fastbuild/48-wipy.rules @@ -0,0 +1,6 @@ +# 0403:6015 - WiPy board ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6015", ENV{ID_MM_DEVICE_IGNORE}="1" +# WiPy appears as /dev/wipy +ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6015", ENV{MTP_NO_PROBE}="1" +SUBSYSTEMS=="usb", ATTRS{idVendor}=="0403", MODE:="0666" +KERNEL=="ttyUSB*", ATTRS{idVendor}=="0403", SYMLINK+="wipy", MODE:="0666" + diff --git a/fastbuild/49-micropython.rules b/fastbuild/49-micropython.rules new file mode 100644 index 0000000..4f6ed6b --- /dev/null +++ b/fastbuild/49-micropython.rules @@ -0,0 +1,8 @@ +# f055:9800, 9801, 9802 MicroPython pyboard +# Instantiate Pyboard V1.0, V1.1 or Lite as /dev/pyoard +ATTRS{idVendor}=="f055", ENV{MTP_NO_PROBE}="1" +ATTRS{idVendor}=="f055", ENV{ID_MM_DEVICE_IGNORE}="1" +SUBSYSTEMS=="usb", ATTRS{idVendor}=="f055", MODE:="0666" +KERNEL=="ttyACM*", ATTRS{idVendor}=="f055", SYMLINK+="pyboard", MODE:="0666", GROUP="adminpete" +KERNEL=="ttyUSB*", ATTRS{idVendor}=="f055", SYMLINK+="pyboard", MODE:="0666", GROUP="adminpete" + diff --git a/fastbuild/pyb_boot b/fastbuild/pyb_boot new file mode 100644 index 0000000..4feeede --- /dev/null +++ b/fastbuild/pyb_boot @@ -0,0 +1,17 @@ +#!/usr/bin/env python +# Edit this to match the location of micropython/tools and the Pyboard device + +import sys +sys.path.append('/mnt/qnap2/data/Projects/MicroPython/micropython/tools') +import pyboard +def main(): + pyb=pyboard.Pyboard('/dev/pyboard') + pyb.enter_raw_repl() + try: + pyb.exec_raw('pyb.bootloader()') + except: + pass + +if __name__ == "__main__": + main() + diff --git a/fastbuild/pybv10_build b/fastbuild/pybv10_build new file mode 100644 index 0000000..acf8e6f --- /dev/null +++ b/fastbuild/pybv10_build @@ -0,0 +1,6 @@ +#! /bin/bash +# PYB V1.1 build script for 4/8 Core i7 and frozen bytecode +# Run from micropython/stmhal +# edit location of pyb_boot +make BOARD=PYBV10 clean && make -j 8 BOARD=PYBV10 FROZEN_MPY_DIR=scripts && /mnt/qnap2/Scripts/pyb_boot && make BOARD=PYBV10 deploy + diff --git a/fastbuild/pybv11_build b/fastbuild/pybv11_build new file mode 100644 index 0000000..7675bf6 --- /dev/null +++ b/fastbuild/pybv11_build @@ -0,0 +1,6 @@ +#! /bin/bash +# PYB V1.1 build script for 4/8 Core i7 and frozen bytecode +# Run from micropython/stmhal +# edit location of pyb_boot +make BOARD=PYBV11 clean && make -j 8 BOARD=PYBV11 FROZEN_MPY_DIR=scripts && /mnt/qnap2/Scripts/pyb_boot && make BOARD=PYBV11 deploy + diff --git a/timed_function/timed_func.py b/timed_function/timed_func.py new file mode 100644 index 0000000..5305580 --- /dev/null +++ b/timed_function/timed_func.py @@ -0,0 +1,18 @@ +# Time a function call by means of a decorator + +import utime + +def timed_function(f, *args, **kwargs): + myname = str(f).split(' ')[1] + def new_func(*args, **kwargs): + t = utime.ticks_us() + result = f(*args, **kwargs) + delta = utime.ticks_diff(t, utime.ticks_us()) + print('Function {} Time = {:6.3f}ms'.format(myname, delta/1000)) + return result + return new_func + +@timed_function +def test(): + utime.sleep_us(10000) +