Day3 实战演练——桌面迷你番茄钟
目录
- Day3 实战演练——桌面迷你番茄钟
- 1. 选择、准备元件、收集资料
- 2. 硬件搭建
- 3.编写代码
- Day0 创想启程——课程与项目预览
- Day1 工具箱构建——开发环境的构建
- Day2 探秘微控制器——单片机与MicroPython初步
- Day3 实战演练——桌面迷你番茄钟
- Day4 迈向高手之路——进一步学习!
今天,我们开始制作这个小项目——桌面迷你番茄钟
1. 选择、准备元件、收集资料
选择元件,要根据我们的功能和实现的目的来选择,在必要的时候,还要考虑开发的难度与元件的价格。
本项目是桌面番茄钟,那功能就需要一个屏幕与一个主控板,还需要一个电池去供电。所以,我们需要先列出我们项目需要和不需要的功能,依次来选择元器件。
功能 | 必要性 | |
---|---|---|
电池 | 需要 | 排除原版树莓派PICO、添加锂电池、RP2040-PLUS开发板 |
无线 | 不需要 | 排除ESP32 |
屏幕显示 | 需要 | Pico-OLED-1.3 1.3英寸OLED扩展板 |
按键 | 需要 | Pico-OLED-1.3 1.3英寸OLED扩展板 |
… | … |
根据此表,我们选择了以下元器件:
- 微雪电子 RP2040-PLUS 开发板
- 微雪电子 Pico-OLED-1.3 1.3英寸OLED扩展板
- 3.7V 800mAh 902040电池
由于我们后续需要使用到微雪电子的元器件,所以我们也需要收集相关资料
- 微雪电子 RP2040-PLUS 开发板
- 官方文档 https://www.waveshare.net/wiki/RP2040-Plus
- MicroPython RP2 快速参考 http://micropython.com.cn/en/latet/rp2/quickref.html
- 微雪电子 Pico-OLED-1.3 1.3英寸OLED扩展板
- 官方文档 https://www.waveshare.net/wiki/Pico-OLED-1.3
- MicroPython SH1107相关驱动程序
- MicroPython
- 官方文档 http://micropython.com.cn/en/latet/index.html
- …
2. 硬件搭建
接下来,我们可以参考文档的图片与文字资料完成硬件搭建。
3.编写代码
我个人首先会测试元器件是否正常,然后编写基本的驱动代码。
在正式开始编写代码之前,我们通常需要借助工具或者在大脑中建立一个基本的流程图,通过一个流程图,可以帮助我们更好地编写代码。
开始编写代码,参照流程图和伪代码,调试,完成功能。下面是完整代码:
from machine import Pin, SPI
import sh1107
import gc
import sys
import time
import framebuf
import arrayspi1 = SPI(1, baudrate=1_000_000, sck=Pin(10), mosi=Pin(11), miso=None)
display = sh1107.SH1107_SPI(128, 64, spi1, Pin(8), Pin(12), Pin(9), rotate=0)
display.sleep(False)key0 = Pin(15,Pin.IN,Pin.PULL_UP)
key1 = Pin(17,Pin.IN,Pin.PULL_UP)
starttime=time.time()
tomato=0
tomatimes=0
tomac=300
while True:display.fill(0)display.fill_rect(5, 5, 60, 54, 15)display.text(f"{(round(tomatimes/60))}",12, 29, 0)display.text(f"min",12, 41, 0)if key0.value() == 0:while key0.value() == 0:passif tomac!=300:tomatimes = tomatimes+(1500-tomato)starttime=time.time()if tomac==1500:tomac=300else: tomac=1500tomato=tomacelse:if tomato >= 0:tomato=tomac-(time.time()-starttime)else:tomato=tomac-(time.time()-starttime)display.fill(15)display.text(f"OVER",40, 20, 0)display.text(f"{round((-tomato),1)}",40, 32, 0)display.show()tomtext = f"{round(tomato/60,1)}"tomx = round((66-len(tomtext)*8)/2)+66display.text(tomtext,tomx, 20, 255)display.text("min", 86, 32, 255)display.show()display.poweroff()