Port of the Arduino library with the same library. Includes a instance of the Print library from arduino

Dependencies:   mbed Print

Summary

A Conversion of the Arduino LiquidCrystal_I2C by Frank de Brabander to MBED
Version of original Library: 1.1.2
Information from the original library: The library allows to control I2C displays with functions extremely similar to LiquidCrystal library.
Original Github: https://github.com/johnrickman/LiquidCrystal_I2C


Code Example

This is a working example using the basic library features

example.cpp

#include "LiquidCrystal_I2C.h"

LiquidCrystal_I2C lcd(0x27, 20, 4, I2C_SDA, I2C_SCL);
void setup();
void loop();

//Arduino style setup function
void setup() {
	lcd.init();
	for (int i = 0; i < 3; i++) {
		lcd.backlight();
		wait_ms(250);
		lcd.noBacklight();
		wait_ms(250);
	}
	lcd.backlight();
	lcd.setCursor(3, 0); // Start at character 4 on line 0
	lcd.print("Hello, world!");
	wait_ms(500);
	lcd.setCursor(2, 1);
	lcd.print("From YourDuino");
	wait_ms(500);
	lcd.setCursor(0, 2);
	lcd.print("20 by 4 Line Display");
	lcd.setCursor(1, 3);
	wait_ms(1000);
	lcd.print("DVSProductions.de");
	wait_ms(8000);
}

//print some random characters
void loop() {
	//lcd.setCursor(rand() % 20, rand() % 4);
	//lcd.print(rand() % 10);      
	for (int x = 0; x != 20; x++)
		for (int y = 0; y != 20; y++)
			lcd.print(rand() % 10);
}

//Behave like an Arduino
int main() {
	setup();
	while (true)loop();
}

Misc

It is important to note that the library requires the init function to be called before sending any data. Instead of the arduino typical:

arduino.ino

LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup(){
	lcd.begin(20, 4);
}

We pass the pins and size to the constructor and then just call the init function:

mbed.cpp

LiquidCrystal_I2C lcd(0x27, 20, 4, I2C_SDA, I2C_SCL);
void setup(){
	lcd.init();
}

Also important is that the begin function still has value. It is now able to change the size of the LCD after the constructor. This might have few applications, but it is more powerful than the Arduino version, where the parameters had barely any use.

Committer:
DVSProductions
Date:
Thu Aug 15 13:55:21 2019 +0000
Revision:
4:402a5c6b2457
Parent:
2:3f791f8019a8
INSANE PERFORMANCE BOOST

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DVSProductions 0:5f9ddd9b7a33 1 // YWROBOT
DVSProductions 0:5f9ddd9b7a33 2 #ifndef LiquidCrystal_I2C_h
DVSProductions 0:5f9ddd9b7a33 3 #define LiquidCrystal_I2C_h
DVSProductions 0:5f9ddd9b7a33 4 #include "Print.h"
DVSProductions 0:5f9ddd9b7a33 5 #include "mbed.h"
DVSProductions 0:5f9ddd9b7a33 6 #include <inttypes.h>
DVSProductions 0:5f9ddd9b7a33 7
DVSProductions 0:5f9ddd9b7a33 8 // commands
DVSProductions 0:5f9ddd9b7a33 9 #define LCD_CLEARDISPLAY 0x01
DVSProductions 0:5f9ddd9b7a33 10 #define LCD_RETURNHOME 0x02
DVSProductions 0:5f9ddd9b7a33 11 #define LCD_ENTRYMODESET 0x04
DVSProductions 0:5f9ddd9b7a33 12 #define LCD_DISPLAYCONTROL 0x08
DVSProductions 0:5f9ddd9b7a33 13 #define LCD_CURSORSHIFT 0x10
DVSProductions 0:5f9ddd9b7a33 14 #define LCD_FUNCTIONSET 0x20
DVSProductions 0:5f9ddd9b7a33 15 #define LCD_SETCGRAMADDR 0x40
DVSProductions 0:5f9ddd9b7a33 16 #define LCD_SETDDRAMADDR 0x80
DVSProductions 0:5f9ddd9b7a33 17
DVSProductions 0:5f9ddd9b7a33 18 // flags for display entry mode
DVSProductions 0:5f9ddd9b7a33 19 #define LCD_ENTRYRIGHT 0x00
DVSProductions 0:5f9ddd9b7a33 20 #define LCD_ENTRYLEFT 0x02
DVSProductions 0:5f9ddd9b7a33 21 #define LCD_ENTRYSHIFTINCREMENT 0x01
DVSProductions 0:5f9ddd9b7a33 22 #define LCD_ENTRYSHIFTDECREMENT 0x00
DVSProductions 0:5f9ddd9b7a33 23
DVSProductions 0:5f9ddd9b7a33 24 // flags for display on/off control
DVSProductions 0:5f9ddd9b7a33 25 #define LCD_DISPLAYON 0x04
DVSProductions 0:5f9ddd9b7a33 26 #define LCD_DISPLAYOFF 0x00
DVSProductions 0:5f9ddd9b7a33 27 #define LCD_CURSORON 0x02
DVSProductions 0:5f9ddd9b7a33 28 #define LCD_CURSOROFF 0x00
DVSProductions 0:5f9ddd9b7a33 29 #define LCD_BLINKON 0x01
DVSProductions 0:5f9ddd9b7a33 30 #define LCD_BLINKOFF 0x00
DVSProductions 0:5f9ddd9b7a33 31
DVSProductions 0:5f9ddd9b7a33 32 // flags for display/cursor shift
DVSProductions 0:5f9ddd9b7a33 33 #define LCD_DISPLAYMOVE 0x08
DVSProductions 0:5f9ddd9b7a33 34 #define LCD_CURSORMOVE 0x00
DVSProductions 0:5f9ddd9b7a33 35 #define LCD_MOVERIGHT 0x04
DVSProductions 0:5f9ddd9b7a33 36 #define LCD_MOVELEFT 0x00
DVSProductions 0:5f9ddd9b7a33 37
DVSProductions 0:5f9ddd9b7a33 38 // flags for function set
DVSProductions 0:5f9ddd9b7a33 39 #define LCD_8BITMODE 0x10
DVSProductions 0:5f9ddd9b7a33 40 #define LCD_4BITMODE 0x00
DVSProductions 0:5f9ddd9b7a33 41 #define LCD_2LINE 0x08
DVSProductions 0:5f9ddd9b7a33 42 #define LCD_1LINE 0x00
DVSProductions 0:5f9ddd9b7a33 43 #define LCD_5x10DOTS 0x04
DVSProductions 0:5f9ddd9b7a33 44 #define LCD_5x8DOTS 0x00
DVSProductions 0:5f9ddd9b7a33 45
DVSProductions 0:5f9ddd9b7a33 46 // flags for backlight control
DVSProductions 0:5f9ddd9b7a33 47 #define LCD_BACKLIGHT 0x08
DVSProductions 0:5f9ddd9b7a33 48 #define LCD_NOBACKLIGHT 0x00
DVSProductions 0:5f9ddd9b7a33 49
DVSProductions 0:5f9ddd9b7a33 50 #define En 0B00000100 // Enable bit
DVSProductions 0:5f9ddd9b7a33 51 #define Rw 0B00000010 // Read/Write bit
DVSProductions 0:5f9ddd9b7a33 52 #define Rs 0B00000001 // Register select bit
DVSProductions 0:5f9ddd9b7a33 53
DVSProductions 0:5f9ddd9b7a33 54 class LiquidCrystal_I2C : public Print {
DVSProductions 0:5f9ddd9b7a33 55 public:
DVSProductions 0:5f9ddd9b7a33 56 LiquidCrystal_I2C(uint8_t lcd_Addr, uint8_t lcd_cols, uint8_t lcd_rows,
DVSProductions 0:5f9ddd9b7a33 57 PinName sda, PinName scl);
DVSProductions 0:5f9ddd9b7a33 58 void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS);
DVSProductions 0:5f9ddd9b7a33 59 void clear();
DVSProductions 0:5f9ddd9b7a33 60 void home();
DVSProductions 0:5f9ddd9b7a33 61 void noDisplay();
DVSProductions 0:5f9ddd9b7a33 62 void display();
DVSProductions 0:5f9ddd9b7a33 63 void noBlink();
DVSProductions 0:5f9ddd9b7a33 64 void blink();
DVSProductions 0:5f9ddd9b7a33 65 void noCursor();
DVSProductions 0:5f9ddd9b7a33 66 void cursor();
DVSProductions 0:5f9ddd9b7a33 67 void scrollDisplayLeft();
DVSProductions 0:5f9ddd9b7a33 68 void scrollDisplayRight();
DVSProductions 0:5f9ddd9b7a33 69 void printLeft();
DVSProductions 0:5f9ddd9b7a33 70 void printRight();
DVSProductions 0:5f9ddd9b7a33 71 void leftToRight();
DVSProductions 0:5f9ddd9b7a33 72 void rightToLeft();
DVSProductions 0:5f9ddd9b7a33 73 void shiftIncrement();
DVSProductions 0:5f9ddd9b7a33 74 void shiftDecrement();
DVSProductions 0:5f9ddd9b7a33 75 void noBacklight();
DVSProductions 0:5f9ddd9b7a33 76 void backlight();
DVSProductions 0:5f9ddd9b7a33 77 void autoscroll();
DVSProductions 0:5f9ddd9b7a33 78 void noAutoscroll();
DVSProductions 0:5f9ddd9b7a33 79 void createChar(uint8_t, uint8_t[]);
DVSProductions 0:5f9ddd9b7a33 80 void setCursor(uint8_t, uint8_t);
DVSProductions 0:5f9ddd9b7a33 81
DVSProductions 0:5f9ddd9b7a33 82 virtual size_t write(uint8_t);
DVSProductions 0:5f9ddd9b7a33 83 void command(uint8_t);
DVSProductions 0:5f9ddd9b7a33 84 void init();
DVSProductions 0:5f9ddd9b7a33 85
DVSProductions 0:5f9ddd9b7a33 86 ////compatibility API function aliases
DVSProductions 0:5f9ddd9b7a33 87 void blink_on(); // alias for blink()
DVSProductions 0:5f9ddd9b7a33 88 void blink_off(); // alias for noBlink()
DVSProductions 0:5f9ddd9b7a33 89 void cursor_on(); // alias for cursor()
DVSProductions 0:5f9ddd9b7a33 90 void cursor_off(); // alias for noCursor()
DVSProductions 0:5f9ddd9b7a33 91 void setBacklight(uint8_t new_val); // alias for backlight() and nobacklight()
DVSProductions 0:5f9ddd9b7a33 92 void load_custom_character(uint8_t char_num,
DVSProductions 0:5f9ddd9b7a33 93 uint8_t *rows); // alias for createChar()
DVSProductions 0:5f9ddd9b7a33 94 void printstr(const char[]);
DVSProductions 0:5f9ddd9b7a33 95
DVSProductions 0:5f9ddd9b7a33 96 ////Unsupported API functions (not implemented in this library)
DVSProductions 0:5f9ddd9b7a33 97 uint8_t status();
DVSProductions 0:5f9ddd9b7a33 98 void setContrast(uint8_t new_val);
DVSProductions 0:5f9ddd9b7a33 99 uint8_t keypad();
DVSProductions 0:5f9ddd9b7a33 100 void setDelay(int, int);
DVSProductions 0:5f9ddd9b7a33 101 void on();
DVSProductions 0:5f9ddd9b7a33 102 void off();
DVSProductions 0:5f9ddd9b7a33 103 uint8_t init_bargraph(uint8_t graphtype);
DVSProductions 0:5f9ddd9b7a33 104 void draw_horizontal_graph(uint8_t row, uint8_t column, uint8_t len,
DVSProductions 0:5f9ddd9b7a33 105 uint8_t pixel_col_end);
DVSProductions 0:5f9ddd9b7a33 106 void draw_vertical_graph(uint8_t row, uint8_t column, uint8_t len,
DVSProductions 0:5f9ddd9b7a33 107 uint8_t pixel_col_end);
DVSProductions 0:5f9ddd9b7a33 108
DVSProductions 0:5f9ddd9b7a33 109 private:
DVSProductions 0:5f9ddd9b7a33 110 mbed::I2C *i2c;
DVSProductions 0:5f9ddd9b7a33 111 void init_priv();
DVSProductions 2:3f791f8019a8 112 void begin_priv(uint8_t charsize = LCD_5x8DOTS);
DVSProductions 0:5f9ddd9b7a33 113 void send(uint8_t, uint8_t);
DVSProductions 0:5f9ddd9b7a33 114 void write4bits(uint8_t);
DVSProductions 0:5f9ddd9b7a33 115 void expanderWrite(uint8_t);
DVSProductions 0:5f9ddd9b7a33 116 void pulseEnable(uint8_t);
DVSProductions 0:5f9ddd9b7a33 117 uint8_t _Addr;
DVSProductions 0:5f9ddd9b7a33 118 uint8_t _displayfunction;
DVSProductions 0:5f9ddd9b7a33 119 uint8_t _displaycontrol;
DVSProductions 0:5f9ddd9b7a33 120 uint8_t _displaymode;
DVSProductions 0:5f9ddd9b7a33 121 uint8_t _numlines;
DVSProductions 0:5f9ddd9b7a33 122 uint8_t _cols;
DVSProductions 0:5f9ddd9b7a33 123 uint8_t _rows;
DVSProductions 0:5f9ddd9b7a33 124 uint8_t _backlightval;
DVSProductions 0:5f9ddd9b7a33 125 };
DVSProductions 0:5f9ddd9b7a33 126
DVSProductions 0:5f9ddd9b7a33 127 #endif