Micropython Documentation Release 1.10 Damien P. George, Paul
Total Page:16
File Type:pdf, Size:1020Kb
MicroPython Documentation Release 1.10 Damien P. George, Paul Sokolovsky, and contributors Jan 25, 2019 CONTENTS 1 MicroPython libraries 1 1.1 Python standard libraries and micro-libraries..............................2 1.1.1 Builtin functions and exceptions................................2 1.1.2 array – arrays of numeric data................................5 1.1.3 cmath – mathematical functions for complex numbers....................5 1.1.4 gc – control the garbage collector...............................6 1.1.5 math – mathematical functions................................7 1.1.6 sys – system specific functions................................9 1.1.7 ubinascii – binary/ASCII conversions........................... 11 1.1.8 ucollections – collection and container types...................... 11 1.1.9 uerrno – system error codes................................. 12 1.1.10 uhashlib – hashing algorithms............................... 13 1.1.11 uheapq – heap queue algorithm................................ 14 1.1.12 uio – input/output streams................................... 14 1.1.13 ujson – JSON encoding and decoding............................ 16 1.1.14 uos – basic “operating system” services............................ 16 1.1.15 ure – simple regular expressions............................... 20 1.1.16 uselect – wait for events on a set of streams........................ 22 1.1.17 usocket – socket module................................... 23 1.1.18 ussl – SSL/TLS module................................... 28 1.1.19 ustruct – pack and unpack primitive data types....................... 29 1.1.20 utime – time related functions................................ 30 1.1.21 uzlib – zlib decompression.................................. 33 1.1.22 _thread – multithreading support.............................. 33 1.2 MicroPython-specific libraries...................................... 33 1.2.1 btree – simple BTree database................................ 33 1.2.2 framebuf — Frame buffer manipulation........................... 36 1.2.3 machine — functions related to the hardware........................ 38 1.2.4 micropython – access and control MicroPython internals................. 55 1.2.5 network — network configuration.............................. 57 1.2.6 ucryptolib – cryptographic ciphers............................ 66 1.2.7 uctypes – access binary data in a structured way...................... 67 1.3 Libraries specific to the pyboard..................................... 71 1.3.1 pyb — functions related to the board............................. 71 1.3.2 lcd160cr — control of LCD160CR display......................... 109 1.4 Libraries specific to the WiPy...................................... 114 1.4.1 wipy – WiPy specific features................................. 114 1.4.2 class TimerWiPy – control hardware timers.......................... 114 1.4.3 class TimerChannel — setup a channel for a timer....................... 116 1.5 Libraries specific to the ESP8266 and ESP32.............................. 117 i 1.5.1 esp — functions related to the ESP8266 and ESP32..................... 117 1.5.2 esp32 — functionality specific to the ESP32......................... 118 2 The MicroPython language 121 2.1 Glossary................................................. 121 2.2 The MicroPython Interactive Interpreter Mode (aka REPL)....................... 123 2.2.1 Auto-indent........................................... 123 2.2.2 Auto-completion........................................ 123 2.2.3 Interrupting a running program................................. 124 2.2.4 Paste Mode........................................... 124 2.2.5 Soft Reset............................................ 125 2.2.6 The special variable _ (underscore)............................... 126 2.2.7 Raw Mode........................................... 126 2.3 Writing interrupt handlers........................................ 126 2.3.1 Tips and recommended practices................................ 126 2.3.2 MicroPython Issues....................................... 127 2.3.3 Exceptions........................................... 129 2.3.4 General Issues.......................................... 130 2.4 Maximising MicroPython Speed..................................... 132 2.4.1 Designing for speed....................................... 133 2.4.2 Identifying the slowest section of code............................. 134 2.4.3 MicroPython code improvements................................ 135 2.4.4 The Native code emitter.................................... 135 2.4.5 The Viper code emitter..................................... 136 2.4.6 Accessing hardware directly.................................. 137 2.5 MicroPython on Microcontrollers.................................... 138 2.5.1 Flash Memory.......................................... 138 2.5.2 RAM.............................................. 138 2.5.3 The Heap............................................ 142 2.5.4 String Operations........................................ 143 2.5.5 Postscript............................................ 144 2.6 Distribution packages, package management, and deploying applications............... 144 2.6.1 Overview............................................ 144 2.6.2 Distribution packages...................................... 144 2.6.3 upip package manager..................................... 145 2.6.4 Cross-installing packages.................................... 146 2.6.5 Cross-installing packages with freezing............................ 146 2.6.6 Creating distribution packages................................. 147 2.6.7 Application resources...................................... 147 2.6.8 References........................................... 148 2.7 Inline Assembler for Thumb2 architectures............................... 148 2.7.1 Document conventions..................................... 149 2.7.2 Instruction Categories...................................... 149 2.7.3 Usage examples......................................... 158 2.7.4 References........................................... 161 3 MicroPython differences from CPython 163 3.1 Syntax.................................................. 163 3.1.1 Spaces.............................................. 163 3.1.2 Unicode............................................. 163 3.2 Core Language.............................................. 164 3.2.1 Classes............................................. 164 3.2.2 Functions............................................ 166 3.2.3 Generator............................................ 166 ii 3.2.4 Runtime............................................. 167 3.2.5 import.............................................. 168 3.3 Builtin Types............................................... 169 3.3.1 Exception............................................ 169 3.3.2 bytearray............................................ 171 3.3.3 bytes............................................... 171 3.3.4 float............................................... 172 3.3.5 int................................................ 173 3.3.6 list................................................ 173 3.3.7 str................................................ 174 3.3.8 tuple............................................... 176 3.4 Modules................................................. 176 3.4.1 array............................................... 176 3.4.2 builtins............................................. 177 3.4.3 deque.............................................. 178 3.4.4 json............................................... 178 3.4.5 struct.............................................. 178 3.4.6 sys................................................ 179 4 MicroPython license information 181 5 Quick reference for the pyboard 183 5.1 General information about the pyboard................................. 183 5.1.1 Local filesystem and SD card.................................. 183 5.1.2 Boot modes........................................... 184 5.1.3 Errors: flashing LEDs...................................... 184 5.1.4 Guide for using the pyboard with Windows.......................... 184 5.1.5 The pyboard hardware..................................... 184 5.1.6 Datasheets for the components on the pyboard......................... 185 5.1.7 Datasheets for other components................................ 185 5.2 MicroPython tutorial for the pyboard.................................. 185 5.2.1 Introduction to the pyboard................................... 185 5.2.2 Running your first script.................................... 186 5.2.3 Getting a MicroPython REPL prompt............................. 189 5.2.4 Turning on LEDs and basic Python concepts.......................... 190 5.2.5 The Switch, callbacks and interrupts.............................. 192 5.2.6 The accelerometer....................................... 194 5.2.7 Safe mode and factory reset.................................. 195 5.2.8 Making the pyboard act as a USB mouse............................ 196 5.2.9 The Timers........................................... 198 5.2.10 Inline assembler......................................... 200 5.2.11 Power control.......................................... 202 5.2.12 Tutorials requiring extra components.............................. 202 5.2.13 Tips, tricks and useful things to know............................. 216 5.3 General board control.......................................... 217 5.4 Delay and timing............................................. 217 5.5 Internal LEDs.............................................