@grizzli06 maybe if you told us a bit more about why are you doing the location tracking calculations and why you might be attempting to write beyond the bounds of LCD geometry, we could offer some solutions.
There are several things related to attempting to tell the library to use out bounds cursor positions.
From what I’ve seen (and I’ve looked at pretty much all the arduino LiquidCrystal/hd44780 LCD libraries out there) none of them will crash when you provide out of bounds parameters to functions like setCursor() , but they likely can create unexpected results.
What they tend to do is if the setCursor() row parameter is larger than the maximum value, they set silently the parameter to the maximum value but don’t check for the column parameter. So if the column parameter is beyond the display geometry, it will create a DDRAM address that may produce unexpected results.
But lets take a step back.
The hd44780 is a simple chipset. There is a block of memory inside the chips and that memory is mapped to the display where 1 byte is 1 character on the display.
That said how that mapping is done is different depending on the LCD geometry.
And the memory for each row is not linear. i.e. memory storage for what you see as row 1 on the LCD display is not directly after row 0 on the LCD display.
Where this creates issues is that the LiquidCrystal API does not map directly to the LCD instruction set. i.e. the actual hd44780 chipset LCD instruction set are not based on col/row but rather display memory locations. 0 to 80
What this all means is that “funny” things can happen when you attempt to write beyond the visible LCD display boundaries.
This is because if you write beyond the visible column, the write will write the character to display memory, you just can’t see it.
There are valid reasons to do this, but I won’t get into that.
The “hd44780” library has a special linewrap mode that can be enabled through the API.
When linewrap is enabled, library will take into consideration the LCD geometry and LCD display memory to LCD display mapping to make things easier for the user.
i.e. when linewrap mode is enabled, if the user writes beyond the end of a row, the character will automatically show up on the next row and the row/col is adjusted accordingly.
Write beyond the end of the bottom row and the text will show up on the top row (row 0).
In linewrap mode, It makes output to the LCD work kind of like TTY terminal. As you write to it, the characters advance then wrap to the line below, and if on the lowest line, it wraps up to the top line.
The hd44780 library setCursor() behaves differently in linewrap mode.
In regular mode, the hd44780 behaves as pretty much all other LiquidCrystal API type libraries. If the row is too large, it gets set to the maximum row, and it blindly accepts the col parameter which will likely not do what most people would expect given the resulting DDRAM address.
In linewrap mode, the hd44780 library still truncates the row parameter to ensure it isn’t too large, but for column, it ripples the math through the the rows to make it land where the cursor would be if you printed that many character.
example, on a 4x20 display. if you set the cursor position to (0,30) the position is set to 1,10
The hd44780 github page and wiki documents this API capability and
the hd44780 library comes with an example for each i/o class that demonstrates the linewrap capability.
– bill