Playing with the FlipFlop Object
bentejuy Sat May 16, 2015 Tags InterfaceGPIO FlipFlop

The object Flipflop is used to store indefinitely the state of an undefined number of bits, like a small memory or register of bits. The length of the bits stored depends on the type of interface associated with it or the number of channels reserved in that interface.

On the electronic level, the flipflop is one of the most basic elements in digital electronics. It's typically used to store data at the bit level or as frequency dividers, and they have other applications as well. They are currently not often seen in electronic schemes, but they are widely integrated into CPUs, GPUs and other types of complex electronic components. It works very simply; there are some variations depending on its configuration. For those who want more information about flipflop, this Wikipedia article has more details: FlipFlop( electronics).

To be honest the library FlipFlop object doesn't work exactly like a real FlipFlop, but it tries to simulate operation with calls to functions of the class. This object is associated with an "Interface" class, and this "Interface" class is associated with a series of channels or it has a certain length of bits. These channels or bits will be those that interact with our object.

To simulate the operation of a real flipflop, the object has three functions: "set", "reset" and "toggle". The "set" and "reset" functions set one or more channels to up or down, or if they are called without parameters, sets all bits/channels on up or down. We can call these functions with an integer, a tuple or list of integers representing the index in the list of bits/channel, and the values of the bits stored will change depending on what function is called. Also, the "toggle" function changes the value of each bit to its opposite value.

In this example, and taking advantage of the electrical design of the previous blog entry with its connections to our Raspberry Pi, its operation is shown. We will use four buttons through the "Buttons" object. I have assigned a function to each channel: the GPIO channel 17 calls the "set" function and the GPIO channel 18 calls the "reset" function. The "toggle" function will be called by the GPIO channel 4, and I assign some small checks to change the status of certain bits with the GPIO channel 14. You can see it working in the video.


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

import time

from raspybot.devices.logic import FlipFlop
from raspybot.devices.button import Buttons
from import InterfaceManager, InterfaceGPIO


def on_action(obj, channel, action):
    global index, flipflop

    if channel == 4:
        print  'Toggle all channels in flipflop'

    elif channel == 14:
        if flipflop.get() & (1 << index):
            print 'Reset the channel number {}'.format(index)

            print 'Set the channel number {}'.format(index)

        if index >= 7:
            index = 0
            index += 1

        print index, bin(flipflop.get())

    elif channel == 17:
        index = 0
        print 'Set to 1 all channels in flipflop'

        index = 0
        print 'Reset to 0 all channels in flipflop'


index = 0
manager = InterfaceManager()

iface1 = InterfaceGPIO(manager, pinin=(4, 14, 17, 18))
iface2 = InterfaceGPIO(manager, pinout=(9, 25, 11, 8, 19, 16, 26, 20))

buttons = Buttons(iface1, 'Buttons Object', release=on_action)
flipflop = FlipFlop(iface2, 'FlipFlop Object')

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

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

    while True:

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


Protoboard Schema