M5StickV参考示例4
1.文件读写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)
2.json序列
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"])
3.打印文件列表
import uos
mount_points = uos.listdir("/")
for fs in mount_points:
print("------------")
print(" dir:", fs)
uos.listdir("/" + "flash")
4.RAM文件读写
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
def write(self, buf, size, addr):
for i in range(len(buf)):
self.fs_data = buf
def erase(self, size, addr):
for i in range(size):
self.fs_data = 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()
5.显示系统信息
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)
6.3D-CUBE显示
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)
rows_B = len(B)
cols_B = len(B)
if(cols_A != rows_B):
print("Cannot multiply the two matrices. Incorrect dimensions.")
return None
C = [ 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 += A * B
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 = [ , , , ]
return m
points_pairs = [
,
,
,
,
,
,
,
,
,
,
,
,
]
colors = [
, , , ,
, , , ,
, , , ,
]
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
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
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), int(m0), int(m1), int(m1)
# 绘制直线
img.draw_line(x0, y0, x1, y1, color = color, thickness = 2)
# 屏幕绘制
lcd.display(img)
7.矩形检查
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, p, 5, color = (0, 255, 0))
# 对连接四角的边执行
for j in range(4):
p1 = i.corners()
p2 = i.corners()[(j + 1) % 4]
# 绘制直线
img.draw_line(p1, p1, p2, p2, color = (0, 0, 255))
# 屏幕显示
lcd.display(img)
8.绘制曼德布洛特图形(mandelbrot)
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 = * 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, y * scale - center)
n = iterate_mandelbrot(c)
if n is None:
v = 1
else:
v = n / 100.0
img.set_pixel(x, y, color = palette)
lcd.display(img)
页:
[1]