SHARE
TWEET

hawtsau5_4

a guest Jun 18th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from tkinter import *
  2. from tkinter import ttk
  3. import tkinter as tk
  4.  
  5. import time
  6. import math
  7. import RPi.GPIO as GPIO
  8. GPIO.setmode(GPIO.BCM)
  9. GPIO.setwarnings(False)
  10. GPIO.setup(21, GPIO.IN)
  11.  
  12.  
  13. def click():
  14.     name = nameEntry.get()
  15.     age = ageEntry.get()
  16.     liveWindow(name, age)
  17.  
  18. def liveWindow(x,y):
  19.     enterName.destroy()                        
  20.     enterAge.destroy()                     #destroy all whidgets
  21.     enterButton.destroy()                            
  22.     nameEntry.destroy()
  23.     ageEntry.destroy()
  24.  
  25.     createLiveWidgets(x,y)
  26.     placeLiveWidgets()
  27.     startRPM()
  28.  
  29. def createLiveWidgets(x,y):
  30.     a.first_frame = tk.LabelFrame(a, text='Power Levels', font='Helvetica 22', bd=border, bg=frameBG, fg=frameTC)
  31.     a.power_label        = tk.Label(a.first_frame, text="power :", font=frameFont, bg=frameBG, fg=frameTC) #create power labels
  32.     a.voltage_label      = tk.Label(a.first_frame, text="voltage :", font=frameFont, bg=frameBG, fg=frameTC)
  33.     a.current_label     = tk.Label(a.first_frame, text="current :", font=frameFont, bg=frameBG, fg=frameTC)
  34.     a.battery_label     = tk.Label(a.first_frame, text="battery :", font=frameFont, bg=frameBG, fg=frameTC)
  35.     a.powerData_label        = tk.Label(a.first_frame, text="   ", font=frameFont, bg=frameBG, fg=frameTC) #create rpm labels
  36.     a.voltageData_label      = tk.Label(a.first_frame, text="   ", font=frameFont, bg=frameBG, fg=frameTC)
  37.     a.currentData_label     = tk.Label(a.first_frame, text="    ", font=frameFont, bg=frameBG, fg=frameTC)
  38.     a.batteryData_label     = tk.Label(a.first_frame, text="    ", font=frameFont, bg=frameBG, fg=frameTC)
  39.     a.powerUnit_label        = tk.Label(a.first_frame, text="W", font=frameFont, bg=frameBG, fg=frameTC) #create rpm labels
  40.     a.voltageUnit_label      = tk.Label(a.first_frame, text="V", font=frameFont, bg=frameBG, fg=frameTC)
  41.     a.currentUnit_label     = tk.Label(a.first_frame, text="A", font=frameFont, bg=frameBG, fg=frameTC)
  42.     a.batteryUnit_label     = tk.Label(a.first_frame, text="%", font=frameFont, bg=frameBG, fg=frameTC)
  43.  
  44.     a.second_frame = tk.LabelFrame(a, text='Speed Levels', font='Helvetica 22', bd=border, bg=frameBG, fg=frameTC)
  45.     a.rpm_label              = tk.Label(a.second_frame, text="RPM :", font=frameFont, bg=frameBG, fg=frameTC)
  46.     a.speed_label            = tk.Label(a.second_frame, text="speed :", font=frameFont, bg=frameBG, fg=frameTC)
  47.     a.distance_label              = tk.Label(a.second_frame, text="distance :", font=frameFont, bg=frameBG, fg=frameTC)
  48.     a.elapsed_time          = tk.Label(a.second_frame, text="time elapsed :", font=frameFont, bg=frameBG, fg=frameTC)
  49.     a.rpmData_label              = tk.Label(a.second_frame, text="  ", font=frameFont, bg=frameBG, fg=frameTC)
  50.     a.speedData_label            = tk.Label(a.second_frame, text="  ", font=frameFont, bg=frameBG, fg=frameTC)
  51.     a.distanceData_label              = tk.Label(a.second_frame, text=" ", font=frameFont, bg=frameBG, fg=frameTC)
  52.     a.elapsedData_time          = tk.Label(a.second_frame, text="   ", font=frameFont, bg=frameBG, fg=frameTC)
  53.     a.rpmUnit_label              = tk.Label(a.second_frame, text="rpms", font=frameFont, bg=frameBG, fg=frameTC)
  54.     a.speedUnit_label            = tk.Label(a.second_frame, text="mph", font=frameFont, bg=frameBG, fg=frameTC)
  55.     a.distanceUnit_label              = tk.Label(a.second_frame, text="m", font=frameFont, bg=frameBG, fg=frameTC)
  56.     a.elapsedUnit_time          = tk.Label(a.second_frame, text="s", font=frameFont, bg=frameBG, fg=frameTC)
  57.  
  58.  
  59.     a.third_frame = tk.LabelFrame(a, text='User Info', font='Helvetica 22', bd=border, bg=frameBG, fg=frameTC)
  60.     a.name_label              = tk.Label(a.third_frame, text="name :", font=frameFont, bg=frameBG, fg=frameTC)
  61.     a.age_label            = tk.Label(a.third_frame, text="age :", font=frameFont, bg=frameBG, fg=frameTC)
  62.     a.heart_label        = tk.Label(a.third_frame, text="heart rate :", font=frameFont, bg=frameBG, fg=frameTC)
  63.     a.calorie_label   = tk.Label(a.third_frame, text="calories burned :", font=frameFont, bg=frameBG, fg=frameTC)
  64.     a.nameData_label              = tk.Label(a.third_frame, text=x, font=frameFont, bg=frameBG, fg=frameTC)
  65.     a.ageData_label            = tk.Label(a.third_frame, text=y, font=frameFont, bg=frameBG, fg=frameTC)
  66.     a.heartData_label        = tk.Label(a.third_frame, text="   ", font=frameFont, bg=frameBG, fg=frameTC)
  67.     a.calorieData_label   = tk.Label(a.third_frame, text="  ", font=frameFont, bg=frameBG, fg=frameTC)
  68.     a.heartUnit_label        = tk.Label(a.third_frame, text="bpm", font=frameFont, bg=frameBG, fg=frameTC)
  69.    
  70.  
  71. def placeLiveWidgets():
  72.     a.first_frame.grid(row=1, column=1)
  73.     a.power_label.grid(row=1, column=1)
  74.     a.voltage_label.grid(row=2, column=1)
  75.     a.current_label.grid(row=3, column=1)
  76.     a.battery_label.grid(row=4, column=1)
  77.     a.powerData_label.grid(row=1, column=2)
  78.     a.voltageData_label.grid(row=2, column=2)
  79.     a.currentData_label.grid(row=3, column=2)
  80.     a.batteryData_label.grid(row=4, column=2)
  81.     a.powerUnit_label.grid(row=1, column=3)
  82.     a.voltageUnit_label.grid(row=2, column=3)
  83.     a.currentUnit_label.grid(row=3, column=3)
  84.     a.batteryUnit_label.grid(row=4, column=3)
  85.  
  86.     a.second_frame.grid(row=1, column=2)
  87.     a.rpm_label.grid(row=1, column=1)
  88.     a.speed_label.grid(row=2, column=1)
  89.     a.distance_label.grid(row=3, column=1)
  90.     a.elapsed_time.grid(row=4, column=1)
  91.     a.rpmData_label.grid(row=1, column=2)
  92.     a.speedData_label.grid(row=2, column=2)
  93.     a.distanceData_label.grid(row=3, column=2)
  94.     a.elapsedData_time.grid(row=4, column=2)
  95.     a.rpmUnit_label.grid(row=1, column=3)
  96.     a.speedUnit_label.grid(row=2, column=3)
  97.     a.distanceUnit_label.grid(row=3, column=3)
  98.     a.elapsedUnit_time.grid(row=4, column=3)
  99.  
  100.     a.third_frame.grid(row=1, column=3)
  101.     a.name_label.grid(row=1, column=1)
  102.     a.age_label.grid(row=2, column=1)
  103.     a.heart_label.grid(row=3, column=1)
  104.     a.calorie_label.grid(row=4, column=1)
  105.     a.nameData_label.grid(row=1, column=2)
  106.     a.ageData_label.grid(row=2, column=2)
  107.     a.heartData_label.grid(row=3, column=2)
  108.     a.calorieData_label.grid(row=4, column=2)
  109.     a.heartUnit_label.grid(row=3, column=3)
  110.    
  111.  
  112. def calculate_elapse(channel):              # callback function
  113.     global pulse, start_timer, elapse
  114.     pulse+=1                                # increase pulse by 1 whenever interrupt occurred
  115.     elapse = time.time() - start_timer      # elapse for every 1 complete rotation made!
  116.     start_timer = time.time()               # let current time equals to start_timer
  117.  
  118. def calculate_speed(r_cm):
  119.     global pulse,elapse,rpm,dist_km,dist_meas,km_per_sec,km_per_hour
  120.     if elapse !=0:                          # to avoid DivisionByZero error
  121.         rpm = 1/elapse * 60
  122.         circ_cm = (2*math.pi)*r_cm          # calculate wheel circumference in CM
  123.         dist_km = circ_cm/100000            # convert cm to km
  124.         km_per_sec = dist_km / elapse       # calculate KM/sec
  125.         km_per_hour = km_per_sec * 3600     # calculate KM/h
  126.         dist_meas = (dist_km*pulse)*1000    # measure distance traverse in meter
  127.         return km_per_hour
  128.  
  129. def init_interrupt():
  130.     GPIO.add_event_detect(sensor, GPIO.FALLING, callback = calculate_elapse, bouncetime = 20)
  131.    
  132. def startRPM():
  133.     init_interrupt()
  134.     while True:
  135.         a.update()
  136.         calculate_speed(20) # call this function with wheel radius as parameter
  137.         print('rpm:{0:.0f}-RPM kmh:{1:.0f}-KMH dist_meas:{2:.2f}m pulse:{3}'.format(rpm,km_per_hour,dist_meas,pulse))
  138.         time.sleep(0.1)
  139.  
  140. border=5
  141. frameBG='#000033'
  142. frameTC='#FFFFFF'
  143. frameFont='Helvetica 42'
  144.  
  145. dist_meas = 0.00
  146. km_per_hour = 0
  147. rpm = 0
  148. elapse = 0
  149. sensor = 21
  150. pulse = 0
  151. start_timer = time.time()
  152.  
  153. a = Tk()                                 #create window
  154. a.minsize(1490, 320)                      #size window
  155. a.title("Current Statistics")            #title window
  156. a.configure(background=frameBG)        #background color  
  157.  
  158. enterName = Label(a, text="Name", font='Helvetica 50', bg=frameBG, fg=frameTC)   #create text variable
  159. enterName.grid(row=1, column=1)
  160.  
  161. nameEntry = Entry(a, bd = 10, font='Helvetica 50')                   #create text entry box
  162. nameEntry.grid(row=1, column=2)
  163.  
  164.  
  165. enterAge = Label(a, text="Age", font='Helvetica 50', bg=frameBG, fg=frameTC)   #create text variable
  166. enterAge.grid(row=2,column=1)
  167.  
  168. ageEntry = Entry(a, bd = 10, font='Helvetica 50')                   #create text entry box
  169. ageEntry.grid(row=2, column=2)
  170.  
  171. enterButton = Button(a, text = "ENTER", command=click, height = 5, width = 30) #create button
  172. enterButton.grid(row=3, column=2)
  173.  
  174.  
  175. a.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top