From 8b5f36c55e104fb97657a637c7b341eb27690700 Mon Sep 17 00:00:00 2001 From: Ted Hess Date: Tue, 7 Nov 2017 17:11:23 -0500 Subject: [PATCH] Upgrade LiquidCrystal driver to recent version and add Prusa custom mods. --- Firmware/LiquidCrystal.cpp | 161 ++++++------------ Firmware/LiquidCrystal.h | 8 +- .../ultralcd_implementation_hitachi_HD44780.h | 2 +- 3 files changed, 53 insertions(+), 118 deletions(-) diff --git a/Firmware/LiquidCrystal.cpp b/Firmware/LiquidCrystal.cpp index 88ac6deac..727d75059 100644 --- a/Firmware/LiquidCrystal.cpp +++ b/Firmware/LiquidCrystal.cpp @@ -21,7 +21,7 @@ // S = 0; No shift // // Note, however, that resetting the Arduino doesn't reset the LCD, so we -// can't assume that it's in that state when a sketch starts (and the +// can't assume that its in that state when a sketch starts (and the // LiquidCrystal constructor is called). LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable, @@ -67,13 +67,6 @@ void LiquidCrystal::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t en _data_pins[6] = d6; _data_pins[7] = d7; - pinMode(_rs_pin, OUTPUT); - // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin# - if (_rw_pin != 255) { - pinMode(_rw_pin, OUTPUT); - } - pinMode(_enable_pin, OUTPUT); - if (fourbitmode) _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS; else @@ -82,21 +75,36 @@ void LiquidCrystal::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t en begin(16, 1); } -void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) { +void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize, bool noclear) { + if (lines > 1) { _displayfunction |= LCD_2LINE; } _numlines = lines; - _currline = 0; + + setRowOffsets(0x00, 0x40, 0x00 + cols, 0x40 + cols); // for some 1 line displays you can select a 10 pixel high font - if ((dotsize != 0) && (lines == 1)) { + if ((dotsize != LCD_5x8DOTS) && (lines == 1)) { _displayfunction |= LCD_5x10DOTS; } + pinMode(_rs_pin, OUTPUT); + // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin# + if (_rw_pin != 255) { + pinMode(_rw_pin, OUTPUT); + } + pinMode(_enable_pin, OUTPUT); + + // Do these once, instead of every time a character is drawn for speed reasons. + for (int i=0; i<((_displayfunction & LCD_8BITMODE) ? 8 : 4); ++i) + { + pinMode(_data_pins[i], OUTPUT); + } + // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION! // according to datasheet, we need at least 40ms after power rises above 2.7V - // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50 + // before sending commands. Arduino can turn on way before 4.5V so we'll wait 50 delayMicroseconds(50000); // Now we pull both RS and R/W low to begin commands digitalWrite(_rs_pin, LOW); @@ -142,137 +150,66 @@ void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) { // finally, set # lines, font size, etc. command(LCD_FUNCTIONSET | _displayfunction); - delayMicroseconds(60); + // turn the display on with no cursor or blinking default _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; display(); - delayMicroseconds(60); - // clear it off - clear(); - delayMicroseconds(3000); - // Initialize to default text direction (for romance languages) - _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT; - // set the entry mode - command(LCD_ENTRYMODESET | _displaymode); - delayMicroseconds(60); -} - - - -void LiquidCrystal::begin_noclear(uint8_t cols, uint8_t lines, uint8_t dotsize) { - if (lines > 1) { - _displayfunction |= LCD_2LINE; - } - _numlines = lines; - _currline = 0; - - // for some 1 line displays you can select a 10 pixel high font - if ((dotsize != 0) && (lines == 1)) { - _displayfunction |= LCD_5x10DOTS; - } - - // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION! - // according to datasheet, we need at least 40ms after power rises above 2.7V - // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50 - delayMicroseconds(50000); - // Now we pull both RS and R/W low to begin commands - digitalWrite(_rs_pin, LOW); - digitalWrite(_enable_pin, LOW); - if (_rw_pin != 255) { - digitalWrite(_rw_pin, LOW); - } - - //put the LCD into 4 bit or 8 bit mode - if (! (_displayfunction & LCD_8BITMODE)) { - // this is according to the hitachi HD44780 datasheet - // figure 24, pg 46 - - // we start in 8bit mode, try to set 4 bit mode - write4bits(0x03); - delayMicroseconds(4500); // wait min 4.1ms - - // second try - write4bits(0x03); - delayMicroseconds(4500); // wait min 4.1ms - - // third go! - write4bits(0x03); - delayMicroseconds(150); - - // finally, set to 4-bit interface - write4bits(0x02); + if (noclear) { + home(); } else { - // this is according to the hitachi HD44780 datasheet - // page 45 figure 23 - - // Send function set command sequence - command(LCD_FUNCTIONSET | _displayfunction); - delayMicroseconds(4500); // wait more than 4.1ms - - // second try - command(LCD_FUNCTIONSET | _displayfunction); - delayMicroseconds(150); - - // third go - command(LCD_FUNCTIONSET | _displayfunction); + // clear it off + clear(); } - // finally, set # lines, font size, etc. - command(LCD_FUNCTIONSET | _displayfunction); - delayMicroseconds(60); - - // turn the display on with no cursor or blinking default - _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; - display(); - delayMicroseconds(60); - // clear it off - //clear(); - - home(); - delayMicroseconds(1600); // Initialize to default text direction (for romance languages) _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT; // set the entry mode command(LCD_ENTRYMODESET | _displaymode); - delayMicroseconds(60); - - setCursor(8,0); - print(" "); - setCursor(8,1); - print(" "); - setCursor(6,2); - print(" "); + if (noclear) { + setCursor(8,0); + print(" "); + setCursor(8,1); + print(" "); + setCursor(6,2); + print(" "); + } } - - +void LiquidCrystal::setRowOffsets(int row0, int row1, int row2, int row3) +{ + _row_offsets[0] = row0; + _row_offsets[1] = row1; + _row_offsets[2] = row2; + _row_offsets[3] = row3; +} /********** high level commands, for the user! */ void LiquidCrystal::clear() { command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero - delayMicroseconds(1600); // this command takes a long time - + delayMicroseconds(2000); // this command takes a long time! } void LiquidCrystal::home() { command(LCD_RETURNHOME); // set cursor position to zero - delayMicroseconds(1600); // this command takes a long time! - + delayMicroseconds(2000); // this command takes a long time! } void LiquidCrystal::setCursor(uint8_t col, uint8_t row) { - int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 }; + const size_t max_lines = sizeof(_row_offsets) / sizeof(*_row_offsets); + if ( row >= max_lines ) { + row = max_lines - 1; // we count rows starting w/0 + } if ( row >= _numlines ) { - row = _numlines-1; // we count rows starting w/0 + row = _numlines - 1; // we count rows starting w/0 } - command(LCD_SETDDRAMADDR | (col + row_offsets[row])); + command(LCD_SETDDRAMADDR | (col + _row_offsets[row])); } // Turn the display on/off (quickly) @@ -388,7 +325,6 @@ void LiquidCrystal::pulseEnable(void) { void LiquidCrystal::write4bits(uint8_t value) { for (int i = 0; i < 4; i++) { - pinMode(_data_pins[i], OUTPUT); digitalWrite(_data_pins[i], (value >> i) & 0x01); } @@ -397,7 +333,6 @@ void LiquidCrystal::write4bits(uint8_t value) { void LiquidCrystal::write8bits(uint8_t value) { for (int i = 0; i < 8; i++) { - pinMode(_data_pins[i], OUTPUT); digitalWrite(_data_pins[i], (value >> i) & 0x01); } diff --git a/Firmware/LiquidCrystal.h b/Firmware/LiquidCrystal.h index 523303b03..8e4bde2d5 100644 --- a/Firmware/LiquidCrystal.h +++ b/Firmware/LiquidCrystal.h @@ -59,9 +59,7 @@ public: uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7); - void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS); - - void begin_noclear(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS); + void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS, bool noclear = false); void clear(); void home(); @@ -79,6 +77,7 @@ public: void autoscroll(); void noAutoscroll(); + void setRowOffsets(int row1, int row2, int row3, int row4); void createChar(uint8_t, uint8_t[]); void setCursor(uint8_t, uint8_t); virtual size_t write(uint8_t); @@ -102,7 +101,8 @@ private: uint8_t _initialized; - uint8_t _numlines,_currline; + uint8_t _numlines; + uint8_t _row_offsets[4]; }; #endif diff --git a/Firmware/ultralcd_implementation_hitachi_HD44780.h b/Firmware/ultralcd_implementation_hitachi_HD44780.h index 275c96d27..883ca5f4d 100644 --- a/Firmware/ultralcd_implementation_hitachi_HD44780.h +++ b/Firmware/ultralcd_implementation_hitachi_HD44780.h @@ -591,7 +591,7 @@ static void lcd_implementation_init_noclear( lcd.backlight(); #else - lcd.begin_noclear(LCD_WIDTH, LCD_HEIGHT); + lcd.begin(LCD_WIDTH, LCD_HEIGHT, LCD_5x8DOTS, true); #endif lcd_set_custom_characters(