Skip to content

Commit 14b5915

Browse files
Replaced LiquidCrystal_I2C lib with newer version which was reported to have worked on the arduino forum - ref http://forum.arduino.cc/index.php?topic=310743.msg2186521#msg2186521
1 parent f92015b commit 14b5915

15 files changed

Lines changed: 386 additions & 313 deletions

File tree

STM32F1/libraries/LiquidCrystal_I2C/LiquidCrystal_I2C.cpp

Lines changed: 89 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
// LiquidCrystal_I2C V2.0
2+
13
#include "LiquidCrystal_I2C.h"
24
#include <inttypes.h>
3-
#include <Arduino.h>
4-
#include <Wire.h>
5+
#include "Wire.h"
6+
#include "Arduino.h"
7+
58

69
// When the display powers up, it is configured as follows:
710
//
@@ -22,55 +25,64 @@
2225
// can't assume that its in that state when a sketch starts (and the
2326
// LiquidCrystal constructor is called).
2427

25-
LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t lcd_addr, uint8_t lcd_cols, uint8_t lcd_rows, uint8_t charsize)
28+
LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t lcd_Addr,uint8_t lcd_cols,uint8_t lcd_rows)
2629
{
27-
_addr = lcd_addr;
28-
_cols = lcd_cols;
29-
_rows = lcd_rows;
30-
_charsize = charsize;
31-
_backlightval = LCD_BACKLIGHT;
30+
_Addr = lcd_Addr;
31+
_cols = lcd_cols;
32+
_rows = lcd_rows;
33+
_backlightval = LCD_NOBACKLIGHT;
34+
}
35+
36+
void LiquidCrystal_I2C::init(){
37+
init_priv();
3238
}
3339

34-
void LiquidCrystal_I2C::begin() {
40+
void LiquidCrystal_I2C::init_priv()
41+
{
3542
Wire.begin();
3643
_displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
44+
begin(_cols, _rows);
45+
}
3746

38-
if (_rows > 1) {
47+
void LiquidCrystal_I2C::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
48+
if (lines > 1) {
3949
_displayfunction |= LCD_2LINE;
4050
}
51+
_numlines = lines;
4152

4253
// for some 1 line displays you can select a 10 pixel high font
43-
if ((_charsize != 0) && (_rows == 1)) {
54+
if ((dotsize != 0) && (lines == 1)) {
4455
_displayfunction |= LCD_5x10DOTS;
4556
}
4657

4758
// SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
4859
// according to datasheet, we need at least 40ms after power rises above 2.7V
4960
// before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
50-
delay(50);
51-
61+
delayMicroseconds(50000);
62+
5263
// Now we pull both RS and R/W low to begin commands
5364
expanderWrite(_backlightval); // reset expanderand turn backlight off (Bit 8 =1)
5465
delay(1000);
5566

56-
//put the LCD into 4 bit mode
67+
//put the LCD into 4 bit mode
5768
// this is according to the hitachi HD44780 datasheet
5869
// figure 24, pg 46
59-
70+
6071
// we start in 8bit mode, try to set 4 bit mode
61-
write4bits(0x03 << 4);
72+
write4bits(0x03);
6273
delayMicroseconds(4500); // wait min 4.1ms
63-
74+
6475
// second try
65-
write4bits(0x03 << 4);
76+
write4bits(0x03);
6677
delayMicroseconds(4500); // wait min 4.1ms
67-
78+
6879
// third go!
69-
write4bits(0x03 << 4);
80+
write4bits(0x03);
7081
delayMicroseconds(150);
71-
82+
7283
// finally, set to 4-bit interface
73-
write4bits(0x02 << 4);
84+
write4bits(0x02);
85+
7486

7587
// set # lines, font size, etc.
7688
command(LCD_FUNCTIONSET | _displayfunction);
@@ -89,8 +101,11 @@ void LiquidCrystal_I2C::begin() {
89101
command(LCD_ENTRYMODESET | _displaymode);
90102

91103
home();
104+
92105
}
93106

107+
108+
94109
/********** high level commands, for the user! */
95110
void LiquidCrystal_I2C::clear(){
96111
command(LCD_CLEARDISPLAY);// clear display, set cursor position to zero
@@ -104,8 +119,8 @@ void LiquidCrystal_I2C::home(){
104119

105120
void LiquidCrystal_I2C::setCursor(uint8_t col, uint8_t row){
106121
int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
107-
if (row > _rows) {
108-
row = _rows-1; // we count rows starting w/0
122+
if ( row > _numlines ) {
123+
row = _numlines-1; // we count rows starting w/0
109124
}
110125
command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
111126
}
@@ -193,6 +208,8 @@ void LiquidCrystal_I2C::backlight(void) {
193208
expanderWrite(0);
194209
}
195210

211+
212+
196213
/*********** mid level commands, for sending data/cmds */
197214

198215
inline void LiquidCrystal_I2C::command(uint8_t value) {
@@ -201,46 +218,67 @@ inline void LiquidCrystal_I2C::command(uint8_t value) {
201218

202219
inline size_t LiquidCrystal_I2C::write(uint8_t value) {
203220
send(value, Rs);
221+
return 0;
204222
}
205223

206224

225+
207226
/************ low level data pushing commands **********/
208227

209228
// write either command or data
210229
void LiquidCrystal_I2C::send(uint8_t value, uint8_t mode) {
211-
uint8_t highnib=value&0xf0;
212-
uint8_t lownib=(value<<4)&0xf0;
230+
uint8_t highnib=value>>4;
231+
uint8_t lownib=value & 0x0F;
213232
write4bits((highnib)|mode);
214-
write4bits((lownib)|mode);
233+
write4bits((lownib)|mode);
215234
}
216235

217236
void LiquidCrystal_I2C::write4bits(uint8_t value) {
218237
expanderWrite(value);
219238
pulseEnable(value);
220239
}
221240

222-
void LiquidCrystal_I2C::expanderWrite(uint8_t _data){
223-
Wire.beginTransmission(_addr);
241+
void LiquidCrystal_I2C::expanderWrite(uint8_t _data){
242+
Wire.beginTransmission(_Addr);
224243
Wire.write((int)(_data) | _backlightval);
225244
Wire.endTransmission();
226245
}
227246

228-
void LiquidCrystal_I2C::pulseEnable(uint8_t _data){
229-
expanderWrite(_data | En); // En high
230-
delayMicroseconds(1); // enable pulse must be >450ns
247+
void LiquidCrystal_I2C::pulseEnable(uint8_t _data){
248+
expanderWrite(_data | En); // En high
249+
delayMicroseconds(1); // enable pulse must be >450ns
231250

232-
expanderWrite(_data & ~En); // En low
233-
delayMicroseconds(50); // commands need > 37us to settle
251+
expanderWrite(_data & ~En); // En low
252+
delayMicroseconds(50); // commands need > 37us to settle
253+
}
254+
255+
256+
// Alias functions
257+
258+
void LiquidCrystal_I2C::cursor_on(){
259+
cursor();
260+
}
261+
262+
void LiquidCrystal_I2C::cursor_off(){
263+
noCursor();
264+
}
265+
266+
void LiquidCrystal_I2C::blink_on(){
267+
blink();
268+
}
269+
270+
void LiquidCrystal_I2C::blink_off(){
271+
noBlink();
234272
}
235273

236274
void LiquidCrystal_I2C::load_custom_character(uint8_t char_num, uint8_t *rows){
237-
createChar(char_num, rows);
275+
createChar(char_num, rows);
238276
}
239277

240278
void LiquidCrystal_I2C::setBacklight(uint8_t new_val){
241-
if (new_val) {
279+
if(new_val){
242280
backlight(); // turn backlight on
243-
} else {
281+
}else{
244282
noBacklight(); // turn backlight off
245283
}
246284
}
@@ -250,3 +288,17 @@ void LiquidCrystal_I2C::printstr(const char c[]){
250288
//it's here so the user sketch doesn't have to be changed
251289
print(c);
252290
}
291+
292+
293+
// unsupported API functions
294+
void LiquidCrystal_I2C::off(){}
295+
void LiquidCrystal_I2C::on(){}
296+
void LiquidCrystal_I2C::setDelay (int cmdDelay,int charDelay) {}
297+
uint8_t LiquidCrystal_I2C::status(){return 0;}
298+
uint8_t LiquidCrystal_I2C::keypad (){return 0;}
299+
uint8_t LiquidCrystal_I2C::init_bargraph(uint8_t graphtype){return 0;}
300+
void LiquidCrystal_I2C::draw_horizontal_graph(uint8_t row, uint8_t column, uint8_t len, uint8_t pixel_col_end){}
301+
void LiquidCrystal_I2C::draw_vertical_graph(uint8_t row, uint8_t column, uint8_t len, uint8_t pixel_row_end){}
302+
void LiquidCrystal_I2C::setContrast(uint8_t new_val){}
303+
304+

0 commit comments

Comments
 (0)