The Blinker Object - Part II
bentejuy Mon Apr 13, 2015 Tags InterfaceGPIO Blinker

As we saw in the previous post, the basic function of the "Blinker" object is to control the states of on and off of one or more channels autonomously. With it, we can control a few LEDs, a single LED, relays, or any other device that we want, reversing its value based on the seconds that we have defined.

But besides this, we can get other types of effects with the "callback" function. This function is passed to the object "Blinker" at the time in which it is declared by the parameter "checker", but we can decide at any time switch it to another function. This function receives an integer and we have to return another integer with the new states of the different channels.

As we explained in the post The Blinker Object - Part I, the Blinker object works at bit level and it associates each bit to a GPIO channel. Therefore, the state of each channel will depend on the bits which are set to either the integer one or zero as returned by the associated function.

To demonstrate how it works, I will take the electrical diagram from the previous post, so you can see how easy it would be to get different effects with minimal effort; the only limitations are this module, Python, our Raspberry Pi and our knowledge.

In the Python code, I have defined six different functions: "random" that simply generates random numbers within the range of 8 bits. The "shift2left", "shiftright" and "pingpong" functions show a simple effect of bit shift. And in the "kitt" and “vumeter” functions, I attempt to simulate the effect of "Kitt" lights. For young folks, "Kitt" was the protagonist of the series "Knight Rider", watch this video if you want more information. ;)

That's all for now!.

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

import time
import random

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)

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

flag = False

def do_random(value, count):
    return random.randint(0x00, 0xFF)


def do_shift2left(value, count):
    if value < 128:
        return value << 1
    else:
        return 1


def do_shift2right(value, count):
    if value:
        return value >> 1
    else:
        return 128


def do_pingpong(value, count):
    global flag

    if flag:
        flag = value > 2
        return value >> 1
    else:
        flag = value >= 64
        return value << 1


# For more info see : https://www.youtube.com/watch?v=wAoKzfbGnCc
def do_kitt(value, count):
    global flag

    if flag:
        flag = value > 7
        return value >> 1
    else:
        flag = value >= 96
        return value << 1


def do_vumeter(value, count):
    x = (0, 24, 60, 126, 255)

    if value == 0:
        return 24
    elif value == 255:
        return 126
    else:
        return x[x.index(value) + (random.randint(0, 1) and 1 or -1)]

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

manager = InterfaceManager()

iface = InterfaceGPIO(manager, pinout=(9, 25, 11, 8, 19, 16, 26, 20))
blinker = Blinker(iface, 'Blinker Test 2', delay=.1, checker=do_random, start=startinfo, stop=stopinfo)

try:
    blinker.start()

    for x in xrange(60):
        if x == 10:
            blinker.stop()
            blinker.set_value(1)
            blinker.set_callback(do_shift2left)
            blinker.start()

        if x == 20:
            blinker.stop()
            blinker.set_value(128)
            blinker.set_callback(do_shift2right)
            blinker.start()

        if x == 30:
            flag = False
            blinker.stop()
            blinker.set_value(1)
            blinker.set_callback(do_pingpong)
            blinker.start()

        if x == 40:
            flag = False
            blinker.stop()
            blinker.set_value(3)
            blinker.set_callback(do_kitt)
            blinker.start()

        if x == 50:
            flag = False
            blinker.stop()
            blinker.set_value(0)
            blinker.set_callback(do_vumeter)
            blinker.start()

        time.sleep(1.5)

except KeyboardInterrupt:
    blinker.stop()

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