What is MircoPython?

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.7k 粉丝
0 条评论
推荐阅读
【贺】来自开发者的点赞,华为云开发者联盟入选 2022 中国技术品牌影响力企业榜
2023 年 1 月 4 日,中国技术先锋年度评选 | 2022 中国技术品牌影响力企业榜单正式发布。作为中国领先的新一代开发者社区,SegmentFault 思否依托数百万开发者用户数据分析,各科技企业在国内技术领域的行为及影...

华为云开发者联盟阅读 635

又一款眼前一亮的Linux终端工具!
今天给大家介绍一款最近发现的功能十分强大,颜值非常高的一款终端工具。这个神器我是在其他公众号文章上看到的,但他们都没把它的强大之处介绍明白,所以我自己体验一波后,再向大家分享自己的体验。

良许6阅读 1.9k

FastAPI性能碾压Flask?
不止一次的听过,FastAPI性能碾压Flask,直追Golang,不过一直没有测试过,今天闲着没事测试一下看看结果。不知道是哪里出了问题,结果大跌眼镜。

二毛erma02阅读 10.3k评论 3

封面图
Linux终端居然也可以做文件浏览器?
大家好,我是良许。在抖音上做直播已经整整 5 个月了,我很自豪我一路坚持到了现在【笑脸】最近我在做直播的时候,也开始学习鱼皮大佬,直播写代码。当然我不懂 Java 后端,因此就写写自己擅长的 Shell 脚本。但...

良许1阅读 2.1k

Python之如何优雅的重试
为了避免偶尔的网络连接失败,需要加上重试机制,那么最简单的形式就是在对应的代码片段加一个循环,循环体里使用异常捕获,连接成功时退出循环,否则就重复执行相关逻辑,此时修改之后的函数f如下

Harpsichord12073阅读 7.4k

万字长文详解如何使用Swift提高代码质量 | 京东云技术团队
京喜APP最早在2019年引入了Swift,使用Swift完成了第一个订单模块的开发。之后一年多我们持续在团队/公司内部推广和普及Swift,目前Swift已经支撑了70%+以上的业务。通过使用Swift提高了团队内同学的开发效率,同...

京东云开发者1阅读 1.1k

封面图
基于 EKS Fargate 搭建微服务性能分析系统
近期 Amazon Fargate 在中国区正式落地,因 Fargate 使用 Serverless 架构,更加适合对性能要求不敏感的服务使用,Pyroscope 是一款基于 Golang 开发的应用程序性能分析工具,Pyroscope 的服务端为无状态服务且性...

亚马逊云开发者阅读 7.8k

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

1.4k 声望
1.7k 粉丝
宣传栏