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

#define PIX_CHAR_WIDTH 13
#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      333333
// the above corresponds approx. to 2Hz
#define SPACE_GAP      500000
// the above corresponds approx. to 2Hz

unsigned char gaAlpha[][PIX_CHAR_WIDTH] = 
{
/*A*/   {73, 73, 0,  73, 73,  0, 127, 73, 73,127, 73, 73,127},
/*B*/   {73, 73, 0,  73, 73,  0,  79, 72, 72,127,  9,  9,121},
/*Z*/   {73, 0,  73, 0,  73,  0, 127, 73, 73,127, 73, 73,127}
};

unsigned char gaNumeric[][PIX_CHAR_WIDTH] = 
{
/*0*/   {73, 0,  73, 0,  73, 73, 127, 73, 73,127, 73, 73,127},
/*1*/   {73, 0,  73, 0,  73, 73, 127, 73, 73,127, 73, 73,127},
/*9*/   {73, 0,  73, 0,  73, 73, 127, 73, 73,127, 73, 73,127}
};

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

        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);
                                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);
                                putchar(rev(intchar));
                                fflush(stdout);
                                usleep(SPACE_GAP);
                        }
                }

                /* otherwise put in some space*/
                else 
                {
                   for(spacecount=0;spacecount<3;spacecount++){
                        putchar((int)0);
                        rev(0);  // just to see it on stderr
                        fflush(stdout);
                        usleep(SPACE_GAP);
                   }
                }
        }
        return 0;
}

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);
}

