Fit long string onto 20 x 4 line LCD display

For general Flowcode discussion that does not belong in the other sections.
Amtor
Posts: 50
http://meble-kuchenne.info.pl
Joined: Thu Dec 17, 2020 4:35 pm
Has thanked: 18 times
Been thanked: 15 times

Fit long string onto 20 x 4 line LCD display

Post 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

medelec35
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

Post by medelec35 »

Hi.
Attached is the way I would do what you require.
Attachments
Print Long String$.fcfx
(11.36 KiB) Downloaded 478 times
Martin

mnfisher
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

Post 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

mnfisher
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

Post by mnfisher »

While you were typing....

I think Martin's code does slightly differently to my suggestion though :-)

Amtor
Posts: 50
Joined: Thu Dec 17, 2020 4:35 pm
Has thanked: 18 times
Been thanked: 15 times

Re: Fit long string onto 20 x 4 line LCD display

Post 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

medelec35
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

Post by medelec35 »

I'm taking another look into this so the space detection can be implemented as well.
It will make it more complex.
Martin

mnfisher
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

Post 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 ?
split.fcfx
(12.88 KiB) Downloaded 449 times
Martin

mnfisher
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

Post 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...
split2.fcfx
(13.66 KiB) Downloaded 364 times
Martin

Amtor
Posts: 50
Joined: Thu Dec 17, 2020 4:35 pm
Has thanked: 18 times
Been thanked: 15 times

Re: Fit long string onto 20 x 4 line LCD display

Post by Amtor »

lcd-2.JPG
lcd-2.JPG (18.39 KiB) Viewed 5416 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

mnfisher
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

Post 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
split3.fcfx
(19.29 KiB) Downloaded 333 times
Martin

Post Reply