MicroPython : Embedded Programming Made Easy

MicroPython is a lightweight implementation of the Python 3 programming language that has been optimized for microcontro

144 63 937KB

English Pages 225 Year 2023

Report DMCA / Copyright

DOWNLOAD EPUB FILE

Table of contents :
Cover Page
Title Page
Copyright Page
Introduction
Use MicroPython
Access the MicroPython environment
Enter MicroPython code
Direct entry
Exit MicroPython
Display tools
Recover an XBee device
MicroPython syntax
Colons
After conditional statements and loop statements
Indentations
FOR loop with one statement indented
FOR loop with two statements indented
Functions
Function with arguments
Errors and exceptions
Name error
Referencing a name that was not created
Referencing a name from one function that was created in a different function
OSError
Socket errors
ENOTCONN: Time out error
ENFILE: No sockets are available
ENXIO: No such device or address
Keyboard shortcuts
Select a previously typed statement
Differences between MicroPython and other programming languages
Memory management
Variable types
Curly braces and indentation
Semicolons
Increment operator
Logical operators
Develop applications on an XBee device
Space allocated to MicroPython
Code storage
Built-in modules embedded in XBee firmware (device flash)
Parsed and compiled code in .mpy files (file system)
Executable code on MicroPython heap (device RAM)
Compiled modules relocated from file system to device flash
How to organize your code
Monitor memory usage
The gc module
The micropython module
Efficient coding
One-liners in the REPL
Flash upload mode
Modules stored as .py files
Compiled modules stored as .mpy files
Compiled modules via Flash upload mode
Compiled modules embedded in device flash
Digi modified slicing for bytes and strings operations
Power management
Prevent sleep from MicroPython
Initiate sleep from MicroPython
Sleeping with AT commands
Idle a device from MicroPython
Enter and exit the idle state
Poll for data while the device is idle
Access the primary UART
How to use the primary UART
sys.stdin limitations
Example: read bytes from the UART in MicroPython
Example: read the first 15 bytes from the UART in MicroPython
REPL (Read-Evaluate-Print Loop) examples
Print the banner
Print the banner and verify that the memory was not wiped
Paste one line of code
Load code to flash memory
Erase the code stored in flash memory
Flash memory and automatic code execution
Run stored code at start-up to flash LEDs
Disable code from running at start up
Perform a soft-reset or reboot
Access file system
Modify file system contents
uos.chdir(dir)
uos.getcwd()
uos.ilistdir([dir])
uos.listdir([dir])
uos.mkdir(dir)
uos.remove(file)
uos.rmdir(dir)
uos.rename(old_path, new_path)
uos.replace(old_path, new_path)
uos.sync()
uos.compile(source_file, mpy_file=None)
uos.format()
uos.hash([secure_file])
Access data in files
File object methods
read(size=-1)
readinto(b)
readline(size=-1)
readlines()
write(b)
seek(offset, whence=0)
tell()
flush()
close()
Import modules from file system
Reload a module
Compiled MicroPython files
Send and receive User Data Relay frames
Interfaces (always defined)
Limits
relay.callback(my_callback)
Machine module
Reset-cause
Constants
Random numbers
Unique identifier
Class PWM (pulse width modulation)
Constructors
Standard bus operations methods
Memory operations methods
Class Pin
Class UART
Test the UART interface
Use the UART class
Class WDT: watchdog timer
Access the XBee device's I/O pins
Use the Pin() constructor
Use mode() to configure a pin
Pin.DISABLED
Pin.IN
Pin.OUT
Pin.ALT
Pin.ANALOG

MicroPython : Embedded Programming Made Easy

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up