The Buttons Object
bentejuy Sat May 2, 2015 Tags InterfaceGPIO Buttons

The Buttons object is an object that allows us to configure one or more GPIO channels as input to manage state changes and events in the input channels.

Basically it's a simple object that allows us to abstract and encapsulate the calls to module RPi.GPIO. When we created the object "Buttons", we have to pass as the first parameter an "InterfaceGPIO" object which we previously defined with one or more channels as inputs, we also had to define one or more "callback" that we must be defined in the parameters "clicked", "pressed" or "release".

The next step will be to associate or define the configuration of those channels that we previously defined in InterfaceGPIO object with the "setup" function. Here, we can define for each channel the function we will want to call when the state changes and state we want to control (CLICKED, PRESSED or RELEASE). Also we can define the initial PUD state and the value of "bouncetime".

Example:
		
iface = InterfaceGPIO(...)
buttons = Buttons(iface, name="My Buttons Objects", clicked=on_clicked, release=on_release)

buttons.setup(pin=5, mode=buttons.RELEASE, pud=buttons.PUD_UP)
buttons.setup(pin=6, mode=buttons.CLICKED, pud=buttons.PUD_DOWN, bouncetime=500)
buttons.setup(7, buttons.CLICKED, gpio.PUD_UP)

def on_clicked(obj, channel, action):
	''' do something '''

def on_release(obj, channel, action):
	''' do something '''
		
	

The "callback" function takes three parameters in this order: the object "Buttons" from where the function is called, the channel that generated the event and the event type received, for example a Buttons.PRESSED or Buttons.CLICKED event. In this way, with a single "callback" we can control more than one object "Buttons", more than one channel and/or more than one event.

Example:
		
iface = InterfaceGPIO(...)
buttons = Buttons(iface, name="My Buttons Objects", clicked=on_action, release=on_action)

buttons.setup(5, buttons.RELEASE, buttons.PUD_UP)
buttons.setup(6, buttons.CLICKED, buttons.PUD_UP)
buttons.setup(7, buttons.CLICKED, buttons.PUD_UP)

def on_action(obj, channel, action):
	print "The {} Buttons receive the action {} in channel {}".format(obj, action, channel)
	
	if channel == 5:
		pass
	elif channel == 6:
		pass
	elif channel == 7:
		pass
		
	

At the bottom of the page I show a simple design consisting of 4 mini push buttons, 4 capacitors of 100nF, 4 resistors of 5k6Ω and another 4 resistors of 10KΩ. We will work with a voltage of +5V DC and to avoid damaging the GPIO input channels of our Raspberry Pi that only works with +3.3V DC, we will use two resistors in series as a voltage divider, this way when the push button is at rest the maximum voltage input received in the GPIO channels will be approximately 3.2V DC. The function of the capacitor is to eliminate any voltage spikes the push buttons may cause. The logic circuit is negative, that is, that in repose the GPIO input has a logic 1 (+3.2V), and when pressed the button will have a logic 0 (Ground), for this reason we must configure the GPIO channels as PUD_UP, if the logic of it is positive we have to define the channels as PUD_DOWN.

Hope you enjoyed!

Download Code
										
#!/usr/bin/env python
# -+- coding: utf-8 -+-
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#
# Name:          tests/buttons.py
# Purpose:
#
# Created:       05/01/2015
# Modified:      12/17/2015
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

import time

from raspybot.devices.button import Buttons
from raspybot.io.interface import InterfaceManager, InterfaceGPIO

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

def on_action(obj, channel, action):
    print '"{}" receives the action "{}" in channel "{}"'.format(obj.get_name(), action, channel)

    if channel == 4:
        print 'Action 1'
    elif channel == 14:
        print 'Action 2'
    elif channel == 17:
        print 'Action 3'
    else:
        print 'From where has come this channel => {} ?'.format(channel)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

manager = InterfaceManager()

iface = InterfaceGPIO(manager, pinin=(4, 14, 17, 18))
buttons = Buttons(iface, name='My Buttons Object', clicked=on_action, release=on_action)

buttons.setup(4, buttons.RELEASE, buttons.PUD_UP)
buttons.setup(14, buttons.CLICKED, buttons.PUD_UP)
buttons.setup(17, buttons.RELEASE, buttons.PUD_UP)
buttons.setup(18, buttons.CLICKED, buttons.PUD_UP)

print 'Program started'
print 'Press CTRL-C to interrupt the program....'

try:
    while True:
        time.sleep(1)

except KeyboardInterrupt:
    print '\nProgram stopped...'

finally:
    manager.delete(iface)
    manager.cleanup()
										
									
Protoboard Schema
										
root@raspberrypi#python buttons.py
Program started
Press CTRL-C to interrupt the program....
"My Buttons Object" receive the action "2" in channel "4"
Action 1
"My Buttons Object" receive the action "0" in channel "14"
Action 2
"My Buttons Object" receive the action "2" in channel "17"
Action 3
"My Buttons Object" receive the action "0" in channel "18"
From where has come this channel => 18 ?
^C
Program stopped...
root@raspberrypi#