4. Software

4.1. library header


/* NHD_i2c_lcd.h
 * Library header for NHD-0420D3Z-FL-GBW-V3
 * Utilizes the Arduino Wire library
 * Free to use and edit
 * Note about sending commands to the LCD: all commands (aside from printing) must start
 * by sending the PREFIX byte. To print characters, send the hex representation of an
 * ASCII character
 */

#ifndef NHD_I2C_LCD
#define NHD_I2C_LCD

#include "Arduino.h"
#include "Wire.h"
#include "Print.h"

/*--- I2C clock frequency (Hz) ---*/
/* Note: at 50 kHz the performance of the lcd seems very stable. At ~53 kHz and above, characters may start dropping out.
 */
#define CLK_FREQ          	50000

/*--- Default i2c address ---*/
#define LCD_ADDR          	0x28

/*--- Command prefix ---*/
#define PREFIX            	0xfe

/*--- Commands ---*/
#define DISP_ON           	0x41
#define DISP_OFF          	0x42
#define SET_CURSOR        	0x45
#define CURSOR_HOME       	0x46
#define UNDERLINE_ON      	0x47
#define UNDERLINE_OFF     	0x48
#define MOVE_CURSOR_LEFT  	0x49
#define MOVE_CURSOR_RIGHT 	0x4a
#define BLINK_ON          	0x4b
#define BLINK_OFF         	0x4c
#define BACKSPACE         	0x4e
#define CLEAR_SCREEN      	0x51
#define SET_CONTRAST      	0x52
#define BACKLIGHT_BRIGHTNESS  0x53
#define LOAD_CUSTOM_CHAR  	0x54
#define MOVE_DISP_LEFT    	0x55
#define MOVE_DISP_RIGHT   	0x56
#define CHANGE_BAUD_RATE  	0x61
#define CHANGE_I2C_ADDR   	0x62
#define DISP_FIRMWARE_VERSION 0x70
#define DISP_BAUD_RATE    	0x71
#define DISP_I2C_ADDR     	0x72

/*--- Custom class for the LCD  ---*/
class NHD_i2c_lcd : public Print {      	// Iheritance from print enables us to use the premade print() method.

public:

	// Constructors and destructor
	NHD_i2c_lcd() {};

	NHD_i2c_lcd(byte contr, byte bright, bool state): contrast(contr), brightness(bright), on(state) {};

	~NHD_i2c_lcd() {};

	// Initialize the lcd according to default values or given constructor parameters
	void init();

	// Turn underline cursor on or off      	// true for on, false for off
	void underline(bool state);             	//
	// Turn display on or off               	//
	void display(bool state);               	//
	// Turn blinking cursor on or off       	//
	void blink(bool state);                 	//

	// Set cursor to desired location
	void setCursor(byte x, byte y);
	// Parse long prints so that words that would be split
	// due to the line ending will be printed on a new line instead
	void smartPrint(String msg);
	// Print out a custom character
	void printCustom(byte addr);
	using Print::write;
	// Display one character on the lcd
	virtual size_t write(uint8_t);
	// Clear a certain number of chars beginning from (x,y)
	void clearChars(byte x, byte y, byte number);
	// Clear the whole screen
	void clear();
	// Contrast value must be between 1 and 50 (decimal)
	void setContrast(byte value);
	// Brightness value must be between 1 and 8 (decimal); the value
	// corresponds to indices in a built-in list of brightness settings
	void backlightBrightness(byte value);
	// Shift the cursor left (direction = 0) or right (direction = 1)
	void moveCursor(byte direction, byte steps);
	// Shift the display left (direction = 0) or right (direction = 1)
	void moveDisplay(byte direction, byte steps);
	void displayBaudRate();
	// The value must be between 1 and 8 (the new value is selected from a list)
	void changeBaudRate(byte baudRate);
	void displayI2Caddr();
	// The new address must be between 0x00 and 0xFE, with the least
	// significant bit always 0 (address must be even)
	void changeI2Caddr(byte addr);

	// Load a custom character into memory. Storage address (addr): value from 0 to 7,
	// describes the memory address of the custom character.
	// Character pattern (bitmap[8]): describes the shape of the custom character
	void loadCustomChar(byte addr, byte bitmap[8]);

private:

	// Various members and their default values
	byte contrast = 40;
	byte brightness = 6;
	bool on = true;
	byte cursorX = 0;
	byte cursorY = 0;
};

// Get the length of a word (a string of characters starting
// from 'start' and terminating in a space or null character)
uint8_t wordLength(char* start);

#endif