Hi everyone,
I followed the library tutorial that uses the Morse example to create my own library.
I created my library from a working sketch in Arduino 1.0 that I use on an Uno. The code seems to make the board hang right after I call my library's constructor.
I would much appreciate it if someone can glance over my code and check my C++ syntax. I have tried to compare it to other working libraries with no luck.
Please excuse me for not strictly following the naming conventions but I will read up and fix that once the library is working.
Thanks in advance.
Richard
H-File
#ifndef UniLcdBtnBrd_h
#define UniLcdBtnBrd_h
#include <Arduino.h>
#include <..\Wire\Wire.h>
class UniLcdBtnBrd
{
public:
UniLcdBtnBrd();
UniLcdBtnBrd(char lcdAdd, char btnsAdd);
void setBoardAddresses(char lcdAdd, char btnsAdd);
void setLCDLinesAdd(char add1);
void setLCDLinesAdd(char add1,[color=black][/color] 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 flashLED();
private:
boolean BLightOn;
char pExpAddLCD;
char pExpAddBtns;
char lcdLine1Add;
char lcdLine2Add;
char lcdLine3Add;
char lcdLine4Add;
char lcdLineLen;
void writeLCDNibble(int data);
void writeLCDByte(char nibbleH,char nibbleL);
};
#endif
CPP File
/*
UniLcdBtnBrd.cpp - library for using the universal lcd and buttons board.
Created by Richard Whittington, 08/05/2012
*/
#include "Arduino.h"
#include <..\Wire\Wire.h>
#include "UniLcdBtnBrd.h"
boolean BLightOn = true;
char pExpAddLCD = 56;
char pExpAddBtns = 57;
char lcdLine1Add = byte(0);
char lcdLine2Add = byte(0);
char lcdLine3Add = byte(0);
char lcdLine4Add = byte(0);
char lcdLineLen = 16;
UniLcdBtnBrd::UniLcdBtnBrd()
{
flashLED();
Wire.begin();
initLCD();
initBtns();
}
UniLcdBtnBrd::UniLcdBtnBrd(char lcdAdd, char btnsAdd)
{
flashLED();
pExpAddLCD = lcdAdd;
pExpAddBtns = btnsAdd;
Wire.begin();
initLCD();
initBtns();
}
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(int 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;
}
Moderator edit: Code rendered legible, colour- and italic-free by correct use of tags.
UniLcdBtnBrd.zip (2.46 KB)
