As on PAM 11 in Trier introduced: Python-Logger-Class (Push Button Library) as part of the Python magnetic field logger and detector project.
######################################## # # Python Magnetic Field Logger and Detector # by Sebastian Folz # Pusbutton library - handles push buttons # Introduced on Pi And More 11 # ######################################## # import GPIO import RPi.GPIO as GPIO # enumerations from enum import Enum class Push(Enum): NONE = 0 SHORT_DOWN = 1 LONG_DOWN = 2 class pushbutton: #pushed = False def __init__(self): GPIO.setmode(GPIO.BOARD) self.pushed = False self.pin = -1 self.cycles = 0 def setLongCycle(self, cycles): self.long_cycles = cycles; def configure(self, aPin): self.pin = aPin GPIO.setup(self.pin, GPIO.IN, pull_up_down = GPIO.PUD_UP) def isPushed(self): # initial push down if (self.pushed == False): if (GPIO.input(self.pin) == GPIO.HIGH): self.pushed = True return Push.NONE # after pushing down count cycles if (self.pushed == True): self.cycles += 1 # final push release if (self.pushed == True): if (GPIO.input(self.pin) == GPIO.LOW): self.pushed = False print("CYCLES: {}/{}".format(self.cycles, self.long_cycles)) if (self.cycles < self.long_cycles): self.cycles = 0 return Push.SHORT_DOWN else: self.cycles = 0 return Push.LONG_DOWN # default return Push.NONE
Kommentare
Kommentar veröffentlichen