I'm writing my own library for the LCD with the idea to get in-depth knowledge of the process and the last thing that I didn't implement in it was the custom characters.
I came up to the problem, where if I don't reset the cursor and clear the display, I can't write anymore after the write to the CG-RAM .... , which forces me to create the custom characters at the beginning and then reset the cursor.
Then I checked the example of the official library to test if the same problem is present in it too and it is:
lcd.begin(16, 2);
// create a new character
lcd.createChar(0, heart);
// set the cursor to the top left.
lcd.setCursor(0, 0);
// Print a message to the LCD.
lcd.print("Hi:");
lcd.write(byte(0)); // when calling lcd.write() '0' must be cast as a byte
The characters are first created, then the cursor is reset to 0,0. If we skip that no character will be printed, but why?
There is a problem with the library, createChar calls the command for setting value to the CG-RAM, but doesn't return to DD-RAM.
Because of that, in their example they all setCursor, which does exactly that.
But that creates a problem.
If you want to create a character after you have written something to the display, you must revert to DD-RAM, but you must know the position of the cursor, which it was before writing to CG-RAM ...
The solution is simple Just keep a record, where you cursor's position was and set the DD-RAM to it.