Guest User

Untitled

a guest
Oct 17th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import tkinter as tk
  3. from threading import Thread
  4. import RPi.GPIO as GPIO
  5. import time
  6. import serial
  7.  
  8. # GPIO BOARD PIN numbers
  9.  
  10. # setup GPIO
  11. GPIO.setmode(GPIO.BCM)
  12. GPIO.setwarnings(False)
  13. ser = serial.Serial ("/dev/ttyS0", 9600)
  14.  
  15. GPIO.setup(21, GPIO.IN)
  16. GPIO.setup(20, GPIO.IN)
  17. GPIO.setup(16, GPIO.IN)
  18. GPIO.setup(26, GPIO.IN)
  19. GPIO.setup(19, GPIO.IN)
  20. GPIO.setup(13, GPIO.IN)
  21.  
  22. # Simple status flag
  23. # False mean the timer is not running
  24. # True means the timer is running (counting)
  25. running = False
  26. # Note: Python 2.6 or higher is required for .format() to work
  27. def update_timeText():
  28. global running
  29. global timer
  30. if running == False and GPIO.input(16) == False:
  31. running = True
  32. #print('Start')
  33. ser.write(b'3')
  34. elif running == True and GPIO.input(13) == False:
  35. running = False
  36. #print('Stop')
  37. ser.write(b'6')
  38. time.sleep(0.3)
  39. elif timer != [0, 0, 0] and running == False and GPIO.input(13) == False:
  40. timer = [0, 0, 0]
  41. timeText.configure(text='00:00:00')
  42. elif timer != [0, 0, 0] and running == False and GPIO.input(16) == False:
  43. running = True
  44. print('Start')
  45. ser.write(b'3')
  46. time.sleep(0.3)
  47.  
  48. if running:
  49. # Every time this function is called, we will increment 1 centisecond (1/100 of a second)
  50. timer[2] += 1
  51. # Every 100 centisecond is equal to 1 second
  52. if (timer[2] >= 100):
  53. timer[2] = 0
  54. timer[1] += 1
  55. # Every 60 seconds is equal to 1 min
  56. if (timer[1] >= 60):
  57. timer[0] += 1
  58. timer[1] = 0
  59.  
  60. # We create our time string here
  61. timeString = pattern.format(timer[0], timer[1], timer[2])
  62. # Update the timeText Label box with the current time
  63. timeText.configure(text=timeString)
  64. # Call the update_timeText() function after 1 centisecond
  65. root.after(10, update_timeText)
  66.  
  67. # To start the timer
  68. def start():
  69. global running
  70. running = True
  71.  
  72. # To pause the kitchen timer
  73. def pause():
  74. global running
  75. running = False
  76.  
  77. # To reset the timer to 00:00:00
  78. def reset():
  79. global timer
  80. timer = [0, 0, 0]
  81. timeText.configure(text='00:00:00')
  82.  
  83. # To exit our program
  84. def exit():
  85. root.destroy()
  86.  
  87. def exitt(event):
  88. root.destroy()
  89.  
  90. def push_buttons():
  91.  
  92. if running == True and GPIO.input(20) == False:
  93. #print('Speed up')
  94. ser.write(b'2')
  95. time.sleep(0.3)
  96. elif running == True and GPIO.input(21) == False:
  97. #print('Speed down')
  98. ser.write(b'1')
  99. time.sleep(0.3)
  100.  
  101.  
  102.  
  103. root = tk.Tk()
  104.  
  105. root.attributes("-fullscreen",True)
  106. root.wm_attributes("-topmost",1)
  107. root.bind("<Escape>", exitt)
  108.  
  109. button_frame = tk.Frame(root)
  110. button_frame.pack(fill=tk.X, side=tk.BOTTOM)
  111.  
  112. start_button = tk.Button(button_frame, text='Start', command=start, width = 12 , height = 3,bg="green")
  113. stop_button = tk.Button(button_frame, text='Stop', command=pause, width = 12 , height = 3,bg="red")
  114. reset_button = tk.Button(button_frame, text='Reset', command=reset, width = 12 , height = 3,bg="yellow")
  115. quit_button = tk.Button(button_frame, text='Quit', command=exit, width = 12 , height = 3,bg="grey")
  116.  
  117. button_frame.columnconfigure(0, weight=1)
  118. button_frame.columnconfigure(1, weight=1)
  119. button_frame.columnconfigure(2, weight=1)
  120. button_frame.columnconfigure(3, weight=1)
  121.  
  122. start_button.grid(row=0, column=0, sticky=tk.W+tk.E)
  123. stop_button.grid(row=0, column=1, sticky=tk.W+tk.E)
  124. reset_button.grid(row=0, column=2, sticky=tk.W+tk.E)
  125. quit_button.grid(row=0, column=3, sticky=tk.W+tk.E)
  126.  
  127.  
  128. # Our time structure [min, sec, centsec]
  129. timer = [0, 0, 0]
  130. # The format is padding all the
  131. pattern = '{0:02d}:{1:02d}:{2:02d}'
  132.  
  133. # Create a timeText Label (a text box)
  134. timeText = tk.Label(root, text="00:00:00", font=("Helvetica", 150))
  135. timeText.pack()
  136.  
  137. thread = Thread(target=push_buttons)
  138. thread.start()
  139. update_timeText()
  140. root.mainloop()
Add Comment
Please, Sign In to add comment