/*********************************************

quick code to generate LCD message by Rue Mohr

**********************************************/

#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

uint8_t  init[] = { 2, 2, 0, 0, 0x0E, 0, 6, 0, 1, 255 };  // 4 bit init nibbles
char * s = "Hi twitter.";        // text string after init.
int fd;

// R/W should be grounded.
#define RS   0x10  // RS output bit
#define E    0x20  // E output bit
#define STOP 0x40  // bit to stop 555, goes high when done.

void outn(uint8_t n) {
 // printf("%02X \n", n);  // debug
  write(fd, &n, 1);        // output to file
}




int main(void) {
  
  uint8_t * p = init;  // pint pointer to init.
  
  // open output file
  if ((fd = open ( "output.bin", O_CREAT|O_WRONLY|O_TRUNC, 00666)) == -1)  {
    printf("Cant open output file\n");
    return 0;
  }           
  
  // end of init is 255, send all init nibbles
  while (*p != 255) {
    
    outn(*p);
    outn((*p)|E);
    outn(*p);
    
    p++;
  }
  
  p = s;  // point to string
  
  while(*p != 0) {  // string is null terminated
    outn(((*p)>>4)|RS);  // pulse in first 4 bits
    outn(((*p)>>4)|RS|E);
    outn(((*p)>>4)|RS);
    
    outn(((*p)&0x0F)|RS);  // pulse in second 4 bits
    outn(((*p)&0x0F)|RS|E);
    outn(((*p)&0x0F)|RS);    
    p++;
  }
  outn(STOP); // last value should be a 555 STOP
  
  close(fd); // close output file
  
  return 0;
}



