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.

Revision:
0:5f9ddd9b7a33
Child:
2:3f791f8019a8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LiquidCrystal_I2C.h	Mon Jul 29 03:39:46 2019 +0000
@@ -0,0 +1,126 @@
+// YWROBOT
+#ifndef LiquidCrystal_I2C_h
+#define LiquidCrystal_I2C_h
+#include "Print.h"
+#include "mbed.h"
+#include <inttypes.h>
+
+// commands
+#define LCD_CLEARDISPLAY 0x01
+#define LCD_RETURNHOME 0x02
+#define LCD_ENTRYMODESET 0x04
+#define LCD_DISPLAYCONTROL 0x08
+#define LCD_CURSORSHIFT 0x10
+#define LCD_FUNCTIONSET 0x20
+#define LCD_SETCGRAMADDR 0x40
+#define LCD_SETDDRAMADDR 0x80
+
+// flags for display entry mode
+#define LCD_ENTRYRIGHT 0x00
+#define LCD_ENTRYLEFT 0x02
+#define LCD_ENTRYSHIFTINCREMENT 0x01
+#define LCD_ENTRYSHIFTDECREMENT 0x00
+
+// flags for display on/off control
+#define LCD_DISPLAYON 0x04
+#define LCD_DISPLAYOFF 0x00
+#define LCD_CURSORON 0x02
+#define LCD_CURSOROFF 0x00
+#define LCD_BLINKON 0x01
+#define LCD_BLINKOFF 0x00
+
+// flags for display/cursor shift
+#define LCD_DISPLAYMOVE 0x08
+#define LCD_CURSORMOVE 0x00
+#define LCD_MOVERIGHT 0x04
+#define LCD_MOVELEFT 0x00
+
+// flags for function set
+#define LCD_8BITMODE 0x10
+#define LCD_4BITMODE 0x00
+#define LCD_2LINE 0x08
+#define LCD_1LINE 0x00
+#define LCD_5x10DOTS 0x04
+#define LCD_5x8DOTS 0x00
+
+// flags for backlight control
+#define LCD_BACKLIGHT 0x08
+#define LCD_NOBACKLIGHT 0x00
+
+#define En 0B00000100 // Enable bit
+#define Rw 0B00000010 // Read/Write bit
+#define Rs 0B00000001 // Register select bit
+
+class LiquidCrystal_I2C : public Print {
+public:
+  LiquidCrystal_I2C(uint8_t lcd_Addr, uint8_t lcd_cols, uint8_t lcd_rows,
+                    PinName sda, PinName scl);
+  void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS);
+  void clear();
+  void home();
+  void noDisplay();
+  void display();
+  void noBlink();
+  void blink();
+  void noCursor();
+  void cursor();
+  void scrollDisplayLeft();
+  void scrollDisplayRight();
+  void printLeft();
+  void printRight();
+  void leftToRight();
+  void rightToLeft();
+  void shiftIncrement();
+  void shiftDecrement();
+  void noBacklight();
+  void backlight();
+  void autoscroll();
+  void noAutoscroll();
+  void createChar(uint8_t, uint8_t[]);
+  void setCursor(uint8_t, uint8_t);
+
+  virtual size_t write(uint8_t);
+  void command(uint8_t);
+  void init();
+
+  ////compatibility API function aliases
+  void blink_on();                    // alias for blink()
+  void blink_off();                   // alias for noBlink()
+  void cursor_on();                   // alias for cursor()
+  void cursor_off();                  // alias for noCursor()
+  void setBacklight(uint8_t new_val); // alias for backlight() and nobacklight()
+  void load_custom_character(uint8_t char_num,
+                             uint8_t *rows); // alias for createChar()
+  void printstr(const char[]);
+
+  ////Unsupported API functions (not implemented in this library)
+  uint8_t status();
+  void setContrast(uint8_t new_val);
+  uint8_t keypad();
+  void setDelay(int, int);
+  void on();
+  void off();
+  uint8_t init_bargraph(uint8_t graphtype);
+  void draw_horizontal_graph(uint8_t row, uint8_t column, uint8_t len,
+                             uint8_t pixel_col_end);
+  void draw_vertical_graph(uint8_t row, uint8_t column, uint8_t len,
+                           uint8_t pixel_col_end);
+
+private:
+  mbed::I2C *i2c;
+  void init_priv();
+  void send(uint8_t, uint8_t);
+  void write4bits(uint8_t);
+  void expanderWrite(uint8_t);
+  void pulseEnable(uint8_t);
+  uint8_t _Addr;
+  uint8_t _displayfunction;
+  uint8_t _displaycontrol;
+  uint8_t _displaymode;
+  uint8_t _numlines;
+  uint8_t _cols;
+  uint8_t _rows;
+  uint8_t _backlightval;
+};
+
+#endif