FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/MicroPython

MicroPython

Contents
    Libraries
    Custom MicroPython libraries
Supporting hardware
    Executing code
    Pyboard
        The booting process
        Boot modes
        Errors
Programming examples
Bytecode
Implementation and uses
See also
References

{{Cite web|url=https://github.com/micropython/micropython/blob/master/LICENSE|title=micropython/LICENSE at master · micropython/micropython|last=George|first=Damien P.|date=4 May 2014|website=[[GitHub]]|access-date=11 February 2017}}</ref>"},"website":{"wt":"{{URL|https://micropython.org}}"}},"i":0}}]}'>
MicroPython
DeveloperDamien P. George
Release3 May 2014;12 years ago (2014-05-03)

MicroPython is a software implementation of a programming language largely compatible with Python 3, written in C, that is optimized to run on a microcontroller.

MicroPython consists of a Python compiler to bytecode and a runtime interpreter of that bytecode. The user is presented with an interactive prompt (the REPL) to execute supported commands immediately. Included are a selection of core Python libraries; MicroPython includes modules which give the programmer access to low-level hardware.

MicroPython does have an inline assembler, which lets the code run at full speed, but it is not portable across different microcontrollers.

The source code for the project is available on GitHub under the MIT License.

help(): help message
  • ↵ Enter-Subtractionvariable * 4 %Modulo divisionexpression1 == expression2 <Less thanexpression1 > expression2 >=Greater than or equals= expression2"}},"i":0}}]}'/>expression1 >= expression2 Logical|bitwise orvariable1 ^ variable2 andlogical andvariable1 or variable2

    Libraries

    edit

    MicroPython is a lean and efficient implementation of Python with libraries similar to those in Python. Some standard Python libraries have an equivalent library in MicroPython renamed to distinguish between the two. MicroPython libraries are smaller with less popular features removed or modified to save memory.

    The three types of libraries in MicroPython:

    • derived from a standard Python library (built-in libraries)
    • specific MicroPython libraries
    • specific libraries to assist with hardware functionality

    MicroPython is highly customisable and configurable, with language differing between each board (microcontroller) and the availability of libraries may differ. Some functions and classes in a module or the entire module may be unavailable or altered.

    Standard Python libraries in MicroPython
    Library nameDescription
    arrayoperations on arrays
    cmathprovides math functions for complex numbers
    gcgarbage collector
    mathprovides basic math operations for floating-point numbers
    syssystem-level functions; provides access to variables used by the interpreter
    binasciifunctions for converting between binary and ASCII
    collectionsoperations for collections and container types that hold various objects
    errnoprovides access to error codes
    hashliboperations for binary hash algorithms
    heapqoperations to implement heap queue algorithm
    iooperations for handling input/output streams
    jsonhandles conversion between JSON documents and Python objects
    osfunctions for filesystem access and basic operating system functions
    reimplements regular expression matching operations
    selectfunctions for handling events on multiple streams
    socketconnecting to sockets (networks), providing access to socket interface
    structperforms conversions to Python objects by packing and unpacking primitive data types
    timeprovides time and date function, including measuring time intervals and implementing delays
    zliboperations to decompress binary data
    MicroPython-specific libraries
    Library nameDescription
    framebufprovides a frame buffer that can be used to create bitmap images to be sent to a display
    machinefunctions assisting with accessing and interacting with hardware blocks
    micropythonaccess and control of MicroPython internals
    networkassists with installing network driver, allowing interactions through networks
    ctypesaccess binary data structures

    Custom MicroPython libraries

    edit

    When developers begin to create a new application, standard MicroPython libraries and drivers may not meet the requirements, with insufficient operations or calculations. Similar to Python, there is the possibility of extending MicroPython's functionality with custom libraries which extend the ability of the existing libraries and firmware.

    In MicroPython, files ending with .py take preference over other library aliases which allows users to extend the use and implementation of the existing libraries.

    Supporting hardware

    edit

    As MicroPython's implementation and popularity continues to grow, more boards have the ability to run MicroPython. Many developers are building processor specific versions that can be downloaded onto different microcontrollers. Installing MicroPython on microcontrollers is well documented and user-friendly. MicroPython allows interactions between microcontroller hardware and applications to be simple, allowing access to a range of functionality while working in a resource constrained environment, with a strong level of responsiveness.

    The two types of boards used to run MicroPython:

    • MicroPython loaded when manufactured, meaning only MicroPython can be run.
    • boards that have firmware that allows MicroPython to be installed to the board.

    Executing code

    edit

    To move a program onto a MicroPython board, create a file and copy it onto the microcontroller in order to execute. With the hardware connected to a device, such as a computer, the board's flash drive will appear on the device allowing files to be moved to the flash drive. There will be two existing python files, boot.py and main.py that are typically not modified, main.py may be modified if you wish to run the program every time the microcontroller is booted, otherwise, programs will be run using the REPL console.

    Pyboard

    edit

    The pyboard is the official MicroPython microcontroller board which fully supports MicroPython's software features. The pyboard's hardware features include:

    The booting process

    edit

    The pyboard contains an internal drive with filesystem named /flash which is stored within the board's flash memory, additionally, a microSD card can be inserted into a slot and is accessible through /sd. When booted up, a pyboard must select a filesystem to boot from either /flash or /sd with the current directory being set to either /flash or /sd. By default, if an SD card is inserted, /sd will be used, if not, /flash is used. If needed, the use of the SD card for the booting process can be avoided by creating an empty file called /flash/SKIPSD which is stored on the board flash memory to skip the SD card for the booting process.

    Boot modes

    edit

    When the pyboard is powered up normally or the reset button is pressed then the pyboard is booted in a standard mode, meaning that the boot.py file is executed, then the USB configured and finally the python program will run.

    There is an ability to override the standard boot sequence through holding down the user switch whilst the board is in the booting process and then pressing reset as you continue to hold the user switch. The pyboard's LEDs will flick between modes and once the LEDs have reached the mode wanted by the user, they can let go of the user switch and the board will boot in the specific mode.

    the boot modes are:

    • standard boot: green LED only (runs boot.py then python program)
    • safe boot: orange LED only (does not run any scripts during boot-up)
    • filesystem reset: green and orange LED together (resets flash drive to factory state and boots in safe mode) – used as a fix when filesystem is corrupted

    Errors

    edit
    • if red and green LEDs flash alternatively then the python script has an error, and you must use the REPL to debug.
    • if all 4 LEDs cycle on and off then there is a hard fault which cannot be recovered from and requires a hard reset.

    Programming examples

    edit

    Source:

    Hello world program:

    importos # Open and read a file with open("/readme.txt") as f: print(f.read())

    Bytecode

    edit

    MicroPython includes a cross compiler which generates MicroPython bytecode (file extension .mpy). The Python code can be compiled into the bytecode either directly on a microcontroller or it can be precompiled elsewhere.

    MicroPython firmware can be built without the compiler, leaving only the virtual machine which can run the precompiled mpy programs.

    Implementation and uses

    edit

    MicroPython is utilised through firmware being loaded by standard software onto a particular microcontroller into flash memory, communicating using a terminal application loaded onto a computer that emulates a serial interface.

    The main uses of MicroPython can be generalised into three categories:

    • educational purposes: using MicroPython's read–eval–print Loop (REPL) to interact with a microcontroller, it is possible to visually explain the concepts of data processing and communicating with boards in a simpler way than more complicated programming languages.
    • developing and testing device and sensor designs: MicroPython offers verified, bug-free, and thoroughly tested reference implementations of interfaces used in microcontrollers solving a common developer's task of implementing peripheral communication setup and control. MicroPython offers direct and interactive accessibility to device registers which makes it easy to verify functionality and develop and test hardware parts and devices and algorithms for control and acquiring data from a device.
    • monitoring and configuring tool for design of complex applications: certain applications require specific applications on high performing microcontrollers. MicroPython is able to assist with state monitoring and set-up of system parameters.

    Implementation of MicroPython can differ depending on the availability of standard and supporting libraries and the microcontroller's flash memory and RAM size.

    See also

    edit

    References

    edit
    Yegulalp, Serdar (5 July 2014). "Micro Python's tiny circuits: Python variant targets microcontrollers". InfoWorld. Retrieved 15 December 2016.
  • 1 2 3 4 5 6 7 8 9 "Micro Python: Python for microcontrollers". Kickstarter. 26 February 2016. Retrieved 15 December 2016.
  • ↑ "MicroPython with esp8266 and esp32: flashing firmware and programming with basic tools". Mischianti. 7 June 2023.
  • ↑ Williams, Alun (7 July 2015). "Hands on with the BBC Micro-Bit user interface". ElectronicsWeekly.com. Retrieved 8 July 2015.
  • ↑ "LEGO releases MicroPython for EV3 based on ev3dev and Pybricks". www.ev3dev.org. Retrieved 21 April 2020.
  • ↑ Bruno, P. (25 November 2021). "An Introduction to MicroPython". All3DP. Retrieved 9 May 2022.
  • 1 2 3 4 5 6 7 8 9 10 11 Rembor, K. "The REPL". Welcome to CircuitPython!. Adafruit Learning System. Retrieved 9 May 2022.
  • ↑ Wang, L.; Li, Y.; Zhang, H.; Han, Q.; Chen, L. (2021). An Efficient Control-flow based Obfuscator for Micropython Bytecode. 2021 7th International Symposium on System and Software Reliability (ISSSR). pp. 54–63.
  • ↑

  • View original on Wikipedia | Back | FazBrowse Home