#use rs232(BAUD=9600,XMIT=PIN_C6,RCV=PIN_C7)
#define SIMDISPLAY_DELAY 100

int display[6]={0,0,0,0,0,0};

// inserts a column to the right of the display, throwing out the
// leftmost column. this is useful for scrolling things. c contains
// the least significant 6 bits being the column to insert. the LSB
// is the bottom and the MSB is the top.
void disp_insertcolumn(int c) {
display[0]=(display[0]>>1)|((c&0b100000));
display[1]=(display[1]>>1)|((c&0b010000)<<1);
display[2]=(display[2]>>1)|((c&0b001000)<<2);
display[3]=(display[3]>>1)|((c&0b000100)<<3);
display[4]=(display[4]>>1)|((c&0b000010)<<4);
display[5]=(display[5]>>1)|((c&0b000001)<<5);
}

// clears the internal copy of the 6x6 pixels.
void disp_clear() {
  display[0]=0;
  display[1]=0;
  display[2]=0;
  display[3]=0;
  display[4]=0;
  display[5]=0;
}

// sends the internal copy of the 6x6 pixels to the display.
// you need to call this to get anything to change at all on the display.
void disp_sendarray() {
  #ifdef SIMDISPLAY_DEBUG
  int i,j;
  for(j=0;j<6;j++) {
    for(i=0;i<6;i++) putc(((display[j]>>i)&1)*3+32);
    putc('\n');
  }
  putc('_');
  putc('_');
  putc('_');
  putc('_');
  putc('_');
  putc('_');
  putc('\n');
  #else
  putc(0xFE);
  putc(1);
  putc(display[0]);
  putc(display[1]);
  putc(display[2]);
  putc(0xFE);
  putc(0);
  putc(display[3]);
  putc(display[4]);
  putc(display[5]);
  #endif
}

// will cause a character to scroll across the display. you can
// chain them up: scrollchar('H');scrollchar('E');scrollchar('L');...
void scrollchar(char s) {
int i;
const int chartable[104]={ // 26 uppercase letters
0b011111,
0b100100,
0b011111,
0b000000,
0b111111,
0b101001,
0b010110,
0b000000,
0b011110,
0b100001,
0b100001,
0b000000,
0b111111,
0b100001,
0b011110,
0b000000,
0b111111,
0b101001,
0b101001,
0b000000,
0b111111,
0b101000,
0b101000,
0b000000,
0b011110,
0b100001,
0b101111,
0b000000,
0b111111,
0b001000,
0b111111,
0b000000,
0b100001,
0b111111,
0b100001,
0b000000,
0b000110,
0b000001,
0b111110,
0b000000,
0b111111,
0b001100,
0b110011,
0b000000,
0b111111,
0b000001,
0b000001,
0b000000,
0b111111,
0b011000,
0b111111,
0b000000,
0b111111,
0b100000,
0b111111,
0b000000,
0b011110,
0b100001,
0b011110,
0b000000,
0b111111,
0b101000,
0b010000,
0b000000,
0b011110,
0b100011,
0b011101,
0b000000,
0b111111,
0b101100,
0b010011,
0b000000,
0b011001,
0b101101,
0b100110,
0b000000,
0b100000,
0b111111,
0b100000,
0b000000,
0b111111,
0b000001,
0b111111,
0b000000,
0b111100,
0b000011,
0b111100,
0b000000,
0b111111,
0b000110,
0b111111,
0b000000,
0b110011,
0b001100,
0b110011,
0b000000,
0b110000,
0b001111,
0b110000,
0b000000,
0b100011,
0b101101,
0b110001,
0b000000
};
  if(s>='A'&&s<='Z') {
    for(i=0;i<4;i++) {
      restart_wdt();
      disp_insertcolumn(chartable[4*(s-'A')+i]);
      disp_sendarray();
      #ifndef SIMDISPLAY_DEBUG
      delay_ms(SIMDISPLAY_DELAY);
      #endif
    }
  } else if(s==' ') {
    for(i=0;i<4;i++) {
      restart_wdt();
      disp_insertcolumn(0b000000);
      disp_sendarray();
      #ifndef SIMDISPLAY_DEBUG
      delay_ms(SIMDISPLAY_DELAY);
      #endif
    }
  } else if(s=='-') {
    restart_wdt();
    disp_insertcolumn(0b001000);
    disp_sendarray();
    #ifndef SIMDISPLAY_DEBUG
    delay_ms(SIMDISPLAY_DELAY);
    #endif
    restart_wdt();
    disp_insertcolumn(0b001000);
    disp_sendarray();
    #ifndef SIMDISPLAY_DEBUG
    delay_ms(SIMDISPLAY_DELAY);
    #endif
    restart_wdt();
    disp_insertcolumn(0b001000);
    disp_sendarray();
    #ifndef SIMDISPLAY_DEBUG
    delay_ms(SIMDISPLAY_DELAY);
    #endif
    restart_wdt();
    disp_insertcolumn(0b000000);
    disp_sendarray();
    #ifndef SIMDISPLAY_DEBUG
    delay_ms(SIMDISPLAY_DELAY);
    #endif
  }
}

// scrolls a null-terminated text string.
// PICC doesn't currently support
// const char pointers, so you'll have to accomplish that by:
// strcpy(q,"HELLO");scrolltext(q); for example.
void scrolltext (char *s) {
  while(*s!=0) scrollchar(*s++);
}
