M5StickV参考示例4-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1125|回复: 0

M5StickV参考示例4

[复制链接]
发表于 2020-3-23 21:04 | 显示全部楼层 |阅读模式
1.文件读写
[mw_shl_code=python,true]import sys

filename = "my_file.txt"
f = open(filename, "w")
f.write("Hello M5stickV!")
f.close()
f = open(filename, "r")
content = f.read()
print(content)[/mw_shl_code]


2.json序列
[mw_shl_code=python,true]import ujson
json_str = '''{
    "name": "m5stickV",
    "babies": [
        {
            "name": "m5",
            "birthday": 2.9102,
            "sex": "unstable"
        }
    ]
}'''

obj = ujson.loads(json_str)

print(obj["name"])
print(obj["babies"])[/mw_shl_code]

3.打印文件列表
[mw_shl_code=python,true]import uos
mount_points = uos.listdir("/")
for fs in mount_points:
    print("------------")
    print(" dir:", fs)
    uos.listdir("/" + "flash")[/mw_shl_code]

4.RAM文件读写
[mw_shl_code=python,true]import uos

class RAMFlashDev:
    def __init__(self):
            self.fs_size = 256 * 1024
            self.fs_data = bytearray(256 * 1024)
            self.erase_block = 32 * 1024
            self.log_block_size = 64 * 1024
            self.log_page_size = 4 * 1024

    def read(self, buf, size, addr):
            for i in range(len(buf)):
                buf = self.fs_data[addr + i]

    def write(self, buf, size, addr):
            for i in range(len(buf)):
                self.fs_data[addr + i] = buf

    def erase(self, size, addr):
            for i in range(size):
                self.fs_data[addr + i] = 0xff

blkdev = RAMFlashDev()
vfs = uos.VfsSpiffs(blkdev)
vfs.mkfs(vfs)
uos.mount(vfs,'/ramdisk')

text_str = "hello maixpy"
f = open("/ramdisk/test.txt", "w")
print("write:", text_str)
f.write(text_str)
f.close()

f = open("/ramdisk/test.txt", "r")
text = f.read()
print("read:", text)
f.close()[/mw_shl_code]

5.显示系统信息
[mw_shl_code=python,true]import sys
print("implementation: ", sys.implementation)
print("platform: ", sys.platform)
print("path: ", sys.path)
print("version: ", sys.version)
print("version_info: ", sys.version_info)
print("argv: ", sys.argv)
print("byteorder: ", sys.byteorder)
print("maxsize: ", sys.maxsize)
print("modules: ", sys.modules)[/mw_shl_code]

6.3D-CUBE显示
[mw_shl_code=python,true]import lcd
import image
import math
lcd.init()
lcd.direction(lcd.YX_LRUD)
def mat_mult(A, B):
    rows_A = len(A)
    cols_A = len(A[0])
    rows_B = len(B)
    cols_B = len(B[0])
    if(cols_A != rows_B):
        print("Cannot multiply the two matrices. Incorrect dimensions.")
        return None

    C = [[0.0 for row in range(cols_B)] for col in range(rows_A)]
    for i in range(rows_A):
        for j in range(cols_B):
            for k in range(cols_A):
                C[j] += A[k] * B[k][j]
    return C

def scale(x, y, z):
    m = [
        [ x, 0, 0, 0 ],
        [ 0, y, 0, 0 ],
        [ 0, 0, z, 0 ],
        [ 0, 0, 0, 1 ],
    ]
    return m

def rotate_x(a):
    s = math.sin(a)
    c = math.cos(a)
    m = [
        [ 1, 0, 0, 0 ],
        [ 0, c,-s, 0 ],
        [ 0, s, c, 0 ],
        [ 0, 0, 0, 1 ],
    ]
    return m

def rotate_y(a):
    s = math.sin(a)
    c = math.cos(a)
    m = [
        [ c, 0, s, 0 ],
        [ 0, 1, 0, 0 ],
        [-s, 0, c, 0 ],
        [ 0, 0, 0, 1 ],
    ]
    return m

def rotate_z(a):
    s = math.sin(a)
    c = math.cos(a)
    m = [
        [ c,-s, 0, 0 ],
        [ s, c, 0, 0 ],
        [ 0, 0, 1, 0 ],
        [ 0, 0, 0, 1 ],
    ]
    return m

def rotate(rx, ry, rz):
    m = rotate_z(rz)
    m = mat_mult(rotate_y(ry), m)
    m = mat_mult(rotate_x(rx), m)
    return m

def translate(x, y, z):
    m = [
        [ 1, 0, 0, x ],
        [ 0, 1, 0, y ],
        [ 0, 0, 1, z ],
        [ 0, 0, 0, 1 ],
    ]
    return m

def point(x, y, z):
    m = [ [x], [y], [z], [1] ]
    return m

points_pairs = [
    [point(0, 0, 0), point(0, 1, 0)],
    [point(0, 1, 0), point(1, 1, 0)],
    [point(1, 1, 0), point(1, 0, 0)],
    [point(1, 0, 0), point(0, 0, 0)],

    [point(0, 0, 1), point(0, 1, 1)],
    [point(0, 1, 1), point(1, 1, 1)],
    [point(1, 1, 1), point(1, 0, 1)],
    [point(1, 0, 1), point(0, 0, 1)],

    [point(0, 0, 0), point(0, 0, 1)],
    [point(0, 1, 0), point(0, 1, 1)],
    [point(1, 1, 0), point(1, 1, 1)],
    [point(1, 0, 0), point(1, 0, 1)],
]

colors = [
    [255, 0, 0], [255, 0, 0], [255, 0, 0], [255, 0, 0],
    [0, 255, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0],
    [0, 0, 255], [0, 0, 255], [0, 0, 255], [0, 0, 255],
]

sx, sy, sz = 100, 100, 100
rx, ry, rz = 0, math.pi / 6, math.pi / 6
tx, ty, tz = lcd.width() / 4, lcd.height() / 2, 100

# カウンタ
i = 0

while True:
#增加计数
    i = i + 1
#计算X轴旋转
    rx = i * math.pi / 100
#创建图像
img = image.Image()

#对所有边执行
for point_pair, color in zip(points_pairs, colors):
        m = point_pair[0]
        m = mat_mult(scale(sx, sy, sz), m)
        m = mat_mult(rotate(rx, ry, rz), m)
        m = mat_mult(translate(tx, ty, tz), m)
        m0 = m

        m = point_pair[1]
        m = mat_mult(scale(sx, sy, sz), m)
        m = mat_mult(rotate(rx, ry, rz), m)
        m = mat_mult(translate(tx, ty, tz), m)
        m1 = m

        x0, y0, x1, y1 = int(m0[0][0]), int(m0[1][0]), int(m1[0][0]), int(m1[1][0])
        # 绘制直线
        img.draw_line(x0, y0, x1, y1, color = color, thickness = 2)
    # 屏幕绘制
    lcd.display(img)[/mw_shl_code]

7.矩形检查
[mw_shl_code=python,true]import lcd
import sensor

lcd.init()
lcd.direction(lcd.YX_LRUD)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.run(1)
while True:
    img = sensor.snapshot()
    # 检查矩形
    res = img.find_rects()
    # 对所有结果执行
    for i in res:
        print(i)
        # 描出矩形范围
        img.draw_rectangle(i.x(), i.y(), i.w(), i.h(), color = (255, 0, 0), thickness = 2)
        # 对四个角坐标执行
        for p in i.corners():
            # 画圆
            img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))
        # 对连接四角的边执行
        for j in range(4):
            p1 = i.corners()[j]
            p2 = i.corners()[(j + 1) % 4]
            # 绘制直线
            img.draw_line(p1[0], p1[1], p2[0], p2[1], color = (0, 0, 255))
    # 屏幕显示
    lcd.display(img)[/mw_shl_code]


8.绘制曼德布洛特图形(mandelbrot)
[mw_shl_code=python,true]import lcd
import image

center = (3, 1.5)
iterate_max = 100
colors_max = 100
lcd.init()
lcd.direction(lcd.YX_LRUD)

def iterate_mandelbrot(c, z = 0):
    for n in range(iterate_max + 1):
        z = z * z + c
        if abs(z) > 2:
            return n
    return None

# HSV to RGB
# h = 0.0 ~ 1.0
# s = 0.0 ~ 1.0
# v = 0.0 ~ 1.0
def hsv_to_rgb(h, s, v):
    h = h * 360
    s = s * 255
    v = v * 255

    i = int(h / 60.0)
    mx = v
    mn = v - ((s / 255.0) * v)

    if h is None:
        return(0, 0, 0)
    if i == 0:
        (r1, g1, b1) = (mx, (h / 60.0) * (mx - mn) + mn, mn)
    elif i == 1:
        (r1, g1, b1) = (((120.0 - h) / 60.0) * (mx - mn) + mn, mx, mn)
    elif i == 2:
        (r1, g1, b1) = (mn, mx, ((h - 120.0) / 60.0) * (mx - mn) + mn)
    elif i == 3:
        (r1, g1, b1) = (mn, ((240.0 - h) / 60.0) * (mx - mn) + mn, mx)
    elif i == 4:
        (r1, g1, b1) = (((h - 240.0) / 60.0) * (mx - mn) + mn, mn, mx)
    elif 5 <= i:
        (r1, g1, b1) = (mx, mn, ((360.0 - h) / 60.0) * (mx - mn) + mn)
    return (int(r1), int(g1), int(b1))

#初始化调色板
palette = [0] * colors_max
for i in range(colors_max):
    f = 1 - abs((float(i) / colors_max - 1) ** 15)
    r, g, b = hsv_to_rgb(.66 + f / 3, 1 - f / 2, f)
    palette = (r, g, b)

img = image.Image()

#计算比例
scale = 1.0 / (img.height() / 3)

##绘制曼德布洛特集合
for x in range(img.width()):
    for y in range(img.height()):
        c = complex(x * scale - center[0], y * scale - center[1])
        n = iterate_mandelbrot(c)
        if n is None:
            v = 1
        else:
            v = n / 100.0
        img.set_pixel(x, y, color = palette[int(v * (colors_max - 1))])
lcd.display(img)[/mw_shl_code]


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 08:49 , Processed in 0.117737 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表