feat(Ubiquitous/RT_Thread): port micropython on RT-Thread for aiit-board

This commit is contained in:
yangtuo250
2022-03-10 16:22:44 +08:00
parent 73d10380ea
commit f83f073fa0
434 changed files with 94105 additions and 111 deletions
@@ -0,0 +1,28 @@
## _thread 多线程支持
`_thread` 模块提供了用于处理多线程的基本方法——多个控制线程共享它们的全局数据空间。为了实现同步,提供了简单的锁(也称为互斥锁或二进制信号量)。
### 示例
```python
import _thread
import time
def testThread():
while True:
print("Hello from thread")
time.sleep(2)
_thread.start_new_thread(testThread, ())
while True:
pass
```
输出结果(启动新的线程,每隔两秒打印字符):
Hello from thread
Hello from thread
Hello from thread
Hello from thread
Hello from thread
更多内容可参考 [_thread](http://docs.micropython.org/en/latest/pyboard/library/_thread.html) 。
@@ -0,0 +1,104 @@
# Builtin functions and exceptions
所有的内饰函数和异常类型都在下面描述。
## 函数和类型
- abs()
- all()
- any()
- bin()
- **class** bool
- **class** bytearray
- **class** bytes
- callable()
- chr()
- classmethod()
- compile()
- **class** complex
- delattr(obj, name)
- **class** dict
- dir()
- divmod()
- enumerate()
- eval()
- exec()
- filter()
- **class** float
- **class** frozenset
- getattr()
- globals()
- hasattr()
- hash()
- hex()
- id()
- input()
- **class** int
- classmethod from_bytes(bytes, byteorder)
In MicroPython, byteorder parameter must be positional (this is compatible with CPython).
- to_bytes(size, byteorder)
In MicroPython, byteorder parameter must be positional (this is compatible with CPython).
- isinstance()
- issubclass()
- iter()
- len()
- class list
- locals()
- map()
- max()
- **class** memoryview
- min()
- next()
- **class** object
- oct()
- open()
- ord()
- pow()
- print()
- property()
- range()
- repr()
- reversed()
- round()
- class set
- setattr()
- **class** slice
- The slice builtin is the type that slice objects have.
- sorted()
- staticmethod()
- **class** str
- sum()
- super()
- **class** tuple
- type()
- zip()
## 异常类型
- **exception** AssertionError
- **exception** AttributeError
- **exception** Exception
- **exception** ImportError
- **exception** IndexError
- **exception** KeyboardInterrupt
- **exception** KeyError
- **exception** MemoryError
- **exception** NameError
- **exception** NotImplementedError
- **exception** OSError
- See CPython documentation: OSError. MicroPython doesnt implement errno attribute, instead use the standard way to access exception arguments: exc.args[0].
- **exception** RuntimeError
- **exception** StopIteration
- **exception** SyntaxError
- **exception** SystemExit
- See CPython documentation: SystemExit.
- **exception** TypeError
- See CPython documentation: TypeError.
- **exception** ValueError
- **exception** ZeroDivisionError
@@ -0,0 +1,42 @@
## **cmath** 复数运算函数功能
`cmath` 模块提供了一些用于复数运算的函数。这个模块中的函数接受整数、浮点数或复数作为参数。他们还将接受任何有复数或浮点方法的 Python 对象:这些方法分别用于将对象转换成复数或浮点数,然后将该函数应用到转换的结果中。
### 函数
#### **cmath.cos**(z)
返回``z``的余弦。
#### **cmath.exp**(z)
返回``z``的指数。
#### **cmath.log**(z)
返回``z``的对数。
#### **cmath.log10**(z)
返回``z``的常用对数。
#### **cmath.phase**(z)
返回``z``的相位, 范围是(-pi, +pi],以弧度表示。
#### **cmath.polar**(z)
返回``z``的极坐标。
#### **cmath.rect**(r, phi)
返回`模量r`和相位``phi``的复数。
#### **cmath.sin**(z)
返回``z``的正弦。
#### **cmath.sqrt**(z)
返回``z``的平方根。
### 常数
#### **cmath.e**
自然对数的指数。
#### **cmath.pi**
圆周率。
更多内容可参考 [cmath](http://docs.micropython.org/en/latest/pyboard/library/cmath.html) 。
@@ -0,0 +1,22 @@
## **gc** 控制垃圾回收
**gc** 模块提供了垃圾收集器的控制接口。
### 函数
#### **gc.enable**()
允许自动回收内存碎片。
#### **gc.disable**()
禁止自动回收,但可以通过collect()函数进行手动回收内存碎片。
#### **gc.collect**()
运行一次垃圾回收。
#### **gc.mem_alloc**()
返回已分配的内存数量。
#### **gc.mem_free**()
返回剩余的内存数量。
更多内容可参考 [gc](http://docs.micropython.org/en/latest/pyboard/library/gc.html) 。
@@ -0,0 +1,283 @@
## **math** 数学函数
**math** 模块提供了对 C 标准定义的数学函数的访问。
> 注意:本模块需要带有硬件 FPU,精度是32位,这个模块需要浮点功能支持。
### 常数
#### **math.e**
自然对数的底数。
示例:
```
>>>import math
>>>print(math.e)
2.718282
```
#### **math.pi**
圆周长与直径的比值。
示例:
```
>>> print(math.pi)
3.141593
```
### 函数
#### **math.acos(x)**
传入弧度值,计算cos(x)的反三角函数。
#### **math.acosh(x)**
返回 ``x`` 的逆双曲余弦。
#### **math.asin(x)**
传入弧度值,计算sin(x)的反三角函数。
示例:
```
>>> x = math.asin(0.5)
>>> print(x)
0.5235988
```
#### **math.asinh(x)**
返回``x`` 的逆双曲正弦。
#### **math.atan(x)**
返回 ``x`` 的逆切线。
#### **math.atan2(y, x)**
Return the principal value of the inverse tangent of y/x.
#### **math.atanh(x)**
Return the inverse hyperbolic tangent of x.
#### **math.ceil(x)**
向上取整。
示例:
```
>>> x = math.ceil(5.6454)
>>> print(x)
6
```
#### **math.copysign(x, y)**
Return x with the sign of y.
#### **math.cos(x)**
传入弧度值,计算余弦。
示例:计算cos60°
```
>>> math.cos(math.radians(60))
0.5
```
#### **math.cosh(x)**
Return the hyperbolic cosine of x.
#### **math.degrees(x)**
弧度转化为角度。
示例:
```
>>> x = math.degrees(1.047198)
>>> print(x)
60.00002
```
#### **math.erf(x)**
Return the error function of x.
#### **math.erfc(x)**
Return the complementary error function of x.
#### **math.exp(x)**
计算e的x次方(幂)。
示例:
```
>>> x = math.exp(2)
>>> print(x)
7.389056
```
#### **math.expm1(x)**
计算 math.exp(x) - 1。
#### **math.fabs(x)**
计算绝对值。
示例:
```
>>> x = math.fabs(-5)
>>> print(x)
5.0
>>> y = math.fabs(5.0)
>>> print(y)
5.0
```
#### **math.floor(x)**
向下取整。
示例:
```
>>> x = math.floor(2.99)
>>> print(x)
2
>>> y = math.floor(-2.34)
>>> print(y)
-3
```
#### **math.fmod(x, y)**
取x除以y的模。
示例:
```
>>> x = math.fmod(4, 5)
>>> print(x)
4.0
```
#### **math.frexp(x)**
Decomposes a floating-point number into its mantissa and exponent. The returned value is the tuple (m, e) such that x == m * 2**e exactly. If x == 0 then the function returns (0.0, 0), otherwise the relation 0.5 <= abs(m) < 1 holds.
#### **math.gamma(x)**
返回伽马函数。
示例:
```
>>> x = math.gamma(5.21)
>>> print(x)
33.08715
```
#### **math.isfinite(x)**
Return True if x is finite.
#### **math.isinf(x)**
Return True if x is infinite.
#### **math.isnan(x)**
Return True if x is not-a-number
#### **math.ldexp(x, exp)**
Return x * (2**exp).
#### **math.lgamma(x)**
返回伽马函数的自然对数。
示例:
```
>>> x = math.lgamma(5.21)
>>> print(x)
3.499145
```
#### **math.log(x)**
计算以e为底的x的对数。
示例:
```
>>> x = math.log(10)
>>> print(x)
2.302585
```
#### **math.log10(x)**
计算以10为底的x的对数。
示例:
```
>>> x = math.log10(10)
>>> print(x)
1.0
```
#### **math.log2(x)**
计算以2为底的x的对数。
示例:
```
>>> x = math.log2(8)
>>> print(x)
3.0
```
#### **math.modf(x)**
Return a tuple of two floats, being the fractional and integral parts of x. Both return values have the same sign as x.
#### **math.pow(x, y)**
计算 x 的 y 次方(幂)。
示例:
```
>>> x = math.pow(2, 3)
>>> print(x)
8.0
```
#### **math.radians(x)**
角度转化为弧度。
示例:
```
>>> x = math.radians(60)
>>> print(x)
1.047198
```
#### **math.sin(x)**
传入弧度值,计算正弦。
示例:计算sin90°
```
>>> math.sin(math.radians(90))
1.0
```
#### **math.sinh(x)**
Return the hyperbolic sine of x.
#### **math.sqrt(x)**
计算平方根。
示例:
```
>>> x = math.sqrt(9)
>>> print(x)
3.0
```
#### **math.tan(x)**
传入弧度值,计算正切。
示例:计算tan60°
```
>>> math.tan(math.radians(60))
1.732051
```
#### **math.tanh(x)**
Return the hyperbolic tangent of x.
#### **math.trunc(x)**
取整。
示例:
```
>>> x = math.trunc(5.12)
>>> print(x)
5
>>> y = math.trunc(-6.8)
>>> print(y)
-6
```
更多内容可参考 [math](http://docs.micropython.org/en/latest/pyboard/library/math.html) 。
@@ -0,0 +1,34 @@
## **rtthread** 系统相关函数
**rtthread** 模块提供了与 RT-Thread 操作系统相关的功能,如查看栈使用情况等。
### 函数
#### rtthread.current_tid()
返回当前线程的 id 。
#### rtthread.is_preempt_thread()
返回是否是可抢占线程。
#### rtthread.stacks_analyze()
返回当前系统线程和栈使用信息。
### 示例
```
>>> import rtthread
>>>
>>> rtthread.is_preempt_thread() # determine if code is running in a preemptible thread
True
>>> rtthread.current_tid() # current thread id
268464956
>>> rtthread.stacks_analyze() # show thread information
thread pri status sp stack size max used left tick error
---------- --- ------- ---------- ---------- ------ ---------- ---
elog_async 31 suspend 0x000000a8 0x00000400 26% 0x00000003 000
tshell 20 ready 0x00000260 0x00001000 39% 0x00000003 000
tidle 31 ready 0x00000070 0x00000100 51% 0x0000000f 000
SysMonitor 30 suspend 0x000000a4 0x00000200 32% 0x00000005 000
timer 4 suspend 0x00000080 0x00000200 25% 0x00000009 000
>>>
```
@@ -0,0 +1,69 @@
## **sys** 系统特有功能函数
**sys** 模块提供系统特有的功能。
### 函数
#### **sys.exit**(retval=0)
终止当前程序给定的退出代码。 函数会抛出 `SystemExit` 异常。
#### **sys.print_exception**(exc, file=sys.stdout)
打印异常与追踪到一个类似文件的对象 file (或者缺省 `sys.stdout` ).
> 提示:这是 CPython 中回溯模块的简化版本。不同于 `traceback.print_exception()`,这个函数用异常值代替了异常类型、异常参数和回溯对象。文件参数在对应位置,不支持更多参数。CPython 兼容回溯模块在 `micropython-lib`。
### 常数
#### **sys.argv**
当前程序启动时参数的可变列表。
#### **sys.byteorder**
系统字节顺序 (“little” or “big”).
#### **sys.implementation**
关于当前 Python 实现的信息,对于 MicroPython 来说,有以下属性:
- 名称 - ‘’micropython“
- 版本 - 元组(主要,次要,小),比如(1,9,3)
#### **sys.modules**
已加载模块的字典。在一部分移植中,它可能不包含内置模块。
#### **sys.path**
用来搜索导入模块地址的列表。
#### **sys.platform**
返回当前平台的信息。
#### **sys.stderr**
标准错误流。
#### **sys.stdin**
标准输入流。
#### **sys.stdout**
标准输出流。
#### **sys.version**
符合的 Python 语言版本,如字符串。
#### **sys.version_info**
本次实现使用的 Python 语言版本,用一个元组的方式表示。
### 示例
```
>>> import sys
>>> sys.version
'3.4.0'
>>> sys.version_info
(3, 4, 0)
>>> sys.path
['', '/libs/mpy/']
>>> sys.__name__
'sys'
>>> sys.platform
'rt-thread'
>>> sys.byteorder
'little'
```
更多内容可参考 [sys](http://docs.micropython.org/en/latest/pyboard/library/sys.html) 。
@@ -0,0 +1,56 @@
## **array** 数字数据数组
**array** 模块定义了一个对象类型,它可以简洁地表示基本值的数组:字符、整数、浮点数。支持代码格式: b, B, h, H, i, I, l, L, q, Q, f, d (最后2个需要支持浮点数)。
### 构造函数
#### **class array.array**(typecode[, iterable])
用给定类型的元素创建数组。数组的初始内容由 iterable 提供,如果没有提供,则创建一个空数组。
```
typecode:数组的类型
iterable:数组初始内容
```
示例:
```python
>>> import array
>>> a = array.array('i', [2, 4, 1, 5])
>>> b = array.array('f')
>>> print(a)
array('i', [2, 4, 1, 5])
>>> print(b)
array('f')
```
### 方法
#### **array.append**(val)
将一个新元素追加到数组的末尾。
示例:
```python
>>> a = array.array('f', [3, 6])
>>> print(a)
array('f', [3.0, 6.0])
>>> a.append(7.0)
>>> print(a)
array('f', [3.0, 6.0, 7.0])
```
#### **array.extend**(iterable)
将一个新的数组追加到数组的末尾,注意追加的数组和原来数组的数据类型要保持一致。
示例:
```python
>>> a = array.array('i', [1, 2, 3])
>>> b = array.array('i', [4, 5])
>>> a.extend(b)
>>> print(a)
array('i', [1, 2, 3, 4, 5])
```
更多内容可参考 [array](http://docs.micropython.org/en/latest/pyboard/library/array.html) 。
@@ -0,0 +1,47 @@
## **ubinascii** 二进制/ ASCII转换
`ubinascii` 模块包含许多在二进制和各种 ascii 编码的二进制表示之间转换的方法。
### 函数
#### **ubinascii.hexlify**(data[, sep])
将字符串转换为十六进制表示的字符串。
示例:
```
>>> ubinascii.hexlify('hello RT-Thread')
b'68656c6c6f2052542d546872656164'
>>> ubinascii.hexlify('summer')
b'73756d6d6572'
```
如果指定了第二个参数sep,它将用于分隔两个十六进制数。
示例:
```
如果指定了第二个参数sep,它将用于分隔两个十六进制数。
示例:
>>> ubinascii.hexlify('hello RT-Thread'," ")
b'68 65 6c 6c 6f 20 52 54 2d 54 68 72 65 61 64'
>>> ubinascii.hexlify('hello RT-Thread',",")
b'68,65,6c,6c,6f,20,52,54,2d,54,68,72,65,61,64'
```
#### **ubinascii.unhexlify**(data)
转换十六进制字符串为二进制字符串,功能和 hexlify 相反。
示例:
```
>>> ubinascii.unhexlify('73756d6d6572')
b'summer'
```
#### **ubinascii.a2b_base64**(data)
Base64编码的数据转换为二进制表示。返回字节串。
#### **ubinascii.b2a_base64**(data)
编码base64格式的二进制数据。返回的字符串。
更多内容可参考 [ubinascii](http://docs.micropython.org/en/latest/pyboard/library/ubinascii.html) 。
@@ -0,0 +1,44 @@
## **ucollections** 集合与容器类型
**ucollections** 模块实现了专门的容器数据类型,它提供了 Python 的通用内置容器的替代方案,包括了字典、列表、集合和元组。
### 类
#### **ucollections.namedtuple**(name, fields)
这是工厂函数创建一个新的 `namedtuple` 型与一个特定的字段名称和集合。`namedtuple` 是元组允许子类要访问它的字段不仅是数字索引,而且还具有属性使用符号字段名访问语法。 字段是字符串序列指定字段名称。为了兼容的实现也可以用空间分隔的字符串命名的字段(但效率较低) 。
代码示例:
```python
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(...)
```
#### **ucollections.OrderedDict**(...)
字典类型的子类,会记住并保留键/值的追加顺序。当有序的字典被迭代输出时,键/值 会按照他们被添加的顺序返回 :
```python
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)
```
输出:
z 1
a 2
w 5
b 3
更多的内容可参考 [ucollections](http://docs.micropython.org/en/latest/pyboard/library/ucollections.html) 。
@@ -0,0 +1,82 @@
## **uctypes** – 以结构化的方式访问二进制数据
uctypes 模块用来访问二进制数据结构,它提供 C 兼容的数据类型。
### 常量
- uctypes.LITTLE_ENDIAN — 小端压缩结构。
- uctypes.BIG_ENDIAN — 大端压缩结构类型。
- NATIVE — mricopython 本地的存储类型
### 构造函数
#### class uctypes.struct(addr, descriptor, type)
将内存中以 c 形式打包的结构体或联合体转换为字典,并返回该字典。
```
addr:开始转换的地址
descriptor:转换描述符
格式:"field_name":offset|uctypes.UINT32
offset:偏移量,
单位:字节、VOID、UINT8、INT8、UINT16、INT16、UINT32、INT32、UINT64、INT64、BFUINT8、BFINT8、BFUINT16、BFINT16、BFUINT32、BFINT32、BF_POS、BF_LEN、FLOAT32、FLOAT64、PTR、ARRAY
type:c 结构体或联合体存储类型,默认为本地存储类型
```
示例:
```python
>>> a = b"0123"
>>> s = uctypes.struct(uctypes.addressof(a), {"a": uctypes.UINT8 | 0, "b": uctypes.UINT16 | 1}, uctypes.LITTLE_ENDIAN)
>>> print(s)
<struct STRUCT 3ffc7360>
>>> print(s.a)
48
>>> s.a = 49
>>> print(a)
b'1123'
```
### 方法
#### **uctypes.sizeof**(struct)
按字节返回数据的大小。参数可以是类或者数据对象 (或集合)。
示例:
```python
>>> a = b"0123"
>>>b = uctypes.struct(uctypes.addressof(a), {"a": uctypes.UINT8 | 0, "b": uctypes.UINT16 | 1}, uctypes.LITTLE_ENDIAN)
>>> b.a
48
>>> print(uctypes.sizeof(b))
3
```
#### **uctypes.addressof**(obj)
返回对象地址。参数需要是 bytes, bytearray 。
示例:
```python
>>> a = b"0123"
>>> print(uctypes.addressof(a))
1073504048
```
#### **uctypes.bytes_at**(addr, size)
捕捉从 addr 开始到 size 个地址偏移量结束的内存数据为 bytearray 对象并返回。
示例:
```python
>>> a = b"0123"
>>>print( uctypes.bytes_at(uctypes.addressof(a), 4))
b'0123'
```
#### **uctypes.bytearray_at**(addr, size)
捕捉给定大小和地址内存为 bytearray 对象。与 bytes_at() 函数不同的是,它可以被再次写入,可以访问给定地址的参数。
示例:
```python
>>> a = b"0123"
>>> print(uctypes.bytearray_at(uctypes.addressof(a), 2))
bytearray(b'01')
```
更多内容可参考 [uctypes](http://docs.micropython.org/en/latest/pyboard/library/uctypes.html) 。
@@ -0,0 +1,20 @@
## **uerrno** 系统错误码模块
`uerrno` 模块提供了标准的 errno 系统符号,每个符号都有对应的整数值。
### 示例
```python
try:
uos.mkdir("my_dir")
except OSError as exc:
if exc.args[0] == uerrno.EEXIST:
print("Directory already exists")
uerrno.errorcode
Dictionary mapping numeric error codes to strings with symbolic error code (see above):
>>> print(uerrno.errorcode[uerrno.EEXIST])
EEXIST
```
更多内容可参考 [uerrno](http://docs.micropython.org/en/latest/pyboard/library/uerrno.html) 。
@@ -0,0 +1,36 @@
## **uhashlib** 哈希算法
`uhashlib` 模块实现了二进制数据哈希算法。
### 算法功能
#### **SHA256**
当代的散列算法(SHA2系列),它适用于密码安全的目的。被包含在 MicroPython 内核中,除非有特定的代码大小限制,否则推荐任何开发板都支持这个功能。
#### **SHA1**
上一代的算法,不推荐新的应用使用这种算法,但是 SHA1 算法是互联网标准和现有应用程序的一部分,所以针对网络连接便利的开发板会提供这种功能。
#### **MD5**
一种遗留下来的算法,作为密码使用被认为是不安全的。只有特定的开发板,为了兼容老的应用才会提供这种算法。
### 函数
#### **class uhashlib.sha256**([data])
创建一个SHA256哈希对象并提供 data 赋值。
#### **class uhashlib.sha1**([data])
创建一个SHA1哈希对象并提供 data 赋值。
#### **class uhashlib.md5**([data])
创建一个MD5哈希对象并提供 data 赋值。
#### **hash.update**(data)
将更多二进制数据放入哈希表中。
#### **hash.digest**()
返回字节对象哈希的所有数据。调用此方法后,将无法将更多数据送入哈希。
#### **hash.hexdigest**()
此方法没有实现, 使用 ubinascii.hexlify(hash.digest()) 达到类似效果。
更多内容可参考 [uhashlib](http://docs.micropython.org/en/latest/pyboard/library/uhashlib.html) 。
@@ -0,0 +1,16 @@
## **uheapq** 堆排序算法
`uheapq` 模块提供了堆排序相关算法,堆队列是一个列表,它的元素以特定的方式存储。
### 函数
#### **uheapq.heappush**(heap, item)
将对象压入堆中。
#### **uheapq.heappop**(heap)
从 heap 弹出第一个元素并返回。 如果是堆时空的会抛出 IndexError。
#### **uheapq.heapify**(x)
将列表 x 转换成堆。
更多内容可参考 [uheapq](http://docs.micropython.org/en/latest/pyboard/library/uheapq.html) 。
@@ -0,0 +1,27 @@
## **uio** 输入/输出流
**uio** 模块包含流类型 (类似文件) 对象和帮助函数。
### 函数
#### **uio.open**(name, mode='r', \*\*kwargs)
打开一个文件,关联到内建函数``open()``。所有端口 (用于访问文件系统) 需要支持模式参数,但支持其他参数不同的端口。
### 类
#### **class uio.FileIO**(...)
这个文件类型用二进制方式打开文件,等于使用``open(name, “rb”)``。 不应直接使用这个实例。
#### **class uio.TextIOWrapper**(...)
这个类型以文本方式打开文件,等同于使用``open(name, “rt”)``不应直接使用这个实例。
#### **class uio.StringIO**([string])
#### **class uio.BytesIO**([string])
内存文件对象。`StringIO` 用于文本模式 I/O (用 “t” 打开文件),`BytesIO` 用于二进制方式 (用 “b” 方式)。文件对象的初始内容可以用字符串参数指定(`stringio`用普通字符串,`bytesio`用`bytes`对象)。所有的文件方法,如 `read(), write(), seek(), flush(), close()` 都可以用在这些对象上,包括下面方法:
#### **getvalue**()
获取缓存区内容。
更多内容可参考 [uio](http://docs.micropython.org/en/latest/pyboard/library/uio.html) 。
@@ -0,0 +1,42 @@
## **ujson** JSON编码与解码
`ujson` 模块提供 Python 对象到 JSONJavaScript Object Notation 数据格式的转换。
### 函数
#### **ujson.dumps**(obj)
将 dict 类型转换成 str。
```
obj:要转换的对象
```
示例:
```
>>> obj = {1:2, 3:4, "a":6}
>>> print(type(obj), obj) #原来为dict类型
<class 'dict'> {3: 4, 1: 2, 'a': 6}
>>> jsObj = json.dumps(obj) #将dict类型转换成str
>>> print(type(jsObj), jsObj)
<class 'str'> {3: 4, 1: 2, "a": 6}
```
#### **ujson.loads**(str)
解析 JSON 字符串并返回对象。如果字符串格式错误将引发 ValueError 异常。
示例:
```
>>> obj = {1:2, 3:4, "a":6}
>>> jsDumps = json.dumps(obj)
>>> jsLoads = json.loads(jsDumps)
>>> print(type(obj), obj)
<class 'dict'> {3: 4, 1: 2, 'a': 6}
>>> print(type(jsDumps), jsDumps)
<class 'str'> {3: 4, 1: 2, "a": 6}
>>> print(type(jsLoads), jsLoads)
<class 'dict'> {'a': 6, 1: 2, 3: 4}
```
更多内容可参考 [ujson](http://docs.micropython.org/en/latest/pyboard/library/ujson.html) 。
@@ -0,0 +1,56 @@
## **uos** – 基本的操作系统服务
`uos` 模块包含了对文件系统的访问操作,是对应 CPython 模块的一个子集。
### 函数
#### **uos.chdir**(path)
更改当前目录。
#### **uos.getcwd**()
获取当前目录。
#### **uos.listdir**([dir])
没有参数就列出当前目录,否则列出给定目录。
#### **uos.mkdir**(path)
创建一个目录。
#### **uos.remove**(path)
删除文件。
#### **uos.rmdir**(path)
删除目录。
#### **uos.rename**(old_path, new_path)
重命名文件或者文件夹。
#### **uos.stat**(path)
获取文件或目录的状态。
#### **uos.sync**()
同步所有的文件系统。
### 示例
```
>>> import uos
>>> uos. # Tab
__name__ uname chdir getcwd
listdir mkdir remove rmdir
stat unlink mount umount
>>> uos.mkdir("rtthread")
>>> uos.getcwd()
'/'
>>> uos.chdir("rtthread")
>>> uos.getcwd()
'/rtthread'
>>> uos.listdir()
['web_root', 'rtthread', '11']
>>> uos.rmdir("11")
>>> uos.listdir()
['web_root', 'rtthread']
>>>
```
更多内容可参考 [uos](http://docs.micropython.org/en/latest/pyboard/library/uos.html) 。
@@ -0,0 +1,174 @@
## **urandom** - 随机数生成模块
`urandom` 模块实现了伪随机数生成器。
### 函数
#### **urandom.choice**(obj)
随机生成对象 obj 中的元数。
```
obj:元数列表
```
示例:
```python
>>> print(random.choice("DFRobot"))
R
>>> print(random.choice("DFRobot"))
D
>>> print(random.choice([0, 2, 4, 3]))
3
>>> print(random.choice([0, 2, 4, 3]))
3
>>> print(random.choice([0, 2, 4, 3]))
2
```
#### **urandom.getrandbits**(size)
随机生成 0 到 size 个位二进制数范围内的正整数。 比如 :
- size = 4,那么便是从 0 到0b1111中随机一个正整数。
- size = 8,那么便是从 0 到 0b11111111中随机一个正整数。
```python
size位大小
```
示例:
```python
>>> print( random.getrandbits(1)) #1位二进制位,范围为0~1(十进制:0~1)
1
>>> print(random.getrandbits(1))
0
>>> print(random.getrandbits(8)) #8位二进制位,范围为0000 0000~1111 11111(十进制:0~255
224
>>> print(random.getrandbits(8))
155
```
#### **urandom.randint**(start, end)
随机生成一个 start 到 end 之间的整数。
```
start:指定范围内的开始值,包含在范围内
end:指定范围内的结束值,包含在范围内
```
示例:
```python
>>> import random
>>> print(random.randint(1, 4))
4
>>> print(random.randint(1, 4))
2
```
#### **urandom.random**()
随机生成一个 0 到 1 之间的浮点数。
示例:
```python
>>> print(random.random())
0.7111824
>>> print(random.random())
0.3168149
```
#### **urandom.randrange**(start, end, step)
随机生成 start 到 end 并且递增为 step 的范围内的正整数。例如,randrange(0, 8, 2)中,随机生成的数为 0、2、4、6 中任一个。
```
start:指定范围内的开始值,包含在范围内
end:指定范围内的结束值,包含在范围内
step:递增基数
```
示例:
```python
>>> print(random.randrange(2, 8, 2))
4
>>> print(random.randrange(2, 8, 2))
6
>>> print(random.randrange(2, 8, 2))
2
```
#### **urandom.seed**(sed)
指定随机数种子,通常和其他随机数生成函数搭配使用。
**注意:**
MicroPython 中的随机数其实是一个稳定算法得出的稳定结果序列,而不是一个随机序列。sed 就是这个算法开始计算的第一个值。所以就会出现只要 sed 是一样的,那么后续所有“随机”结果和顺序也都完全一致。
```
sed:随机数种子
```
示例:
```python
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")
```
运行结果:
```
5
2
3
2
3
4
2
5
8
2
end
5
2
3
2
3
4
2
5
8
2
end
```
从上面可以看到生成两个随机数列表是一样的,你也可以多生成几个随机数列表查看结果。
#### **urandom.uniform**(start, end)
随机生成start到end之间的浮点数。
```
start:指定范围内的开始值,包含在范围内
stop:指定范围内的结束值,包含在范围内
```
示例:
```python
>>> print(random.uniform(2, 4))
2.021441
>>> print(random.uniform(2, 4))
3.998012
```
更多内容可参考 [urandom](https://docs.python.org/3/library/random.html?highlight=random#module-random) 。
@@ -0,0 +1,52 @@
## **ure** 正则表达式
`ure` 模块用于测试字符串的某个模式,执行正则表达式操作。
### 匹配字符集
#### 匹配任意字符
``'.'``
#### 匹配字符集合,支持单个字符和一个范围
``'[]'``
#### 支持多种匹配元字符
``'^'``
``'$'``
``'?'``
``'*'``
``'+'``
``'??'``
``'*?'``
``'+?'``
``'{m,n}'``
### 函数
#### **ure.compile**(regex)
编译正则表达式,返回 regex 对象。
#### **ure.match**(regex, string)
用 string 匹配 regex,匹配总是从字符串的开始匹配。
#### **ure.search**(regex, string)
在 string 中搜索 regex。不同于匹配,它搜索第一个匹配位置的正则表达式字符串 (结果可能会是0)。
#### **ure.DEBUG**
标志值,显示表达式的调试信息。
### **正则表达式对象**:
编译正则表达式,使用 `ure.compile()` 创建实例。
#### **regex.match**(string)
#### **regex.search**(string)
#### **regex.split**(string, max_split=-1)
### **匹配对象** :
匹配对象是 match() 和 search() 方法的返回值。
#### **match.group**([index])
只支持数字组。
更多内容可参考 [ure](http://docs.micropython.org/en/latest/pyboard/library/ure.html) 。
@@ -0,0 +1,102 @@
## **uselect** 等待流事件
`uselect` 模块提供了等待数据流的事件功能。
### 常数
#### **select.POLLIN** - 读取可用数据
#### **select.POLLOUT** - 写入更多数据
#### **select.POLLERR** - 发生错误
#### **select.POLLHUP** - 流结束/连接终止检测
### 函数
#### **select.select**(rlist, wlist, xlist[, timeout])
监控对象何时可读或可写,一旦监控的对象状态改变,返回结果(阻塞线程)。这个函数是为了兼容,效率不高,推荐用 `poll` 函数 。
```
rlist:等待读就绪的文件描述符数组
wlist:等待写就绪的文件描述符数组
xlist:等待异常的数组
timeout:等待时间(单位:秒)
```
示例:
```python
def selectTest():
global s
rs, ws, es = select.select([s,], [], [])
#程序会在此等待直到对象s可读
print(rs)
for i in rs:
if i == s:
print("s can read now")
data,addr=s.recvfrom(1024)
print('received:',data,'from',addr)
```
### Poll 类
#### **select.poll**()
创建 poll 实例。
示例:
```
>>>poller = select.poll()
>>>print(poller)
<poll>
```
#### **poll.register**(obj[, eventmask])
注册一个用以监控的对象,并设置被监控对象的监控标志位 flag。
```
obj:被监控的对象
flag:被监控的标志
select.POLLIN — 可读
select.POLLHUP — 已挂断
select.POLLERR — 出错
select.POLLOUT — 可写
```
#### **poll.unregister**(obj)
解除监控的对象的注册。
```
obj:注册过的对象
```
示例:
```
>>>READ_ONLY = select.POLLIN | select.POLLHUP | select.POLLERR
>>>READ_WRITE = select.POLLOUT | READ_ONLY
>>>poller.register(s, READ_WRITE)
>>>poller.unregister(s)
```
#### **poll.modify**(obj, eventmask)
修改已注册的对象监控标志。
```
obj:已注册的被监控对象
flag:修改为的监控标志
```
示例:
```
>>>READ_ONLY = select.POLLIN | select.POLLHUP | select.POLLERR
>>>READ_WRITE = select.POLLOUT | READ_ONLY
>>>poller.register(s, READ_WRITE)
>>>poller.modify(s, READ_ONLY)
```
#### **poll.poll**([timeout])
等待至少一个已注册的对象准备就绪。返回 (obj, event, ...) 元组, event 元素指定了一个流发生的事件,是上面所描述的 `select.POLL*`常量组合。 根据平台和版本的不同,在元组中可能有其他元素,所以不要假定元组的大小是 2 。如果超时,则返回空列表。
更多内容可参考 [uselect](http://docs.micropython.org/en/latest/pyboard/library/uselect.html) 。
@@ -0,0 +1,195 @@
## **usocket** 套接字模块
`usocket` 模块提供对BSD套接字接口的访问。
### 常数
#### 地址簇
- socket.AF_INET =2 — TCP/IP IPv4
- socket.AF_INET6 =10 — TCP/IP IPv6
#### 套接字类型
- socket.SOCK_STREAM =1 — TCP流
- socket.SOCK_DGRAM =2 — UDP数据报
- socket.SOCK_RAW =3 — 原始套接字
- socket.SO_REUSEADDR =4 — socket可重用
#### IP协议号
- socket.IPPROTO_TCP =16
- socket.IPPROTO_UDP =17
#### 套接字选项级别
- socket.SOL_SOCKET =4095
### 函数
#### **socket.socket**
`socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)`
创建新的套接字,使用指定的地址、类型和协议号。
#### **socket.getaddrinfo**(host, port)
将主机域名(host)和端口(port)转换为用于创建套接字的5元组序列。元组列表的结构如下:
```
(family, type, proto, canonname, sockaddr)
```
示例:
```
>>> info = socket.getaddrinfo("rt-thread.org", 10000)
>>> print(info)
[(2, 1, 0, '', ('118.31.15.152', 10000))]
```
#### **socket.close**()
关闭套接字。一旦关闭后,套接字所有的功能都将失效。远端将接收不到任何数据 (清理队列数据后)。 虽然在垃圾回收时套接字会自动关闭,但还是推荐在必要时用 close() 去关闭。
#### **socket.bind**(address)
将套接字绑定到地址,套接字不能是已经绑定的。
#### **socket.listen**([backlog])
监听套接字,使服务器能够接收连接。
```
backlog:接受套接字的最大个数,至少为0,如果没有指定,则默认一个合理值。
```
#### **socket.accept**()
接收连接请求。
**注意:**
只能在绑定地址端口号和监听后调用,返回 conn 和 address。
```
conn:新的套接字对象,可以用来收发消息
address:连接到服务器的客户端地址
```
#### **socket.connect**(address)
连接服务器。
```
address:服务器地址和端口号的元组或列表
```
#### **socket.send**(bytes)
发送数据,并返回成功发送的字节数,返回字节数可能比发送的数据长度少。
```
bytesbytes类型数据
```
#### **socket.recv**(bufsize)
接收数据,返回接收到的数据对象。
```
bufsize:指定一次接收的最大数据量
```
示例:
```
data = conn.recv(1024)
```
#### **socket.sendto**(bytes, address)
发送数据,目标由address决定,常用于UDP通信,返回发送的数据大小。
```
bytesbytes类型数据
address:目标地址和端口号的元组
```
示例:
```
data = sendto("hello RT-Thread", ("192.168.10.110", 100))
```
#### **socket.recvfrom**(bufsize)
接收数据,常用于UDP通信,并返回接收到的数据对象和对象的地址。
```
bufsize:指定一次接收的最大数据量
```
示例:
```
data,addr=fd.recvfrom(1024)
```
#### **socket.setsockopt**(level, optname, value)
根据选项值设置套接字。
```
level:套接字选项级别
optname:套接字的选项
value:可以是一个整数,也可以是一个表示缓冲区的bytes类对象。
```
示例:
```
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
```
#### **socket.settimeout**(value)
设置超时时间,单位:秒。
示例:
```
s.settimeout(2)
```
#### **socket.setblocking**(flag)
设置阻塞或非阻塞模式: 如果 flag 是 false,设置非阻塞模式。
#### **socket.read**([size])
Read up to size bytes from the socket. Return a bytes object. If size is not given, it reads all data available from the socket until EOF; as such the method will not return until the socket is closed. This function tries to read as much data as requested (no “short reads”). This may be not possible with non-blocking socket though, and then less data will be returned.
#### **socket.readinto**(buf[, nbytes])
Read bytes into the buf. If nbytes is specified then read at most that many bytes. Otherwise, read at most len(buf) bytes. Just as read(), this method follows “no short reads” policy.
Return value: number of bytes read and stored into buf.
#### **socket.readline**()
接收一行数据,遇换行符结束,并返回接收数据的对象 。
#### **socket.write**(buf)
将字节类型数据写入套接字,并返回写入成功的数据大小。
### 示例
#### TCP Server example
```
>>> 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)
b'rt-thread\r'
>>> s.close()
```
#### TCP Client example
```
>>> import usocket
>>> s = usocket.socket(usocket.AF_INET,usocket.SOCK_STREAM)
>>> s.connect(("192.168.10.110",6000))
>>> s.send("micropython")
11
>>> s.close()
```
`connect to a web site example`:
```
s = socket.socket()
s.connect(socket.getaddrinfo('www.micropython.org', 80)[0][-1])
```
更多的内容可参考 [usocket](http://docs.micropython.org/en/latest/pyboard/library/usocket.html) 。
@@ -0,0 +1,28 @@
# ussl SSL/TLS 模块
This module implements a subset of the corresponding* [`CPython`](http://docs.micropython.org/en/latest/reference/glossary.html#term-cpython) *module, as described below. For more information, refer to the original CPython documentation: [`ssl`](https://docs.python.org/3.5/library/ssl.html#module-ssl).
This module provides access to Transport Layer Security (previously and widely known as “Secure Sockets Layer”) encryption and peer authentication facilities for network sockets, both client-side and server-side.
## 功能函数
- `ussl.wrap_socket`(sock, server_side=False, key=None, cert=None)
Takes a [`stream`](http://docs.micropython.org/en/latest/reference/glossary.html#term-stream) *sock* (usually usocket.socket instance of `SOCK_STREAM` type), and returns an instance of ssl.SSLSocket, which wraps the underlying stream in an SSL context. Returned object has the usual [`stream`](http://docs.micropython.org/en/latest/reference/glossary.html#term-stream) interface methods like `read()`, `write()`, etc. In MicroPython, the returned object does not expose socket interface and methods like `recv()`, `send()`. In particular, a server-side SSL socket should be created from a normal socket returned from[`accept()`](http://docs.micropython.org/en/latest/library/usocket.html#usocket.socket.accept) on a non-SSL listening server socket. Depending on the underlying module implementation in a particular [`MicroPython port`](http://docs.micropython.org/en/latest/reference/glossary.html#term-micropython-port), some or all keyword arguments above may be not supported.
Warning: Some implementations of `ussl` module do NOT validate server certificates, which makes an SSL connection established prone to man-in-the-middle attacks.
## 异常类型
- `ssl.SSLError`
This exception does NOT exist. Instead its base class, OSError, is used.
## 常量
- `ussl.CERT_NONE`
- `ussl.CERT_OPTIONAL`
- `ussl.CERT_REQUIRED`
- Supported values for **cert_reqs** parameter.
@@ -0,0 +1,82 @@
## **ustruct** 打包和解包原始数据类型
**ustruct** 模块在 Python 值和以 Python 字节对象表示的 C 结构之间执行转换。
- 支持 size/byte 的前缀: @, <, >, !.
- 支持的格式代码: b, B, h, H, i, I, l, L, q, Q, s, P, f, d (最后2个需要支持浮点数).
### 函数
#### **ustruct.calcsize**(fmt)
返回存放某一类型数据 fmt 需要的字节数。
```
fmt:数据类型
b — 字节型
B — 无符号字节型
h — 短整型
H — 无符号短整型
i — 整型
I — 无符号整型
l — 整型
L — 无符号整型
q — 长整型
Q — 无符号长整型
f — 浮点型
d — 双精度浮点型
P — 无符号型
```
示例:
```python
>>> print(struct.calcsize("i"))
4
>>> print(struct.calcsize("B"))
1
```
#### **ustruct.pack**(fmt, v1, v2, ...)
按照格式字符串 fmt 打包参数 v1, v2, ... 。返回值是参数打包后的字节对象。
```
fmt:同上
```
示例:
```python
>>> struct.pack("ii", 3, 2)
b'\x03\x00\x00\x00\x02\x00\x00\x00'
```
#### **ustruct.unpack**(fmt, data)
从 fmt 中解包数据。返回值是解包后参数的元组。
```
data:要解压的字节对象
```
示例:
```python
>>> buf = struct.pack("bb", 1, 2)
>>> print(buf)
b'\x01\x02'
>>> print(struct.unpack("bb", buf))
(1, 2)
```
#### **ustruct.pack_into**(fmt, buffer, offset, v1, v2, ...)
按照格式字符串 fmt 压缩参数 v1, v2, ... 到缓冲区 buffer,开始位置是 offset。当offset 为负数时,从缓冲区末尾开始计数。
#### **ustruct.unpack_from**(fmt, data, offset=0)
以 fmt 作为规则从 data 的 offset 位置开始解包数据,如果 offset 是负数就是从缓冲区末尾开始计算。返回值是解包后的参数元组。
```python
>>> buf = struct.pack("bb", 1, 2)
>>> print(struct.unpack("bb", buf))
(1, 2)
>>> print(struct.unpack_from("b", buf, 1))
(2,)
```
更多的内容可参考 [ustruct](http://docs.micropython.org/en/latest/pyboard/library/ustruct.html) 。
@@ -0,0 +1,107 @@
## **utime** 时间相关函数
**utime** 模块提供获取当前时间和日期、测量时间间隔和延迟的功能。
**初始时刻**: `Unix` 使用 `POSIX` 系统标准,从 1970-01-01 00:00:00 `UTC` 开始。
嵌入式程序从 2000-01-01 00:00:00 `UTC` 开始。
**保持实际日历日期/时间**:需要一个实时时钟 `(RTC)`。在底层系统 (包括一些 `RTOS` 中)`RTC` 已经包含在其中。设置时间是通过 `OS/RTOS` 而不是 MicroPython 完成,查询日期/时间也需要通过系统 `API`。对于裸板系统时钟依赖于 ``machine.RTC()`` 对象。设置时间通过 ``machine.RTC().datetime(tuple)`` 函数,并通过下面方式维持:
* 后备电池 (可能是选件、扩展板等)。
* 使用网络时间协议 (需要用户设置)。
* 每次上电时手工设置 (大部分只是在硬复位时需要设置,少部分每次复位都需要设置)。
如果实际时间不是通过系统 / MicroPython RTC 维持,那么下面函数结果可能不是和预期的相同。
### 函数
#### **utime.localtime**([secs])
从初始时间的秒转换为元组: (年, 月, 日, 时, 分, 秒, 星期, ``yearday``) 。如果 ``secs`` 是空或者 ``None``,那么使用当前时间。
- `year ` 年份包括世纪(例如2014)。
- `month` 范围 1-12
- `day` 范围 1-31
- `hour` 范围 0-23
- `minute` 范围 0-59
- `second` 范围 0-59
- `weekday` 范围 0-6 对应周一到周日
- `yearday` 范围 1-366
#### **utime.mktime**()
时间的反函数,它的参数是完整8参数的元组,返回值一个整数自2000年1月1日以来的秒数。
#### **utime.sleep**(seconds)
休眠指定的时间(秒),``Seconds`` 可以是浮点数。注意有些版本的 MicroPython不支持浮点数,为了兼容可以使用 ``sleep_ms()`` 和 ``sleep_us()``函数。
#### **utime.sleep_ms**(ms)
延时指定毫秒,参数不能小于0。
#### **utime.sleep_us**(us)
延时指定微秒,参数不能小于0。
#### **utime.ticks_ms**()
返回不断递增的毫秒计数器,在某些值后会重新计数(未指定)。计数值本身无特定意义,只适合用在``ticks_diff()``。
注: 直接在这些值上执行标准数学运算(+,-)或关系运算符(<,>,>,> =)会导致无效结果。执行数学运算然后传递结果作为参数给`ticks_diff()` 或 ` ticks_add() ` 也将导致函数产生无效结果。
#### **utime.ticks_us**()
和上面 `ticks_ms()` 类似,只是返回微秒。
#### **utime.ticks_cpu**()
与 ``ticks_ms()`` 和 ``ticks_us()`` 类似,具有更高精度 (使用 CPU 时钟),并非每个端口都实现此功能。
#### **utime.ticks_add**(ticks, delta)
给定一个数字作为节拍的偏移值 `delta`,这个数字的值是正数或者负数都可以。
给定一个 `ticks` 节拍值,本函数允许根据节拍值的模算数定义来计算给定节拍值之前或者之后 `delta` 个节拍的节拍值 。
`ticks` 参数必须是 `ticks_ms()`, `ticks_us()`, or `ticks_cpu()` 函数的直接返回值。然而,`delta` 可以是一个任意整数或者是数字表达式。`ticks_add` 函数对计算事件/任务的截至时间很有用。(注意:必须使用 `ticksdiff()` 函数来处理
最后期限)。
代码示例:
```python
## 查找 100ms 之前的节拍值
print(utime.ticks_add(utime.ticks_ms(), -100))
## 计算操作的截止时间然后进行测试
deadline = utime.ticks_add(utime.ticks_ms(), 200)
while utime.ticks_diff(deadline, utime.ticks_ms()) > 0:
do_a_little_of_something()
## 找出本次移植节拍值的最大值
print(utime.ticks_add(0, -1))
```
#### **utime.ticks_diff**(ticks1, ticks2)
计算两次调用 `ticksms()`, `ticks_us()`, 或 `ticks_cpu()`之间的时间。因为这些函数的计数值可能会回绕,所以不能直接相减,需要使用 `ticks_diff()` 函数。“旧” 时间需要在 “新” 时间之前,否则结果无法确定。这个函数不要用在计算很长的时间 (因为 `ticks*()` 函数会回绕,通常周期不是很长)。通常用法是在带超时的轮询事件中调用:
代码示例:
```python
## 等待 GPIO 引脚有效,但是最多等待500微秒
start = time.ticks_us()
while pin.value() == 0:
if time.ticks_diff(time.ticks_us(), start) > 500:
raise TimeoutError
```
#### **utime.time**()
返回从开始时间的秒数(整数),假设 `RTC` 已经按照前面方法设置好。如果 `RTC` 没有设置,函数将返回参考点开始计算的秒数 (对于 `RTC` 没有后备电池的板子,上电或复位后的情况)。如果你开发便携版的 MicroPython 应用程序,你不要依赖函数来提供超过秒级的精度。如果需要高精度,使用 `ticks_ms()` 和 `ticks_us()` 函数。如果需要日历时间,使用不带参数的 `localtime()` 是更好选择。
!!! tip "与 CPython 的区别"
在 `CPython` 中,这个函数用浮点数返回从 `Unix` 开始时间(1970-01-01 00:00 `UTC`)的秒数,通常是毫秒级的精度。在 MicroPython 中,只有 `Unix` 版才使用相同开始时间,如果允许浮点精度,将返回亚秒精度。嵌入式硬件通常没有用浮点数表示长时间访问和亚秒精度,所以返回值是整数。一些嵌入式系统硬件不支持 `RTC` 电池供电方式,所以返回的秒数是从最后上电、或相对某个时间、以及特定硬件时间 (如复位)。
### 示例
```
>>> 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
>>> delta
6928
>>> print(utime.ticks_add(utime.ticks_ms(), -100))
1140718
>>> print(utime.ticks_add(0, -1))
1073741823
```
更多内容可参考 [`utime`](http://docs.micropython.org/en/latest/pyboard/library/utime.html#module-utime) 。
@@ -0,0 +1,10 @@
## **uzlib** zlib 解压缩
`uzlib` 模块实现了使用 DEFLATE 算法解压缩二进制数据 (常用的 zlib 库和 gzip 文档)。目前不支持压缩。
### 函数
#### **uzlib.decompress**(data)
返回解压后的 bytes 数据。
更多内容可参考 [uzlib](http://docs.micropython.org/en/latest/pyboard/library/uzlib.html) 。