1、hello micropython

#MicroPython动手做(04)——零基础学MaixPy之基本示例

#程序之一:hello micropython

#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)

sys – 系统特定功能模块(标准库之一)

sys.implementation——包含有关当前Python实现的信息的对象
系统:micropython
固件:V0.5.0

sys.platform——运行 MicroPython 的平台
平台:MaixPy

sys.path——用于搜索导入模块的可变目录列表
路径:['','。','/ flash']

sys.version——实现的 Python 版本, 返回一个字符串
Python版本:3.4.0

sys.stdin——标准输入 
sys.stdout——标准输出


2、查询闪存目录

#MicroPython动手做(04)——零基础学MaixPy之基本示例

#程序之二:查询闪存目录

 

import uos

mount_points = uos.listdir("/")

for fs in mount_points:

    print("------------")

    print(" dir:", fs)

    uos.listdir("/"+fs)

uos – 基本的“操作系统”服务模块(标准库)

uos.ilistdir([dir])

此函数返回一个迭代器,然后生成与列出的目录中的条目对应的元组。如果不传参数,它列出了当前目录,否则它列出了dir给出的目录。

dir: flash

['boot.py', 'main.py', 'freq.conf']

3、JSON编码和解码

#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"])

ujson –编码和解码模块(标准库)

该模块实现了相应 CPython 模块的子集,允许在 Python 对象和 JSON 数据格式之间进行转换。

load

ujson.load(stream)

解析给定的流,将其解释为 JSON 字符串并将数据反序列化为 Python 对象。返回结果对象。解析继续,直到遇到文件结尾。如果未正确形成流中的数据,则会引发 ValueError。

loads

ujson.loads(str)

解析JSON str并返回一个对象。如果字符串格式出错,则引发ValueError。

4、thread多线程

#MicroPython动手做(04)——零基础学MaixPy之基本示例

#程序之四:thread多线程

 

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

_thread多线程支持模块

该模块提供了用于处理多个线程(也称为轻量级进程或任务)的低级原语-多个控件线程共享其全局数据空间。为了进行同步,提供了简单的锁(也称为互斥体或二进制信号量)。该threading模块提供了易于使用的功能,并在此模块之上构建了更高级别的线程API。

_thread.start_new_thread(函数,args [,kwargs ] )

启动一个新线程并返回其标识符。线程使用参数列表args(必须是元组)执行函数 功能。可选的 kwargs参数指定关键字参数的字典。当函数返回时,线程以静默方式退出。当函数以未处理的异常终止时,将打印堆栈跟踪,然后线程退出(但其他线程继续运行)。

5、更新频率演示

#MicroPython动手做(04)——零基础学MaixPy之基本示例

#程序之五:更新频率演示

 

from Maix import freq

cpu_freq, kpu_freq = freq.get()

print(cpu_freq, kpu_freq)

freq.set(cpu = 400, pll1=400, kpu_div = 1)

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐