内容介绍
内容介绍
任务要求:
- 自行设计一个电子沙漏的物理结构,将提供的两个LED灯板和RP2040 Game Kit固定
- 通过RP2040 Game Kit上的按键和LCD屏幕设定沙漏一个周期的时间,实现如上图中LED的效果
- 通过RP2040 Game Kit上的姿态传感器来感知沙漏的方向变化,并开始新的沙漏操作
一:环境配置:
1、thonny:
官方推荐的开发软件
2、硬禾学堂树莓派pico平台:
融合了rp2040游戏机
二.程序实现:
按键初始化
buttonB = Pin(5,Pin.IN, Pin.PULL_UP) #B
buttonA = Pin(6,Pin.IN, Pin.PULL_UP) #A
buttonValueA=1
buttonValueB=1
lcd初始化
st7789_res = 0
st7789_dc = 1
disp_width = 240
disp_height = 240
CENTER_Y = int(disp_width/2)
CENTER_X = int(disp_height/2)
print(uos.uname())
spi_sck=machine.Pin(2)
spi_tx=machine.Pin(3)
spi0=machine.SPI(0,baudrate=4000000, phase=1, polarity=1, sck=spi_sck, mosi=spi_tx)
#
print(spi0)
display = st7789.ST7789(spi0, disp_width, disp_width,
reset=machine.Pin(st7789_res, machine.Pin.OUT),
dc=machine.Pin(st7789_dc, machine.Pin.OUT),
xstart=0, ystart=0, rotation=0)
陀螺仪和灯板初始化
spi = SPI(1, 5000_000, sck=Pin(26), mosi=Pin(27))
rclk = Pin(22, Pin.OUT)
sr = SR(spi, rclk, 2) # chain of 2 shift registers
buffer1=bytearray(1) #
buffer0=bytearray(1) #
buffer=bytearray(8*5) #
buffer_clear=bytearray(1)
fbuf = framebuf.FrameBuffer(buffer,8*5,8,framebuf.MONO_VLSB)
def show(buf):
for y in range(8): #每循环控制所有灯板同一列
buffer1[0]=0x80 #选中行
buffer1[0] = buffer1[0]>> y
for i in range (5): #每循环控制一个灯板
buffer0[0] = buf[(5-i)*8-1-y] #选中列
spi.write(buffer0)
spi.write(buffer1)
sr.latch()
a = 0
m = 0
buffer_clear[0]=0X00
#陀螺仪
i2c1 = I2C(1, scl=Pin(pin_cfg.i2c1_scl), sda=Pin(pin_cfg.i2c1_sda))
acc = MMA7660(i2c1)
acc.on(True)
d = bytearray(3)
r = [0 for x in range(3)]
def twos_compliment(n, nbits):
sign_bit = 1 << nbits - 1
sign = 1 if n & sign_bit == 0 else -1
val = n & ~sign_bit if sign > 0 else sign * ((sign_bit << 1) - n)
return val
def thumb_filter(a):
return a / 23.0 * 32.0
#陀螺仪初始化结束
计时器中断初始化用作陀螺仪识别
tim = Timer()
def tick(timer):
acc.getSample(d)
for i in range(3):
r[i] = twos_compliment(d[i], 6)
print(r[1])
tim.init(period=1000, mode=Timer.PERIODIC, callback=tick)
主程序循环灯板
while buttonValueB==1 :
buttonValueA = buttonA.value()
buttonValueB = buttonB.value()
display.text(font1,"CHOOSE 7 s", 10, 55, WHITE,BLACK)
mode = 0
if buttonValueA==0:
display.text(font1,"CHOOSE 14 s", 10, 55, WHITE,BLACK)
buttonValueB=0
mode =1
#print(str(buttonValueA) + " -- "+ str(buttonValueB))
while True:
if mode == 0:#7s
if r[1] < 0 :
fbuf.fill_rect(9, 0, 7, 8, 1)# fbuf.fill_rect(1, 0, 7, 8, 1)
display.text(font1," 7 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 0, 6, 8, 1)
fbuf.fill_rect(7, 0, 1, 8, 1)
display.text(font1," 6 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 0, 5, 8, 1)
fbuf.fill_rect(6, 0, 2, 8, 1)
display.text(font1," 5 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 0, 4, 8, 1)
fbuf.fill_rect(5, 0, 3, 8, 1)
display.text(font1," 4 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 0, 3, 8, 1)
fbuf.fill_rect(4, 0, 4, 8, 1)
display.text(font1," 3 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 0, 2, 8, 1)
fbuf.fill_rect(3, 0, 5, 8, 1)
display.text(font1," 2 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 0, 1, 8, 1)
fbuf.fill_rect(2, 0, 6, 8, 1)
display.text(font1," 1 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 0, 1, 8, 0)
fbuf.fill_rect(1, 0, 7, 8, 1)
display.text(font1," 0 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 0, 1, 8, 0)
fbuf.fill_rect(1, 0, 7, 8, 1)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
while r[1] < 0 :
fbuf.fill_rect(9, 0, 1, 8, 0)
fbuf.fill_rect(1, 0, 7, 8, 1)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
elif r[1]>0 :
fbuf.fill_rect(1, 0, 7, 8, 1)
display.text(font1," 0 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(2, 0, 6, 8, 1)
fbuf.fill_rect(9, 0, 1, 8, 1)
display.text(font1," 1 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(3, 0, 5, 8, 1)
fbuf.fill_rect(9, 0, 2, 8, 1)
display.text(font1," 2 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(4, 0, 4, 8, 1)
fbuf.fill_rect(9, 0, 3, 8, 1)
display.text(font1," 3 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(5, 0, 3, 8, 1)
fbuf.fill_rect(9, 0, 4, 8, 1)
display.text(font1," 4 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(6, 0, 2, 8, 1)
fbuf.fill_rect(9, 0, 5, 8, 1)
display.text(font1," 5 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(7, 0, 1, 8, 1)
fbuf.fill_rect(9, 0, 6, 8, 1)
display.text(font1," 6 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(8, 0, 1, 8, 0)
fbuf.fill_rect(9, 0, 7, 8, 1)
display.text(font1," 7 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(15, 0, 1, 8, 0)
fbuf.fill_rect(9, 0, 7, 8, 1)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
while r[1] > 0 :
fbuf.fill_rect(15, 0, 1, 8, 0)
fbuf.fill_rect(9, 0, 7, 8, 1)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
if mode == 1:#14s
if r[1] < 0 :
fbuf.fill_rect(9, 0, 7, 8, 1)# fbuf.fill_rect(1, 0, 7, 8, 1)
display.text(font1,"14 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(15, 4, 1, 4, 1)
fbuf.fill_rect(9, 0, 6, 8, 1)
fbuf.fill_rect(7, 0, 1, 4, 1)
display.text(font1,"13 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 0, 6, 8, 1)
fbuf.fill_rect(7, 0, 1, 8, 1)
display.text(font1,"12 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(14, 4, 1, 4, 1)
fbuf.fill_rect(9, 0, 5, 8, 1)
fbuf.fill_rect(7, 0, 1, 4, 1)
fbuf.fill_rect(6, 0, 1, 8, 1)
display.text(font1,"11 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 0, 5, 8, 1)
fbuf.fill_rect(6, 0, 2, 8, 1)
display.text(font1,"10 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(13, 4, 1, 4, 1)
fbuf.fill_rect(9, 0, 4, 8, 1)
fbuf.fill_rect(7, 0, 1, 4, 1)
fbuf.fill_rect(5, 0, 2, 8, 1)
display.text(font1," 9 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 0, 4, 8, 1)
fbuf.fill_rect(5, 0, 3, 8, 1)
display.text(font1," 8 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(12, 4, 1, 4, 1)
fbuf.fill_rect(9, 0, 3, 8, 1)
fbuf.fill_rect(7, 0, 1, 4, 1)
fbuf.fill_rect(4, 0, 3, 8, 1)
display.text(font1," 7 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 0, 3, 8, 1)
fbuf.fill_rect(4, 0, 4, 8, 1)
display.text(font1," 6 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(11, 4, 1, 4, 1)
fbuf.fill_rect(9, 0, 2, 8, 1)
fbuf.fill_rect(7, 0, 1, 4, 1)
fbuf.fill_rect(3, 0, 4, 8, 1)
display.text(font1," 5 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 0, 2, 8, 1)
fbuf.fill_rect(3, 0, 5, 8, 1)
display.text(font1," 4 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(10, 4, 1, 4, 1)
fbuf.fill_rect(9, 0, 1, 8, 1)
fbuf.fill_rect(7, 0, 1, 4, 1)
fbuf.fill_rect(2, 0, 5, 8, 1)
display.text(font1," 3 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 0, 1, 8, 1)
fbuf.fill_rect(2, 0, 6, 8, 1)
display.text(font1," 2 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 4, 1, 4, 1)
fbuf.fill_rect(9, 0, 0, 8, 1)
fbuf.fill_rect(7, 0, 1, 4, 1)
fbuf.fill_rect(1, 0, 6, 8, 1)
display.text(font1," 1 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 0, 1, 8, 0)
fbuf.fill_rect(1, 0, 7, 8, 1)
display.text(font1," 0 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 0, 1, 8, 0)
fbuf.fill_rect(1, 0, 7, 8, 1)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
while r[1] < 0 :
fbuf.fill_rect(9, 0, 1, 8, 0)
fbuf.fill_rect(1, 0, 7, 8, 1)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
elif r[1]>0 :
fbuf.fill_rect(1, 0, 7, 8, 1)
display.text(font1," 0 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(9, 4, 1, 4, 1)
fbuf.fill_rect(2, 0, 6, 8, 1)
fbuf.fill_rect(1, 0, 1, 4, 1)
display.text(font1," 1 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(2, 0, 6, 8, 1)
fbuf.fill_rect(9, 0, 1, 8, 1)
display.text(font1," 2 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(10, 0, 1, 8, 1)
fbuf.fill_rect(9, 4, 1, 4, 1)
fbuf.fill_rect(3, 0, 5, 8, 1)
fbuf.fill_rect(2, 0, 1, 4, 1)
display.text(font1," 3 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(3, 0, 5, 8, 1)
fbuf.fill_rect(9, 0, 2, 8, 1)
display.text(font1," 4 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(10, 0, 2, 8, 1)
fbuf.fill_rect(9, 4, 1, 4, 1)
fbuf.fill_rect(4, 0, 4, 8, 1)
fbuf.fill_rect(3, 0, 1, 4, 1)
display.text(font1," 5 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(4, 0, 4, 8, 1)
fbuf.fill_rect(9, 0, 3, 8, 1)
display.text(font1," 6 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(10, 0, 3, 8, 1)
fbuf.fill_rect(9, 4, 1, 4, 1)
fbuf.fill_rect(5, 0, 3, 8, 1)
fbuf.fill_rect(4, 0, 1, 4, 1)
display.text(font1," 7 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(5, 0, 3, 8, 1)
fbuf.fill_rect(9, 0, 4, 8, 1)
display.text(font1," 8 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(10, 0, 4, 8, 1)
fbuf.fill_rect(9, 4, 1, 4, 1)
fbuf.fill_rect(6, 0, 2, 8, 1)
fbuf.fill_rect(5, 0, 1, 4, 1)
display.text(font1," 9 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(6, 0, 2, 8, 1)
fbuf.fill_rect(9, 0, 5, 8, 1)
display.text(font1,"10 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(10, 0, 5, 8, 1)
fbuf.fill_rect(9, 4, 1, 4, 1)
fbuf.fill_rect(7, 0, 1, 8, 1)
fbuf.fill_rect(6, 0, 1, 4, 1)
display.text(font1,"11 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(7, 0, 1, 8, 1)
fbuf.fill_rect(9, 0, 6, 8, 1)
display.text(font1,"12 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(10, 0, 6, 8, 1)
fbuf.fill_rect(9, 4, 1, 4, 1)
fbuf.fill_rect(7, 0, 1, 4, 1)
display.text(font1,"13 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(8, 0, 1, 8, 0)
fbuf.fill_rect(9, 0, 7, 8, 1)
display.text(font1,"14 s", 150, 145, WHITE,BLACK)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
fbuf.fill_rect(15, 0, 1, 8, 0)
fbuf.fill_rect(9, 0, 7, 8, 1)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
while r[1] > 0 :
fbuf.fill_rect(15, 0, 1, 8, 0)
fbuf.fill_rect(9, 0, 7, 8, 1)
for i in range(370):
show(buffer)
for i in range (5*8):
buffer[i]=0x00
底层import则是根据电子森林提供的example里的库添加,直接调用即可,十分方便
三.效果展示
可以通过按键A和B实现模式选择,按下A是7秒定时,按下B是14秒定时,并且时时确认当前陀螺仪的数值,判断沙漏在什么状态。
主要在于灯板的控制计算,实现沙漏状态的显示
详情见视频
四.感悟
首先很感谢硬禾学堂,能够提供一个平台,能够在空余时间提高自己开发的能力。我作为一名准大三学生,第一次接触到mircopython,可以说有很大的新鲜感,同时,也学习到了更多的知识。同时我也期待未来还能够通过电子森林的寒暑假练习等活动学习更多的内容。感谢电子森林能够提供这个平台。希望未来电子森林能够有更多好玩有趣的项目。
在进行项目的过程中,首先遇到的问题肯定就是没有接触过mircooython。但是很感谢电子森林提供了直播视频讲解,还有相关文档。以下是我借鉴过文档连接
听说可以免费玩游戏机?还附有一系列教程?小六老师带你揭秘_哔哩哔哩_bilibili
https://www.eetree.cn/wiki/pico_micropython
Search — MicroPython 1.19.1 documentation
并且借鉴以前同学的项目,参考他们是如何实现陀螺仪的使用,灯板的控制,lcd屏幕的显示,最后自己一锅杂烩,就成功制作了电子沙漏
团队介绍
韩威(myself)
评论
0 / 100
查看更多
猜你喜欢