I begin from this script:
Code: Select all
import RPi.GPIO as GPIO
import time
RoAPin = 12 # CLK Pin
RoBPin = 26 # DT Pin
globalCounter = 0
flag = 0
Last_RoB_Status = 0
Current_RoB_Status = 0
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(RoAPin, GPIO.IN) # input mode
GPIO.setup(RoBPin, GPIO.IN)
def rotaryDeal():
global flag
global Last_RoB_Status
global Current_RoB_Status
global globalCounter
Last_RoB_Status = GPIO.input(RoBPin)
while(not GPIO.input(RoAPin)):
Current_RoB_Status = GPIO.input(RoBPin)
flag = 1
if flag == 1:
flag = 0
if (Last_RoB_Status == 0) and (Current_RoB_Status == 1):
globalCounter = globalCounter + 1
if (Last_RoB_Status == 1) and (Current_RoB_Status == 0):
globalCounter = globalCounter - 1
def loop():
global globalCounter
tmp = 0 # Rotary Temperary
while True:
rotaryDeal()
if tmp != globalCounter:
print 'globalCounter = %d' % globalCounter
tmp = globalCounter
#f = open('file.txt', 'wb')
#f.write('GlobalCounter= %d'%globalCounter)
#f.close()
def destroy():
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
destroy() and I write this one:
Code: Select all
import RPi.GPIO as GPIO
import time
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.config import Config
from kivy.core.window import Window
from kivy.clock import Clock
#Clock.max_iteration = 20
from kivy.uix.widget import Widget
RoAPin = 12 # CLK Pin
RoBPin = 26 # DT Pin
globalCounter = 0
flag = 0
Last_RoB_Status = 0
Current_RoB_Status = 0
def setup(dt):
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(RoAPin, GPIO.IN) # input mode
GPIO.setup(RoBPin, GPIO.IN)
Clock.schedule_once(setup, 0)
def funct(dt):
global flag
global Last_RoB_Status
global Current_RoB_Status
global globalCounter
Last_RoB_Status = GPIO.input(RoBPin)
for i in range (61):
if (not GPIO.input(RoAPin)) :
Current_RoB_Status = GPIO.input(RoBPin)
flag = 1
if flag == 1:
flag = 0
if (Last_RoB_Status == 0) and (Current_RoB_Status == 1):
globalCounter = globalCounter + 1
if (Last_RoB_Status == 1) and (Current_RoB_Status == 0):
globalCounter = globalCounter - 1
Clock.schedule_once(funct,0)
Class FlickerGUI(GridLayout):
def __init__(self, **kwargs):
super(FlickerGUI, self).__init__(**kwargs)
self.cols = 1
self.add_widget(Label(text='Saturation Level'))
self.sat_label = Label(text=str(globalCounter))
self.add_widget(self.sat_label)
Clock.schedule_once(setup,0)
Clock.schedule_once(funct,0)
Clock.schedule_once(self.update_labels,0)
def update_labels(self, *args):
self.sat_label.text = str(globalCounter)
Clock.schedule_once(self.update_labels,0)
class MyApp(App):
def build(self):
return FlickerGUI()
if __name__ == '__main__':
MyApp().run()
GPIO.cleanup()Thank you in advance