Python RPi.GPIO.add_event_callback() Examples

The following are 5 code examples of RPi.GPIO.add_event_callback(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module RPi.GPIO , or try the search function .
Example #1
Source File: Midi.py    From OP_Manager with MIT License 7 votes vote down vote up
def startKeyEvent(self):
        print('Start Key Event Catcher')
        # Add return key to interrupt callback
        pins = [27, 23, 4, 17, 22, 5, 6]  # [L, R, C, U, D, A, B]
        # Add each to to key detect
        for i in pins:
            GPIO.add_event_detect(i, GPIO.RISING)

        # GPIO.add_event_callback(27, callback=self._semitone_down)
        # GPIO.add_event_callback(23, callback=self._semitone_up)
        # GPIO.add_event_callback(4, callback=self._reset_change)
        # GPIO.add_event_callback(17, callback=self._octave_up)
        # GPIO.add_event_callback(22, callback=self._octave_down)
        GPIO.add_event_callback(5, callback=self.stopInterruptCallBack)
        # GPIO.add_event_callback(6, callback=self.stopInterruptCallBack)

        # GPIO.add_event_callback(27, callback=self.leftKey)
        # GPIO.add_event_callback(23, callback=self.rightKey)
        # GPIO.add_event_callback(4, callback=self.centerKey)
        # GPIO.add_event_callback(17, callback=self.upKey)
        # GPIO.add_event_callback(22, callback=self.downKay)
        # GPIO.add_event_callback(5, callback=self.aKey)
        # GPIO.add_event_callback(6, callback=self.bKey) 
Example #2
Source File: gengpioin.py    From genmon with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, gpio = None, trigger = GPIO.FALLING, resistorpull = GPIO.PUD_UP, log = None, callback = None, uselibcallbacks = False, bouncetime = None):
        super(MyGPIOInput, self).__init__()
        self.Trigger = trigger
        self.ResistorPull = resistorpull
        self.GPIO = gpio
        self.log = log
        self.TimeoutSeconds = 1
        self.BounceTime = bouncetime
        self.Callback = callback
        self.UseLibCallbacks = uselibcallbacks
        self.Exiting = False

        try:

            GPIO.setmode(GPIO.BOARD)
            GPIO.setwarnings(True)
            GPIO.setup(gpio, GPIO.IN, pull_up_down=resistorpull)

            if callback != None and callable(callback):
                if self.BounceTime > 0:
                    GPIO.add_event_detect(gpio  = self.GPIO, edge = self.Trigger, bouncetime = self.BounceTime)
                else:
                    GPIO.add_event_detect(gpio  = self.GPIO, edge = self.Trigger)
                if self.UseLibCallbacks:
                    GPIO.add_event_callback(gpio = self.GPIO, callback = self.Callback)
                else:
                    # setup callback
                    self.Threads["GPIOInputMonitor"] = MyThread(self.GPIOInputMonitor, Name = "GPIOInputMonitor", start = False)
                    self.Threads["GPIOInputMonitor"].Start()

        except Exception as e1:
            self.LogErrorLine("Error in MyGPIOInput:init: " + str(gpio) + " : " + str(e1))

    #-----------------GPIOInputMonitor------------------------------------------ 
Example #3
Source File: moistureHandler.py    From aws-builders-fair-projects with Apache License 2.0 5 votes vote down vote up
def collect_temperature():    
    humidity, temperature = Adafruit_DHT.read_retry(sensor_temperature, channel_temperature)
    logger.info("humidity: {} temperature: {}".format(humidity, temperature))
    if humidity is not None and temperature is not None:
        return humidity, temperature
    logger.error("Falha ao ler dados do DHT11")
    return 0, 0
    

#GPIO.add_event_detect(channel, GPIO.BOTH)
#GPIO.add_event_callback(channel, collect_moisture) 
Example #4
Source File: temperatureHandler.py    From aws-builders-fair-projects with Apache License 2.0 5 votes vote down vote up
def collect_moisture(channel):
    # Efetua a leitura do sensor
    umid, temp = 0,0
    #umid, temp = Adafruit_DHT.read_retry(sensor, 4)    
    # Caso leitura esteja ok, mostra os valores na tela
    if umid is not None and temp is not None:
        print ("Temperatura = {0:0.1f}  Umidade = {1:0.1f}n").format(temp, umid)
        print ("Aguarda 5 segundos para efetuar nova leitura...n")
        publishTemperature(temp, umid)
    else:
        # Mensagem de erro de comunicacao com o sensor
        print("Falha ao ler dados do DHT11 !!!")

#GPIO.add_event_detect(channel, GPIO.BOTH)
#GPIO.add_event_callback(channel, collect_moisture) 
Example #5
Source File: motor_hat.py    From letsrobot with Apache License 2.0 4 votes vote down vote up
def setup(robot_config):
    global mh
    global motorA
    global motorB
    global turningSpeedActuallyUsed
    global dayTimeDrivingSpeedActuallyUsed
    global nightTimeDrivingSpeedActuallyUsed
    global secondsToCharge
    global secondsToDischarge
    global chargeIONumber
    global forward
    global backward
    global left
    global right
    global straightDelay
    global turnDelay

    GPIO.setmode(GPIO.BCM)
    chargeIONumber = robot_config.getint('motor_hat', 'chargeIONumber')
    GPIO.setup(chargeIONumber, GPIO.IN)
    secondsToCharge = 60.0 * 60.0 * robot_config.getfloat('motor_hat', 'charge_hours')
    secondsToDischarge = 60.0 * 60.0 * robot_config.getfloat('motor_hat', 'discharge_hours')

    forward = json.loads(robot_config.get('motor_hat', 'forward'))
    backward = times(forward, -1)
    left = json.loads(robot_config.get('motor_hat', 'left'))
    right = times(left, -1)

    straightDelay = robot_config.getfloat('robot', 'straight_delay')
    turnDelay = robot_config.getfloat('robot', 'turn_delay')


    if motorsEnabled:
        # create a default object, no changes to I2C address or frequency
        mh = Adafruit_MotorHAT(addr=0x60)
        #mhArm = Adafruit_MotorHAT(addr=0x61)
        atexit.register(turnOffMotors)
        motorA = mh.getMotor(1)
        motorB = mh.getMotor(2)

    # Initialise the PWM device
#    pwm = PWM(0x42)
#    pwm.setPWMFreq(60)    # Set frequency to 60 Hz
    
    turningSpeedActuallyUsed = robot_config.getint('motor_hat', 'turning_speed')
    dayTimeDrivingSpeedActuallyUsed = robot_config.getint('motor_hat', 'day_speed')
    nightTimeDrivingSpeedActuallyUsed = robot_config.getint('motor_hat', 'night_speed')

    if robot_config.getboolean('motor_hat', 'slow_for_low_battery'):
        GPIO.add_event_detect(chargeIONumber, GPIO.BOTH)
        GPIO.add_event_callback(chargeIONumber, sendChargeStateCallback)
        chargeCheckInterval = int(robot_config.getint('motor_hat', 'chargeCheckInterval'))
    
        if (robot_config.get('tts', 'type') != 'none'):
            schedule.repeat_task(60, reportBatteryStatus_task)
            schedule.repeat_task(17, reportNeedToCharge)
            schedule.task(chargeCheckInterval, sendChargeState_task)