Facebook
From wingtiger, 9 Months ago, written in Python.
Embed
Download Paste or View Raw
Hits: 88
  1. from m5stack import *
  2. from m5stack_ui import *
  3. from uiflow import *
  4. import ntptime
  5. import time
  6. from m5stack import touch
  7. import wifiCfg
  8.  
  9. screen = M5Screen()
  10. screen.clean_screen()
  11. screen.set_screen_bg_color(0x000000)
  12.  
  13.  
  14. timerMinutes = None
  15. batteryLevel = None
  16. hour = None
  17. isSleep = None
  18. timerMin = None
  19. isTimeupSoundPlaying = None
  20. isTimerPaused = None
  21. lastActiveTick = None
  22. touchY = None
  23. timerIsRunning = None
  24. minute = None
  25. timerIsTimeup = None
  26. timerSec = None
  27. tickToNextLoop = None
  28. i = None
  29. clockDelimiter = None
  30. isClockMode = None
  31. timerStartTick = None
  32. idleTime = None
  33. timerTargetTick = None
  34. timeToDisplay = None
  35. timerTimeSpent = None
  36. pausedTick = None
  37. clockToDisplay = None
  38. ntp = None
  39.  
  40. wifiCfg.autoConnect(lcdShow=True)
  41. TimerBG = M5Img("res/TimerBG2.png", x=0, y=0, parent=None)
  42. charging = M5Img("res/chargning2.png", x=255, y=12, parent=None)
  43.  
  44. from numbers import Number
  45. import math
  46.  
  47.  
  48. # この関数の説明…
  49. def initialize():
  50.   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
  51.   screen.set_screen_brightness(35)
  52.   lcd.font(lcd.FONT_DejaVu24, transparent=False)  # This is to avoid M5Stack 1.60 bug in LCD.
  53.   tickToNextLoop = 0
  54.   isClockMode = False
  55.   initTimer()
  56.   showBatteryMeter()
  57.   showTimerMinutes()
  58.   if not (wifiCfg.wlan_sta.isconnected()):
  59.     pass
  60.   ntp = 0
  61.   ntp = ntptime.client(host='jp.pool.ntp.org', timezone=9)
  62.  
  63. # Describe this function...
  64. def checkTouchForPause():
  65.   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
  66.   if timerIsRunning and (touch.status()):
  67.     touchY = touch.read()[1]
  68.     if touchY > 50 and 190 > touchY:
  69.       # U need to upload a wav using image uploader (AddImage >> any filetype)
  70.       speaker.playWAV('/sd/pause1.wav')
  71.       if isTimerPaused:
  72.         isTimerPaused = False
  73.         timerTargetTick = (timerTargetTick if isinstance(timerTargetTick, Number) else 0) + ((time.ticks_ms()) - pausedTick)
  74.         timerStartTick = (timerStartTick if isinstance(timerStartTick, Number) else 0) + ((time.ticks_ms()) - pausedTick)
  75.       else:
  76.         isTimerPaused = True
  77.         pausedTick = time.ticks_ms()
  78.         lcd.font(lcd.FONT_DejaVu56)
  79.         lcd.print('         ', 80, 115, 0x000000)
  80.     while touch.status():
  81.       wait_ms(50)
  82.   else:
  83.     pass
  84.  
  85. # この関数の説明…
  86. def timerApp():
  87.   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
  88.   showClock()
  89.   if isTimeupSoundPlaying:
  90.     playTimeupSound()
  91.   if timerIsRunning:
  92.     for count2 in range(9):
  93.       checkTouchForPause()
  94.       wait_ms(100)
  95.   else:
  96.     idleTime = (time.ticks_ms()) - lastActiveTick
  97.     if idleTime > 240000:
  98.       sleep()
  99.     wait_ms(950)
  100.  
  101. # この関数の説明…
  102. def initTimer():
  103.   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
  104.   timerMinutes = 10
  105.   timerIsRunning = False
  106.   timerIsTimeup = False
  107.   isTimeupSoundPlaying = False
  108.   lastActiveTick = time.ticks_ms()
  109.  
  110. # この関数の説明…
  111. def showBatteryMeter():
  112.   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
  113.   batteryLevel = ((power.getBatVoltage()) - 3.2) * 100
  114.   if power.getChargeState():
  115.     charging.set_hidden(False)
  116.   else:
  117.     charging.set_hidden(True)
  118.   for i in range(4):
  119.     if batteryLevel >= (i * 25 + 15):
  120.       lcd.fillRect(275+i*9, 12,7,13, lcd.GREEN)
  121.     else:
  122.       lcd.fillRect(275+i*9, 12,7,13, lcd.BLACK)
  123.  
  124. # Describe this function...
  125. def sleep():
  126.   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
  127.   power.setPowerLED(False)
  128.   power.setLCDBacklightVoltage(0)
  129.   power.setBusPowerMode(1)
  130.   screen.set_screen_brightness(5)
  131.   isSleep = True
  132.   while isSleep:
  133.     if touch.status():
  134.       updateActive()
  135.     wait_ms(1000)
  136.  
  137. # Describe this function...
  138. def showClock():
  139.   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
  140.   hour = ntp.hour()
  141.   minute = ntp.minute()
  142.   clockDelimiter = ':'
  143.   clockToDisplay='{:02d}'.format(hour) + clockDelimiter + '{:02d}'.format(minute)
  144.   lcd.setColor(0x111111, 0x99cccc);
  145.   lcd.font(lcd.FONT_DejaVu24)
  146.   lcd.print(clockToDisplay, 6, 8, 0x330000)
  147.   lcd.setColor(0xffffff, 0x000000);
  148.  
  149. # Describe this function...
  150. def updateActive():
  151.   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
  152.   isSleep = False
  153.   lastActiveTick = time.ticks_ms()
  154.   isClockMode = False
  155.   screen.set_screen_brightness(35)
  156.   power.setPowerLED(True)
  157.   power.setLCDBacklightVoltage(2.8)
  158.  
  159. # この関数の説明…
  160. def stopTimer():
  161.   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
  162.   timerSch.stop('timer1')
  163.   if timerIsTimeup == False:
  164.     speaker.playWAV('/sd/clear1.wav')
  165.   else:
  166.     speaker.playWAV('/sd/over1.wav')
  167.   timerIsRunning = False
  168.   lcd.font(lcd.FONT_DejaVu24)
  169.   lcd.print(timerTimeSpent, 125, 69, 0xffff33)
  170.   lcd.font(lcd.FONT_DejaVu56)
  171.   lcd.print(timeToDisplay, 80, 115, 0xffffff)
  172.   lastActiveTick = time.ticks_ms()
  173.  
  174. # この関数の説明…
  175. def runTimer():
  176.   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
  177.   speaker.playWAV('/sd/coin1.wav')
  178.   isTimerPaused = False
  179.   timerStartTick = time.ticks_ms()
  180.   timerTargetTick = timerStartTick + timerMinutes * 60000
  181.   timerIsTimeup = False
  182.   timerIsRunning = True
  183.   pausedTick = time.ticks_ms()
  184.   timerSch.run('timer1', 500, 0x00)
  185.  
  186. # この関数の説明…
  187. def timerTimeUp():
  188.   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
  189.   if timerIsTimeup == False:
  190.     timerIsTimeup = True
  191.     lcd.font(lcd.FONT_DejaVu56)
  192.     lcd.print('00:00', 80, 115, 0xffff33)
  193.     isTimeupSoundPlaying = True
  194.  
  195. # Describe this function...
  196. def showTimeToTarget():
  197.   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
  198.   timerMin = math.floor((timerTargetTick - (time.ticks_ms())) / 60000)
  199.   timerSec = math.ceil(((timerTargetTick - (time.ticks_ms())) % 60000) / 1000)
  200.   if timerSec >= 60:
  201.     timerSec = 59
  202.   if timerMin < 0:
  203.     timerSec = 0
  204.     timerMin = 0
  205.   timeToDisplay='{:02d}'.format(timerMin) + ":" + '{:02d}'.format(timerSec)
  206.   lcd.font(lcd.FONT_DejaVu56)
  207.   lcd.print(timeToDisplay, 80, 115, 0xffffff)
  208.   if timerTargetTick <= (time.ticks_ms()):
  209.     timerTimeUp()
  210.  
  211. # この関数の説明…
  212. def playTimeupSound():
  213.   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
  214.   isTimeupSoundPlaying = False
  215.   speaker.playWAV('/sd/hurry2.wav')
  216.  
  217. # この関数の説明…
  218. def showTimerMinutes():
  219.   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
  220.   timeToDisplay='{:02d}'.format(timerMinutes) + ":00"
  221.   lcd.font(lcd.FONT_DejaVu56)
  222.   lcd.print(timeToDisplay, 80, 115, 0xffffff)
  223.  
  224. # Describe this function...
  225. def showTimeSpent():
  226.   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
  227.   timerMin = math.floor(((time.ticks_ms()) - timerStartTick) / 60000)
  228.   timerSec = math.floor((((time.ticks_ms()) - timerStartTick) % 60000) / 1000)
  229.   timerTimeSpent='{:02d}'.format(timerMin) + ":" + '{:02d}'.format(timerSec) + ' '
  230.   lcd.font(lcd.FONT_DejaVu24)
  231.   lcd.print(timerTimeSpent, 125, 69, 0xffff66)
  232.  
  233.  
  234. def buttonB_wasPressed():
  235.   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
  236.   updateActive()
  237.   if timerIsRunning == False:
  238.     # U need to upload a wav using image uploader (AddImage >> any filetype)
  239.     speaker.playWAV('/sd/big1.wav')
  240.     while touch.status():
  241.       timerMinutes = (timerMinutes if isinstance(timerMinutes, Number) else 0) + 1
  242.       if timerMinutes > 60:
  243.         timerMinutes = 1
  244.       showTimerMinutes()
  245.       wait(0.3)
  246.   pass
  247. btnB.wasPressed(buttonB_wasPressed)
  248.  
  249. def buttonC_wasPressed():
  250.   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
  251.   updateActive()
  252.   if timerIsRunning == False:
  253.     runTimer()
  254.   else:
  255.     stopTimer()
  256.   pass
  257. btnC.wasPressed(buttonC_wasPressed)
  258.  
  259. def buttonA_wasPressed():
  260.   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
  261.   updateActive()
  262.   if timerIsRunning == False:
  263.     speaker.playWAV('/sd/small1.wav')
  264.     while touch.status():
  265.       timerMinutes = (timerMinutes if isinstance(timerMinutes, Number) else 0) + -1
  266.       if timerMinutes < 1:
  267.         timerMinutes = 60
  268.       showTimerMinutes()
  269.       wait(0.3)
  270.   pass
  271. btnA.wasPressed(buttonA_wasPressed)
  272.  
  273. @timerSch.event('timer1')
  274. def ttimer1():
  275.   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
  276.   if isTimerPaused:
  277.     lcd.font(lcd.FONT_DejaVu40)
  278.     lcd.print('PAUSE', 95, 120, 0xffff00)
  279.   else:
  280.     if timerIsTimeup == False:
  281.       showTimeToTarget()
  282.     showTimeSpent()
  283.   lastActiveTick = time.ticks_ms()
  284.   pass
  285.  
  286.  
  287. power.setPowerLED(False)
  288. power.setBusPowerMode(1)
  289. initialize()
  290. while True:
  291.   showBatteryMeter()
  292.   for count in range(10):
  293.     timerApp()
  294.   wait_ms(2)
  295.