more rpi gpio code using wiring pi

May 27, 2016

My second little C-language app using Wiring Pi as the enabling framework. This one flashes the LEDs in a more elaborate pattern and uses bit patterns in a byte array that we move down. The bit patterns in the low nibble of each byte allow any (or all) of the LEDs to be turned on or off. Code follows.

#include <wiringPi.h>

// Instead of hard coding a blink pattern, we use bit patterns in a byte
// array, which can be set to any low nibble value. A '1' turns on an LED
// in that corresponding position, a '0' turns it off.
//
static unsigned char glyphs[] = { 0x0, 0x1, 0x3, 0x7, 0xf, 0xe, 0xc, 0x8,
                                  0x0, 0x8, 0xc, 0xe, 0xf, 0x7, 0x3, 0x1 };

int main () {
    wiringPiSetup();

    pinMode(0, OUTPUT);
    pinMode(1, OUTPUT);
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);

    for (int i = 0; i < 20; ++i) {
        for (int j = 0; j < sizeof(glyphs); ++j ) { 
            // Just a series of shifts and ANDs to create the bit necessary 
            // to write out to the GPIO pin.
            //
            digitalWrite(0, glyphs[j] & 0x1); 
            digitalWrite(1, (glyphs[j] >> 1 ) & 0x1 );
            digitalWrite(2, (glyphs[j] >> 2 ) & 0x1 );
            digitalWrite(3, (glyphs[j] >> 3 ) & 0x1 );

            delay(50);
        }
    }

    // Turn everything off.
    //
    digitalWrite(0, LOW);
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);

    return 0 ;
}