Skip to content

Commit 430a41a

Browse files
authored
Merge pull request #75 from SummerGGift/add_examples
【添加】micropython 例程并重新整理模块分类
2 parents 22ff754 + fcbb5e5 commit 430a41a

File tree

21 files changed

+434
-34
lines changed

21 files changed

+434
-34
lines changed

docs/03-MicroPython_libraries.md

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,47 @@ Python 的标准库被 “微型化”后,就是 MicroPython 标准库,也
1414

1515
## RT-Thread MicroPython 模块
1616

17-
### 系统模块
18-
- [rtthread][1] – RT-Thread 系统相关函数
19-
- [utime][2] – 时间相关函数
20-
- [sys][3] – 系统特有功能函数
21-
- [math][4] – 数学函数
22-
- [uio][5] – 输入/输出流
23-
- [ucollections][6] – 收集和容器类型
24-
- [ustruct][7] – 打包和解包原始数据类型
25-
- [array][8] – 数字数据数组
26-
- [gc][9] – 控制垃圾回收
17+
### 常用内建模块
18+
- [rtthread][1] – RT-Thread 系统相关函数
19+
- [utime][2] – 时间相关函数
20+
- [sys][3] – 系统特有功能函数
21+
- [math][4] – 数学函数
22+
- [uio][5] – 输入/输出流
23+
- [ucollections][6] – 提供有用的集合类
24+
- [ustruct][7] – 打包和解包原始数据类型
25+
- [array][8] – 数字数据数组
26+
- [gc][9] – 控制垃圾回收
27+
- [uos][15] – 基本的 “操作系统” 服务
28+
- [select][16] – 等待流事件
29+
- [uctypes][17] – 以结构化的方式访问二进制数据
30+
- [uerrno][18] – 系统错误码模块
31+
- [_thread][19] – 多线程支持
2732

2833
### 硬件模块
29-
- [machine][10] – 与硬件相关的功能
30-
- [machine.Pin][11]
31-
- [machine.I2C][12]
32-
- [machine.SPI][13]
33-
- [machine.UART][14]
34-
35-
### 系统模块
36-
- [uos][15] – 基本的 “操作系统” 服务
37-
- [select][16] – 等待流事件
38-
- [uctypes][17] – 以结构化的方式访问二进制数据
39-
- [uerrno][18] – 系统错误码模块
40-
- [_thread][19] – 多线程支持
41-
42-
### 工具模块
43-
- [cmath][20] – 复数的数学函数
44-
- [ubinascii][21] – 二进制/ ASCII转换
45-
- [uhashlib][22] – 哈希算法
46-
- [uheapq][23] – 堆排序算法
47-
- [ujson][24] – JSON编码与解码
48-
- [ure][25] – 正则表达式
49-
- [uzlib][26] – zlib 解压缩
50-
- [urandom][27] – 随机数生成模块
34+
- [machine][10] – 与硬件相关的功能
35+
- [machine.Pin][11] - Pin 引脚控制类
36+
- [machine.UART][14] - UART 外设控制类
37+
- [machine.I2C][12] - I2C 外设控制类
38+
- [machine.SPI][13] - SPI 外设控制类
39+
- [machine.RTC][29] - RTC 外设控制类
40+
- [machine.PWM][30] - PWM 外设控制类
41+
- [machine.ADC][31] - ADC 外设控制类
42+
- [machine.LCD][34] - LCD 外设控制类
5143

5244
### 网络模块
53-
- [usocket][28] – 套接字模块
45+
- [usocket][28] – 网络套接字模块
46+
- [network][32] – 网络连接控制模块
47+
- [network.WLAN][33] – WiFi 连接控制类
48+
49+
### 常用第三方模块
50+
- [cmath][20] – 复数的数学函数
51+
- [ubinascii][21] – 二进制/ ASCII转换
52+
- [uhashlib][22] – 哈希算法
53+
- [uheapq][23] – 堆排序算法
54+
- [ujson][24] – JSON编码与解码
55+
- [ure][25] – 正则表达式
56+
- [uzlib][26] – zlib 解压缩
57+
- [urandom][27] – 随机数生成模块
5458

5559
[1]: 03-Basic_Module/01-rtthread.md
5660
[2]: 03-Basic_Module/02-utime.md
@@ -80,4 +84,9 @@ Python 的标准库被 “微型化”后,就是 MicroPython 标准库,也
8084
[26]: 06-Tools_Module/07-uzlib.md
8185
[27]: 06-Tools_Module/08-urandom.md
8286
[28]: 07-Network_Module/01-usocket.md
83-
87+
[29]: 04-Hardware_Control_Module/07-machine-RTC.md
88+
[30]: 04-Hardware_Control_Module/08-machine-PWM.md
89+
[31]: 04-Hardware_Control_Module/09-machine-ADC.md
90+
[32]: 07-Network_Module/02-network.md
91+
[33]: 07-Network_Module/03-network-WLAN.md
92+
[34]: 04-Hardware_Control_Module/06-machine-LCD.md

examples/basic/_thread.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Copyright (c) 2006-2019, RT-Thread Development Team
3+
#
4+
# SPDX-License-Identifier: MIT License
5+
#
6+
# Change Logs:
7+
# Date Author Notes
8+
# 2019-06-13 SummerGift first version
9+
#
10+
11+
import _thread
12+
import utime as time
13+
14+
def testThread():
15+
while True:
16+
print("Hello from thread")
17+
time.sleep(2)
18+
19+
_thread.start_new_thread(testThread, ())
20+
while True:
21+
pass

examples/basic/array.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# Copyright (c) 2006-2019, RT-Thread Development Team
3+
#
4+
# SPDX-License-Identifier: MIT License
5+
#
6+
# Change Logs:
7+
# Date Author Notes
8+
# 2019-06-13 SummerGift first version
9+
#
10+
11+
import array
12+
13+
a = array.array('i', [2, 4, 1, 5])
14+
b = array.array('f')
15+
print(a)
16+
print(b)
17+
18+
a = array.array('f', [3, 6])
19+
print(a)
20+
a.append(7.0)
21+
print(a)
22+
23+
a = array.array('i', [1, 2, 3])
24+
b = array.array('i', [4, 5])
25+
a.extend(b)
26+
print(a)

examples/basic/random.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# Copyright (c) 2006-2019, RT-Thread Development Team
3+
#
4+
# SPDX-License-Identifier: MIT License
5+
#
6+
# Change Logs:
7+
# Date Author Notes
8+
# 2019-06-13 SummerGift first version
9+
#
10+
11+
import random
12+
13+
for j in range(0, 2):
14+
random.seed(13) #指定随机数种子
15+
for i in range(0, 10): #生成0到10范围内的随机序列
16+
print(random.randint(1, 10))
17+
print("end")
18+

examples/basic/rtthread.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#
2+
# Copyright (c) 2006-2019, RT-Thread Development Team
3+
#
4+
# SPDX-License-Identifier: MIT License
5+
#
6+
# Change Logs:
7+
# Date Author Notes
8+
# 2019-06-13 SummerGift first version
9+
#
10+
11+
import rtthread
12+
13+
print(rtthread.is_preempt_thread()) # determine if code is running in a preemptible thread
14+
print(rtthread.current_tid() ) # current thread id
15+
rtthread.stacks_analyze() # show thread information

examples/basic/sys.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# Copyright (c) 2006-2019, RT-Thread Development Team
3+
#
4+
# SPDX-License-Identifier: MIT License
5+
#
6+
# Change Logs:
7+
# Date Author Notes
8+
# 2019-06-13 SummerGift first version
9+
#
10+
11+
import sys
12+
13+
print(sys.version)
14+
print(sys.version_info)
15+
print(sys.path)
16+
print(sys.__name__)
17+
print(sys.platform)
18+
print(sys.byteorder)

examples/basic/ucollections.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#
2+
# Copyright (c) 2006-2019, RT-Thread Development Team
3+
#
4+
# SPDX-License-Identifier: MIT License
5+
#
6+
# Change Logs:
7+
# Date Author Notes
8+
# 2019-06-13 SummerGift first version
9+
#
10+
11+
from ucollections import namedtuple
12+
13+
MyTuple = namedtuple("MyTuple", ("id", "name"))
14+
t1 = MyTuple(1, "foo")
15+
t2 = MyTuple(2, "bar")
16+
print(t1.name)
17+
assert t2.name == t2[1]
18+
ucollections.OrderedDict(...)
19+
20+
from ucollections import OrderedDict
21+
22+
# To make benefit of ordered keys, OrderedDict should be initialized
23+
# from sequence of (key, value) pairs.
24+
d = OrderedDict([("z", 1), ("a", 2)])
25+
# More items can be added as usual
26+
d["w"] = 5
27+
d["b"] = 3
28+
for k, v in d.items():
29+
print(k, v)

examples/basic/uos.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#
2+
# Copyright (c) 2006-2019, RT-Thread Development Team
3+
#
4+
# SPDX-License-Identifier: MIT License
5+
#
6+
# Change Logs:
7+
# Date Author Notes
8+
# 2019-06-13 SummerGift first version
9+
#
10+
11+
import uos
12+
13+
uos.mkdir("rtthread")
14+
uos.getcwd()
15+
uos.chdir("rtthread")
16+
uos.getcwd()
17+
uos.listdir()
18+
uos.rmdir("11")
19+
uos.listdir()

examples/basic/utime.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Copyright (c) 2006-2019, RT-Thread Development Team
3+
#
4+
# SPDX-License-Identifier: MIT License
5+
#
6+
# Change Logs:
7+
# Date Author Notes
8+
# 2019-06-13 SummerGift first version
9+
#
10+
11+
import utime
12+
13+
utime.sleep(1) # sleep for 1 second
14+
utime.sleep_ms(500) # sleep for 500 milliseconds
15+
utime.sleep_us(10) # sleep for 10 microseconds
16+
start = utime.ticks_ms() # get value of millisecond counter
17+
delta = utime.ticks_diff(utime.ticks_ms(), start) # compute time difference
18+
print(utime.ticks_add(utime.ticks_ms(), -100))
19+
print(utime.ticks_add(0, -1))
20+
21+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Copyright (c) 2006-2019, RT-Thread Development Team
3+
#
4+
# SPDX-License-Identifier: MIT License
5+
#
6+
# Change Logs:
7+
# Date Author Notes
8+
# 2019-06-13 SummerGift first version
9+
#
10+
11+
import network
12+
13+
ap = network.WLAN(network.AP_IF)
14+
ap.config(essid="hello_rt-thread", password="88888888")
15+
ap.active(True)
16+
ap.config("essid")

0 commit comments

Comments
 (0)