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

#define PIX_CHAR_WIDTH 5
#define BEGIN_NUMERIC  '0'
#define END_NUMERIC    '9'
#define BEGIN_ALPHA        'A'
#define END_ALPHA          'Z'
#define SPACE_GAP      55000
// the above corresponds approx. to 18Hz
#define SPACE_GAP       100000
// the above corresponds approx. to 10Hz
#define SPACE_GAP       200000
// the above corresponds approx. to 5Hz
#define SPACE_GAP       250000
// the above corresponds approx. to 4Hz
#define SPACE_GAP      500000
// the above corresponds approx. to 2Hz
#define SPACE_GAP      333333
// the above corresponds approx. to 2Hz

unsigned char gaAlpha[][PIX_CHAR_WIDTH] = 
{
/*A*/   {252,18 ,17,  18,252}, 
/*B*/   {255,137,137,137,118},
/*C*/   {126,129,129,129, 66},
/*D*/   {255,129,129, 66, 60},
/*E*/   {255,137,137,129,  0},
/*F*/   {255,  9,  9,  1,  0},
/*G*/   {126,129,145, 81,242},
/*H*/   {255, 16, 16, 16,255},
/*I*/   {129,255,129,  0,  0},
/*J*/   { 64,128,129,127,  1},
/*K*/   {255, 24, 36, 66,129},
/*L*/   {255,128,128,128,  0},
/*M*/   {255,  2, 12,  2,255},
/*N*/   {255,  6, 24, 96,255},
/*O*/   {126,129,129,129,126},
/*P*/   {255, 17, 17, 17, 14},
/*Q*/   {126,129,161, 65,190},
/*R*/   {255, 17, 17, 49,206},
/*S*/   { 78,137,137,137,114},
/*T*/   {  1,  1,255,  1,  1},
/*U*/   {127,128,128,128,127},
/*V*/   { 31, 96,128, 96, 31},
/*W*/   { 63,192, 56,192, 63},
/*X*/   {195, 36, 24, 36,195},
/*Y*/   {  7,  8,240,  8,  7},
/*Z*/   {225,145,137,133,131}
};

unsigned char gaNumeric[][PIX_CHAR_WIDTH] = 
{
/*0*/   {126,145,137,133,126},
/*1*/   {130,255,128,  0,  0},
/*2*/   {198,161,145,137,134},
/*3*/   { 66,129,137,137,118},
/*4*/   { 56, 36, 34,255, 32},
/*5*/   { 79,137,137,137,113},
/*6*/   {126,137,137,137,113},
/*7*/   {  1,  1,241, 13,  3},
/*8*/   {118,137,137,137,118},
/*9*/   { 14, 17, 17, 17,255}
};

int main(int argc, char *argv[])
{
        int  i           = 0;
        int  iChar       = 0;
        int  iCharIndex  = 0;
        int  intchar;
	int  fd;

	fd = open("/dev/ibus0", O_RDWR);
	if (fd == -1) {
		fprintf(stderr, "Error opening ibus0 for writing\n");
		exit(1);
	}

        for(;;)
        {
                iChar = getchar();

                if (iChar == EOF)
                        break;

                /* uppercase the chars... */
                iChar = toupper(iChar);

                /* ALPHA */
                if (iChar >= BEGIN_ALPHA && 
                        iChar <= END_ALPHA     )
                {
                        iCharIndex = iChar - BEGIN_ALPHA;

                        for (i=0;i < PIX_CHAR_WIDTH; i++)
                        {
                                /* push out gaAlpha[iCharIndex][i] */
                            //  putchar((int)gaAlpha[iCharIndex][i]);
                                intchar = (int)gaAlpha[iCharIndex][i];
                            //    rev(intchar);  // just to see the bits
                            //    putchar(intchar);
				putpush(fd, intchar);
                                putchar(rev(intchar));
                                fflush(stdout);
                                usleep(SPACE_GAP);
                        }
                }

                /* NUMERIC */
                else if (iChar >= BEGIN_NUMERIC && 
                             iChar <= END_NUMERIC     )
                {
                        iCharIndex = iChar - BEGIN_NUMERIC;

                        for (i=0; i < PIX_CHAR_WIDTH; i++)
                        {
                                /* push out gaNumeric[iCharIndex][i] */
                            //    putchar((int)gaNumeric[iCharIndex][i]);
                                intchar = (int)gaNumeric[iCharIndex][i];
                            //    rev(intchar);  // just to see the bits
                            //    putchar(intchar);
				putpush(fd, intchar);
                                putchar(rev(intchar));
                                fflush(stdout);
                                usleep(SPACE_GAP);
                        }
                }

                /* otherwise assume space */
                else 
                {
                        /* push out 0 five times */
			putpush(fd, (int)0);
                        putchar((int)0);
                        rev(0);  // just to see it on stderr
                        fflush(stdout);
                        usleep(SPACE_GAP);

			putpush(fd, (int)0);
                        putchar((int)0);
                        rev(0);  // just to see it on stderr
                        fflush(stdout);
                        usleep(SPACE_GAP);

			putpush(fd, (int)0);
                        putchar((int)0);
                        rev(0);  // just to see it on stderr
                        fflush(stdout);
                        usleep(SPACE_GAP);

			putpush(fd, (int)0);
                        putchar((int)0);
                        rev(0);  // just to see it on stderr
                        fflush(stdout);
                        usleep(SPACE_GAP);
 
			putpush(fd, (int)0);
                        putchar((int)0);
                        rev(0);  // just to see it on stderr
                        fflush(stdout);
                        usleep(SPACE_GAP);
                }

                /* intra-character space */
                /* push out 0 two times */
		putpush(fd, (int)0);
                putchar((int)0);
                rev(0);  // just to see it on stderr
                fflush(stdout);
                usleep(SPACE_GAP);
		
		putpush(fd, (int)0);
                putchar((int)0);
                rev(0);  // just to see it on stderr
                fflush(stdout);
                usleep(SPACE_GAP);

        }

        return 0;
}

void putpush(int fd, int c) {
	write(fd, &c, 1);
}

int rev(int in) {
//  int in;
  int out;
  int b0=0;
  int b1=0;
  int b2=0;
  int b3=0;
  int b4=0;
  int b5=0;
  int b6=0;
  int b7=0;
  if (in>127) {b7=1; in=in-128; fprintf(stderr,"+");} else fprintf(stderr," ");
  if (in> 63) {b6=1; in=in- 64; fprintf(stderr,"+");} else fprintf(stderr," ");
  if (in> 31) {b5=1; in=in- 32; fprintf(stderr,"+");} else fprintf(stderr," ");
  if (in> 15) {b4=1; in=in- 16; fprintf(stderr,"+");} else fprintf(stderr," ");
  if (in>  7) {b3=1; in=in-  8; fprintf(stderr,"+");} else fprintf(stderr," ");
  if (in>  3) {b2=1; in=in-  4; fprintf(stderr,"+");} else fprintf(stderr," ");
  if (in>  1) {b1=1; in=in-  2; fprintf(stderr,"+");} else fprintf(stderr," ");
  if (in>  0) {b0=1;            fprintf(stderr,"+");} else fprintf(stderr," ");
  fprintf(stderr, "\n");
//  fprintf(stderr, "%d%d%d%d%d%d%d%d\n", b0,b1,b2,b3,b4,b5,b6,b7);
  out = b7 + 2*b6 + 4*b5 + 8*b4 + 16*b3 + 32*b2 + 64*b1 +128*b0;
//  fprintf(stderr, "output number= %03d\n", out);
  return(out);
}

