#ifndef DISPLAY_DRIVER
#define DISPLAY_DRIVER
//
// display_driver.h
//
//
// The DisplayDriver class is used to update the 14-seg displays used
// by John Mack's Stock Ticker Tape.
// 
// The display chips contain 2 character with 14-segments each. Also there 
// are 2 decimal points and 4 external outputs.  This adds up to 34 bits that
// are sent serially to each chip.  Since we will never us the last two 
// external outputs pins, the last two bits are always zero.
// The update() routine compares the 10 "next" dwords with the "current" dwords.
// If they are different, it sents the new dword+00 to the display chip and
// update the curDisplay[] to match.

class DisplayDriver 
  {
  //
  // Constants
  //
  #define LED_ADDR0_PIN   3
  #define LED_ADDR1_PIN   4
  #define LED_ADDR2_PIN   5
  #define LED_ADDR3_PIN   6
  #define LED_ENABLE_PIN  7
  #define LED_DATA_PIN    8
  #define LED_CLOCK_PIN   9

  #define DIGITS_PER_CHIP       2
  #define CHIPS_PER_LINE        5
  #define CHAR_PER_LINE         10
  #define TOP_LINE              0
  #define BOTTOM_LINE           1
  #define NUM_OF_LINES          2
  #define CHAR_PER_DISPLAY      CHAR_PER_LINE * NUM_OF_LINES
  #define NUM_OF_DISPLAY_CHIPS  10

  // for wrapMode                // print(char) 
  #define VSCROLL               0x14  // '\x14' or '\S' Wrap and scroll like a TTY terminal.
  #define WRAP_1LINE            0x15  // '\x15' or '\w' Wrap around and stay on the same line.
  #define WRAP_2LINES           0x16  // '\x16' or '\W' Wrap end of line 2 to start of line 1.
  #define HSCOLL_1LINE          0x17  // '\x17' or '\h' Scroll just the bottom line.
  #define HSCOLL_2LINES         0x18  // '\x18' or '\H' Ticker-tape mode. Scroll both lines.
  #define DEFAULT_SCROLL_SPEED  150 // milliseconds per character

  // for printMode
  #define PRINT_MODE_OR         0x19  // '\x19' or '\O'
  #define PRINT_MODE_XOR        0x1A  // '\x1A' or '\X'
  #define PRINT_MODE_REPLACE    0x1B  // '\x1B' or '\R'

  #define HOME                  0x1E  // '\x1E' or '\H'
  #define BACKSPACE             0x1F  // '\x1F' or '\B'

  //
  // Member Data
  //

  private:
    long  curDisplay[NUM_OF_DISPLAY_CHIPS];
    long  nextDisplay[NUM_OF_DISPLAY_CHIPS];
    int   cursor; // 0 to 21
    int   wrapMode;
    int   printMode;
    char* msg_ptr;
    char* nextMsg_ptr;
    int   scrollSpeed;
    long  waitUntilMillis; // Used to regulate the display speed.

  public:
    //
    // Constructor
    //
    DisplayDriver();

    //
    // Member Functions
    //
    void  setHScrollSpeed( int speed );
    bool  print( char*  msg, bool  preempt );
    int   printQueueLen();
    bool  msgPump( long  millis );

  private:
    void  cls();
    void  vScroll();
    void  hScoll( int  line );
    void  printChar( char  c );
    void  update();

    void  dumpDisplay();
  };

#endif