--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mash/lcd-pcf8574.c Sat Jul 12 21:59:19 2014 +0200 @@ -0,0 +1,144 @@ +/* + * lcd-pcf8574.c: + * Text-based LCD driver library code + * This is designed to drive the HD44780U LCD display connected via + * a "LCM1602 IIC A0 A1 A2" board with a PCF8574 I2C controller. + * + * Copyright (c) 2012-2013 Gordon Henderson. + * Copyright (c) 2014 Michiel Broek. + *********************************************************************** + * + * mbsePi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * mbsePi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with wiringPi. If not, see <http://www.gnu.org/licenses/>. + *********************************************************************** + */ + +#include "mash.h" +#include "lcd-pcf8574.h" + + +#ifdef HAVE_WIRINGPI_H + +int lcdHandle; +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 + */ + + +/* + * setBacklight: + ********************************************************************************* + */ + +void setBacklight (int value) +{ + pinMode (AF_BACKLIGHT, OUTPUT) ; + digitalWrite (AF_BACKLIGHT, (value & 1)) ; +} + + + +/* + * initLCD: + ********************************************************************************* + */ + +int initLCD (int cols, int rows) +{ + 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 + + /* + * 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 ; + } + + lcdClear (lcdHandle) ; + for (x = 0; x < 20; x++) + for (y = 0; y < 4; y++) + lcdbuf[lcdHandle][x][y] = ' '; + + setBacklight (1) ; + + return 0 ; +} + + + +void mb_lcdPutchar(const int fd, unsigned char data) +{ + struct lcdDataStruct *lcd = lcds[fd]; + + /* + * 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); +} + + + +void mb_lcdPuts(const int fd, const char *string) +{ + while (*string) + mb_lcdPutchar (fd, *string++); +} + + + +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 +