The Blinker Object - Part I
bentejuy Thu Apr 9, 2015 Tags InterfaceGPIO Blinker

The Blinker object is a simple object that can send zeros or ones with a certain cadence from one to eight different channels. In this example we will look at its simplest use: controlling eight LEDs through a GPIO interface.

On the electrical level, the materials we need for this example are 1 or 2 Protoboards, 8 LEDs, 8 330Ω resistors, one ULN2804A (or similar) and as many wires as needed to make the connections as shown below in the electrical diagram.

The ULN2804A is used to supply power to the eight LED diodes, that we use in this example, and securely protect the outputs of our Raspberry Pi. These diodes will be fed a voltage of 5V DC and to limit the electrical current we will use eight resistors of 330Ω. We must make sure to orient the polarities of the LED diodes for they correct operation and must also remember to feed the ULN2804A with +5V and the ground connection. We only need to make the connections as indicated in the diagram, of course, in the order that we have chosen for the different GPIO channels.

To start with the Python code, we need to create the InterfaceManager object. This object will be responsible for managing the different interfaces that attempt to use the GPIO channels of our Raspberry Pi. Also, we will have to create an InterfaceGPIO object where we will define which GPIO channels we will use for our example. In this case, it will be eight ports configured as outputs.

Once this is done, we just need to create the Blinker object to associate with the InterfaceGPIO object we previously created. Also, we can define the cadence with which we want it to work and the initial value of the different output channels. Because the Blinker object works at bit level, the initial value must be an integer of as many bits as output channels we have configured, associating each bit to the channel previously defined in the InterfaceGPIO object. In this example we will see its simplest use; in the next installment we will see how to use other options with the Blinker object.

Enjoy!

Download Code
										
#!/usr/bin/env python
# -+- coding: utf-8 -+-
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#
# Name:          tests/blinker1
# Purpose:
#
# Created:       04/02/2015
# Modified:      12/17/2015
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

import sys
import time

from raspybot.devices.logic import Blinker
from raspybot.io.interface import InterfaceManager, InterfaceGPIO

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

def startinfo(dev):
    print 'Starting => {}'.format(dev)


def stopinfo(dev):
    print 'Stopping => {}'.format(dev)

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

manager = InterfaceManager()

iface = InterfaceGPIO(manager, pinout=(9, 25, 8, 11, 19, 16, 26, 20))
blinker = Blinker(iface, 'Blinker Test 1', delay=0.3, initial=0b10000001, start=startinfo, stop=stopinfo)

try:
    blinker.start()

    for x in xrange(50):
        if x == 10:
            blinker.stop()
            blinker.set_value(0b00001111)
            blinker.start()

        if x == 20:
            blinker.stop()
            blinker.set_value(0b01010101)
            blinker.start()

        if x == 30:
            blinker.stop()
            blinker.set_value(0b11011011)
            blinker.start()

        if x == 40:
            blinker.stop()
            blinker.set_value(0b11000011)
            blinker.start()

        time.sleep(1.5)

except KeyboardInterrupt:
    blinker.stop()

finally:
    manager.delete(iface)
    manager.cleanup()
										
									
Protoboard Schema