from m5stack import * from m5stack_ui import * from uiflow import * import ntptime import time from m5stack import touch import wifiCfg screen = M5Screen() screen.clean_screen() screen.set_screen_bg_color(0x000000) timerMinutes = None batteryLevel = None hour = None isSleep = None timerMin = None isTimeupSoundPlaying = None isTimerPaused = None lastActiveTick = None touchY = None timerIsRunning = None minute = None timerIsTimeup = None timerSec = None tickToNextLoop = None i = None clockDelimiter = None isClockMode = None timerStartTick = None idleTime = None timerTargetTick = None timeToDisplay = None timerTimeSpent = None pausedTick = None clockToDisplay = None ntp = None wifiCfg.autoConnect(lcdShow=True) TimerBG = M5Img("res/TimerBG2.png", x=0, y=0, parent=None) charging = M5Img("res/chargning2.png", x=255, y=12, parent=None) from numbers import Number import math # この関数の説明… def initialize(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, i, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, clockToDisplay, ntp screen.set_screen_brightness(35) lcd.font(lcd.FONT_DejaVu24, transparent=False) # This is to avoid M5Stack 1.60 bug in LCD. tickToNextLoop = 0 isClockMode = False initTimer() showBatteryMeter() showTimerMinutes() if not (wifiCfg.wlan_sta.isconnected()): pass ntp = 0 ntp = ntptime.client(host='jp.pool.ntp.org', timezone=9) # Describe this function... def checkTouchForPause(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, i, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, clockToDisplay, ntp if timerIsRunning and (touch.status()): touchY = touch.read()[1] if touchY > 50 and 190 > touchY: # U need to upload a wav using image uploader (AddImage >> any filetype) speaker.playWAV('/sd/pause1.wav') if isTimerPaused: isTimerPaused = False timerTargetTick = (timerTargetTick if isinstance(timerTargetTick, Number) else 0) + ((time.ticks_ms()) - pausedTick) timerStartTick = (timerStartTick if isinstance(timerStartTick, Number) else 0) + ((time.ticks_ms()) - pausedTick) else: isTimerPaused = True pausedTick = time.ticks_ms() lcd.font(lcd.FONT_DejaVu56) lcd.print(' ', 80, 115, 0x000000) while touch.status(): wait_ms(50) else: pass # この関数の説明… def timerApp(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, i, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, clockToDisplay, ntp showClock() if isTimeupSoundPlaying: playTimeupSound() if timerIsRunning: for count2 in range(9): checkTouchForPause() wait_ms(100) else: idleTime = (time.ticks_ms()) - lastActiveTick if idleTime > 240000: sleep() wait_ms(950) # この関数の説明… def initTimer(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, i, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, clockToDisplay, ntp timerMinutes = 10 timerIsRunning = False timerIsTimeup = False isTimeupSoundPlaying = False lastActiveTick = time.ticks_ms() # この関数の説明… def showBatteryMeter(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, i, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, clockToDisplay, ntp batteryLevel = ((power.getBatVoltage()) - 3.2) * 100 if power.getChargeState(): charging.set_hidden(False) else: charging.set_hidden(True) for i in range(4): if batteryLevel >= (i * 25 + 15): lcd.fillRect(275+i*9, 12,7,13, lcd.GREEN) else: lcd.fillRect(275+i*9, 12,7,13, lcd.BLACK) # Describe this function... def sleep(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, i, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, clockToDisplay, ntp power.setPowerLED(False) power.setLCDBacklightVoltage(0) power.setBusPowerMode(1) screen.set_screen_brightness(5) isSleep = True while isSleep: if touch.status(): updateActive() wait_ms(1000) # Describe this function... def showClock(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, i, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, clockToDisplay, ntp hour = ntp.hour() minute = ntp.minute() clockDelimiter = ':' clockToDisplay='{:02d}'.format(hour) + clockDelimiter + '{:02d}'.format(minute) lcd.setColor(0x111111, 0x99cccc); lcd.font(lcd.FONT_DejaVu24) lcd.print(clockToDisplay, 6, 8, 0x330000) lcd.setColor(0xffffff, 0x000000); # Describe this function... def updateActive(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, i, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, clockToDisplay, ntp isSleep = False lastActiveTick = time.ticks_ms() isClockMode = False screen.set_screen_brightness(35) power.setPowerLED(True) power.setLCDBacklightVoltage(2.8) # この関数の説明… def stopTimer(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, i, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, clockToDisplay, ntp timerSch.stop('timer1') if timerIsTimeup == False: speaker.playWAV('/sd/clear1.wav') else: speaker.playWAV('/sd/over1.wav') timerIsRunning = False lcd.font(lcd.FONT_DejaVu24) lcd.print(timerTimeSpent, 125, 69, 0xffff33) lcd.font(lcd.FONT_DejaVu56) lcd.print(timeToDisplay, 80, 115, 0xffffff) lastActiveTick = time.ticks_ms() # この関数の説明… def runTimer(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, i, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, clockToDisplay, ntp speaker.playWAV('/sd/coin1.wav') isTimerPaused = False timerStartTick = time.ticks_ms() timerTargetTick = timerStartTick + timerMinutes * 60000 timerIsTimeup = False timerIsRunning = True pausedTick = time.ticks_ms() timerSch.run('timer1', 500, 0x00) # この関数の説明… def timerTimeUp(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, i, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, clockToDisplay, ntp if timerIsTimeup == False: timerIsTimeup = True lcd.font(lcd.FONT_DejaVu56) lcd.print('00:00', 80, 115, 0xffff33) isTimeupSoundPlaying = True # Describe this function... def showTimeToTarget(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, i, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, clockToDisplay, ntp timerMin = math.floor((timerTargetTick - (time.ticks_ms())) / 60000) timerSec = math.ceil(((timerTargetTick - (time.ticks_ms())) % 60000) / 1000) if timerSec >= 60: timerSec = 59 if timerMin < 0: timerSec = 0 timerMin = 0 timeToDisplay='{:02d}'.format(timerMin) + ":" + '{:02d}'.format(timerSec) lcd.font(lcd.FONT_DejaVu56) lcd.print(timeToDisplay, 80, 115, 0xffffff) if timerTargetTick <= (time.ticks_ms()): timerTimeUp() # この関数の説明… def playTimeupSound(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, i, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, clockToDisplay, ntp isTimeupSoundPlaying = False speaker.playWAV('/sd/hurry2.wav') # この関数の説明… def showTimerMinutes(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, i, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, clockToDisplay, ntp timeToDisplay='{:02d}'.format(timerMinutes) + ":00" lcd.font(lcd.FONT_DejaVu56) lcd.print(timeToDisplay, 80, 115, 0xffffff) # Describe this function... def showTimeSpent(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, i, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, clockToDisplay, ntp timerMin = math.floor(((time.ticks_ms()) - timerStartTick) / 60000) timerSec = math.floor((((time.ticks_ms()) - timerStartTick) % 60000) / 1000) timerTimeSpent='{:02d}'.format(timerMin) + ":" + '{:02d}'.format(timerSec) + ' ' lcd.font(lcd.FONT_DejaVu24) lcd.print(timerTimeSpent, 125, 69, 0xffff66) def buttonB_wasPressed(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, i, clockToDisplay, ntp updateActive() if timerIsRunning == False: # U need to upload a wav using image uploader (AddImage >> any filetype) speaker.playWAV('/sd/big1.wav') while touch.status(): timerMinutes = (timerMinutes if isinstance(timerMinutes, Number) else 0) + 1 if timerMinutes > 60: timerMinutes = 1 showTimerMinutes() wait(0.3) pass btnB.wasPressed(buttonB_wasPressed) def buttonC_wasPressed(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, i, clockToDisplay, ntp updateActive() if timerIsRunning == False: runTimer() else: stopTimer() pass btnC.wasPressed(buttonC_wasPressed) def buttonA_wasPressed(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, i, clockToDisplay, ntp updateActive() if timerIsRunning == False: speaker.playWAV('/sd/small1.wav') while touch.status(): timerMinutes = (timerMinutes if isinstance(timerMinutes, Number) else 0) + -1 if timerMinutes < 1: timerMinutes = 60 showTimerMinutes() wait(0.3) pass btnA.wasPressed(buttonA_wasPressed) @timerSch.event('timer1') def ttimer1(): global timerMinutes, batteryLevel, hour, isSleep, timerMin, isTimeupSoundPlaying, isTimerPaused, lastActiveTick, touchY, timerIsRunning, minute, timerIsTimeup, timerSec, tickToNextLoop, clockDelimiter, isClockMode, timerStartTick, idleTime, timerTargetTick, timeToDisplay, timerTimeSpent, pausedTick, i, clockToDisplay, ntp if isTimerPaused: lcd.font(lcd.FONT_DejaVu40) lcd.print('PAUSE', 95, 120, 0xffff00) else: if timerIsTimeup == False: showTimeToTarget() showTimeSpent() lastActiveTick = time.ticks_ms() pass power.setPowerLED(False) power.setBusPowerMode(1) initialize() while True: showBatteryMeter() for count in range(10): timerApp() wait_ms(2)