def event_loop(self):
self.keyDown=False
self.hitCount=0
keys_per_rev=5
key_press_delay=0.2
inter_key_delay=0.001
self.last_hit_time=0
self.current_hit_time=0
self.rev_time=0.5
GPIO.add_event_detect(DeskCycle.PIN, GPIO.FALLING, callback=self.pin_event,bouncetime=250)
while True:
if(self.hitCount >0):
if(not self.keyDown):
print "On"
self.state[4]=DeskCycle.KEYCODE
self.send_key_state()
self.keyDown=True
self.hitCount=0
else:
if(self.keyDown):
if(time.time()-self.last_hit_time > 1):
print "Off"
self.state[4]=0
self.send_key_state()
self.keyDown=False
time.sleep(0.001)
python类FALLING的实例源码
def __init__(self):
print "setting up GPIO"
GPIO.setmode(GPIO.BOARD)
GPIO.setup(DeskCycle.PIN,GPIO.IN,pull_up_down=GPIO.PUD_UP)
self.hitCount=0
pin2=38
GPIO.setup(pin2,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(pin2, GPIO.FALLING, callback=self.pin_2_event,bouncetime=100)
def reset_radio(self):
"""
Reset the Si4707 chip
"""
# Ref https://github.com/AIWIndustries/Pi_4707/blob/master/firmware/NWRSAME_v2.py
if self.gpio_started:
gpio.cleanup()
self.gpio_started = True
gpio.setmode(gpio.BCM) # Use board pin numbering
gpio.setup(17, gpio.OUT) # Setup the reset pin
gpio.output(17, gpio.LOW) # Reset the Si4707.
sleep(0.4)
gpio.output(17, gpio.HIGH)
gpio.setup(23, gpio.IN, pull_up_down=gpio.PUD_UP)
gpio.add_event_detect(23, gpio.FALLING)
# Initialize the onboard relays
for pin in self.relay_gpio_pins:
gpio.setup(pin, gpio.OUT) # setup gpio pin for relay
gpio.output(pin, gpio.LOW) # boot to disabled state
# set up the LED
# https://www.reddit.com/r/raspberry_pi/comments/3641ug/blinking_an_onboard_led_on_the_pi_2_model_b/
# http://raspberrypi.stackexchange.com/questions/697/how-do-i-control-the-system-leds-using-my-
# sudo echo none >/sys/class/leds/led0/trigger
# GPIO 16 LOW is on, HIGH is off
gpio.setup(16, gpio.OUT)
gpio.output(16, gpio.HIGH)
sleep(1.5)
def __init__(self):
GPIO.setmode(GPIO.BCM)
#Setup each hall effect pin for interrupts
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
print ("Encoders Ready")
GPIO.add_event_detect(18, GPIO.FALLING, callback=self.add_right_pulse)
GPIO.add_event_detect(24, GPIO.FALLING, callback=self.add_left_pulse)
while 1:
pass
# Callback function for Hall Sensor interrupts
def eventBegin(self, callback, object=0, edge='BOTH'):
# eventBegin(callback)
# eventBegin(callback, object, edge)
# ** callback: call function when Interrupt
# ** object: set Interrupt pin: 1=Button, 2=Encoder >> default: 0(all)
# ** edge: set edge detection: RISING, FALLING, BOTH >> default: BOTH
self.Callback=callback
if object==0 and ((self.__SWtype==1 and self.__eventStatus&0x03!=0x00) or (self.__SWtype==2 and self.__eventStatus&0x0F!=0x00)):
self.eventEnd(0)
elif object==1 and self.__eventStatus&0x03!=0x00:
self.eventEnd(1)
elif object==2 and (self.__SWtype==2 and self.__eventStatus&0x0C!=0x00):
self.eventEnd(2)
if object==0 or object==1:
if edge.upper().find('RISING')>=0:
GPIO.add_event_detect(self.__PinSW, GPIO.RISING, callback=self.eventButton, bouncetime=40)
self.__eventStatus|=0x01
elif edge.upper().find('FALLING')>=0:
GPIO.add_event_detect(self.__PinSW, GPIO.FALLING, callback=self.eventButton, bouncetime=40)
self.__eventStatus|=0x02
elif edge.upper().find('BOTH')>=0:
GPIO.add_event_detect(self.__PinSW, GPIO.BOTH, callback=self.eventButton, bouncetime=40)
self.__eventStatus|=0x03
if (object==0 or object==2) and self.__SWtype==2:
if edge.upper().find('RISING')>=0:
GPIO.add_event_detect(self.__PinA , GPIO.RISING, callback=self.eventEncoder, bouncetime=20)
self.__eventStatus|=0x04
elif edge.upper().find('FALLING')>=0:
GPIO.add_event_detect(self.__PinA , GPIO.FALLING, callback=self.eventEncoder, bouncetime=20)
self.__eventStatus|=0x08
elif edge.upper().find('BOTH')>=0:
GPIO.add_event_detect(self.__PinA , GPIO.BOTH, callback=self.eventEncoder, bouncetime=20)
self.__eventStatus|=0x0C
def start(self):
GPIO.add_event_detect(self.clockPin,
GPIO.FALLING,
callback=self._clockCallback,
bouncetime=50)
GPIO.add_event_detect(self.buttonPin,
GPIO.FALLING,
callback=self._switchCallback,
bouncetime=300)
def __setup(self, channels):
self.__mappings = {}
number = 0
for channel in channels:
number += 1
GPIO.setup(channel, GPIO.IN)
GPIO.add_event_detect(channel, GPIO.FALLING, callback=self.__teamscores)
self.__mappings[channel] = number
logging.info("Listening for events for %s on channel %s" % (self.__mappings[channel], channel))
def start(self):
GPIO.add_event_detect(self.clockPin, GPIO.FALLING, callback=self._clockCallback, bouncetime=self.DEBOUNCE)
GPIO.add_event_detect(self.switchPin, GPIO.FALLING, callback=self.switchCallback, bouncetime=self.DEBOUNCE)
def init_c1(self, reset = True):
if(reset):
self.reset_counter1()
self.__soft_debounce_1 = False
GPIO.add_event_detect(self.__input_pins[0], GPIO.FALLING, callback=self.__worker_c1)
## Initialize counter 1.
# @param self The object pointer.
# @param reset Reset the counter.
def init_c2(self, reset=True):
if(reset):
self.reset_counter2()
self.__soft_debounce_2 = False
GPIO.add_event_detect(self.__input_pins[1], GPIO.FALLING, callback=self.__worker_c2)
## Retuens counter 1 value.
# @param self The object pointer.
# @return Counter 1 value.
02_buttonControlLed.py 文件源码
项目:SunFounder_Super_Kit_V3.0_for_Raspberry_Pi
作者: sunfounder
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def setup():
# Set the GPIO modes to BCM Numbering
GPIO.setmode(GPIO.BCM)
# Set LedPin's mode to output,
# and initial level to high (3.3v)
GPIO.setup(LedPin, GPIO.OUT, initial=GPIO.HIGH)
# Set BtnPin's mode to input,
# and pull up to high (3.3V)
GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Set up a falling detect on BtnPin,
# and callback function to swLed
GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=swLed)
# Define a callback function for button callback
def __init__(self):
''' initialize the pin for the anemometer sensor '''
self.SENSOR_PIN = 4
self.count = 0
# tell the GPIO module that we want to use the chip's pin numbering scheme
GPIO.setmode(GPIO.BCM)
# setup pin as an input with pullup
GPIO.setup(self.SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# threaded event, to detect voltage falling on anemometer
# bouncetime is in ms - edges within this time will be ignored
GPIO.add_event_detect(self.SENSOR_PIN, GPIO.FALLING, bouncetime=30)
self.starttime = time.time()
# deal with events by calling a function
GPIO.add_event_callback(self.SENSOR_PIN, self.inputEventHandler)
def main():
#initial build out
GPIO.add_event_detect(17, GPIO.FALLING, callback=sensorCallback1)
stillAlive()
try:
while True:
time.sleep(1)
#logging.info('in sleep loop')
#push this off to definition
if(fm.clicks > 0):
pourDrinkEvent(fm.clicks)
except KeyboardInterrupt:
GPIO.cleanup()