/*! gcc -o mytest -O2 -Wall -I.. mytest.c -L.. -lparapin */
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include "parapin.h"

#define LED(x)      LP_PIN[(x) + 2]
#define msleep(x)   usleep((x) * 1000)

void sighandler(int sig)
{
    int i;
    if(sig == SIGINT) {
        for(i=0; i<6; i++) {
            clear_pin(LP_PIN[i + 2]);
        }
        exit(0);
    }
}

int main(void)
{
    int i, prev_i, dir;
    if(pin_init_user(LPT1) < 0) {
        fprintf(stderr, "failed to init parapin\n");
        return EXIT_FAILURE;
    }
    pin_output_mode(LP_DATA_PINS | LP_SWITCHABLE_PINS);

    signal(SIGINT, sighandler);

    i = 0;
    prev_i = 5;
    dir = -1;

    for(;;) {
        set_pin(LED(i));
        clear_pin(LED(prev_i));
        msleep(100);

        prev_i = i;
        if(i >= 5 || i <= 0) dir = -dir;
        i = (dir > 0 ? i + 1 : i - 1);
    }

    return 0;
}