Просмотр исходного кода

Merge pull request #75 from SummerGGift/add_examples

【添加】micropython 例程并重新整理模块分类
朱天龙 (Armink) 6 лет назад
Родитель
Сommit
430a41af34

+ 43 - 34
docs/03-MicroPython_libraries.md

@@ -14,43 +14,47 @@ Python 的标准库被 “微型化”后,就是 MicroPython 标准库,也
 
 ## RT-Thread MicroPython 模块
 
-### 系统模块
-- [rtthread][1] – RT-Thread 系统相关函数
-- [utime][2]  – 时间相关函数
-- [sys][3]  – 系统特有功能函数
-- [math][4]  – 数学函数
-- [uio][5]  – 输入/输出流
-- [ucollections][6]  – 收集和容器类型
-- [ustruct][7]  – 打包和解包原始数据类型
-- [array][8]  – 数字数据数组
-- [gc][9]  – 控制垃圾回收
+### 常用内建模块
+- [rtthread][1]       – RT-Thread 系统相关函数
+- [utime][2]          – 时间相关函数
+- [sys][3]            – 系统特有功能函数
+- [math][4]           – 数学函数
+- [uio][5]            – 输入/输出流
+- [ucollections][6]   – 提供有用的集合类
+- [ustruct][7]        – 打包和解包原始数据类型
+- [array][8]          – 数字数据数组
+- [gc][9]             – 控制垃圾回收
+- [uos][15]           – 基本的 “操作系统” 服务
+- [select][16]        – 等待流事件
+- [uctypes][17]       – 以结构化的方式访问二进制数据
+- [uerrno][18]        – 系统错误码模块
+- [_thread][19]       – 多线程支持
 
 ### 硬件模块
-- [machine][10] – 与硬件相关的功能
-- [machine.Pin][11]
-- [machine.I2C][12]
-- [machine.SPI][13]
-- [machine.UART][14]
-
-### 系统模块
-- [uos][15] – 基本的 “操作系统” 服务
-- [select][16] – 等待流事件
-- [uctypes][17] – 以结构化的方式访问二进制数据
-- [uerrno][18] – 系统错误码模块
-- [_thread][19] – 多线程支持
-
-### 工具模块
-- [cmath][20] – 复数的数学函数
-- [ubinascii][21] – 二进制/ ASCII转换
-- [uhashlib][22] – 哈希算法
-- [uheapq][23] – 堆排序算法
-- [ujson][24] – JSON编码与解码
-- [ure][25] – 正则表达式
-- [uzlib][26] – zlib 解压缩
-- [urandom][27] – 随机数生成模块
+- [machine][10]       – 与硬件相关的功能
+- [machine.Pin][11]   - Pin 引脚控制类
+- [machine.UART][14]  - UART 外设控制类
+- [machine.I2C][12]   - I2C 外设控制类
+- [machine.SPI][13]   - SPI 外设控制类
+- [machine.RTC][29]   - RTC 外设控制类
+- [machine.PWM][30]   - PWM 外设控制类
+- [machine.ADC][31]   - ADC 外设控制类
+- [machine.LCD][34]   - LCD 外设控制类
 
 ### 网络模块
-- [usocket][28] – 套接字模块
+- [usocket][28]       – 网络套接字模块
+- [network][32]       – 网络连接控制模块
+- [network.WLAN][33]  – WiFi 连接控制类
+
+### 常用第三方模块
+- [cmath][20]         – 复数的数学函数
+- [ubinascii][21]     – 二进制/ ASCII转换
+- [uhashlib][22]      – 哈希算法
+- [uheapq][23]        – 堆排序算法
+- [ujson][24]         – JSON编码与解码
+- [ure][25]           – 正则表达式
+- [uzlib][26]         – zlib 解压缩
+- [urandom][27]       – 随机数生成模块
 
 [1]: 03-Basic_Module/01-rtthread.md
 [2]: 03-Basic_Module/02-utime.md
@@ -80,4 +84,9 @@ Python 的标准库被 “微型化”后,就是 MicroPython 标准库,也
 [26]: 06-Tools_Module/07-uzlib.md
 [27]: 06-Tools_Module/08-urandom.md
 [28]: 07-Network_Module/01-usocket.md
-
+[29]: 04-Hardware_Control_Module/07-machine-RTC.md
+[30]: 04-Hardware_Control_Module/08-machine-PWM.md
+[31]: 04-Hardware_Control_Module/09-machine-ADC.md
+[32]: 07-Network_Module/02-network.md
+[33]: 07-Network_Module/03-network-WLAN.md
+[34]: 04-Hardware_Control_Module/06-machine-LCD.md

+ 21 - 0
examples/basic/_thread.py

@@ -0,0 +1,21 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+import _thread
+import utime as time
+
+def testThread():
+    while True:
+        print("Hello from thread")
+        time.sleep(2)
+
+_thread.start_new_thread(testThread, ())
+while True:
+    pass

+ 26 - 0
examples/basic/array.py

@@ -0,0 +1,26 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+import array
+
+a = array.array('i', [2, 4, 1, 5])
+b = array.array('f')
+print(a)
+print(b)
+
+a = array.array('f', [3, 6])
+print(a)
+a.append(7.0)
+print(a)
+
+a = array.array('i', [1, 2, 3])
+b = array.array('i', [4, 5])
+a.extend(b)
+print(a)

+ 18 - 0
examples/basic/random.py

@@ -0,0 +1,18 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+import random
+
+for j in range(0, 2):
+    random.seed(13)         #指定随机数种子
+    for i in range(0, 10):  #生成0到10范围内的随机序列
+        print(random.randint(1, 10))
+    print("end")
+

+ 15 - 0
examples/basic/rtthread.py

@@ -0,0 +1,15 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+import rtthread
+
+print(rtthread.is_preempt_thread())       # determine if code is running in a preemptible thread
+print(rtthread.current_tid() )            # current thread id
+rtthread.stacks_analyze()                 # show thread information

+ 18 - 0
examples/basic/sys.py

@@ -0,0 +1,18 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+import sys
+
+print(sys.version)
+print(sys.version_info)
+print(sys.path)
+print(sys.__name__)
+print(sys.platform)
+print(sys.byteorder)

+ 29 - 0
examples/basic/ucollections.py

@@ -0,0 +1,29 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+from ucollections import namedtuple
+
+MyTuple = namedtuple("MyTuple", ("id", "name"))
+t1 = MyTuple(1, "foo")
+t2 = MyTuple(2, "bar")
+print(t1.name)
+assert t2.name == t2[1]
+ucollections.OrderedDict(...)
+
+from ucollections import OrderedDict
+
+# To make benefit of ordered keys, OrderedDict should be initialized
+# from sequence of (key, value) pairs.
+d = OrderedDict([("z", 1), ("a", 2)])
+# More items can be added as usual
+d["w"] = 5
+d["b"] = 3
+for k, v in d.items():
+    print(k, v)

+ 19 - 0
examples/basic/uos.py

@@ -0,0 +1,19 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+import uos
+
+uos.mkdir("rtthread")
+uos.getcwd()
+uos.chdir("rtthread")
+uos.getcwd()
+uos.listdir()
+uos.rmdir("11")
+uos.listdir()

+ 21 - 0
examples/basic/utime.py

@@ -0,0 +1,21 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+import utime
+
+utime.sleep(1)                                    # sleep for 1 second
+utime.sleep_ms(500)                               # sleep for 500 milliseconds
+utime.sleep_us(10)                                # sleep for 10 microseconds
+start = utime.ticks_ms()                          # get value of millisecond counter
+delta = utime.ticks_diff(utime.ticks_ms(), start) # compute time difference
+print(utime.ticks_add(utime.ticks_ms(), -100))
+print(utime.ticks_add(0, -1))
+
+

+ 16 - 0
examples/network/network_wlan_ap.py

@@ -0,0 +1,16 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+import network
+
+ap = network.WLAN(network.AP_IF)
+ap.config(essid="hello_rt-thread", password="88888888")
+ap.active(True)
+ap.config("essid")

+ 16 - 0
examples/network/network_wlan_sta.py

@@ -0,0 +1,16 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+import network
+
+wlan = network.WLAN(network.STA_IF)
+wlan.scan()
+wlan.connect("rtthread","02188888888")
+wlan.isconnected()

+ 16 - 0
examples/network/tcp_client.py

@@ -0,0 +1,16 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+import usocket
+
+s = usocket.socket(usocket.AF_INET,usocket.SOCK_STREAM)
+s.connect(("192.168.10.110",6000))  
+s.send("micropython")               
+s.close()

+ 19 - 0
examples/network/tcp_server.py

@@ -0,0 +1,19 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+import usocket
+
+s = usocket.socket(usocket.AF_INET,usocket.SOCK_STREAM)  # Create STREAM TCP socket
+s.bind(('192.168.12.32', 6001))   
+s.listen(5)
+s.setblocking(True)
+sock,addr=s.accept()              
+sock.recv(10)                    
+s.close()

+ 16 - 0
examples/w601_iot_board/adc.py

@@ -0,0 +1,16 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+from machine import ADC     # 从 machine 导入 ADC 类
+
+adc = ADC(2, 5)             # 创建 ADC 对象,当前使用编号为 2 的 ADC 设备的 5 通道
+adc.read()                  # 获取 ADC 对象采样值
+adc.deinit()                # 关闭 ADC 对象
+adc.init(5)                 # 开启并重新配置 ADC 对象

+ 21 - 0
examples/w601_iot_board/i2c.py

@@ -0,0 +1,21 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+from machine import Pin, I2C
+
+clk = Pin(("clk", 43), Pin.OUT_OD)   # Select the 43 pin device as the clock
+sda = Pin(("sda", 44), Pin.OUT_OD)   # Select the 44 pin device as the data line
+i2c = I2C(-1, clk, sda, freq=100000) # create I2C peripheral at frequency of 100kHz
+i2c.scan()                           # scan for slaves, returning a list of 7-bit addresses
+i2c.writeto(0x51, b'123')            # write 3 bytes to slave with 7-bit address 42
+i2c.readfrom(0x51, 4)                # read 4 bytes from slave with 7-bit address 42
+i2c.readfrom_mem(0x51, 0x02, 1)      # read 1 bytes from memory of slave 0x51(7-bit)
+i2c.writeto_mem(0x51, 2, b'\x10')    # write 1 byte to memory of slave 42
+

+ 27 - 0
examples/w601_iot_board/lcd.py

@@ -0,0 +1,27 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+from machine import LCD     # 从 machine 导入 LCD 类
+
+lcd = LCD()                             # 创建一个 lcd 对象
+lcd.light(False)                        # 关闭背光
+lcd.light(True)                         # 打开背光
+lcd.fill(lcd.BLACK)                     # 将整个 LCD 填充为黑色
+lcd.fill(lcd.RED)                       # 将整个 LCD 填充为红色
+lcd.fill(lcd.GRAY)                      # 将整个 LCD 填充为灰色
+lcd.fill(lcd.WHITE)                     # 将整个 LCD 填充为白色
+lcd.pixel(50, 50, lcd.BLUE)             # 将(50,50)位置的像素填充为蓝色
+lcd.text("hello RT-Thread", 0, 0, 16)   # 在(0, 0) 位置以 16 字号打印字符串
+lcd.text("hello RT-Thread", 0, 16, 24)  # 在(0, 16)位置以 24 字号打印字符串
+lcd.text("hello RT-Thread", 0, 48, 32)  # 在(0, 48)位置以 32 字号打印字符串
+lcd.line(0, 50, 239, 50)                # 以起点(0,50),终点(239,50)画一条线
+lcd.line(0, 50, 239, 50)                # 以起点(0,50),终点(239,50)画一条线
+lcd.rectangle(100, 100, 200, 200)       # 以左上角为(100,100),右下角(200,200)画矩形
+lcd.circle(150, 150, 80)                # 以圆心位置(150,150),半径为 80 画圆

+ 18 - 0
examples/w601_iot_board/pin.py

@@ -0,0 +1,18 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+from machine import Pin
+
+p_out = Pin(("X1", 33), Pin.OUT_PP)
+p_out.value(1)              # set io high
+p_out.value(0)              # set io low
+
+p_in = Pin(("X2", 32), Pin.IN, Pin.PULL_UP)
+p_in.value()                # get value, 0 or 1

+ 19 - 0
examples/w601_iot_board/pwm.py

@@ -0,0 +1,19 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+from machine import PWM     # 从 machine 导入 PWM 类
+
+pwm = PWM(1, 4, 1000, 100)  # 创建 PWM 对象,当前使用编号为 1 的 PWM 设备的 4 通道,初始化的频率为 1000Hz,占空比数值为 100(占空比为 100/255 = 39.22%)
+pwm.freq(2000)              # 设置 PWM 对象频率
+pwm.freq()                  # 获取 PWM 对象频率
+pwm.duty(200)               # 设置 PWM 对象占空比数值
+pwm.duty()                  # 获取 PWM 对象占空比数值
+pwm.deinit()                # 关闭 PWM 对象
+pwm.init(4, 1000, 100)      # 开启并重新配置 PWM 对象

+ 17 - 0
examples/w601_iot_board/rtc.py

@@ -0,0 +1,17 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+from machine import RTC
+
+rtc = RTC()                        # 创建 RTC 设备对象
+rtc.init((2019,6,5,2,10,22,30,0))  # 设置初始化时间
+rtc.now()                          # 获取当前时间
+rtc.deinit()                       # 重置时间到2015年1月1日
+rtc.now()                          # 获取当前时间

+ 20 - 0
examples/w601_iot_board/spi.py

@@ -0,0 +1,20 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+from machine import Pin, SPI
+
+clk = Pin(("clk", 43), Pin.OUT_PP)
+mosi = Pin(("mosi", 44), Pin.OUT_PP)
+miso = Pin(("miso", 45), Pin.IN)
+spi = SPI(-1, 500000, polarity = 0, phase = 0, bits = 8, firstbit = 0, sck = clk, mosi = mosi, miso = miso)
+print(spi)
+spi.write("hello rt-thread!")
+spi.read(10)
+

+ 19 - 0
examples/w601_iot_board/uart.py

@@ -0,0 +1,19 @@
+# 
+# Copyright (c) 2006-2019, RT-Thread Development Team
+# 
+# SPDX-License-Identifier: MIT License
+# 
+# Change Logs:
+# Date           Author       Notes
+# 2019-06-13     SummerGift   first version
+#
+
+from machine import UART
+
+uart = UART(1, 9600)                         # init with given baudrate
+uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters
+uart.read(10)                                # read 10 characters, returns a bytes object
+uart.read()                                  # read all available characters
+uart.readline()                              # read a line
+uart.readinto(buf)                           # read and store into the given buffer
+uart.write('abc')                            # write the 3 characters