From a5daeb5db2615d4f9ea0e71700fb32b80c848347 Mon Sep 17 00:00:00 2001 From: Zaid Mughal <51900989+zaidMughal@users.noreply.github.com> Date: Fri, 20 Mar 2020 15:34:16 +0500 Subject: [PATCH 01/11] Added reading functions --- src/LiquidCrystal.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/LiquidCrystal.h b/src/LiquidCrystal.h index da950ce..b5528d1 100644 --- a/src/LiquidCrystal.h +++ b/src/LiquidCrystal.h @@ -82,12 +82,16 @@ class LiquidCrystal : public Print { void setCursor(uint8_t, uint8_t); virtual size_t write(uint8_t); void command(uint8_t); + uint8_t read_BF_addr(void); + uint8_t read_RAM(void); using Print::write; private: void send(uint8_t, uint8_t); void write4bits(uint8_t); void write8bits(uint8_t); + uint8_t read4bits(); + uint8_t read8bits(); void pulseEnable(); uint8_t _rs_pin; // LOW: command. HIGH: character. From f432865b51eb181519f4402fd0bd7164b008afc5 Mon Sep 17 00:00:00 2001 From: Zaid Mughal <51900989+zaidMughal@users.noreply.github.com> Date: Fri, 20 Mar 2020 16:03:35 +0500 Subject: [PATCH 02/11] Added reading functions and fixed issue #6 Issue # 6 was that createchar() function leave data address in CGRAM position. After that, begin(), setCursor(),clear() or home() functions must be executed before write() function in order for it to work. It is fixed but require rw pin to be set for it to work. This is because, when address register goes to CGRAM, there is no record of its previous position in our MCU. MCU must read its previous position in order for it to go back. In case rw pin is not specified, the register is not returned and it cannot untill rw is specified. Also Added following functions, read_BF_addr read_RAM read read4bits read8bits --- src/LiquidCrystal.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/src/LiquidCrystal.cpp b/src/LiquidCrystal.cpp index 8c6cdf0..964febf 100644 --- a/src/LiquidCrystal.cpp +++ b/src/LiquidCrystal.cpp @@ -263,15 +263,25 @@ void LiquidCrystal::noAutoscroll(void) { // Allows us to fill the first 8 CGRAM locations // with custom characters void LiquidCrystal::createChar(uint8_t location, uint8_t charmap[]) { + int ramAddr = read_BF_addr(); location &= 0x7; // we only have 8 locations 0-7 command(LCD_SETCGRAMADDR | (location << 3)); for (int i=0; i<8; i++) { write(charmap[i]); } + if(ramAddr !=-1) + command(LCD_SETDDRAMADDR | ramAddr); } /*********** mid level commands, for sending data/cmds */ +inline uint8_t LiquidCrystal::read_BF_addr(void) { + return read(LOW); +} +inline uint8_t LiquidCrystal::read_RAM(void) { + return read(HIGH); +} + inline void LiquidCrystal::command(uint8_t value) { send(value, LOW); } @@ -300,6 +310,35 @@ void LiquidCrystal::send(uint8_t value, uint8_t mode) { } } +int LiquidCrystal::read(bool mode) { + if (_rw_pin == 255) { // if there is a RW pin indicated, only then we can read + return -1; + } + else{ + digitalWrite(_rw_pin, HIGH);//for read + digitalWrite(_rs_pin, mode); + + for (int i=0; i<((_displayfunction & LCD_8BITMODE) ? 8 : 4); ++i) + { + pinMode(_data_pins[i], INPUT);//library initializes data pins in output mode, so we need to change mode for reading + } + + uint8_t address; + if (_displayfunction & LCD_8BITMODE) { + address = read8bits(); + } else { + address = read4bits(); + } + + for (int i=0; i<((_displayfunction & LCD_8BITMODE) ? 8 : 4); ++i) + { + pinMode(_data_pins[i], OUTPUT); + } + + return int(address); + } +} + void LiquidCrystal::pulseEnable(void) { digitalWrite(_enable_pin, LOW); delayMicroseconds(1); @@ -324,3 +363,34 @@ void LiquidCrystal::write8bits(uint8_t value) { pulseEnable(); } + + +uint8_t LiquidCrystal::read4bits() { + uint8_t address; + digitalWrite(_enable_pin, LOW); + delayMicroseconds(1); + digitalWrite(_enable_pin, HIGH); + delayMicroseconds(1); // enable pulse must be >450ns + address = digitalRead(_data_pins[3])<<7|digitalRead(_data_pins[2])<<6|digitalRead(_data_pins[1])<<5|digitalRead(_data_pins[0])<<4; + digitalWrite(_enable_pin, LOW); + delayMicroseconds(100); // commands need > 37us to settle + + digitalWrite(_enable_pin, HIGH); + delayMicroseconds(1); // enable pulse must be >450ns + address |= digitalRead(_data_pins[3])<<3|digitalRead(_data_pins[2])<<2|digitalRead(_data_pins[1])<<1|digitalRead(_data_pins[0]); + digitalWrite(_enable_pin, LOW); + delayMicroseconds(100); // commands need > 37us to settle + return address; +} + +uint8_t LiquidCrystal::read8bits() { + uint8_t address; + digitalWrite(_enable_pin, LOW); + delayMicroseconds(1); + digitalWrite(_enable_pin, HIGH); + delayMicroseconds(1); // enable pulse must be >450ns + address = digitalRead(_data_pins[7])<<7|digitalRead(_data_pins[6])<<6|digitalRead(_data_pins[5])<<5|digitalRead(_data_pins[4])<<4|digitalRead(_data_pins[3])<<3|digitalRead(_data_pins[2])<<2|digitalRead(_data_pins[1])<<1|digitalRead(_data_pins[0]); + digitalWrite(_enable_pin, LOW); + delayMicroseconds(100); // commands need > 37us to settle + return address; +} From 017f4898c508f950e6e267dfc54b097e4cd9a55b Mon Sep 17 00:00:00 2001 From: Zaid Mughal <51900989+zaidMughal@users.noreply.github.com> Date: Fri, 20 Mar 2020 16:05:02 +0500 Subject: [PATCH 03/11] Added reading functions --- src/LiquidCrystal.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/LiquidCrystal.h b/src/LiquidCrystal.h index b5528d1..916ada4 100644 --- a/src/LiquidCrystal.h +++ b/src/LiquidCrystal.h @@ -90,6 +90,7 @@ class LiquidCrystal : public Print { void send(uint8_t, uint8_t); void write4bits(uint8_t); void write8bits(uint8_t); + int read(bool); uint8_t read4bits(); uint8_t read8bits(); void pulseEnable(); From 666c4b7283060a4f4521127aea3c5f2e1364b46d Mon Sep 17 00:00:00 2001 From: Zaid Mughal <51900989+zaidMughal@users.noreply.github.com> Date: Fri, 20 Mar 2020 21:59:16 +0500 Subject: [PATCH 04/11] camelCase corrected camelCase corrected in two functions. --- src/LiquidCrystal.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/LiquidCrystal.h b/src/LiquidCrystal.h index 916ada4..4639cef 100644 --- a/src/LiquidCrystal.h +++ b/src/LiquidCrystal.h @@ -82,8 +82,8 @@ class LiquidCrystal : public Print { void setCursor(uint8_t, uint8_t); virtual size_t write(uint8_t); void command(uint8_t); - uint8_t read_BF_addr(void); - uint8_t read_RAM(void); + uint8_t readBFAaddr(void); + uint8_t readRAM(void); using Print::write; private: From c8e036bb50b40de22d8238aff458c30bcbcc1f28 Mon Sep 17 00:00:00 2001 From: Zaid Mughal <51900989+zaidMughal@users.noreply.github.com> Date: Fri, 20 Mar 2020 22:09:35 +0500 Subject: [PATCH 05/11] camelCase and indentation correction --- src/LiquidCrystal.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/LiquidCrystal.cpp b/src/LiquidCrystal.cpp index 964febf..0a71651 100644 --- a/src/LiquidCrystal.cpp +++ b/src/LiquidCrystal.cpp @@ -263,7 +263,7 @@ void LiquidCrystal::noAutoscroll(void) { // Allows us to fill the first 8 CGRAM locations // with custom characters void LiquidCrystal::createChar(uint8_t location, uint8_t charmap[]) { - int ramAddr = read_BF_addr(); + int ramAddr = readBFAddr(); location &= 0x7; // we only have 8 locations 0-7 command(LCD_SETCGRAMADDR | (location << 3)); for (int i=0; i<8; i++) { @@ -275,10 +275,10 @@ void LiquidCrystal::createChar(uint8_t location, uint8_t charmap[]) { /*********** mid level commands, for sending data/cmds */ -inline uint8_t LiquidCrystal::read_BF_addr(void) { +inline uint8_t LiquidCrystal::readBFAddr(void) { return read(LOW); } -inline uint8_t LiquidCrystal::read_RAM(void) { +inline uint8_t LiquidCrystal::readRAM(void) { return read(HIGH); } @@ -330,7 +330,7 @@ int LiquidCrystal::read(bool mode) { address = read4bits(); } - for (int i=0; i<((_displayfunction & LCD_8BITMODE) ? 8 : 4); ++i) + for (int i=0; i<((_displayfunction & LCD_8BITMODE) ? 8 : 4); ++i) { pinMode(_data_pins[i], OUTPUT); } From 3648cc226ee9fa56acb2012f360635bae082f0c0 Mon Sep 17 00:00:00 2001 From: Zaid Mughal <51900989+zaidMughal@users.noreply.github.com> Date: Fri, 20 Mar 2020 22:12:38 +0500 Subject: [PATCH 06/11] read functions keywords added --- keywords.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/keywords.txt b/keywords.txt index b6aa7fd..92b9279 100644 --- a/keywords.txt +++ b/keywords.txt @@ -31,6 +31,8 @@ scrollDisplayLeft KEYWORD2 scrollDisplayRight KEYWORD2 createChar KEYWORD2 setRowOffsets KEYWORD2 +readBFAddr KEYWORD2 +readRAM KEYWORD2 ####################################### # Constants (LITERAL1) From 157f8eeceeb461223ce7dafd64d731f5c1151ca4 Mon Sep 17 00:00:00 2001 From: Zaid Mughal <51900989+zaidMughal@users.noreply.github.com> Date: Fri, 20 Mar 2020 22:17:31 +0500 Subject: [PATCH 07/11] Typo correction --- src/LiquidCrystal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LiquidCrystal.h b/src/LiquidCrystal.h index 4639cef..77b0df6 100644 --- a/src/LiquidCrystal.h +++ b/src/LiquidCrystal.h @@ -82,7 +82,7 @@ class LiquidCrystal : public Print { void setCursor(uint8_t, uint8_t); virtual size_t write(uint8_t); void command(uint8_t); - uint8_t readBFAaddr(void); + uint8_t readBFAddr(void); uint8_t readRAM(void); using Print::write; From 81e28171b794a25443d8ae284f030fdcdf139be3 Mon Sep 17 00:00:00 2001 From: Zaid Mughal <51900989+zaidMughal@users.noreply.github.com> Date: Fri, 20 Mar 2020 23:31:01 +0500 Subject: [PATCH 08/11] read functions returned type changed to int --- src/LiquidCrystal.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/LiquidCrystal.h b/src/LiquidCrystal.h index 77b0df6..4ad556f 100644 --- a/src/LiquidCrystal.h +++ b/src/LiquidCrystal.h @@ -82,8 +82,8 @@ class LiquidCrystal : public Print { void setCursor(uint8_t, uint8_t); virtual size_t write(uint8_t); void command(uint8_t); - uint8_t readBFAddr(void); - uint8_t readRAM(void); + int readBFAddr(void); + int readRAM(void); using Print::write; private: From 8d6b9f3989f4a9e70f8ef009b00416cd7421e7c9 Mon Sep 17 00:00:00 2001 From: Zaid Mughal <51900989+zaidMughal@users.noreply.github.com> Date: Fri, 20 Mar 2020 23:51:19 +0500 Subject: [PATCH 09/11] read function return type changed to int read function return type changed to int so they give -1 if not read. Also readRAM generates an error if both inlined and used in cpp file. Either we put it in .h file or not make it a normal function. So I made it a normal function. --- src/LiquidCrystal.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/LiquidCrystal.cpp b/src/LiquidCrystal.cpp index 0a71651..49f5ae9 100644 --- a/src/LiquidCrystal.cpp +++ b/src/LiquidCrystal.cpp @@ -275,10 +275,10 @@ void LiquidCrystal::createChar(uint8_t location, uint8_t charmap[]) { /*********** mid level commands, for sending data/cmds */ -inline uint8_t LiquidCrystal::readBFAddr(void) { +inline int LiquidCrystal::readBFAddr(void) { return read(LOW); } -inline uint8_t LiquidCrystal::readRAM(void) { +int LiquidCrystal::readRAM(void) { return read(HIGH); } From 88fe4f44e78baf9fca2af3c85135f3d72332bd6b Mon Sep 17 00:00:00 2001 From: Zaid Mughal <51900989+zaidMughal@users.noreply.github.com> Date: Sat, 21 Mar 2020 11:58:52 +0500 Subject: [PATCH 10/11] spaces changed to tabs --- keywords.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/keywords.txt b/keywords.txt index 92b9279..d302e67 100644 --- a/keywords.txt +++ b/keywords.txt @@ -11,7 +11,7 @@ LiquidCrystal KEYWORD1 LiquidCrystal ####################################### # Methods and Functions (KEYWORD2) ####################################### - + begin KEYWORD2 clear KEYWORD2 home KEYWORD2 @@ -31,8 +31,8 @@ scrollDisplayLeft KEYWORD2 scrollDisplayRight KEYWORD2 createChar KEYWORD2 setRowOffsets KEYWORD2 -readBFAddr KEYWORD2 -readRAM KEYWORD2 +readBFAddr KEYWORD2 +readRAM KEYWORD2 ####################################### # Constants (LITERAL1) From 58531741e2f9b0dbf7f6dcad18a400f9e4cc0073 Mon Sep 17 00:00:00 2001 From: Zaid Mughal <51900989+zaidMughal@users.noreply.github.com> Date: Sun, 22 Mar 2020 13:55:06 +0500 Subject: [PATCH 11/11] extra tab deleted --- keywords.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keywords.txt b/keywords.txt index d302e67..11622cb 100644 --- a/keywords.txt +++ b/keywords.txt @@ -11,7 +11,7 @@ LiquidCrystal KEYWORD1 LiquidCrystal ####################################### # Methods and Functions (KEYWORD2) ####################################### - + begin KEYWORD2 clear KEYWORD2 home KEYWORD2