Page 1 of 2
Fit long string onto 20 x 4 line LCD display
Posted: Sun Dec 12, 2021 2:36 pm
by Amtor
Hello
Is there an easy way to fit a long string onto a 20 x 4 lcd without the words getting split at the end, ie go down to the next line
when there is a space towards the end of the line your on.
I'm getting into a right state.
Thanks
Mark
Re: Fit long string onto 20 x 4 line LCD display
Posted: Sun Dec 12, 2021 3:28 pm
by medelec35
Hi.
Attached is the way I would do what you require.
Re: Fit long string onto 20 x 4 line LCD display
Posted: Sun Dec 12, 2021 3:38 pm
by mnfisher
Hi Mark,
I don't know if there is an easy way - there are quite a few possibilities (for example a 'word' with 20 characters) - do you want to 'squash' words together of there are multiple spaces etc Should 'long' words be hyphenated to avoid large 'gaps'
How is the string formatted - do you just have a block of 'words' separated by spaces or are there newlines / tabs or other
'formatting' information?
However: Thinking aloud - you need an algorithm along the lines of:
split (takes a 80char (or longer) string (inp) and returns an array of 4 x 20 char string (out))
Look at inp[20] - if it's a space (' ') then out[0] = inp[0..19]
else if it's a character then need to find start of the word..
using a loop - look back to find a space say at inp[x]
then out[0] = inp[0..x -1]
then repeat for out[1] with inp[x + 20]
Might result in 'losing' some text (output in 'next' pass)
It shouldn't be too hard to convert to FC...
Martin
Re: Fit long string onto 20 x 4 line LCD display
Posted: Sun Dec 12, 2021 3:41 pm
by mnfisher
While you were typing....
I think Martin's code does slightly differently to my suggestion though

Re: Fit long string onto 20 x 4 line LCD display
Posted: Sun Dec 12, 2021 3:46 pm
by Amtor
Thanks very much, I've been messing about for ages, it was looking ridiculous on the screen,
I'm glad someone knows what they are doing. Thanks to both of you.
All the best
Mark
Re: Fit long string onto 20 x 4 line LCD display
Posted: Sun Dec 12, 2021 4:25 pm
by medelec35
I'm taking another look into this so the space detection can be implemented as well.
It will make it more complex.
Re: Fit long string onto 20 x 4 line LCD display
Posted: Sun Dec 12, 2021 5:39 pm
by mnfisher
My attempt at implementing this...
There's no error correction and it will fail if there is a word > 20chars long...
I think it will probably go wrong if input is less than 4 lines long ? (I didn't test)
It seems to work in simulation - I'm not sure it really needs to write the data to an array (notice the extra byte (4 x 21) - to allow for a 0 termination character) It could just write to the display and pad the line to 20 chars ?
Martin
Re: Fit long string onto 20 x 4 line LCD display
Posted: Sun Dec 12, 2021 6:04 pm
by mnfisher
It bothered me slightly that you couldn't just overprint the display (without getting the annoying abcXXX where XXX is what was there before) Clearing the display first is visually jarring.
So I made it pad the strings to 20chars with spaces...
Martin
Re: Fit long string onto 20 x 4 line LCD display
Posted: Mon Dec 13, 2021 12:16 pm
by Amtor

- lcd-2.JPG (18.39 KiB) Viewed 5419 times
That's fantastic thanks, no cut in half words at all,
It's going to take me a fortnight to figure out how it's working. Sometimes if only 2 lines need printing the
previous 2 lines are still showing at the bottom but I'm sure I can figure out how to solve that at least.
I'll study the termination character.
Thanks very much to both of you for the help
Mark
Re: Fit long string onto 20 x 4 line LCD display
Posted: Mon Dec 13, 2021 10:34 pm
by mnfisher
This version handles the 'trailing' lines issue.
I've added several more 'test' cases.. The only thing it can't handle is words > 20 characters in length. I notice one case gives an ArrayIndex out of bounds error - it would be good to fix this especially if using it in 'production' code...
Note I've added a comment about passing 'start' to split to allow long strings to be split in place (rather than having to remove the displayed data)
It also uncovered a small bug in the LCD component I used (Adafruit 20x 4 OLED) - ClearLine should move back to the start of the line to allow data to be displayed:
ClearLine(3)
PrintNumber(.i)
Just clears the line (at least in simulation).
ClearLine(3)
Cursor(0, 3)
PrintNumber(.i)
correctly displays .i
Martin