/*=================================================================
Program: captPike.c
Purpose: To flash an LED indicating an IP address.
Usage  : sudo ./captPIke wlan0 (example: could be eth0, lo, etc...)
Author : Bob Schumann (SampleBandit)
Date   : July 2013
Notes  : Prior to calling this program, a script runs the command
       : ifconfg > ifconfig.txt. This program opens that text file
       : and searches for the ip address that matches the command
       : line argument interface (see Usage above ^ ) Using the
       : wiringPi component requires the 'sudo' preface.
       :
       : The captPIke blink protocol is as follows:
       : ------------------------------------------
       : 3 seconds 'on' as a start 'get ready' indicator
       : digits are displayed as half second 'on'/'off'
       : space between digits is a second and a half
       : 'dots' are displayed as a 150 millisecond 'blip'
       : Note: digit 0 (zero) is displayed as 10 blinks
       :       you can't blink zero times!
Thanks : Gordon Henderson for wiringPi
=================================================================*/

#include <stdio.h>
#include <string.h>
#include <wiringPi.h>

int main(int argc, char *argv[])
{
    int  i, j;
    char *infilename = "ifconfig.txt";
    FILE *infile;
    char line_buffer[BUFSIZ];
    char *search = "inet addr:";
    char *found;
    int  ip[15];
    int  ip_count;

    /* open text file */
    infile = fopen(infilename, "r");

    /* wiringPi settings */
    wiringPiSetup();
    pinMode(1,OUTPUT);

    /* get to the section where argument interface is defined */
    while (fgets(line_buffer, sizeof(line_buffer), infile)) {
        found = strstr(line_buffer, argv[1]);
        if (found != NULL) {
            /* the next line has the ip address */
            fgets(line_buffer, sizeof(line_buffer), infile);
            /* the ip address is after "inet addr:" */
            found = strstr(line_buffer, search);
            found += strlen(search);
            /* copy ip to array */
            i=0;
            while (found[i] != 32) {
                /* normalize ASCII to integer */
                ip[i]=found[i]-48;
                i++;
            }
            ip_count=i;
        }
    }

    /* close text file */
    fclose(infile);

    /* 3 second start indicator */
    digitalWrite(1, HIGH);
    delay(3000);
    digitalWrite(1, LOW);
    delay(3000);
    /* display IP to LED */
    for (i=0;i<ip_count;i++) {
        if (ip[i] >= 0) {
            /* digits */
            if (ip[i] == 0)
                /* Zero is special case blink 10 times */
                ip[i]=10;
            for (j=0;j<ip[i];j++) {
                digitalWrite(1, HIGH);
                delay(500);
                digitalWrite(1, LOW);
                delay(500);
            }
                delay(1500);
        }
        else
        {
            /* dot */
            digitalWrite(1, HIGH);
            delay(150);
            digitalWrite(1, LOW);
            delay(1500);
        }
    }
    return(0);
}
