lib/lcd-pcf8574.c

changeset 40
dafbbd5e9922
parent 28
32ed1ea4d0b6
child 41
f534ace74eea
--- a/lib/lcd-pcf8574.c	Sat May 17 10:50:16 2014 +0200
+++ b/lib/lcd-pcf8574.c	Sat May 17 23:06:39 2014 +0200
@@ -28,7 +28,24 @@
 
 #ifdef HAVE_WIRINGPI_H
 
-int lcdHandle;
+int				lcdHandle;
+static unsigned char		lcdbuf[MAX_LCDS][20][4];
+
+struct lcdDataStruct
+{
+    int bits, rows, cols ;
+    int rsPin, strbPin ;
+    int dataPins [8] ;
+    int cx, cy ;
+};
+extern struct lcdDataStruct	*lcds [MAX_LCDS];
+
+
+/*
+ * Some LCD functions are extended shadow copies of the wiringPi functions.
+ * The difference is that the lcdbuf will be updated with the contents on
+ * the hardware display. This copy can then be used for a remote display
+ */
 
 
 /*
@@ -51,38 +68,76 @@
 
 int initLCD (int cols, int rows)
 {
-  if (!((rows == 1) || (rows == 2) || (rows == 4)))
-  {
-    fprintf (stderr, "rows must be 1, 2 or 4\n") ;
-    return EXIT_FAILURE ;
-  }
+    int	x, y;
+
+    if (!((rows == 1) || (rows == 2) || (rows == 4))) {
+    	fprintf (stderr, "rows must be 1, 2 or 4\n") ;
+    	return EXIT_FAILURE ;
+    }
+
+    if (!((cols == 16) || (cols == 20))) {
+    	fprintf (stderr, "cols must be 16 or 20\n") ;
+    	return EXIT_FAILURE ;
+    }
+
+    pcf8574Setup(AF_BASE, 0x27) ;
+    pinMode (AF_RW, OUTPUT) ;
+    digitalWrite (AF_RW, LOW) ;        // Not used with wiringPi - always in write mode
 
-  if (!((cols == 16) || (cols == 20)))
-  {
-    fprintf (stderr, "cols must be 16 or 20\n") ;
-    return EXIT_FAILURE ;
-  }
+    /*
+     * The other control pins are initialised with lcdInit ()
+     */
+    lcdHandle = lcdInit (rows, cols, 4, AF_RS, AF_E, AF_DB4, AF_DB5, AF_DB6, AF_DB7, 0, 0, 0, 0) ;
+    if (lcdHandle < 0) {
+    	fprintf (stderr, "lcdInit failed\n") ;
+    	return -1 ;
+    }
 
-  pcf8574Setup (AF_BASE, 0x27) ;
+    lcdClear (lcdHandle) ;
+    for (x = 0; x < 20; x++)
+	for (y = 0; y < 4; y++)
+	    lcdbuf[lcdHandle][x][y] = ' ';
 
-  setBacklight (1) ;
+    setBacklight (1) ;
+
+    return 0 ;
+}
 
-  pinMode (AF_RW, OUTPUT) ;
-  digitalWrite (AF_RW, LOW) ;        // Not used with wiringPi - always in write mode
+
+
+void mb_lcdPutchar(const int fd, unsigned char data)
+{
+    struct lcdDataStruct *lcd = lcds[fd];
 
-  // The other control pins are initialised with lcdInit ()
+    /*
+     * Write to our buffer first, then to the wiringPi driver.
+     * Writing to wiringPi updates the cursor position.
+     */
+    lcdbuf[fd][lcd->cx][lcd->cy] = data;
+    lcdPutchar(fd, data);
+}
 
-  lcdHandle = lcdInit (rows, cols, 4, AF_RS, AF_E, AF_DB4, AF_DB5, AF_DB6, AF_DB7, 0, 0, 0, 0) ;
+
 
-  if (lcdHandle < 0)
-  {
-    fprintf (stderr, "lcdInit failed\n") ;
-    return -1 ;
-  }
+void mb_lcdPuts(const int fd, const char *string)
+{
+    while (*string)
+	mb_lcdPutchar (fd, *string++);
+}
+
+
 
-  lcdClear (lcdHandle) ;
-  return 0 ;
+void mb_lcdClear(const int fd)
+{
+    int	x, y;
+
+    lcdClear(fd);
+    for (x = 0; x < 20; x++)
+	for (y = 0; y < 4; y++)
+	    lcdbuf[fd][x][y] = ' ';
 }
 
+
+
 #endif
 

mercurial