我们使用一些必要的Cookie来确保网站正常运行。

我们使用可选的 Cookie,具体详情请参阅我们的Cookie政策,用于记住您的设置并了解您如何使用我们的网站。

使用Galactic Unicorn制作一个巨大的番茄钟

这个#树莓派星期一我们为你准备了一个来自最新一期的优秀教程。用这个超大尺寸的计时器来提高工作效率。

pomodoro timer galactic unicorn

番茄工作法是一种简单的工作与奖励系统:工作25分钟,休息5分钟。每完成四个工作块后,可以休息20分钟。就是这样。但是,如果你使用手机上的计时器来跟踪这些时间段,很容易错过休息——或者因为玩手机而完全分心,导致什么也做不成。在这个教程中,我们将通过使用令人惊艳的Galactic Unicorn来解决这两个问题,它能自动切换工作和休息时段。按下背面的A键开始计时,C键手动切换工作和休息状态,D键则完全结束周期。

01. 设置环境

我们的代码前四行导入了例行程序所需的库。然后我们创建了一个帧缓冲区,称为我们将使用A、C和D按钮来控制番茄钟 pomo.pybuffer, in which we can temporarily stage any graphics we want to send to the display before committing them to the LEDs. We then declare the variable gu, which represents the Galactic Unicorn in our routine, and store its width in the variable displaywidth. We’ll use that width to calculate the panel’s midpoint when we want to centralise text later.

We’ll use buttons A, C, and D to control the Pomodoro timer
02. 定义三个例程

我们将大部分代码拆分为三个函数:

03. cycle() 函数cycle(), fill(), and empty(). The cycle() function manages the transition between fill() and empty() and keeps track of whether we’re in a Pomodoro work cycle or rest cycle. The fill() function handles wiping and then illuminating every pixel on the display in a subtle red-biased gradient for a work cycle, and a green-biased gradient for a rest cycle. The empty() function extinguishes each LED in turn, at a rate determined by the period: work, rest, or extended rest. Let’s take a look at the cycle() function in more detail.

我们的第一个任务是在第19行定义一个标志——称为

背面有一个集成killswitch – and set its condition to True. As you’ll see from line 23, we’ve set the cycle() function to carry on repeating so long as that condition doesn’t change. We’ve also established a counter, called tally, which will increment by one at the end of every work cycle. Why? Because most Pomodoro routines allow for five minutes of rest after a regular work cycle, and 20 after every fourth work cycle. By keeping track of how many work cycles we’ve completed, we can check whether the number stored in tally is a multiple of four when we get to the empty() function.

There’s an integrated Raspberry Pi Pico W around the back that runs our code
运行我们的代码树莓派 Pico W04. 解释发生的情况

第24到30行告诉用户正在发生的事情。我们将笔颜色设置为亮粉色,之前已将该值分配给变量

……暂停一秒钟,这应该足够阅读显示屏内容,然后再调用fg (for foreground) on line 12, then measure the width of our message, which happens to be “Go get ’em”. We subtract the message length from the width of the Galaxy Unicorn display and divide the result by two. The result of this calculation gives us the position of the left-hand margin necessary for centralising the text on the display. We then use:

utime.sleep(1)

05. 清除显示屏fill() function.

在调用

……但是一次点亮一个像素,并在每个LED之间稍作停顿,视觉上会更有趣。fill(), we pass it the variable ‘type’. This variable is used throughout the code, and will always be set to either ‘work’ or ‘rest’. It’s used to define both the colours used to illuminate the LEDs (in the fill() function), and the interval between each LED blinking out (in the empty() routine). The fill() function’s first job is to wipe away the instructions we just gave the user, so it first switches to the black pen, which we assigned to the value bg (for background) on line 13, then works through each of the 53 columns and 11 rows, turning off each pixel in turn. We could have blanked the whole screen at once using the command:

buffer.clear()

由于板载有Raspberry Pi Pico W,因此无需焊接任何引脚

As Raspberry Pi Pico W is on board, there’s no need to solder any headers
06. 彩虹无处不在

06. 彩虹无处不在

当所有灯光熄灭后,我们再次点亮它们。请注意,在45到47行中,我们将红色、绿色和蓝色的值设置为最大(每个255)。如果我们使用这些值来点亮LED,那么板上的每一盏灯都会是白色的。这会很无聊。相反,我们希望引入彩虹或渐变效果,所以在60行和62行,如果是在工作周期内,我们通过将绿色的最大强度除以53(LED列数),然后乘以列号来减少绿色的强度;如果是在休息周期,则减少红色的强度,从而实现渐变效果。同样地,我们在每点亮一个LED之间短暂暂停,纯粹是为了效果。

07. 检查紧急停止开关

随着每一个LED被点亮,我们回到了cycle() function, which immediately calls the empty() function in a way that will pipe any value returned by that function to our killswitch variable:

killswitch = empty(type, tally)

这样一来,我们可以检查killswitch was activated while the empty() function was running. Notice that, at the same time, we’re passing two variables to empty(): type and tally. That’s so empty() knows how long to leave between each LED going out, and whether we’ve earned ourselves a longer rest period.

08. 注意间隔

从78行到81行有效地设置了每个LED熄灭之间的延迟时间分别为2572毫秒(2.572秒)和515毫秒(0.515秒),分别对应工作周期和休息周期。然后我们测试存储在中的数字是否tally variable is a multiple of four on line 83, and, if it is, we extend the gap between each LED going out in the upcoming rest period to 2058 ms (2.058 seconds). Multiply each of these timings by 583, which is the total number of LEDs on the board, and you’ll see how long each cycle lasts. A work cycle is 1,499,476 ms (1499.476 seconds, or 24.99 minutes), a regular rest cycle is 300,245 ms (300.245 seconds, or five minutes), and an extended rest cycle is 1,199,814 ms (1199.814 seconds, or 19.99 minutes).

Although our project doesn’t use it, the Galactic Unicorn has a built-in speaker that could be used to sound an alert between cycles
尽管我们的项目没有使用它,Galactic Unicorn内置了一个扬声器,可以在每个周期之间用来发出警报。

09. 测试按钮按下

在57行到109行之间,我们一次熄灭一个像素,从板子的右下角开始,最后到达左上角。注意第92行:

for zzz in range(delay):

这里是我们计算的地方。函数并不是在每个工作周期LED之间暂停2058毫秒,而是重复2058次,每次暂停1毫秒(0.001秒),同时检测Galactic Unicorn背面的C或D按钮是否被按下。如果有任何一个被触碰,函数就会结束,我们返回到cycle(). For regular and extended rest cycles, the function repeats 515 and 2058 times respectively.

10. 一切是如何开始的

如果是按钮C让我们回到cycle(), we switch between the work and rest routines (or vice versa) and, as the killswitch flag hasn’t yet been set to False, the loop continues. However, if it was button D, the flag will be set to False, and the loop will terminate. We’ll be returned to the code that called cycle() in the first place, which is the six lines from 113 to 118, at the very end of our script. This puts the integrated Raspberry Pi Pico W into a watchful state, where it’s waiting for someone to press button A. When someone does this, it makes sure the screen is blank, then launches cycle(), and we’re back where we started.

Once we know the size of the display, we can work out the midpoint for centralised text
一旦我们知道显示屏的大小,我们就可以计算出居中文本的中点。

11.测试你的代码

要将代码复制到你的Galactic Unicorn上,请确保电路板已连接到电脑,在Thonny中粘贴代码,然后点击文件,接着选择另存为。在对话框中点击Raspberry Pi Pico,然后用你喜欢的任何名称保存到设备上,比如‘pomo.py’。要在Thonny上但在Galactic Unicorn上运行它,点击Thonny窗口右下角的状态行并选择'MicroPython (Raspberry Pi Pico)',然后点击Thonny工具栏上的播放按钮。

12. 开机时运行

如果你想让你的Galactic Unicorn在不连接电脑的情况下也能运行计时器,请确保你将代码保存为main.py,直接保存在设备本身上。由于设备上已经有一个名为main.py的程序,Thonny会询问你是否确定要覆盖它。点击是(你可能想要先做一个副本,以防将来想恢复它)。脚本保存后,你可以拔掉你的Galactic Unicorn与电脑的连接,并将其插入普通的USB适配器,在任何你喜欢的地方运行Pomodoro计时器。

2条评论

Deckard avatar

你读过这个吗:https://www.theregister.com/2023/03/17/ken_thompson_is_a_maccie/?td=rt-3a
“……在最近一两个月里,我决定:尽管我在苹果上投入了无数年的时间,我还是决定放弃它,转而使用Linux。特别是Raspbian。”

凯撒的赞美确实是真正的赞美。

Liz Upton avatar

我们确实有!当那篇文章发表时,Eben和我正在度假。我们的对话是这样的:

“……下次我想确保我们有一个预订……”
“Eben!KEN THOMPSON说他首选的操作系统是Raspbian!”
“那很好啊。”
“我说的是KEN BLOODY THOMPSON!!”
暂停
“KEN BLOODY THOMPSON???"
“真的是KEN THOMPSON!”

诸如此类。

评论已关闭