///////////////////////////////////////////////////////////////////////
//                     GLCD - modified.C
//
//  How to use: 1. Create a new file in your CCS C library folder
//              titled 'GLCD - modified.C'
//              2. Copy all of the code from your 'GLCD.C' file to the
//              new 'GLCD - modified.C' file.
//              3. Paste the following code into the appropriate places
//              4. Replace all "output_D" with "output_B"
//              5. Replace all "input_D" with "input_B"
//
//  Why all this work?  The GLCD.C file is copyrighted Custom Computer
//  Services, so I cannot distribute the library to other individuals
//  unless they are licensed users of the CCS C compiler.  Sorry!
//
//  Version 1.1 08/21/2006
//              Steven Cholewiak (www.semifluid.com) modified the
//              code for operation with a PIC18F2550 by changing
//              control lines and changing the data port to Port B.
//              He also added a 3x5 font, which was based upon the
//              font format from http://instruct1.cit.cornell.edu/
//              courses/ee476/video/Video32v2.c and modified to
//              include characters defined from http://mckoss.com/
//              jscript/tinyalice.htm
///////////////////////////////////////////////////////////////////////


//================================
// Paste in the top of your 'GLCD - modified.C' file!
//================================
#define GLCD_DI  PIN_C0   // Data or Instruction input
#define GLCD_RW  PIN_C1   // Read/Write
#define GLCD_E   PIN_C2   // Enable
#define GLCD_CS1 PIN_A3   // Chip Selection 1
#define GLCD_CS2 PIN_A4   // Chip Selection 2
#define GLCD_RST PIN_A5   // Reset

//================================
// Paste above "const int8 TEXT[51][5]"
//================================
//3x5 Font packed two per definition
//Original format from http://instruct1.cit.cornell.edu/courses/ee476/video/Video32v2.c
// - Modified by Steven Cholewiak (www.semifluid.com)
//    - 08/08/06 - Included punctuation characters
const int8 TEXT35[95][5]={
   0b00000000,	0b00000000,	0b00000000,	0b00000000,	0b00000000, //SPACE
   0b01000100,	0b01000100,	0b01000100,	0b00000000,	0b01000100, //!
   0b10101010,	0b10101010,	0b00000000,	0b00000000,	0b00000000, //"
   0b10101010,	0b11101110,	0b10101010,	0b11101110,	0b10101010, //#
   0b01100110,	0b11001100,	0b11001100,	0b01100110,	0b11101110, //$
   0b10101010,	0b00100010,	0b01000100,	0b10001000,	0b10101010, //%
   0b01000100,	0b10101010,	0b01000100,	0b10101010,	0b01100110, //&
   0b01000100,	0b01000100,	0b00000000,	0b00000000,	0b00000000, //'
   0b01000100,	0b10001000,	0b10001000,	0b10001000,	0b01000100, //(
   0b01000100,	0b00100010,	0b00100010,	0b00100010,	0b01000100, //)
   0b00000000,	0b10101010,	0b01000100,	0b10101010,	0b00000000, //*
   0b00000000,	0b01000100,	0b11101110,	0b01000100,	0b00000000, //+
   0b00000000,	0b00000000,	0b00000000,	0b01000100,	0b10001000, //,
   0b00000000,	0b00000000,	0b11101110,	0b00000000,	0b00000000, //-
   0b00000000,	0b00000000,	0b00000000,	0b00000000,	0b01000100, //.
   0b00100010,	0b00100010,	0b01000100,	0b10001000,	0b10001000, ///
   0b11101110,	0b10101010,	0b10101010,	0b10101010,	0b11101110, //0
	0b01000100,	0b11001100,	0b01000100,	0b01000100,	0b11101110, //1
	0b11101110,	0b00100010,	0b11101110,	0b10001000,	0b11101110, //2
	0b11101110,	0b00100010,	0b11101110,	0b00100010,	0b11101110, //3
	0b10101010,	0b10101010,	0b11101110,	0b00100010,	0b00100010, //4
	0b11101110,	0b10001000,	0b11101110,	0b00100010,	0b11101110, //5
	0b11001100,	0b10001000,	0b11101110,	0b10101010,	0b11101110, //6
	0b11101110,	0b00100010,	0b01000100,	0b10001000,	0b10001000, //7
	0b11101110,	0b10101010,	0b11101110,	0b10101010,	0b11101110, //8
	0b11101110,	0b10101010,	0b11101110,	0b00100010,	0b01100110, //9
	0b00000000,	0b01000100,	0b00000000,	0b01000100,	0b00000000, //:
	0b00000000,	0b01000100,	0b00000000,	0b01000100,	0b10001000, //;
	0b00100010,	0b01000100,	0b10001000,	0b01000100,	0b00100010, //<
	0b00000000,	0b11101110,	0b00000000,	0b11101110,	0b00000000, //=
	0b10001000,	0b01000100,	0b00100010,	0b01000100,	0b10001000, //>
	0b11001100,	0b00100010,	0b01100110,	0b00000000,	0b01000100, //?
	0b01000100,	0b10101010,	0b11101110,	0b10001000,	0b01100110, //@
	0b11101110,	0b10101010,	0b11101110,	0b10101010,	0b10101010, //A
	0b11001100,	0b10101010,	0b11101110,	0b10101010,	0b11001100, //B
	0b11101110,	0b10001000,	0b10001000,	0b10001000,	0b11101110, //C
	0b11001100,	0b10101010,	0b10101010,	0b10101010,	0b11001100, //D
	0b11101110,	0b10001000,	0b11101110,	0b10001000,	0b11101110, //E
	0b11101110,	0b10001000,	0b11101110,	0b10001000,	0b10001000, //F
	0b11101110,	0b10001000,	0b10001000,	0b10101010,	0b11101110, //G
	0b10101010,	0b10101010,	0b11101110,	0b10101010,	0b10101010, //H
	0b11101110,	0b01000100,	0b01000100,	0b01000100,	0b11101110, //I
	0b00100010,	0b00100010,	0b00100010,	0b10101010,	0b11101110, //J
	0b10001000,	0b10101010,	0b11001100,	0b11001100,	0b10101010, //K
	0b10001000,	0b10001000,	0b10001000,	0b10001000,	0b11101110, //L
	0b10101010,	0b11101110,	0b11101110,	0b10101010,	0b10101010, //M
	0b00000000,	0b11001100,	0b10101010,	0b10101010,	0b10101010, //N
	0b01000100,	0b10101010,	0b10101010,	0b10101010,	0b01000100, //O
	0b11101110,	0b10101010,	0b11101110,	0b10001000,	0b10001000, //P
	0b01000100,	0b10101010,	0b10101010,	0b11101110,	0b01100110, //Q
	0b11101110,	0b10101010,	0b11001100,	0b11101110,	0b10101010, //R
	0b11101110,	0b10001000,	0b11101110,	0b00100010,	0b11101110, //S
	0b11101110,	0b01000100,	0b01000100,	0b01000100,	0b01000100, //T
	0b10101010,	0b10101010,	0b10101010,	0b10101010,	0b11101110, //U
	0b10101010,	0b10101010,	0b10101010,	0b10101010,	0b01000100, //V
	0b10101010,	0b10101010,	0b11101110,	0b11101110,	0b10101010, //W
	0b00000000,	0b10101010,	0b01000100,	0b01000100,	0b10101010, //X
	0b10101010,	0b10101010,	0b01000100,	0b01000100,	0b01000100, //Y
	0b11101110,	0b00100010,	0b01000100,	0b10001000,	0b11101110, //Z
	0b11101110,	0b10001000,	0b10001000,	0b10001000,	0b11101110, //[
	0b10001000,	0b10001000,	0b01000100,	0b00100010,	0b00100010, //\
	0b11101110,	0b00100010,	0b00100010,	0b00100010,	0b11101110, //]
	0b01000100,	0b10101010,	0b00000000,	0b00000000,	0b00000000, //^
	0b00000000,	0b00000000,	0b00000000,	0b00000000,	0b11101110, //_
	0b10001000,	0b01000100,	0b00000000,	0b00000000,	0b00000000, //`
	0b00000000, 0b01000100, 0b10101010, 0b10101010, 0b01100110, //a
	0b10001000, 0b11001100, 0b10101010, 0b10101010, 0b11001100, //b
	0b00000000,	0b01100110, 0b10001000, 0b10001000, 0b01100110, //c
	0b00100010,	0b01100110, 0b10101010, 0b10101010, 0b01100110, //d
	0b00000000, 0b01000100, 0b10101010, 0b11001100, 0b01100110, //e
	0b01100110,	0b01000100, 0b11101110, 0b01000100, 0b01000100, //f
	0b00000000, 0b01000100, 0b10101010, 0b01100110, 0b11001100, //g
	0b10001000, 0b11001100, 0b10101010, 0b10101010, 0b10101010, //h
	0b01000100,	0b00000000, 0b01000100, 0b01000100, 0b01000100, //i
	0b01000100, 0b00000000, 0b01000100, 0b01000100, 0b10001000, //j
	0b10001000,	0b10001000, 0b10101010, 0b11001100, 0b10101010, //k
	0b01000100,	0b01000100, 0b01000100, 0b01000100, 0b01000100, //l
	0b00000000,	0b11101110, 0b11101110, 0b10101010, 0b10101010, //m
	0b00000000,	0b11001100, 0b10101010, 0b10101010, 0b10101010, //n
	0b00000000,	0b01000100, 0b10101010, 0b10101010, 0b01000100, //o
	0b00000000,	0b11001100, 0b10101010, 0b11001100, 0b10001000, //p
	0b00000000,	0b01100110, 0b10101010, 0b01100110, 0b00100010, //q
	0b00000000,	0b10001000, 0b11101110, 0b10001000, 0b10001000, //r
	0b00000000, 0b01100110, 0b11001100, 0b00100010, 0b11001100, //s
	0b01000100,	0b11101110, 0b01000100, 0b01000100, 0b01000100, //t
	0b00000000, 0b10101010, 0b10101010, 0b10101010, 0b01000100, //u
	0b00000000, 0b10101010, 0b10101010, 0b01000100, 0b01000100, //v
	0b00000000,	0b10101010, 0b10101010, 0b11101110, 0b10101010, //w
	0b00000000,	0b10101010, 0b01000100, 0b01000100, 0b10101010, //x
	0b00000000,	0b10101010, 0b10101010, 0b01100110, 0b11001100, //y
	0b00000000, 0b11101110, 0b01100110, 0b11001100, 0b11101110, //z
	0b00100010,	0b01000100,	0b11001100,	0b01000100,	0b00100010, //{
	0b01000100,	0b01000100,	0b01000100,	0b01000100,	0b01000100, //|
	0b10001000,	0b01000100,	0b01100110,	0b01000100,	0b10001000, //}
	0b00000000,	0b11001100,	0b10101010,	0b00000000,	0b00000000  //~
	};

//================================
// Paste above "glcd_fillScreen(int1 color)"
//================================
// Purpose:       Write small text on a graphic LCD
// Inputs:        (x,y) - The upper left coordinate of the first letter
//                textptr - A pointer to an array of text to display
//                color - ON or OFF

//char characterSet[] = " !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
void glcd_text35(int8 x, int8 y, char* textptr, int1 color)
{
   int i, j, k;                           // Loop counters
   BYTE pixelData[5];                     // Stores character data

   for(i=0; textptr[i] != '\0'; ++i, ++x) // Loop through the passed string
   {
      if((textptr[i] >= ' ') && (textptr[i] <= '~'))
         memcpy(pixelData, TEXT35[textptr[i]-' '], 5);
      else
         memcpy(pixelData, TEXT35[0], 5); // Default to space

      if(x+3 >= GLCD_WIDTH)          // Performs character wrapping
      {
         x = 0;                           // Set x at far left position
         y += 5 + 1;                      // Set y at next position down
      }
      for(j=3; j>0; j--, x++)             // Loop through character byte data
      {
         for(k=0; k<5; k++)               // Loop through the vertical pixels
         {
            if(bit_test(pixelData[k], j)) // Check if the pixel should be set
            {
               glcd_pixel(x, y+k, color); // Draws the pixel
            }
         }
      }
   }
}
