Bring devLib/lcd* back

Signed-off-by: Deokgyu Yang <secugyu@gmail.com>
Change-Id: Ifcc4338c95d185b730e87542a5c053a6906c86cb
This commit is contained in:
Deokgyu Yang
2020-03-30 15:25:23 +09:00
parent 40b5bc139c
commit 3f787a1912
9 changed files with 4293 additions and 2 deletions

View File

@@ -43,6 +43,29 @@ cc_library_shared {
], ],
} }
cc_library_shared {
header_libs: ["wiringPi-headers"],
name: "libwiringPiDev",
srcs: [
"devLib/lcd128x64.c",
"devLib/lcd.c",
],
shared_libs: ["libwiringPi"],
host_ldlibs: [
"-ldl",
"-llog",
],
cflags: [
"-UNDEBUG",
"-DANDROID",
"-Wno-return-type",
],
}
cc_binary { cc_binary {
name: "gpio", name: "gpio",
@@ -52,12 +75,19 @@ cc_binary {
], ],
header_libs: ["wiringPi-headers"], header_libs: ["wiringPi-headers"],
local_include_dirs: [
"devLib",
],
cflags: [ cflags: [
"-UNDEBUG", "-UNDEBUG",
"-DANDROID", "-DANDROID",
"-Wno-return-type", "-Wno-return-type",
], ],
host_ldlibs: ["-llog"], host_ldlibs: ["-llog"],
shared_libs: ["libwiringPi"], shared_libs: [
"libwiringPi",
"libwiringPiDev",
],
} }

20
build
View File

@@ -104,6 +104,20 @@ install() {
fi fi
check_make_ok check_make_ok
echo
echo "WiringPi Devices Library"
cd ../devLib
if [ x$1 = "xstatic" ]; then
$make static
check_make_ok
$sudo $make install-static
else
$make
check_make_ok
$sudo $make install
fi
check_make_ok
echo echo
echo "GPIO Utility" echo "GPIO Utility"
cd ../gpio cd ../gpio
@@ -120,7 +134,7 @@ install() {
echo All Done. echo All Done.
echo "" echo ""
echo "NOTE: To compile programs with wiringPi, you need to add:" echo "NOTE: To compile programs with wiringPi, you need to add:"
echo " -lwiringPi" echo " -lwiringPi -wiringPiDev"
echo " to your compile line(s)." echo " to your compile line(s)."
echo "" echo ""
} }
@@ -128,6 +142,8 @@ install() {
uninstall() { uninstall() {
cd wiringPi cd wiringPi
echo -n "wiringPi: " ; $sudo $make uninstall echo -n "wiringPi: " ; $sudo $make uninstall
cd ../devLib
echo -n "DevLib: " ; $sudo $make uninstall
cd ../gpio cd ../gpio
echo -n "gpio: " ; $sudo $make uninstall echo -n "gpio: " ; $sudo $make uninstall
cd .. cd ..
@@ -138,6 +154,8 @@ uninstall() {
clean() { clean() {
cd wiringPi cd wiringPi
echo -n "wiringPi: " ; $make clean echo -n "wiringPi: " ; $make clean
cd ../devLib
echo -n "DevLib: " ; $make clean
cd ../gpio cd ../gpio
echo -n "gpio: " ; $make clean echo -n "gpio: " ; $make clean
cd ../examples cd ../examples

121
devLib/Makefile Normal file
View File

@@ -0,0 +1,121 @@
#
# Makefile:
# wiringPi device - A "wiring" library for the Raspberry Pi
#
# Copyright (c) 2012-2016 Gordon Henderson
#################################################################################
# This file is part of wiringPi:
# https://projects.drogon.net/raspberry-pi/wiringpi/
#
# wiringPi 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.
#
# wiringPi 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/>.
#################################################################################
VERSION=$(shell cat ../VERSION)
DESTDIR?=/usr
PREFIX?=/local
MAJOR=$(shell cat ../VERSION | cut -d'.' -f1)
LDCONFIG?=ldconfig
ifneq ($V,1)
Q ?= @
endif
STATIC=libwiringPiDev.a
DYNAMIC=libwiringPiDev.so.$(VERSION)
#DEBUG = -g -O0
DEBUG = -O2
CC = gcc
INCLUDE = -I. -I../wiringPi
DEFS = -D_GNU_SOURCE
CFLAGS = $(DEBUG) $(DEFS) -Wformat=2 -Wall -Winline $(INCLUDE) -pipe -fPIC
LIBS =
###############################################################################
SRC = lcd128x64.c lcd.c
OBJ = $(SRC:.c=.o)
HEADERS = lcd128x64.h lcd.h
all: $(DYNAMIC)
static: $(STATIC)
$(STATIC): $(OBJ)
$Q echo "[Link (Static)]"
$Q ar rcs $(STATIC) $(OBJ)
$Q ranlib $(STATIC)
# @size $(STATIC)
$(DYNAMIC): $(OBJ)
$Q echo "[Link (Dynamic)]"
$Q $(CC) -shared -Wl,-soname,libwiringPiDev.so$(WIRINGPI_SONAME_SUFFIX) -o libwiringPiDev.so.$(VERSION) -lpthread $(OBJ)
.c.o:
$Q echo [Compile] $<
$Q $(CC) -c $(CFLAGS) $< -o $@
.PHONY: clean
clean:
$Q echo "[Clean]"
$Q rm -f $(OBJ) $(OBJ_I2C) *~ core tags Makefile.bak libwiringPiDev.*
.PHONY: tags
tags: $(SRC)
$Q echo [ctags]
$Q ctags $(SRC)
.PHONY: install
install: $(DYNAMIC)
$Q echo "[Install Headers]"
$Q install -m 0755 -d $(DESTDIR)$(PREFIX)/include
$Q install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/include
$Q echo "[Install Dynamic Lib]"
$Q install -m 0755 -d $(DESTDIR)$(PREFIX)/lib
$Q install -m 0755 libwiringPiDev.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libwiringPiDev.so.$(VERSION)
$Q ln -sf libwiringPiDev.so.$(VERSION) $(DESTDIR)/lib/libwiringPiDev.so.$(MAJOR)
$Q ln -sf libwiringPiDev.so.$(MAJOR) $(DESTDIR)/lib/libwiringPiDev.so
$Q $(LDCONFIG)
.PHONY: install-static
install-static: $(STATIC)
$Q echo "[Install Headers]"
$Q install -m 0755 -d $(DESTDIR)$(PREFIX)/include
$Q install -m 0644 $(HEADERS) $(DESTDIR)$(PREFIX)/include
$Q echo "[Install Static Lib]"
$Q install -m 0755 -d $(DESTDIR)$(PREFIX)/lib
$Q install -m 0755 libwiringPiDev.a $(DESTDIR)$(PREFIX)/lib
.PHONY: uninstall
uninstall:
$Q echo "[UnInstall]"
$Q cd $(DESTDIR)$(PREFIX)/include/ && rm -f $(HEADERS)
$Q cd $(DESTDIR)$(PREFIX)/lib/ && rm -f libwiringPiDev.*
$Q $(LDCONFIG)
.PHONY: depend
depend:
makedepend -Y $(SRC)
# DO NOT DELETE
lcd128x64.o: font.h lcd128x64.h
lcd.o: lcd.h

2577
devLib/font.h Normal file

File diff suppressed because it is too large Load Diff

495
devLib/lcd.c Normal file
View File

@@ -0,0 +1,495 @@
/*
* lcd.c:
* Text-based LCD driver.
* This is designed to drive the parallel interface LCD drivers
* based in the Hitachi HD44780U controller and compatables.
*
* Copyright (c) 2012 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi 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.
*
* wiringPi 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 <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <wiringPi.h>
#include "lcd.h"
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==2)
#endif
// HD44780U Commands
#define LCD_CLEAR 0x01
#define LCD_HOME 0x02
#define LCD_ENTRY 0x04
#define LCD_CTRL 0x08
#define LCD_CDSHIFT 0x10
#define LCD_FUNC 0x20
#define LCD_CGRAM 0x40
#define LCD_DGRAM 0x80
// Bits in the entry register
#define LCD_ENTRY_SH 0x01
#define LCD_ENTRY_ID 0x02
// Bits in the control register
#define LCD_BLINK_CTRL 0x01
#define LCD_CURSOR_CTRL 0x02
#define LCD_DISPLAY_CTRL 0x04
// Bits in the function register
#define LCD_FUNC_F 0x04
#define LCD_FUNC_N 0x08
#define LCD_FUNC_DL 0x10
#define LCD_CDSHIFT_RL 0x04
struct lcdDataStruct
{
int bits, rows, cols ;
int rsPin, strbPin ;
int dataPins [8] ;
int cx, cy ;
} ;
struct lcdDataStruct *lcds [MAX_LCDS] ;
static int lcdControl ;
// Row offsets
static const int rowOff [4] = { 0x00, 0x40, 0x14, 0x54 } ;
/*
* strobe:
* Toggle the strobe (Really the "E") pin to the device.
* According to the docs, data is latched on the falling edge.
*********************************************************************************
*/
static void strobe (const struct lcdDataStruct *lcd)
{
// Note timing changes for new version of delayMicroseconds ()
digitalWrite (lcd->strbPin, 1) ; delayMicroseconds (50) ;
digitalWrite (lcd->strbPin, 0) ; delayMicroseconds (50) ;
}
/*
* sentDataCmd:
* Send an data or command byte to the display.
*********************************************************************************
*/
static void sendDataCmd (const struct lcdDataStruct *lcd, unsigned char data)
{
register unsigned char myData = data ;
unsigned char i, d4 ;
if (lcd->bits == 4)
{
d4 = (myData >> 4) & 0x0F;
for (i = 0 ; i < 4 ; ++i)
{
digitalWrite (lcd->dataPins [i], (d4 & 1)) ;
d4 >>= 1 ;
}
strobe (lcd) ;
d4 = myData & 0x0F ;
for (i = 0 ; i < 4 ; ++i)
{
digitalWrite (lcd->dataPins [i], (d4 & 1)) ;
d4 >>= 1 ;
}
}
else
{
for (i = 0 ; i < 8 ; ++i)
{
digitalWrite (lcd->dataPins [i], (myData & 1)) ;
myData >>= 1 ;
}
}
strobe (lcd) ;
}
/*
* putCommand:
* Send a command byte to the display
*********************************************************************************
*/
static void putCommand (const struct lcdDataStruct *lcd, unsigned char command)
{
digitalWrite (lcd->rsPin, 0) ;
sendDataCmd (lcd, command) ;
delay (2) ;
}
static void put4Command (const struct lcdDataStruct *lcd, unsigned char command)
{
register unsigned char myCommand = command ;
register unsigned char i ;
digitalWrite (lcd->rsPin, 0) ;
for (i = 0 ; i < 4 ; ++i)
{
digitalWrite (lcd->dataPins [i], (myCommand & 1)) ;
myCommand >>= 1 ;
}
strobe (lcd) ;
}
/*
*********************************************************************************
* User Callable code below here
*********************************************************************************
*/
/*
* lcdHome: lcdClear:
* Home the cursor or clear the screen.
*********************************************************************************
*/
void lcdHome (const int fd)
{
struct lcdDataStruct *lcd = lcds [fd] ;
putCommand (lcd, LCD_HOME) ;
lcd->cx = lcd->cy = 0 ;
delay (5) ;
}
void lcdClear (const int fd)
{
struct lcdDataStruct *lcd = lcds [fd] ;
putCommand (lcd, LCD_CLEAR) ;
putCommand (lcd, LCD_HOME) ;
lcd->cx = lcd->cy = 0 ;
delay (5) ;
}
/*
* lcdDisplay: lcdCursor: lcdCursorBlink:
* Turn the display, cursor, cursor blinking on/off
*********************************************************************************
*/
void lcdDisplay (const int fd, int state)
{
struct lcdDataStruct *lcd = lcds [fd] ;
if (state)
lcdControl |= LCD_DISPLAY_CTRL ;
else
lcdControl &= ~LCD_DISPLAY_CTRL ;
putCommand (lcd, LCD_CTRL | lcdControl) ;
}
void lcdCursor (const int fd, int state)
{
struct lcdDataStruct *lcd = lcds [fd] ;
if (state)
lcdControl |= LCD_CURSOR_CTRL ;
else
lcdControl &= ~LCD_CURSOR_CTRL ;
putCommand (lcd, LCD_CTRL | lcdControl) ;
}
void lcdCursorBlink (const int fd, int state)
{
struct lcdDataStruct *lcd = lcds [fd] ;
if (state)
lcdControl |= LCD_BLINK_CTRL ;
else
lcdControl &= ~LCD_BLINK_CTRL ;
putCommand (lcd, LCD_CTRL | lcdControl) ;
}
/*
* lcdSendCommand:
* Send any arbitary command to the display
*********************************************************************************
*/
void lcdSendCommand (const int fd, unsigned char command)
{
struct lcdDataStruct *lcd = lcds [fd] ;
putCommand (lcd, command) ;
}
/*
* lcdPosition:
* Update the position of the cursor on the display.
* Ignore invalid locations.
*********************************************************************************
*/
void lcdPosition (const int fd, int x, int y)
{
struct lcdDataStruct *lcd = lcds [fd] ;
if ((x > lcd->cols) || (x < 0))
return ;
if ((y > lcd->rows) || (y < 0))
return ;
putCommand (lcd, x + (LCD_DGRAM | rowOff [y])) ;
lcd->cx = x ;
lcd->cy = y ;
}
/*
* lcdCharDef:
* Defines a new character in the CGRAM
*********************************************************************************
*/
void lcdCharDef (const int fd, int index, unsigned char data [8])
{
struct lcdDataStruct *lcd = lcds [fd] ;
int i ;
putCommand (lcd, LCD_CGRAM | ((index & 7) << 3)) ;
digitalWrite (lcd->rsPin, 1) ;
for (i = 0 ; i < 8 ; ++i)
sendDataCmd (lcd, data [i]) ;
}
/*
* lcdPutchar:
* Send a data byte to be displayed on the display. We implement a very
* simple terminal here - with line wrapping, but no scrolling. Yet.
*********************************************************************************
*/
void lcdPutchar (const int fd, unsigned char data)
{
struct lcdDataStruct *lcd = lcds [fd] ;
digitalWrite (lcd->rsPin, 1) ;
sendDataCmd (lcd, data) ;
if (++lcd->cx == lcd->cols)
{
lcd->cx = 0 ;
if (++lcd->cy == lcd->rows)
lcd->cy = 0 ;
putCommand (lcd, lcd->cx + (LCD_DGRAM | rowOff [lcd->cy])) ;
}
}
/*
* lcdPuts:
* Send a string to be displayed on the display
*********************************************************************************
*/
void lcdPuts (const int fd, const char *string)
{
while (*string)
lcdPutchar (fd, *string++) ;
}
/*
* lcdPrintf:
* Printf to an LCD display
*********************************************************************************
*/
void lcdPrintf (const int fd, const char *message, ...)
{
va_list argp ;
char buffer [1024] ;
va_start (argp, message) ;
vsnprintf (buffer, 1023, message, argp) ;
va_end (argp) ;
lcdPuts (fd, buffer) ;
}
/*
* lcdInit:
* Take a lot of parameters and initialise the LCD, and return a handle to
* that LCD, or -1 if any error.
*********************************************************************************
*/
int lcdInit (const int rows, const int cols, const int bits,
const int rs, const int strb,
const int d0, const int d1, const int d2, const int d3, const int d4,
const int d5, const int d6, const int d7)
{
static int initialised = 0 ;
unsigned char func ;
int i ;
int lcdFd = -1 ;
struct lcdDataStruct *lcd ;
if (initialised == 0)
{
initialised = 1 ;
for (i = 0 ; i < MAX_LCDS ; ++i)
lcds [i] = NULL ;
}
// Simple sanity checks
if (! ((bits == 4) || (bits == 8)))
return -1 ;
if ((rows < 0) || (rows > 20))
return -1 ;
if ((cols < 0) || (cols > 20))
return -1 ;
// Create a new LCD:
for (i = 0 ; i < MAX_LCDS ; ++i)
{
if (lcds [i] == NULL)
{
lcdFd = i ;
break ;
}
}
if (lcdFd == -1)
return -1 ;
lcd = (struct lcdDataStruct *)malloc (sizeof (struct lcdDataStruct)) ;
if (lcd == NULL)
return -1 ;
lcd->rsPin = rs ;
lcd->strbPin = strb ;
lcd->bits = 8 ; // For now - we'll set it properly later.
lcd->rows = rows ;
lcd->cols = cols ;
lcd->cx = 0 ;
lcd->cy = 0 ;
lcd->dataPins [0] = d0 ;
lcd->dataPins [1] = d1 ;
lcd->dataPins [2] = d2 ;
lcd->dataPins [3] = d3 ;
lcd->dataPins [4] = d4 ;
lcd->dataPins [5] = d5 ;
lcd->dataPins [6] = d6 ;
lcd->dataPins [7] = d7 ;
lcds [lcdFd] = lcd ;
digitalWrite (lcd->rsPin, 0) ; pinMode (lcd->rsPin, OUTPUT) ;
digitalWrite (lcd->strbPin, 0) ; pinMode (lcd->strbPin, OUTPUT) ;
for (i = 0 ; i < bits ; ++i)
{
digitalWrite (lcd->dataPins [i], 0) ;
pinMode (lcd->dataPins [i], OUTPUT) ;
}
delay (35) ; // mS
// 4-bit mode?
// OK. This is a PIG and it's not at all obvious from the documentation I had,
// so I guess some others have worked through either with better documentation
// or more trial and error... Anyway here goes:
//
// It seems that the controller needs to see the FUNC command at least 3 times
// consecutively - in 8-bit mode. If you're only using 8-bit mode, then it appears
// that you can get away with one func-set, however I'd not rely on it...
//
// So to set 4-bit mode, you need to send the commands one nibble at a time,
// the same three times, but send the command to set it into 8-bit mode those
// three times, then send a final 4th command to set it into 4-bit mode, and only
// then can you flip the switch for the rest of the library to work in 4-bit
// mode which sends the commands as 2 x 4-bit values.
if (bits == 4)
{
func = LCD_FUNC | LCD_FUNC_DL ; // Set 8-bit mode 3 times
put4Command (lcd, func >> 4) ; delay (35) ;
put4Command (lcd, func >> 4) ; delay (35) ;
put4Command (lcd, func >> 4) ; delay (35) ;
func = LCD_FUNC ; // 4th set: 4-bit mode
put4Command (lcd, func >> 4) ; delay (35) ;
lcd->bits = 4 ;
}
else
{
func = LCD_FUNC | LCD_FUNC_DL ;
putCommand (lcd, func ) ; delay (35) ;
putCommand (lcd, func ) ; delay (35) ;
putCommand (lcd, func ) ; delay (35) ;
}
if (lcd->rows > 1)
{
func |= LCD_FUNC_N ;
putCommand (lcd, func) ; delay (35) ;
}
// Rest of the initialisation sequence
lcdDisplay (lcdFd, TRUE) ;
lcdCursor (lcdFd, FALSE) ;
lcdCursorBlink (lcdFd, FALSE) ;
lcdClear (lcdFd) ;
putCommand (lcd, LCD_ENTRY | LCD_ENTRY_ID) ;
putCommand (lcd, LCD_CDSHIFT | LCD_CDSHIFT_RL) ;
return lcdFd ;
}

52
devLib/lcd.h Normal file
View File

@@ -0,0 +1,52 @@
/*
* lcd.h:
* Text-based LCD driver.
* This is designed to drive the parallel interface LCD drivers
* based in the Hitachi HD44780U controller and compatables.
*
* Copyright (c) 2012 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi 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.
*
* wiringPi 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/>.
***********************************************************************
*/
#define MAX_LCDS 8
#ifdef __cplusplus
extern "C" {
#endif
extern void lcdHome (const int fd) ;
extern void lcdClear (const int fd) ;
extern void lcdDisplay (const int fd, int state) ;
extern void lcdCursor (const int fd, int state) ;
extern void lcdCursorBlink (const int fd, int state) ;
extern void lcdSendCommand (const int fd, unsigned char command) ;
extern void lcdPosition (const int fd, int x, int y) ;
extern void lcdCharDef (const int fd, int index, unsigned char data [8]) ;
extern void lcdPutchar (const int fd, unsigned char data) ;
extern void lcdPuts (const int fd, const char *string) ;
extern void lcdPrintf (const int fd, const char *message, ...) ;
extern int lcdInit (const int rows, const int cols, const int bits,
const int rs, const int strb,
const int d0, const int d1, const int d2, const int d3, const int d4,
const int d5, const int d6, const int d7) ;
#ifdef __cplusplus
}
#endif

673
devLib/lcd128x64.c Normal file
View File

@@ -0,0 +1,673 @@
/*
* lcd128x64.c:
* Graphics-based LCD driver.
* This is designed to drive the parallel interface LCD drivers
* based on the generic 12864H chips
*
* There are many variations on these chips, however they all mostly
* seem to be similar.
* This implementation has the Pins from the Pi hard-wired into it,
* in particular wiringPi pins 0-7 so that we can use
* digitalWriteByete() to speed things up somewhat.
*
* Copyright (c) 2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi 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.
*
* wiringPi 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 <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>
#include "font.h"
#include "lcd128x64.h"
// Size
#define LCD_WIDTH 128
#define LCD_HEIGHT 64
// Hardware Pins
// Note pins 0-7 are the 8-bit data port
#define CS1 10
#define CS2 11
#define STROBE 12
#define RS 13
// Software copy of the framebuffer
// it's 8-bit deep although the display itself is only 1-bit deep.
static unsigned char frameBuffer [LCD_WIDTH * LCD_HEIGHT] ;
static int maxX, maxY ;
static int lastX, lastY ;
static int xOrigin, yOrigin ;
static int lcdOrientation = 0 ;
/*
* strobe:
* Toggle the strobe (Really the "E") pin to the device.
* According to the docs, data is latched on the falling edge.
*********************************************************************************
*/
static void strobe (void)
{
digitalWrite (STROBE, 1) ; delayMicroseconds (1) ;
digitalWrite (STROBE, 0) ; delayMicroseconds (5) ;
}
/*
* sentData:
* Send an data or command byte to the display.
*********************************************************************************
*/
static void sendData (const int data, const int chip)
{
digitalWrite (chip, 0) ;
digitalWriteByte (data) ;
strobe () ;
digitalWrite (chip, 1) ;
}
/*
* sendCommand:
* Send a command byte to the display
*********************************************************************************
*/
static void sendCommand (const int command, const int chip)
{
digitalWrite (RS, 0) ;
sendData (command, chip) ;
digitalWrite (RS, 1) ;
}
/*
* setCol: SetLine:
* Set the column and line addresses
*********************************************************************************
*/
static void setCol (int col, const int chip)
{ sendCommand (0x40 | (col & 0x3F), chip) ; }
static void setLine (int line, const int chip)
{ sendCommand (0xB8 | (line & 0x07), chip) ; }
/*
* lcd128x64update:
* Copy our software version to the real display
*********************************************************************************
*/
void lcd128x64update (void)
{
int line, x, y, fbLoc ;
unsigned char byte ;
// Left side
for (line = 0 ; line < 8 ; ++line)
{
setCol (0, CS1) ;
setLine (line, CS1) ;
for (x = 63 ; x >= 0 ; --x)
{
byte = 0 ;
for (y = 0 ; y < 8 ; ++y)
{
fbLoc = x + (((7 - line) * 8) + (7 - y)) * LCD_WIDTH ;
if (frameBuffer [fbLoc] != 0)
byte |= (1 << y) ;
}
sendData (byte, CS1) ;
}
}
// Right side
for (line = 0 ; line < 8 ; ++line)
{
setCol (0, CS2) ;
setLine (line, CS2) ;
for (x = 127 ; x >= 64 ; --x)
{
byte = 0 ;
for (y = 0 ; y < 8 ; ++y)
{
fbLoc = x + (((7 - line) * 8) + (7 - y)) * LCD_WIDTH ;
if (frameBuffer [fbLoc] != 0)
byte |= (1 << y) ;
}
sendData (byte, CS2) ;
}
}
}
/*
* lcd128x64setOrigin:
* Set the display offset origin
*********************************************************************************
*/
void lcd128x64setOrigin (int x, int y)
{
xOrigin = x ;
yOrigin = y ;
}
/*
* lcd128x64setOrientation:
* Set the display orientation:
* 0: Normal, the display is portrait mode, 0,0 is top left
* 1: Landscape
* 2: Portrait, flipped
* 3: Landscape, flipped
*********************************************************************************
*/
void lcd128x64setOrientation (int orientation)
{
lcdOrientation = orientation & 3 ;
lcd128x64setOrigin (0,0) ;
switch (lcdOrientation)
{
case 0:
maxX = LCD_WIDTH ;
maxY = LCD_HEIGHT ;
break ;
case 1:
maxX = LCD_HEIGHT ;
maxY = LCD_WIDTH ;
break ;
case 2:
maxX = LCD_WIDTH ;
maxY = LCD_HEIGHT ;
break ;
case 3:
maxX = LCD_HEIGHT ;
maxY = LCD_WIDTH ;
break ;
}
}
/*
* lcd128x64orientCoordinates:
* Adjust the coordinates given to the display orientation
*********************************************************************************
*/
void lcd128x64orientCoordinates (int *x, int *y)
{
register int tmp ;
*x += xOrigin ;
*y += yOrigin ;
*y = maxY - *y - 1 ;
switch (lcdOrientation)
{
case 0:
break;
case 1:
tmp = maxY - *y - 1 ;
*y = *x ;
*x = tmp ;
break;
case 2:
*x = maxX - *x - 1 ;
*y = maxY - *y - 1 ;
break;
case 3:
*x = maxX - *x - 1 ;
tmp = *y ;
*y = *x ;
*x = tmp ;
break ;
}
}
/*
* lcd128x64getScreenSize:
* Return the max X & Y screen sizes. Needs to be called again, if you
* change screen orientation.
*********************************************************************************
*/
void lcd128x64getScreenSize (int *x, int *y)
{
*x = maxX ;
*y = maxY ;
}
/*
*********************************************************************************
* Standard Graphical Functions
*********************************************************************************
*/
/*
* lcd128x64point:
* Plot a pixel.
*********************************************************************************
*/
void lcd128x64point (int x, int y, int colour)
{
lastX = x ;
lastY = y ;
lcd128x64orientCoordinates (&x, &y) ;
if ((x < 0) || (x >= LCD_WIDTH) || (y < 0) || (y >= LCD_HEIGHT))
return ;
frameBuffer [x + y * LCD_WIDTH] = colour ;
}
/*
* lcd128x64line: lcd128x64lineTo:
* Classic Bressenham Line code
*********************************************************************************
*/
void lcd128x64line (int x0, int y0, int x1, int y1, int colour)
{
int dx, dy ;
int sx, sy ;
int err, e2 ;
lastX = x1 ;
lastY = y1 ;
dx = abs (x1 - x0) ;
dy = abs (y1 - y0) ;
sx = (x0 < x1) ? 1 : -1 ;
sy = (y0 < y1) ? 1 : -1 ;
err = dx - dy ;
for (;;)
{
lcd128x64point (x0, y0, colour) ;
if ((x0 == x1) && (y0 == y1))
break ;
e2 = 2 * err ;
if (e2 > -dy)
{
err -= dy ;
x0 += sx ;
}
if (e2 < dx)
{
err += dx ;
y0 += sy ;
}
}
}
void lcd128x64lineTo (int x, int y, int colour)
{
lcd128x64line (lastX, lastY, x, y, colour) ;
}
/*
* lcd128x64rectangle:
* A rectangle is a spoilt days fishing
*********************************************************************************
*/
void lcd128x64rectangle (int x1, int y1, int x2, int y2, int colour, int filled)
{
register int x ;
if (filled)
{
/**/ if (x1 == x2)
lcd128x64line (x1, y1, x2, y2, colour) ;
else if (x1 < x2)
for (x = x1 ; x <= x2 ; ++x)
lcd128x64line (x, y1, x, y2, colour) ;
else
for (x = x2 ; x <= x1 ; ++x)
lcd128x64line (x, y1, x, y2, colour) ;
}
else
{
lcd128x64line (x1, y1, x2, y1, colour) ;
lcd128x64lineTo (x2, y2, colour) ;
lcd128x64lineTo (x1, y2, colour) ;
lcd128x64lineTo (x1, y1, colour) ;
}
}
/*
* lcd128x64circle:
* This is the midpoint circle algorithm.
*********************************************************************************
*/
void lcd128x64circle (int x, int y, int r, int colour, int filled)
{
int ddF_x = 1 ;
int ddF_y = -2 * r ;
int f = 1 - r ;
int x1 = 0 ;
int y1 = r ;
if (filled)
{
lcd128x64line (x, y + r, x, y - r, colour) ;
lcd128x64line (x + r, y, x - r, y, colour) ;
}
else
{
lcd128x64point (x, y + r, colour) ;
lcd128x64point (x, y - r, colour) ;
lcd128x64point (x + r, y, colour) ;
lcd128x64point (x - r, y, colour) ;
}
while (x1 < y1)
{
if (f >= 0)
{
y1-- ;
ddF_y += 2 ;
f += ddF_y ;
}
x1++ ;
ddF_x += 2 ;
f += ddF_x ;
if (filled)
{
lcd128x64line (x + x1, y + y1, x - x1, y + y1, colour) ;
lcd128x64line (x + x1, y - y1, x - x1, y - y1, colour) ;
lcd128x64line (x + y1, y + x1, x - y1, y + x1, colour) ;
lcd128x64line (x + y1, y - x1, x - y1, y - x1, colour) ;
}
else
{
lcd128x64point (x + x1, y + y1, colour) ; lcd128x64point (x - x1, y + y1, colour) ;
lcd128x64point (x + x1, y - y1, colour) ; lcd128x64point (x - x1, y - y1, colour) ;
lcd128x64point (x + y1, y + x1, colour) ; lcd128x64point (x - y1, y + x1, colour) ;
lcd128x64point (x + y1, y - x1, colour) ; lcd128x64point (x - y1, y - x1, colour) ;
}
}
}
/*
* lcd128x64ellipse:
* Fast ellipse drawing algorithm by
* John Kennedy
* Mathematics Department
* Santa Monica College
* 1900 Pico Blvd.
* Santa Monica, CA 90405
* jrkennedy6@gmail.com
* -Confirned in email this algorithm is in the public domain -GH-
*********************************************************************************
*/
static void plot4ellipsePoints (int cx, int cy, int x, int y, int colour, int filled)
{
if (filled)
{
lcd128x64line (cx + x, cy + y, cx - x, cy + y, colour) ;
lcd128x64line (cx - x, cy - y, cx + x, cy - y, colour) ;
}
else
{
lcd128x64point (cx + x, cy + y, colour) ;
lcd128x64point (cx - x, cy + y, colour) ;
lcd128x64point (cx - x, cy - y, colour) ;
lcd128x64point (cx + x, cy - y, colour) ;
}
}
void lcd128x64ellipse (int cx, int cy, int xRadius, int yRadius, int colour, int filled)
{
int x, y ;
int xChange, yChange, ellipseError ;
int twoAsquare, twoBsquare ;
int stoppingX, stoppingY ;
twoAsquare = 2 * xRadius * xRadius ;
twoBsquare = 2 * yRadius * yRadius ;
x = xRadius ;
y = 0 ;
xChange = yRadius * yRadius * (1 - 2 * xRadius) ;
yChange = xRadius * xRadius ;
ellipseError = 0 ;
stoppingX = twoBsquare * xRadius ;
stoppingY = 0 ;
while (stoppingX >= stoppingY) // 1st set of points
{
plot4ellipsePoints (cx, cy, x, y, colour, filled) ;
++y ;
stoppingY += twoAsquare ;
ellipseError += yChange ;
yChange += twoAsquare ;
if ((2 * ellipseError + xChange) > 0 )
{
--x ;
stoppingX -= twoBsquare ;
ellipseError += xChange ;
xChange += twoBsquare ;
}
}
x = 0 ;
y = yRadius ;
xChange = yRadius * yRadius ;
yChange = xRadius * xRadius * (1 - 2 * yRadius) ;
ellipseError = 0 ;
stoppingX = 0 ;
stoppingY = twoAsquare * yRadius ;
while (stoppingX <= stoppingY) //2nd set of points
{
plot4ellipsePoints (cx, cy, x, y, colour, filled) ;
++x ;
stoppingX += twoBsquare ;
ellipseError += xChange ;
xChange += twoBsquare ;
if ((2 * ellipseError + yChange) > 0 )
{
--y ;
stoppingY -= twoAsquare ;
ellipseError += yChange ;
yChange += twoAsquare ;
}
}
}
/*
* lcd128x64putchar:
* Print a single character to the screen
*********************************************************************************
*/
void lcd128x64putchar (int x, int y, int c, int bgCol, int fgCol)
{
int y1, y2 ;
unsigned char line ;
unsigned char *fontPtr ;
// Can't print if we're offscreen
//if ((x < 0) || (x >= (maxX - fontWidth)) || (y < 0) || (y >= (maxY - fontHeight)))
// return ;
fontPtr = font + c * fontHeight ;
for (y1 = fontHeight - 1 ; y1 >= 0 ; --y1)
{
y2 = y + y1 ;
line = *fontPtr++ ;
lcd128x64point (x + 0, y2, (line & 0x80) == 0 ? bgCol : fgCol) ;
lcd128x64point (x + 1, y2, (line & 0x40) == 0 ? bgCol : fgCol) ;
lcd128x64point (x + 2, y2, (line & 0x20) == 0 ? bgCol : fgCol) ;
lcd128x64point (x + 3, y2, (line & 0x10) == 0 ? bgCol : fgCol) ;
lcd128x64point (x + 4, y2, (line & 0x08) == 0 ? bgCol : fgCol) ;
lcd128x64point (x + 5, y2, (line & 0x04) == 0 ? bgCol : fgCol) ;
lcd128x64point (x + 6, y2, (line & 0x02) == 0 ? bgCol : fgCol) ;
lcd128x64point (x + 7, y2, (line & 0x01) == 0 ? bgCol : fgCol) ;
}
}
/*
* lcd128x64puts:
* Send a string to the display. Obeys \n and \r formatting
*********************************************************************************
*/
void lcd128x64puts (int x, int y, const char *str, int bgCol, int fgCol)
{
int c, mx, my ;
mx = x ; my = y ;
while (*str)
{
c = *str++ ;
if (c == '\r')
{
mx = x ;
continue ;
}
if (c == '\n')
{
mx = x ;
my -= fontHeight ;
continue ;
}
lcd128x64putchar (mx, my, c, bgCol, fgCol) ;
mx += fontWidth ;
if (mx >= (maxX - fontWidth))
{
mx = 0 ;
my -= fontHeight ;
}
}
}
/*
* lcd128x64clear:
* Clear the display to the given colour.
*********************************************************************************
*/
void lcd128x64clear (int colour)
{
register int i ;
register unsigned char *ptr = frameBuffer ;
for (i = 0 ; i < (maxX * maxY) ; ++i)
*ptr++ = colour ;
}
/*
* lcd128x64setup:
* Initialise the display and GPIO.
*********************************************************************************
*/
int lcd128x64setup (void)
{
int i ;
for (i = 0 ; i < 8 ; ++i)
pinMode (i, OUTPUT) ;
digitalWrite (CS1, 1) ;
digitalWrite (CS2, 1) ;
digitalWrite (STROBE, 0) ;
digitalWrite (RS, 1) ;
pinMode (CS1, OUTPUT) ;
pinMode (CS2, OUTPUT) ;
pinMode (STROBE, OUTPUT) ;
pinMode (RS, OUTPUT) ;
sendCommand (0x3F, CS1) ; // Display ON
sendCommand (0xC0, CS1) ; // Set display start line to 0
sendCommand (0x3F, CS2) ; // Display ON
sendCommand (0xC0, CS2) ; // Set display start line to 0
lcd128x64clear (0) ;
lcd128x64setOrientation (0) ;
lcd128x64update () ;
return 0 ;
}

39
devLib/lcd128x64.h Normal file
View File

@@ -0,0 +1,39 @@
/*
* lcd128x64.h:
*
* Copyright (c) 2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi 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.
*
* wiringPi 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/>.
***********************************************************************
*/
extern void lcd128x64setOrigin (int x, int y) ;
extern void lcd128x64setOrientation (int orientation) ;
extern void lcd128x64orientCoordinates (int *x, int *y) ;
extern void lcd128x64getScreenSize (int *x, int *y) ;
extern void lcd128x64point (int x, int y, int colour) ;
extern void lcd128x64line (int x0, int y0, int x1, int y1, int colour) ;
extern void lcd128x64lineTo (int x, int y, int colour) ;
extern void lcd128x64rectangle (int x1, int y1, int x2, int y2, int colour, int filled) ;
extern void lcd128x64circle (int x, int y, int r, int colour, int filled) ;
extern void lcd128x64ellipse (int cx, int cy, int xRadius, int yRadius, int colour, int filled) ;
extern void lcd128x64putchar (int x, int y, int c, int bgCol, int fgCol) ;
extern void lcd128x64puts (int x, int y, const char *str, int bgCol, int fgCol) ;
extern void lcd128x64update (void) ;
extern void lcd128x64clear (int colour) ;
extern int lcd128x64setup (void) ;

286
examples/lcd.c Normal file
View File

@@ -0,0 +1,286 @@
/*
* lcd.c:
* Text-based LCD driver.
* This is designed to drive the parallel interface LCD drivers
* based in the Hitachi HD44780U controller and compatables.
*
* This test program assumes the following:
*
* 8-bit displays:
* GPIO 0-7 is connected to display data pins 0-7.
* GPIO 11 is the RS pin.
* GPIO 10 is the Strobe/E pin.
*
* For 4-bit interface:
* GPIO 4-7 is connected to display data pins 4-7.
* GPIO 11 is the RS pin.
* GPIO 10 is the Strobe/E pin.
*
* Copyright (c) 2012-2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi 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.
*
* wiringPi 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 <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <wiringPi.h>
#include <lcd.h>
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==2)
#endif
static unsigned char newChar [8] =
{
0b11111,
0b10001,
0b10001,
0b10101,
0b11111,
0b10001,
0b10001,
0b11111,
} ;
// Global lcd handle:
static int lcdHandle ;
/*
* usage:
*********************************************************************************
*/
int usage (const char *progName)
{
fprintf (stderr, "Usage: %s bits cols rows\n", progName) ;
return EXIT_FAILURE ;
}
/*
* scrollMessage:
*********************************************************************************
*/
static const char *message =
" "
"Wiring Pi by Gordon Henderson. HTTP://WIRINGPI.COM/"
" " ;
void scrollMessage (int line, int width)
{
char buf [32] ;
static int position = 0 ;
static int timer = 0 ;
if (millis () < timer)
return ;
timer = millis () + 200 ;
strncpy (buf, &message [position], width) ;
buf [width] = 0 ;
lcdPosition (lcdHandle, 0, line) ;
lcdPuts (lcdHandle, buf) ;
if (++position == (strlen (message) - width))
position = 0 ;
}
/*
* pingPong:
* Bounce a character - only on 4-line displays
*********************************************************************************
*/
static void pingPong (int lcd, int cols)
{
static int position = 0 ;
static int dir = 0 ;
if (dir == 0) // Setup
{
dir = 1 ;
lcdPosition (lcdHandle, 0, 3) ;
lcdPutchar (lcdHandle, '*') ;
return ;
}
lcdPosition (lcdHandle, position, 3) ;
lcdPutchar (lcdHandle, ' ') ;
position += dir ;
if (position == cols)
{
dir = -1 ;
--position ;
}
if (position < 0)
{
dir = 1 ;
++position ;
}
lcdPosition (lcdHandle, position, 3) ;
lcdPutchar (lcdHandle, '#') ;
}
/*
* waitForEnter:
*********************************************************************************
*/
static void waitForEnter (void)
{
printf ("Press ENTER to continue: ") ;
(void)fgetc (stdin) ;
}
/*
* The works
*********************************************************************************
*/
int main (int argc, char *argv[])
{
int i ;
int lcd ;
int bits, rows, cols ;
struct tm *t ;
time_t tim ;
char buf [32] ;
if (argc != 4)
return usage (argv [0]) ;
printf ("Raspberry Pi LCD test\n") ;
printf ("=====================\n") ;
bits = atoi (argv [1]) ;
cols = atoi (argv [2]) ;
rows = atoi (argv [3]) ;
if (!((rows == 1) || (rows == 2) || (rows == 4)))
{
fprintf (stderr, "%s: rows must be 1, 2 or 4\n", argv [0]) ;
return EXIT_FAILURE ;
}
if (!((cols == 16) || (cols == 20)))
{
fprintf (stderr, "%s: cols must be 16 or 20\n", argv [0]) ;
return EXIT_FAILURE ;
}
wiringPiSetup () ;
if (bits == 4)
lcdHandle = lcdInit (rows, cols, 4, 11,10, 4,5,6,7,0,0,0,0) ;
else
lcdHandle = lcdInit (rows, cols, 8, 11,10, 0,1,2,3,4,5,6,7) ;
if (lcdHandle < 0)
{
fprintf (stderr, "%s: lcdInit failed\n", argv [0]) ;
return -1 ;
}
lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, "Gordon Henderson") ;
lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi.com ") ;
waitForEnter () ;
if (rows > 1)
{
lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, " wiringpi.com ") ;
if (rows == 4)
{
lcdPosition (lcdHandle, 0, 2) ;
for (i = 0 ; i < ((cols - 1) / 2) ; ++i)
lcdPuts (lcdHandle, "=-") ;
lcdPuts (lcdHandle, "=3") ;
lcdPosition (lcdHandle, 0, 3) ;
for (i = 0 ; i < ((cols - 1) / 2) ; ++i)
lcdPuts (lcdHandle, "-=") ;
lcdPuts (lcdHandle, "-4") ;
}
}
waitForEnter () ;
lcdCharDef (lcdHandle, 2, newChar) ;
lcdClear (lcdHandle) ;
lcdPosition (lcdHandle, 0, 0) ;
lcdPuts (lcdHandle, "User Char: ") ;
lcdPutchar (lcdHandle, 2) ;
lcdCursor (lcdHandle, TRUE) ;
lcdCursorBlink (lcdHandle, TRUE) ;
waitForEnter () ;
lcdCursor (lcdHandle, FALSE) ;
lcdCursorBlink (lcdHandle, FALSE) ;
lcdClear (lcdHandle) ;
for (;;)
{
scrollMessage (0, cols) ;
if (rows == 1)
continue ;
tim = time (NULL) ;
t = localtime (&tim) ;
sprintf (buf, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec) ;
lcdPosition (lcdHandle, (cols - 8) / 2, 1) ;
lcdPuts (lcdHandle, buf) ;
if (rows == 2)
continue ;
sprintf (buf, "%02d/%02d/%04d", t->tm_mday, t->tm_mon + 1, t->tm_year+1900) ;
lcdPosition (lcdHandle, (cols - 10) / 2, 2) ;
lcdPuts (lcdHandle, buf) ;
pingPong (lcd, cols) ;
}
return 0 ;
}