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
Fit long string onto 20 x 4 line LCD display
-
- Posts: 50
- http://meble-kuchenne.info.pl
- Joined: Thu Dec 17, 2020 4:35 pm
- Has thanked: 18 times
- Been thanked: 15 times
-
- Matrix Staff
- Posts: 1917
- Joined: Wed Dec 02, 2020 11:07 pm
- Has thanked: 621 times
- Been thanked: 645 times
Re: Fit long string onto 20 x 4 line LCD display
Hi.
Attached is the way I would do what you require.
Attached is the way I would do what you require.
- Attachments
-
- Print Long String$.fcfx
- (11.36 KiB) Downloaded 478 times
Martin
-
- Valued Contributor
- Posts: 1460
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 135 times
- Been thanked: 712 times
Re: Fit long string onto 20 x 4 line LCD display
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
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
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
I'm glad someone knows what they are doing. Thanks to both of you.
All the best
Mark
-
- Valued Contributor
- Posts: 1460
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 135 times
- Been thanked: 712 times
Re: Fit long string onto 20 x 4 line LCD display
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
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
-
- Valued Contributor
- Posts: 1460
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 135 times
- Been thanked: 712 times
Re: Fit long string onto 20 x 4 line LCD display
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
So I made it pad the strings to 20chars with spaces...
Martin
Re: Fit long string onto 20 x 4 line LCD display
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
-
- Valued Contributor
- Posts: 1460
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 135 times
- Been thanked: 712 times
Re: Fit long string onto 20 x 4 line LCD display
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
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