python 2.7
I'm using a raspberry pi to monitor some digital input signals as an hobby project. I want to have a large display that shows the accumulated values and updates them with global variables that will keep track of the input signals. I'm going to be using push buttons on a breadboard for now so I tried to use a holding variable to keep the counters from incrementing every cycle. I am very new to Python as I'm sure this is obvious so please keep it simple if possible. I am calling one input PE_MATTRESSES
, another PE_COMBINED
, and the other RESET
, I want to display a third value that subtracts PE_MATTRESSES
from PE_COMBINED
. Of course RESET
will zero everything..
My pseudo code though process is:
- establish I/O and global variables
- create GUI
- while 1
-- look at I/O
-- update global variables
-- update labels to display them
-- loop
My code is as follows:
import Tkinter as tk
import tkFont
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
PE_MATTRESS = GPIO.input(17)
PE_MATTRESS_LATCH = 0
PE_MATTRESS_ACCUMULATOR = 0
PE_COMBINED = GPIO.input(21)
PE_COMBINED_LATCH = 0
PE_COMBINED_ACCUMULATOR = 0
BOXSPRING_ACCUMULATOR = 0
BOXSPRING_ACCUMULATOR = PE_COMBINED_ACCUMULATOR - PE_MATTRESS_ACCUMULATOR
RESET = GPIO.input(22)
class App:
def __init__(self):
root=tk.Tk()
# create a custom font
self.customFont = tkFont.Font(family="Helvetica", size=117)
# create widgets
label1 = tk.Label(root, text="MATTRESSES: %s" % PE_MATTRESS_ACCUMULATOR , font=self.customFont)
label1.pack()
spacer1 = tk.Label(root, text="", font=self.customFont)
spacer1.pack()
Quit = tk.Button(root, text="QUIT", command=quit)
Increment = tk.Button(root, text="Test", command=Inc)
Quit.pack()
Increment.pack()
label2 = tk.Label(root, text="BOXSPRINGS: %s" % BOXSPRING_ACCUMULATOR, font=self.customFont)
label2.pack()
spacer2 = tk.Label(root, text="", font=self.customFont)
spacer2.pack()
label3 = tk.Label(root, text=" COMBINED: %s" % PE_COMBINED_ACCUMULATOR, font=self.customFont)
label3.pack()
root.geometry('1800x1000+0+0')
root.after(10,Inputs)
root.mainloop()
def Inc():
PE_MATTRESS_ACCUMULATOR = PE_MATTRESS_ACCUMULATOR + 1
label1.setText("MATTRESSES: %s" % PE_MATTRESS_ACCUMULATOR)
label2.setText("BOXSPRINGS: %s" % BOXSPRING_ACCUMULATOR)
label3.setText(" COMBINED: %s" % PE_COMBINED_ACCUMULATOR)
root.update_idletasks()
def Inputs():
if PE_MATTRESS == True and PE_MATTRESS_LATCH == False:
PE_MATTRESS_ACCUMULATOR = PE_MATTRESS_ACCUMULATOR + 1
PE_MATTRESS_LATCH = True
elif PE_MATTRESS == False and PE_MATTRESS_LATCH == True:
PE_MATTRESS_LATCH = False
elif PE_COMBINED == True and PE_COMBINED_LATCH == False:
PE_COMBINED_ACCUMULATOR = PE_COMBINED_ACCUMULATOR + 1
PE_COMBINED_LATCH = True
elif PE_COMBINED == False and PE_COMBINED_LATCH == True:
PE_COMBINED_LATCH = False
elif RESET == True:
PE_COMBINED_ACCUMULATOR = 0
PE_MATTRESS_ACCUMULATOR = 0
app=App()