diff --git a/src/LiquidCrystal.cpp b/src/LiquidCrystal.cpp index 8c6cdf0..9ae7f6f 100644 --- a/src/LiquidCrystal.cpp +++ b/src/LiquidCrystal.cpp @@ -270,6 +270,126 @@ void LiquidCrystal::createChar(uint8_t location, uint8_t charmap[]) { } } +// Get the current cursor position and put the information in the passed values +void LiquidCrystal::getCursorPos(int &col, int &row) { + uint8_t value = readBusyFlagAndAddress(); + value &= 0x7f; //get rid of the busy flag if it is set + + row=0; + if(_numlines > 1) + { + //if there are 2 rows + if(value >= 64 )//it means it is on line 2 + { + value-=64; + row=1; + } + } + col = value; +} + +// Retrieves a number of characters starting from the specified position +// and puts them into the buffer, the buffer is NOT null terminated. +// Returns 0 if it could not read or returns the length if it was successful. +// Much faster with more characters than calling getCharAt for each character. +uint8_t LiquidCrystal::getChars(uint8_t col, uint8_t row, char* buffer, uint8_t length) +{ + + if (_rw_pin == 255) + {//if there is no rw pin just return 0 + return 0; + } + + //save the last position to return to it when the function is finished + int prev_col, prev_row; + getCursorPos(prev_col, prev_row); + + setCursor(col, row); + + for(int i=0; i=0; i--) + { + value= value<< 1; + + pinMode(_data_pins[i], INPUT); + if(digitalRead(_data_pins[i]) == HIGH) + { + value+=1; + } + } + digitalWrite(_enable_pin, LOW); + delayMicroseconds(1); //tDHR time + + return value; +} + +// Reads data from the LCD in 8Bit Mode +uint8_t LiquidCrystal::read8bits() +{ + digitalWrite(_enable_pin, HIGH); + delayMicroseconds(1); // tDDR time + uint8_t value =0; + for(int i=7; i>=0; i--) + { + value= value<< 1; + + pinMode(_data_pins[i], INPUT); + if(digitalRead(_data_pins[i]) == HIGH) + { + value+=1; + } + } + digitalWrite(_enable_pin, LOW); + delayMicroseconds(1); //tDHR time + return value; +} diff --git a/src/LiquidCrystal.h b/src/LiquidCrystal.h index da950ce..cdc271f 100644 --- a/src/LiquidCrystal.h +++ b/src/LiquidCrystal.h @@ -82,12 +82,22 @@ class LiquidCrystal : public Print { void setCursor(uint8_t, uint8_t); virtual size_t write(uint8_t); void command(uint8_t); - + void getCursorPos(int &col, int &row); + char getCharAt(uint8_t col, uint8_t row); + char getCharAt(uint8_t col, uint8_t row, uint8_t ret); + uint8_t getChars(uint8_t col, uint8_t row, char* buffer, uint8_t length); + void deleteLast(); + void deleteAt(uint8_t col, uint8_t row); + using Print::write; private: + uint8_t readBusyFlagAndAddress(); void send(uint8_t, uint8_t); + uint8_t receive(int rs_pin_mode); void write4bits(uint8_t); void write8bits(uint8_t); + uint8_t read4bits(); + uint8_t read8bits(); void pulseEnable(); uint8_t _rs_pin; // LOW: command. HIGH: character.