The trial plan originally had an LCD clock, but after learning, I found that the unihiker library of the Xingkong board supports the display of an analog clock. All you need to do is read the system time and configure the display. After the Xingkong board is connected to the network, the system time is automatically checked against Beijing time.
The code is as follows:
from unihiker import GUI #导入包
import datetime
gui=GUI() #实例化GUI类
clock=gui.draw_clock(x=120, y=160, r=80, h=10, m=8, s=0, color="#f8ad30", onclick=lambda: print("clock clicked"))
import time
while True:
now = datetime.datetime.now()
#print(now.strftime("%Y-%m-%d %H:%M:%S"))
#print(int(now.strftime("%H")))
clock.config(h=int(now.strftime("%H")),m=int(now.strftime("%M")),s=int(now.strftime("%S")))
#增加等待,防止程序退出和卡住
time.sleep(1)
The effect is as follows:
ca516fe79cb08bb1e552d44540d19e4e
It seems a little too simple, so, based on this, make a tomato clock.
Tomato Clock Features:
Press A to start working and press B to stop.
In order to concentrate on studying and not always stare at the time, I couldn't find a function to turn off the LCD screen backlight, so I set the line blank board screen to flip and started timing for 45 minutes.
When the time is up, the buzzer will sound, and you will flip over the empty board and start the 15-minute break.
When the 15 minutes are up, the buzzer sounds, the screen flips, and the countdown starts again for 45 minutes, repeating the cycle over and over again.
The flipping function is completed using the six-axis sensor on the Xingkong board. In fact, it only needs to judge the acceleration in the vertical direction.
The buttons and buzzer use the pinpong library.
The code is as follows. The time is sped up here for demonstration purposes.
# -*- coding: utf-8 -*-
import time
from pinpong.board import *
from pinpong.extension.unihiker import *
from unihiker import GUI #导入包
from threading import Timer
Board().begin() #初始化
gui=GUI() #实例化GUI类
#对应的工作与休息时间设置
work_min=45
work_sec=0
relax_min=15
relax_sec =0
min=work_min
sec=work_sec
s="%2d:"%min+'{:0>2d}'.format(sec)
times=gui.draw_text(x=120, y=160, text=s, font_size=40,origin = "center",color="red")
state=gui.draw_text(x=120, y=50, text='工作中...', font_size=20,origin = "center",color="green")
work_flag=False
time_over=False
def time_down():
global sec
global min
if (sec > 0 or min >0):
sec-=1
if(sec<0):
sec=59
min-=1
if min==0 and sec ==0:
return 1
else:
return 0
def work(var):
global time_over
global work_flag
global sec
global min
if work_flag==True:
if accelerometer.get_z()<0 and (sec > 0 or min >0) and time_over == False:
if time_down()==1:
time_over=True
state.config(text='休息一下')
print("工作时间结束")
#蜂鸣器
if accelerometer.get_z()>0 and (sec > 0 or min >0) and time_over == True:
if time_down()==1:
time_over=False
state.config(text='开始工作了')
print("休息时间结束")
#蜂鸣器
s="%2d:"%min+'{:0>2d}'.format(sec)
times.config(text=s)
#print(accelerometer.get_z())
#print(time_over)
t=Timer(var,work,args=(var,))
t.start()
work(0.01)#时间要修改成1
while True:
if button_a.is_pressed() == True:
work_flag=True
#set time 45
print("按钮A按下")
if button_b.is_pressed() == True:
work_flag=False
time_over=False
print("按钮B按下")
min=work_min
sec=0
s="%2d:"%min+'{:0>2d}'.format(sec)
times.config(text=s)
if time_over==True and accelerometer.get_z()>0 and min==0 and sec==0:
min=relax_min
sec=relax_sec
if time_over==False and accelerometer.get_z()>0 and min==0 and sec==0:
min=work_min
sec=work_sec
time.sleep(0.05)
Operation effect:
cb3d6a84e90fd1c0e237537380aa8fca