Release 1.9.2 Damien P. George, Paul Sokolovsky, and Contributors
Total Page:16
File Type:pdf, Size:1020Kb
MicroPython Documentation Release 1.9.2 Damien P. George, Paul Sokolovsky, and contributors August 23, 2017 CONTENTS 1 Quick reference for the WiPy1 1.1 General board control (including sleep modes).............................1 1.2 Pins and GPIO..............................................1 1.3 Timers..................................................1 1.4 PWM (pulse width modulation).....................................2 1.5 ADC (analog to digital conversion)...................................2 1.6 UART (serial bus)............................................2 1.7 SPI bus..................................................2 1.8 I2C bus..................................................3 1.9 Watchdog timer (WDT).........................................3 1.10 Real time clock (RTC)..........................................3 1.11 SD card..................................................4 1.12 WLAN (WiFi)..............................................4 1.13 Telnet and FTP server..........................................4 1.14 Heart beat LED..............................................4 2 General information about the WiPy7 2.1 No floating point support.........................................7 2.2 Before applying power..........................................7 2.3 WLAN default behaviour........................................7 2.4 Telnet REPL...............................................7 2.5 Local file system and FTP access....................................7 2.6 FileZilla settings.............................................8 2.7 Upgrading the firmware Over The Air..................................8 2.8 Boot modes and safe boot........................................9 2.9 The heartbeat LED............................................9 2.10 Details on sleep modes..........................................9 2.11 Additional details for machine.Pin.................................... 10 2.12 Additional details for machine.I2C................................... 11 2.13 Known issues............................................... 11 2.13.1 Incompatible way to create SSL sockets............................ 11 2.13.2 Incompatibilities in uhashlib module.............................. 11 2.13.3 Unrelated function in machine module............................. 12 2.13.4 Adhoc way to control telnet/FTP server via network module................. 12 2.13.5 Adhoc VFS-like support.................................... 12 3 WiPy tutorials and examples 15 3.1 Introduction to the WiPy......................................... 15 3.1.1 Caring for your WiPy and expansion board.......................... 15 3.1.2 Plugging into the expansion board and powering on...................... 15 3.1.3 Expansion board hardware guide................................ 15 i 3.1.4 Powering by an external power source............................. 15 3.1.5 Performing firmware upgrades................................. 16 3.2 Getting a MicroPython REPL prompt.................................. 16 3.2.1 Windows............................................ 16 3.2.2 Mac OS X............................................ 16 3.2.3 Linux.............................................. 17 3.2.4 Using the REPL prompt.................................... 17 3.2.5 Resetting the board....................................... 18 3.3 Getting started with Blynk and the WiPy................................ 18 3.4 WLAN step by step........................................... 18 3.4.1 Connecting to your home router................................ 19 3.4.2 Assigning a static IP address when booting.......................... 19 3.5 Hardware timers............................................. 19 3.5.1 Additional constants for Timer class.............................. 20 3.6 Reset and boot modes.......................................... 21 3.6.1 Safe boot............................................ 21 3.6.2 Factory reset the filesystem................................... 21 4 MicroPython libraries 23 4.1 Python standard libraries and micro-libraries.............................. 23 4.1.1 Builtin functions and exceptions................................ 24 4.1.2 array – arrays of numeric data................................ 26 4.1.3 gc – control the garbage collector............................... 27 4.1.4 sys – system specific functions................................ 28 4.1.5 ubinascii – binary/ASCII conversions........................... 29 4.1.6 ujson – JSON encoding and decoding............................ 30 4.1.7 uos – basic “operating system” services............................ 30 4.1.8 ure – simple regular expressions............................... 32 4.1.9 uselect – wait for events on a set of streams........................ 33 4.1.10 usocket – socket module................................... 34 4.1.11 ussl – SSL/TLS module................................... 38 4.1.12 utime – time related functions................................ 39 4.2 MicroPython-specific libraries...................................... 42 4.2.1 btree – simple BTree database................................ 42 4.2.2 framebuf — Frame buffer manipulation........................... 44 4.2.3 machine — functions related to the hardware........................ 46 4.2.4 micropython – access and control MicroPython internals................. 62 4.2.5 network — network configuration.............................. 63 4.2.6 uctypes – access binary data in a structured way...................... 67 4.3 Libraries specific to the WiPy...................................... 70 4.3.1 wipy – WiPy specific features................................. 70 5 The MicroPython language 71 5.1 Glossary................................................. 71 5.2 The MicroPython Interactive Interpreter Mode (aka REPL)....................... 72 5.2.1 Auto-indent........................................... 72 5.2.2 Auto-completion........................................ 73 5.2.3 Interrupting a running program................................. 73 5.2.4 Paste Mode........................................... 74 5.2.5 Soft Reset............................................ 74 5.2.6 The special variable _ (underscore)............................... 75 5.2.7 Raw Mode........................................... 75 5.3 Writing interrupt handlers........................................ 75 5.3.1 Tips and recommended practices................................ 76 ii 5.3.2 MicroPython Issues....................................... 76 5.3.3 Exceptions........................................... 78 5.3.4 General Issues.......................................... 78 5.4 Maximising MicroPython Speed..................................... 81 5.4.1 Designing for speed....................................... 81 5.4.2 Identifying the slowest section of code............................. 83 5.4.3 MicroPython code improvements................................ 83 5.4.4 The Native code emitter.................................... 84 5.4.5 The Viper code emitter..................................... 84 5.4.6 Accessing hardware directly.................................. 86 5.5 MicroPython on Microcontrollers.................................... 86 5.5.1 Flash Memory.......................................... 86 5.5.2 RAM.............................................. 87 5.5.3 The Heap............................................ 90 5.5.4 String Operations........................................ 92 5.5.5 Postscript............................................ 92 6 MicroPython differences from CPython 93 6.1 Syntax.................................................. 93 6.1.1 Spaces.............................................. 93 6.1.2 Unicode............................................. 93 6.2 Core Language.............................................. 94 6.2.1 Classes............................................. 94 6.2.2 Functions............................................ 96 6.2.3 Generator............................................ 97 6.2.4 import.............................................. 97 6.3 Builtin Types............................................... 99 6.3.1 Exception............................................ 99 6.3.2 bytearray............................................ 100 6.3.3 bytes............................................... 101 6.3.4 float............................................... 101 6.3.5 int................................................ 102 6.3.6 list................................................ 102 6.3.7 str................................................ 103 6.3.8 tuple............................................... 105 6.4 Modules................................................. 106 6.4.1 array............................................... 106 6.4.2 deque.............................................. 107 6.4.3 json............................................... 107 6.4.4 struct.............................................. 107 6.4.5 sys................................................ 108 7 MicroPython license information 109 Python Module Index 111 Index 113 iii iv CHAPTER ONE QUICK REFERENCE FOR THE WIPY 1.1 General board control (including sleep modes) See the machine module: import machine help(machine) # display all members from the machine module machine.freq() # get the CPU frequency machine.unique_id() # return the 6-byte unique id of the board (the WiPy's MAC address) machine.idle() # average current decreases to (~12mA), any interrupts wake it up machine.sleep() # everything except for WLAN is powered down (~950uA avg. current) # wakes from Pin, RTC or WLAN machine.deepsleep()