I have added the begin() method.
I have add #include "Wire.h" in the sketch where I am using the library. This seems odd as I have already included it in the library itself.
In the CPP-File I have to redefine this variable char _pExpAddBtns = 57; even though it is already in the H-File. Why is this?
The library is working despite the few questions I have.
Thank you for the help.

Here is the completed H-File
#ifndef UniLcdBtnBrd_h
#define UniLcdBtnBrd_h
#include <..\Wire\Wire.h>
#include <Arduino.h>
class UniLcdBtnBrd
{
public:
UniLcdBtnBrd();
UniLcdBtnBrd(char lcdAdd, char btnsAdd);
void setBoardAddresses(char lcdAdd, char btnsAdd);
void setLcdLinesAdd(char add1);
void setLcdLinesAdd(char add1, char add2);
void setLcdLinesAdd(char add1, char add2, char add3);
void setLcdLinesAdd(char add1, char add2, char add3, char add4);
void setLcdLineLen(char len);
void initLcd();
void writeLcdChar(char data);
void setLcdAddr(char data);
void writeLcdStr(String data_str, int lineNr);
void clearLcd();
void setLcdDisplay(boolean displayOn, boolean cursorOn,boolean cursorBlink,boolean BLOn);
void initBtns();
char readBtns();
void begin();
private:
boolean _bLightOn;
char _pExpAddLcd;
char _lcdLine1Add;
char _lcdLine2Add;
char _lcdLine3Add;
char _lcdLine4Add;
char _lcdLineLen;
char _pExpAddBtns;
void writeLcdNibble(char data);
void writeLcdByte(char nibbleH,char nibbleL);
};
#endif
Here is the completed CPP-File
/*
UniLcdBtnBrd.cpp - library for using the universal lcd and buttons board.
Created by Richard Whittington, 08/05/2012
*/
#include "..\Wire\Wire.h"
#include "Arduino.h"
#include "UniLcdBtnBrd.h"
char _pExpAddBtns = 57;//I have added this in the H-File. Why do I have to add it here too?
UniLcdBtnBrd::UniLcdBtnBrd()
{
_bLightOn = true;
_pExpAddLcd = 56;
_pExpAddBtns = 57;
_lcdLine1Add = byte(0);
_lcdLine2Add = byte(0);
_lcdLine3Add = byte(0);
_lcdLine4Add = byte(0);
_lcdLineLen = 16;
}
UniLcdBtnBrd::UniLcdBtnBrd(char lcdAdd, char btnsAdd)
{
boolean _bLightOn = true;
_pExpAddLcd = lcdAdd;
_pExpAddBtns = btnsAdd;
_lcdLine1Add = byte(0);
_lcdLine2Add = byte(0);
_lcdLine3Add = byte(0);
_lcdLine4Add = byte(0);
_lcdLineLen = 16;
}
void UniLcdBtnBrd::begin()
{
Wire.begin();
initLcd();
initBtns();
setLcdDisplay(true,true,true,true);
}
void UniLcdBtnBrd::setBoardAddresses(char lcdAdd, char btnsAdd)
{
_pExpAddLcd = lcdAdd;
_pExpAddBtns = btnsAdd;
}
void UniLcdBtnBrd::setLcdLinesAdd(char add1)
{
_lcdLine1Add = add1;
}
void UniLcdBtnBrd::setLcdLinesAdd(char add1, char add2)
{
_lcdLine1Add = add1;
_lcdLine2Add = add2;
}
void UniLcdBtnBrd::setLcdLinesAdd(char add1, char add2, char add3)
{
_lcdLine1Add = add1;
_lcdLine2Add = add2;
_lcdLine3Add = add3;
}
void UniLcdBtnBrd::setLcdLinesAdd(char add1, char add2, char add3, char add4)
{
_lcdLine1Add = add1;
_lcdLine2Add = add2;
_lcdLine3Add = add3;
_lcdLine4Add = add4;
}
void UniLcdBtnBrd::setLcdLineLen(char len)
{
_lcdLineLen = len;
}
void UniLcdBtnBrd::initLcd()
{ //# E RW RS D7 D6 D5 D4
delay(20);
writeLcdNibble(0b00000011); //0 0 0 0 0 0 1 1 |thrice Function set (Interface is 8 bits long.)
delay(20);
writeLcdNibble(0b00000011);
delay(10);
writeLcdNibble(0b00000011);
delay(10);
writeLcdNibble(0b00000010); //Function set (Set interface to be 4 bits long.)
writeLcdNibble(0b00000010);
writeLcdNibble(0b00001000); //0 0 0 0 N F # # |Specify the number of display lines and character font
writeLcdNibble(0b00000000);
writeLcdNibble(0b00001000); //0 0 0 0 1 0 0 0 |Display off
writeLcdNibble(0b00000000);
writeLcdNibble(0b00000001); //0 0 0 0 0 0 0 1 |Display clear
writeLcdNibble(0b00000000);
writeLcdNibble(0b00000010); //0 0 0 0 0 1 ID S |Entry mode set: Sets cursor move direction and specifies display shift. These operations are performed during data write and read.
writeLcdNibble(0b00000000);
writeLcdNibble(0b00001100); //0 0 0 0 1 D C B |Display on: Sets entire display (D) on/off,cursor on/off (C), and blinkingof cursor position character(B).
}
void UniLcdBtnBrd::writeLcdNibble(char data)
{
if (_bLightOn)
data = data|0b10000000;
Wire.beginTransmission(_pExpAddLcd);//write nibble
Wire.write(data);
Wire.endTransmission();
Wire.beginTransmission(_pExpAddLcd);//enable High
if (_bLightOn)
Wire.write(data|0b11000000);
else
Wire.write(data|0b01000000);
Wire.endTransmission();
Wire.beginTransmission(_pExpAddLcd);//enable Low
if (_bLightOn)
Wire.write(0b00011111&data)|0b10000000;
else
Wire.write(0b00011111&data);
Wire.endTransmission();
}
void UniLcdBtnBrd::writeLcdByte(char nibbleH,char nibbleL)
{
writeLcdNibble(nibbleH);
writeLcdNibble(nibbleL);
Wire.beginTransmission(_pExpAddLcd);//enable Low
if (_bLightOn)
Wire.write(0b10000000);
else
Wire.write(byte(0));
Wire.endTransmission();
}
void UniLcdBtnBrd::writeLcdChar(char data)
{
char high=data;
char low = data;
low = 0b00001111&low;
low = 0b00010000|low;
high = (data>>4);
high = 0b00001111&high;
high = 0b00010000|high;
writeLcdByte(high,low);
}
void UniLcdBtnBrd::setLcdAddr(char data)
{
char high=data;
char low = data;
low = 0b00001111&low;
low = 0b00000000|low;
high = (data>>4);
high = 0b00001111&high;
high = 0b00001000|high;
writeLcdByte(high,low);
}
void UniLcdBtnBrd::writeLcdStr(String data_str, int lineNr)
{
char address;
int i =0;
switch (lineNr)
{
case 1:
address =_lcdLine1Add;
break;
case 2:
address =_lcdLine2Add;
break;
case 3:
address =_lcdLine3Add;
break;
case 4:
address =_lcdLine4Add;
break;
default:
address =_lcdLine1Add;
break;
}
setLcdAddr(address);
while(data_str[i]&&i<_lcdLineLen)
{
writeLcdChar(data_str[i]);
i++;
}
while(i<_lcdLineLen)
{
writeLcdChar(' ');
i++;
}
}
void UniLcdBtnBrd::clearLcd()
{
writeLcdByte(0x0,0x1);
}
void UniLcdBtnBrd::setLcdDisplay(boolean displayOn, boolean cursorOn,boolean cursorBlink,boolean BLOn)
{
char instruction = 0b1000;
if(displayOn)
instruction = instruction|0b0100;
if(cursorOn)
instruction = instruction|0b0010;
if(cursorBlink)
instruction = instruction|0b0001;
_bLightOn = BLOn;
writeLcdByte(0x0,instruction);
}
void UniLcdBtnBrd::initBtns()
{
Wire.beginTransmission(_pExpAddBtns);
Wire.write((byte)0);
Wire.endTransmission();
}
char readBtns()
{
char data;
Wire.requestFrom(_pExpAddBtns, 1); // request 1 byte from slave device
data = Wire.read(); // receive a byte as character
return data;
}
Here is an example of how to use the Library with the LCD
#include "Wire.h"
#include "UniLcdBtnBrd.h"
UniLcdBtnBrd brd1 = UniLcdBtnBrd();
void setup()
{
brd1.begin();
brd1.setLcdLinesAdd(0,0x40);
brd1.writeLcdStr(" Hello ",1);
brd1.writeLcdStr(" World! ",2);
}
void loop()
{
// put your main code here, to run repeatedly:
}
UniLcdBtnBrd.zip (2.98 KB)