1
Abstract: Internet players in order to make Python as easy to learn, easy to learn, and rich in community API language can be used in the embedded field, gradually began a round of embedded migration on Python, so that there is today's protagonist— -MircoPython.

This article is shared from the Huawei Cloud Community " Python's IoT-derived microPython [Grow! Python】 ", original author: o0龙龙0o.

introduction

With the development of the Internet of Things and the sinking of the Internet to embedded applications, many programming languages used in the Internet are slowly developing in the fields of embedded and single-chip computers.

Because of the rapid development of the Internet of Things, side-end embedded applications have also begun to be involved by Internet of Things players. Originally, the embedded C language was mainly used in the development of embedded, because C language is slower to learn than the scripting language used on the Internet. , Community APIs are few and other issues are not conducive to the intervention of Internet players and models. Internet players want to make Python easy to learn, easy to learn, and a rich community API language that can be used in the embedded field. Some people say that 1000 lines of C programs, A 100-line Java program and Python only need 10 lines, reflecting the ease of Python, and gradually began a round of embedded migration on Python, so that there is today's protagonist-MircoPython.

MicroPython, like Python, is an open source interpretive language that follows the GPL (GNU General Public License) protocol. It can run on any hardware board that has been ported to control hardware and perform arithmetic operations, such as common hardware IO-memory and sensors. , Display, light switch, moving parts, network and communication can all be operated by MicroPython language.

MicroPython allows people with Python language experience but no C language foundation, embedded foundation and hardware foundation to get in touch with the embedded system more quickly and control the MCU in their hands.

MicroPython

MicroPython is a streamlined and efficient implementation of the Python 3 programming language, which includes a small part of the Python standard library and is optimized for operation in microcontrollers and restricted environments.

MicroPython pyboard is a compact electronic circuit board that can run MicroPython on bare metal, providing you with a low-level Python operating system that can be used to control various electronic projects.

MicroPython is full of advanced features, such as interactive prompts, arbitrary precision integers, closures, list comprehensions, generators, exception handling, etc. However, it is compact enough to run in only 256k of code space and 16k of RAM. MicroPython aims to be as compatible as possible with ordinary Python, allowing you to easily transfer code from the desktop to a microcontroller or embedded system.

MicroPython is a complete Python compiler and runtime that runs on bare metal. You will get an interactive prompt (REPL) to execute commands immediately, as well as the ability to run and import scripts from the built-in file system.

MicroPython tries to be as compatible as possible with Python (called CPython), and modules such as "machines" used to access low-level hardware have also implemented some core Python libraries.

Take a look at how MicroPython operates the hardware

import pyb
 
# 点亮LED1
pyb.LED(1).on()
 
# 打印信息到串口窗口
print('Hello MicroPython!')

With MicroPython, I can simply turn on and off an LED like this, or print log to the serial port window.

More complicated we can operate a memory, as follows:

from machine import Pin, I2C
 
# 创建一个I2C总线
i2c = I2C(scl=Pin('X1'), sda=Pin('X2'))
 
#查找总线上的设备
dev_list = i2c.scan()
 
# 向设备地址0x42写入‘4’同时从0x42读出4个字节
i2c.writeto(0x42, b'4')
data = i2c.readfrom(0x42, 4)
 
# 存储器和内存互相访问
i2c.writeto_mem(0x42, 0x12, b'')
data = i2c.readfrom_mem(0x42, 0x12, 2)

Through the above commands, we can simply operate an I2C memory, which is much more convenient than the C language. With a mircoPython, we can even directly use embedded mathematical operations to realize the transfer of calculations. Of course, large-scale operations still need embedded It has a certain amount of computing power. This is somewhat different from the current device definition. It is fun. For example, we can directly run the following calculations on the mircoPython IDE. At that time, the calculations were not done by the computer but by the connected embedded MCU.

# mircoPython 数据表示及计算
# 小整数
>>> 123
123
# 大整数
>>> 1 << 160
1461501637330902918203684832716283019655932542976
# 浮点数
>>> 1.23e6
1230000.0
# 复数计算
>>> (1 + 2j) * 4j
(-8+4j)

microPython application

Currently MicroPython supports 32-bit-based ARM processors, such as pyboard (STM32F405), NRF51822 (micro:bit), FireBeetle-ESP32, WiPy, ESP8266 core master, CC3200, etc.

More and more manufacturers are beginning to adapt to MicroPython. After all, there are potential user groups. Maybe LiteOS will inherit the support of MicroPython in the future. It seems that there is already Javascript support, and there is a Python corresponding language package support. Internet people It is easier to get involved in the development of the perception layer of the Internet of Things, which is a great thing for the development of the industry. I hope that there will be more support in the future, such as Hongmeng, and also support more language packs.

Click to follow and learn about Huawei Cloud's fresh technology for the first time~


华为云开发者联盟
1.4k 声望1.8k 粉丝

生于云,长于云,让开发者成为决定性力量