08_timeCounter.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python
  2. #version : 2023.12.31
  3. #language : ch
  4. from maix import display
  5. from maix import image
  6. from maix import camera
  7. import time
  8. import sys
  9. sys.path.append('/root/')
  10. from CocoPi import BUTTON
  11. # 秒表小程序
  12. key_C = BUTTON(13)
  13. key_B = BUTTON(8)
  14. camera.camera.config(size=(240,320))
  15. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  16. signal=0
  17. startPointer=0
  18. startTime=0
  19. global deltaTime
  20. deltaTime=0
  21. # 初始化
  22. startTime = time.time()
  23. oldState=0
  24. newState=0
  25. canvas = image.new(color=(0,0,0),size = (320, 240))
  26. while True:
  27. canvas.clear()
  28. if key_B.is_pressed():
  29. time.sleep(0.02)
  30. deltaTime = 0
  31. startTime = round(time.time(), 3)
  32. else:
  33. pass
  34. newState=key_C.is_pressed()
  35. if (newState==0 and oldState==1):
  36. time.sleep(0.02)
  37. signal=1-signal
  38. if deltaTime == 0:
  39. startTime = round(time.time(), 3)
  40. else:
  41. startTime = round(time.time(), 3) - deltaTime
  42. else:
  43. pass
  44. if signal==1:
  45. if startPointer==0:
  46. startPointer=1
  47. #print("startPoniter",startPoniter)
  48. else:
  49. #pass
  50. deltaTime = round(time.time() - startTime, 3)
  51. #deltaTime = time.time()-startTime
  52. #deltaTime = round(time.time()-startTime, 5)
  53. #canvas.draw_string(80, 128, "持续时间:"+str(deltaTime), 1, color=(0, 255, 0))
  54. elif signal==0:
  55. startPointer=0
  56. else:
  57. pass
  58. if signal==1:
  59. canvas.draw_string(80, 108, "正在计时中...", 1, color=(0, 255, 0))
  60. canvas.draw_image((image.open("/root/preset/img/stop24.png")).rotate(0, adjust=0),288,2,alpha=1)
  61. else:
  62. canvas.draw_string(80, 108, "等待计时...", 1, color=(0, 255, 0))
  63. canvas.draw_image((image.open("/root/preset/img/play24.png")).rotate(0, adjust=0),288,2,alpha=1)
  64. canvas.draw_string(80, 128, "计时持续时间(秒):"+str(deltaTime), 1, color=(0, 255, 0))
  65. canvas.draw_string(16, 8, "按下C键来开始/停止计时!", 1, color=(0, 255, 0))
  66. canvas.draw_image((image.open("/root/preset/img/restart_ff0000_24x24.png")).rotate(0, adjust=0),2,216,alpha=1)
  67. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  68. display.show(canvas)
  69. oldState=newState