//
// testmain.cpp - Test driver for the display_driver class for stock ticker tape.
//
#include "stdio.h"
#include "display_driver.h"
#include <Windows.h>

static int  chipNum = -1;
static int  pinArrLen = 1; // First entry has zeros. There is always a first entry.
static int  pins[71][10];    

void  digitalWrite( int pin, int level )
  {
  if( pin == 99  ||  pinArrLen > 70)
    {
    for( int i=3; i < 10; i++ )
      {
      printf( "pin%2.2d: ", i );
      for( int j=0; j<pinArrLen; j++ )
        printf( "%c", (pins[j][i]==0 ? '_' : '-') );
      printf( "\n" );
      // Copy the current levels down to the first entry.
      for( int p=0; p<10 ; p++ )
        pins[0][p] = pins[pinArrLen-1][p];
      }
    pinArrLen = 1;
    if( pin == 99 )
      {
      printf( "\n\n" );
      return;
      }
    }

  if( pin >= 0  &&  pin <= 9 )
    {
    // Copy the current level
    for( int p=0; p<10; p++ )
      pins[pinArrLen][p] = pins[pinArrLen-1][p];
    pins[pinArrLen][pin] = level;
    pinArrLen++;
    }
  }


#define LINE1 "AMD _  FSLR  _  F   _  T   _  ED   _  BAC  _  GE   _  C  _  NOK   _  "
#define LINE2 "  $9.14  $138.53 $12.11 $26.45  $45.27  $16.93  $16.76 $3.63   $13.19"
int main()
  {
  char            buf[2] = { 0, 0 };
  SYSTEMTIME      st;
  DisplayDriver   displayDriver;
  char*           topLine = LINE1;
  char*           botLine = LINE2;
  char            lines[sizeof(LINE1) + sizeof(LINE2) + 100];

  int i = 0;
  int j = 0;
  while( topLine[i] != '\0' )
    {
    if( botLine[i] == '.' )
      {
      lines[j++] = '\x1F'; // Put a backspace infront of the period.
      lines[j++] = '.';
      // ignor char in top line to keep allignment.
      }
    else
      {
      lines[j++] = topLine[i];
      lines[j++] = botLine[i];
      }
    i++;
    }
  lines[j++] = '\0';


  //displayDriver.print( "To be, or not to be. that is the question. Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune, Or to take arms against a sea of troubles, And by opposing end them? To die. to sleep. No more. and by a sleep to say we end The heart-ache and the thousand natural shocks That flesh is heir to, 'tis a consummation Devoutly to be wish'd. To die, to sleep. To sleep. perchance to dream: ay, there's the rub; For in that sleep of death what dreams may come When we have shuffled off this mortal coil, Must give us pause: there's the respect That makes calamity of so long life;", false );
  //displayDriver.print( "Don\x1F't Panic", true );
  displayDriver.print( "\x18", false );

  for(;;)
    {
    GetSystemTime( &st );
    int  millis  = (st.wHour*3600000) + (st.wMinute*60000) + (st.wSecond*1000) + st.wMilliseconds;
    
    if( displayDriver.printQueueLen() < 2 )
      displayDriver.print( lines, false );

    displayDriver.msgPump( millis );
    }

  //for( char c=32; c<128; c++ )
  //  {
  //  buf[0] = c;
  //  displayDriver.print( buf );
  //  int a;
  //  for( int i=0; i<30000000; i++ )
  //    a *= 1234;
  //  }

  return 0;
  }