#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define uchar unsigned char
#define uint unsigned int

void secint(void);			//timer isr per second
void print_byte(char byte);	//converted to binary formatted

struct{
	uint delay;
	uchar pattern;
} cycle[] = {

{1, 0x12},	//0
{3, 0x41},	//1
{1, 0x21},	//2
{5, 0x14},	//3
{3, 0xc1},	//4
{5, 0x1c}	//5
};

const char* bit_rep[16] = {
	[ 0] = "0000", [ 1] = "0001", [ 2] = "0010", [ 3] = "0011",
	[ 4] = "0100", [ 5] = "0101", [ 6] = "0110", [ 7] = "0111",
	[ 8] = "1000", [ 9] = "1001", [10] = "1010", [11] = "1011",
	[12] = "1100", [13] = "1101", [14] = "1110", [15] = "1111",
};

uchar walk, LEDS;

void main(void){
	/*initialize*/
	clock_t start_t = 0, end_t = 0;
	
	while(1){
		if(end_t-start_t){
			start_t = end_t;
			/*inerrupt function*/
			secint();
		}
		else{
			end_t = clock() / CLOCKS_PER_SEC;
		/*main program*/
			walk=2;
		}
	}
	printf("should not be here\n");
}

void secint(void){
	static uchar count;
	static uchar index;
	static uint countdown;
	uchar i;
	
	count++;
	printf("count = %d\n",count);
	LEDS=cycle[i].pattern; print_byte(LEDS);
	countdown = cycle[i].delay;
	
	if(--countdown == 0){
		index++;
		if(index==4)index=0;
		switch(index){
			case 1:
				if((walk&0x2) == 2){
					printf("should be here\n");
					walk = walk & 0xfd;
					printf("walk = %d\n", walk);
					i=index+3;
					printf("i = %d\n", i);
				}
				break;
			case 3:
				if((walk&0x1) == 1){
					walk= walk & 0xfe;
					i=index+2;
				}
				break;
			default: i=index;
		}
	}
}
void print_byte(char byte){
	printf("%s %s\n", bit_rep[byte>>4], bit_rep[byte&0x0f]);
}
