/*
  HCMS Display 
 Language: Arduino/Wiring
 
 Displays a scrolling string on an Avago HCMS-297x display
 
 String library based on the Wiring String library:
 http://wiring.org.co/learning/reference/String.html
 
 created 1 Oct. 2009
 by KaR]V[aN
 
 */
#include "stdio.h"
#include <LedDisplay.h>


// Define pins for the LED display. 
// You can change these, just re-wire your board:
#define dataPin 9              // connects to the display's data in
#define registerSelect 8       // the display's register select pin 
#define clockPin 7             // the display's clock pin
#define enable 6               // the display's chip enable pin
#define reset 5               // the display's reset pin

#define displayLength 8        // number of characters in the display

// create am instance of the LED display library:
LedDisplay myDisplay = LedDisplay(dataPin, registerSelect, clockPin, 
enable, reset, displayLength);

int brightness = 15;        // screen brightness

// Text we want to display scrolling
char text[] = "    http://karman.homelinux.net/blog/ by KaR]V[aN    ";

/* NOTE: The blanks at the begining and at the end of the text (exactly 4 blanks)
	 are to give de aparience of aprearing from the right side and disapearing 
	 in the left side. */

void setup() {
  // initialize the display library:
  myDisplay.begin();
  // set the brightness of the display:
  myDisplay.setBrightness(brightness);
}

void loop() {
  // scrollText(char text[], int size_of_text, int delay_scrolling_ms);
  scrollText(text,sizeof(text),200); 
}

// prints a 4 char text in the screen
void printS(char* text) {
  resetS();
  myDisplay.print(text[0]);
  myDisplay.print(text[1]);
  myDisplay.print(text[2]);
  myDisplay.print(text[3]);
}

// Moves cursor to the begining, then to the fourth position
// This hack is needed since my screen its detected like a 8 
// character screen, so text showing betwen 0 and 4 positions
// are not shown.
void resetS() {
  myDisplay.home();
  myDisplay.setCursor(4);
}

// scrolls the text[] 1 by 1 position and prints in the screen the 
// actual position to the actual + 4
void scrollText(char* text, int length, int sleep) {
  for (int i=0;i<length-4;i++) {
    char tmp[4];
    sprintf(tmp, "%c%c%c%c", text[i], text[i+1], text[i+2], text[i+3]);
    printS(tmp);
    delay(sleep);
  }
}

