此程序为MaixPy之基本示例。
# Hello World Example
#
# Welcome to the MaixPy IDE!
# 1. Conenct board to computer
# 2. Select board at the top of MaixPy IDE: `tools->Select Board`
# 3. Click the connect buttion below to connect board
# 4. Click on the green run arrow button below to run the script!
#MicroPython动手做(04)——零基础学MaixPy之基本示例
#程序之一:hello micropython
import sys
for i in range(0, 2):
print("hello micropython")
print("hello ", end="micropython\n")
print("implementation:", sys.implementation)
print("platform:", sys.platform)
print("path:", sys.path)
print("Python version:", sys.version)
print("please input string, end with Enter")
r = sys.stdin.readline()
w_len = sys.stdout.write(r)
运行效果:
>>> hello micropython
hello micropython
hello micropython
hello micropython
implementation: (name='micropython', version=(0, 6, 2))
platform: MaixPy
path: ['', '.', '/flash']
Python version: 3.4.0
please input string, end with Enter
sys – 系统特定功能模块(标准库之一): - sys.implementation:包含有关当前Python实现的信息的对象 - sys.platform:运行MicroPython 的平台 - sys.path:用于搜索导入模块的可变目录列表 - sys.version:实现的 Python 版本, 返回一个字符串 - sys.stdin:标准输入 - sys.stdout:标准输出
在Python中,sys.stdin是sys模块的一部分,用于处理标准输入流(stdin)。 由于K210板子没有类似电脑键盘默认的输入设备,因此程序最后无法测试给系统输入一个字符串,并输出该字符串 。我们可以在pycharm里运行以下代码测试基本输入输出效果。
import sys
def main():
print("请输入您的尊姓大名:")
# 从标准输入读取一行,并移除行尾的换行符
user_input = sys.stdin.readline().strip()
print(f"您好:{user_input}")
if __name__ == "__main__":
main()
输出效果:
请输入您的尊姓大名:
齐天大圣
您好:齐天大圣
# Hello World Example
#
# Welcome to the MaixPy IDE!
# 1. Conenct board to computer
# 2. Select board at the top of MaixPy IDE: `tools->Select Board`
# 3. Click the connect buttion below to connect board
# 4. Click on the green run arrow button below to run the script!
#MicroPython动手做(04)——零基础学MaixPy之基本示例
#程序之二:查询闪存目录
import uos
mount_points = uos.listdir("/")
for fs in mount_points:
print("------------")
print(" dir:", fs)
uos.listdir("/"+fs)
运行效果:
------------
dir: flash
------------
dir: sd
MicroPython v0.6.2-84-g8fcd84a58 on 2022-08-26; Sipeed_M1 with kendryte-k210
Type "help()" for more information.
uos – 基本的"操作系统"服务模块(标准库): - uos.ilistdir([dir]):此函数返回一个迭代器,然后生成与列出的目录中的条目对应的元组。如果不传参数,它列出了当前目录,否则它列出了dir给出的目录。
# Hello World Example
#
# Welcome to the MaixPy IDE!
# 1. Conenct board to computer
# 2. Select board at the top of MaixPy IDE: `tools->Select Board`
# 3. Click the connect buttion below to connect board
# 4. Click on the green run arrow button below to run the script!
#MicroPython动手做(04)——零基础学MaixPy之基本示例
#程序之三:JSON编码和解码
import ujson
json_str = '''{
"name": "sipeed",
"babies": [
{
"name": "maixpy",
"birthday": 2.9102,
"sex": "unstable"
}
]
}'''
obj = ujson.loads(json_str)
print(obj["name"])
print(obj["babies"])
运行效果:
>>> sipeed
[{'birthday': 2.9102, 'name': 'maixpy', 'sex': 'unstable'}]
MicroPython v0.6.2-84-g8fcd84a58 on 2022-08-26; Sipeed_M1 with kendryte-k210
Type "help()" for more information
ujson –编码和解码模块(标准库):
该模块实现了相应CPython模块的子集,允许在Python对象和JSON数据格式之间进行转换。
# Hello World Example
#
# Welcome to the MaixPy IDE!
# 1. Conenct board to computer
# 2. Select board at the top of MaixPy IDE: `tools->Select Board`
# 3. Click the connect buttion below to connect board
# 4. Click on the green run arrow button below to run the script!
#程序之七:定时3秒后打印信息
from machine import Timer
def on_timer(timer):
print("time up:",timer)
print("param:",timer.callback_arg())
tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_ONE_SHOT, period=3000, callback=on_timer, arg=on_timer)
print("period:",tim.period())
machine.Timer函数,硬件定时器,可以用来定时触发任务或者处理任务,设定时间到了后可以触发中断(调用回调函数),精度比软件定时器高。 需要注意的是,定时器在不同的硬件中可能会有不同的表现。 MicroPython 的 Timer 类定义了在给定时间段内(或在一段延迟后执行一次回调)执行回调的基本操作,并允许特定的硬件上定义更多的非标准行为(因此不能移植到其他板)。
共有3个定时器,每个定时器有4个通道可以使用。参数详解如下:
注意:回调函数是在中断中调用的,所以在回调函数中请不要占用太长时间以及做动态分配开关中断等动作。 arg: 希望传给回调函数的参数,作为回调函数的第二个参数 start: 是否在对象构建成功后立即开始定时器, True:立即开始, False:不立即开启,需要调用start()函数来启动定时器 priority: 硬件定时器中断优先级, 与特定的CPU相关, 在K210中,取值范围是[1,7], 值越小优先级越高 div: 硬件定时器分频器,取值范围[0,255], 默认为0, clk_timer(定时器时钟频率) = clk_pll0(锁相环0频率)/2^(div+1),clk_timer*period(unit:s) 应该 < 2^32 并且 >=1
# Hello World Example
#
# Welcome to the MaixPy IDE!
# 1. Conenct board to computer
# 2. Select board at the top of MaixPy IDE: `tools->Select Board`
# 3. Click the connect buttion below to connect board
# 4. Click on the green run arrow button below to run the script!
help('modules')
运行效果:
KPU gc os uheapq
Maix hashlib random uio
__main__ heapq re ujson
_boot ide_debug sensor uos
_webrepl image socket urandom
array json struct ure
audio lcd sys usocket
binascii machine time ustruct
board math ubinascii utime
builtins math ucollections utimeq
cmath micropython ucryptolib uzlib
collections modules uctypes zlib
errno modules uerrno
fpioa_manager network uhashlib
Plus any modules on the filesystem
MicroPython v0.6.2-84-g8fcd84a58 on 2022-08-26; Sipeed_M1 with kendryte-k210
如果没有_thread模块,说明安装了精简版的maxipy固件系统。
#【AI】运用MaixPy平台的Maixduino几个小项目
# 项目之六:thread多线程
# 1. 将板子连接到计算机
# 2. 在 MaixPy IDE 顶部选择板:`Maixduino`
# 3. 点击下方的连接按钮连接板子
# 4. 点击下方绿色运行箭头按钮运行脚本!
import _thread
import time
def func(name):
while 1:
print("hello {}".format(name))
time.sleep(1)
_thread.start_new_thread(func,("1",))
_thread.start_new_thread(func,("2",))
while 1:
pass