Working with long strings

I have a Tiny Thermal Receipt Printer from Adafruit connected to an Arduino UNO. It can print 32 characters wide. Currently I use a python program using textwrap to insert "\n" to create lines of text under 32 characters while not breaking up words.

import textwrap

phrase = input("What text do you want to print?\n: ")
wrapped = textwrap.fill(phrase, width=32, fix_sentence_endings=1)
print()
print(repr(wrapped))

Is there an Arduino library, or a way to do this on the Arduino so I can print text from the serial port instead of coding what I want to print each time?

don't think it requires a library. Just grab at most 32 chars from a string buffer and print that, rinse and repeat...

where does the data come from?

ust grab at most 32 chars from a string buffer and print that, rinse and repeat...

The key requirement seems to be

while not breaking up words.

where does the data come from?

Currently I use the python program, put it in a print statement, and upload to arduino.

printer.println(F("SOMETHING I WANT TO PRINT"))

I would like to be able to send text via serial monitor, but I don't know how to automatically split the text and keep words. The python program preserves words...

UKHeliBob:
The key requirement seems to be

I said "at most" --> some engineering required :slight_smile:

something like this may be

char veryLongString[] = "0123456789ABCDEFGHIJ    My very photogenic mother died in a freak accident (picnic, lightning) when I was three, and, save for a pocket of warmth in the darkest past, nothing of her subsists within the hollows and dells of memory, over which, if you can still stand my style (I am writing under observation), the sun of my infancy had set: surely, you all know those redolent remnants of day suspended, with the midges, about some hedge in bloom or suddenly entered and traversed by the rambler, at the bottom of a hill, in the summer dusk; a furry warmth, golden midges.";

void printCol(const char* s, const size_t nbCol = 32)
{
  char tmpBuffer[nbCol + 1];
  tmpBuffer[nbCol] = '\0'; // ensure null terminated C string

  size_t len = strlen(s);
  size_t pos = 0;

  while (pos < len) {
    strncpy(tmpBuffer, &(s[pos]), nbCol);
    char * lastSpace = strrchr(tmpBuffer, ' ');
    if (strlen(tmpBuffer) >= nbCol) {
      if (lastSpace) {
        *lastSpace = '\0';
        pos++; // +1 to start after the space
      }
    }
    pos += strlen(tmpBuffer);
    char * skipStartingSpace = tmpBuffer;
    while (*skipStartingSpace == ' ') skipStartingSpace++;
    Serial.println(skipStartingSpace);
  }
}

void setup() {
  Serial.begin(115200);

  Serial.println(F("\n\n----- 80 columns -----"));
  printCol(veryLongString, 80);

  Serial.println(F("\n\n----- 32 columns -----"));
  printCol(veryLongString); // 32 is default so no need to pass the parameter

  Serial.println(F("\n\n----- 10 columns -----"));
  printCol(veryLongString, 10);

  Serial.println(F("\n\n----- 1 column -----"));
  printCol(veryLongString, 1);
}

void loop() {}

Serial Monitor (@ 115200 bauds) will show

[color=purple]


----- 80 columns -----
0123456789ABCDEFGHIJ    My very photogenic mother died in a freak accident
(picnic, lightning) when I was three, and, save for a pocket of warmth in the
darkest past, nothing of her subsists within the hollows and dells of memory,
over which, if you can still stand my style (I am writing under observation),
the sun of my infancy had set: surely, you all know those redolent remnants of
day suspended, with the midges, about some hedge in bloom or suddenly entered
and traversed by the rambler, at the bottom of a hill, in the summer dusk; a
furry warmth, golden midges.


----- 32 columns -----
0123456789ABCDEFGHIJ    My very
photogenic mother died in a
freak accident (picnic,
lightning) when I was three,
and, save for a pocket of
warmth in the darkest past,
nothing of her subsists within
the hollows and dells of
memory, over which, if you can
still stand my style (I am
writing under observation), the
sun of my infancy had set:
surely, you all know those
redolent remnants of day
suspended, with the midges,
about some hedge in bloom or
suddenly entered and traversed
by the rambler, at the bottom
of a hill, in the summer dusk;
a furry warmth, golden midges.


----- 10 columns -----
0123456789
ABCDEFGHIJ
My
very
photogenic
mother
died in a
freak
accident
(picnic,
lightning)
when I
was
three,
and, save
for a
pocket of
warmth in
the
darkest
past,
nothing
of her
subsists
within
the
hollows
and dells
of
memory,
over
which, if
you can
still
stand my
style (I
am
writing
under
observatio
n), the
sun of my
infancy
had set:
surely,
you all
know
those
redolent
remnants
of day
suspended,
with the
midges,
about
some
hedge in
bloom or
suddenly
entered
and
traversed
by the
rambler,
at the
bottom of
a hill,
in the
summer
dusk; a
furry
warmth,
golden
midges.


----- 1 column -----
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
G
H
I
J




M
y

v
e
r
y

p
h
o
t
o
g
e
n
i
c

m
o
t
h
e
r

d
i
e
d

i
n

a

f
r
e
a
k

a
c
c
i
d
e
n
t

(
p
i
c
n
i
c
,

l
i
g
h
t
n
i
n
g
)

w
h
e
n

I

w
a
s

t
h
r
e
e
,

a
n
d
,

s
a
v
e

f
o
r

a

p
o
c
k
e
t

o
f

w
a
r
m
t
h

i
n

t
h
e

d
a
r
k
e
s
t

p
a
s
t
,

n
o
t
h
i
n
g

o
f

h
e
r

s
u
b
s
i
s
t
s

w
i
t
h
i
n

t
h
e

h
o
l
l
o
w
s

a
n
d

d
e
l
l
s

o
f

m
e
m
o
r
y
,

o
v
e
r

w
h
i
c
h
,

i
f

y
o
u

c
a
n

s
t
i
l
l

s
t
a
n
d

m
y

s
t
y
l
e

(
I

a
m

w
r
i
t
i
n
g

u
n
d
e
r

o
b
s
e
r
v
a
t
i
o
n
)
,

t
h
e

s
u
n

o
f

m
y

i
n
f
a
n
c
y

h
a
d

s
e
t
:

s
u
r
e
l
y
,

y
o
u

a
l
l

k
n
o
w

t
h
o
s
e

r
e
d
o
l
e
n
t

r
e
m
n
a
n
t
s

o
f

d
a
y

s
u
s
p
e
n
d
e
d
,

w
i
t
h

t
h
e

m
i
d
g
e
s
,

a
b
o
u
t

s
o
m
e

h
e
d
g
e

i
n

b
l
o
o
m

o
r

s
u
d
d
e
n
l
y

e
n
t
e
r
e
d

a
n
d

t
r
a
v
e
r
s
e
d

b
y

t
h
e

r
a
m
b
l
e
r
,

a
t

t
h
e

b
o
t
t
o
m

o
f

a

h
i
l
l
,

i
n

t
h
e

s
u
m
m
e
r

d
u
s
k
;

a

f
u
r
r
y

w
a
r
m
t
h
,

g
o
l
d
e
n

m
i
d
g
e
s
.

[/color]

PS: might have a small bug, if the last word of a line ends up perfectly at the 32 characters boundary it will likely be bumped to the next line. fixing this would require to read one extra byte and double check if it's a space.

left to the reader :slight_smile:

Arodd2000:
Currently I use the python program, put it in a print statement, and upload to arduino.

printer.println(F("SOMETHING I WANT TO PRINT"))

I would like to be able to send text via serial monitor, but I don't know how to automatically split the text and keep words. The python program preserves words...

Arduino people WAKE UP! This is why Python is eating your market. BETTER libraries! Python is all about "What you want to solve". Not "How we can solve it with pre-historic tools". The sad thing is, we can easily do better than this. But at every turn, libraries that might push us past first gear, are met with absurd intolerance.

-jim lee

Curious to see a full Python with all its libraries on my UNO !

Arduino doesn't end with an UNO. ::slight_smile:

-jim lee

True. Take the right tool for the job.. a supercomputer can run Python for sure, you just didn’t get it for a few bucks nor power it with a few hundreds mA

But you miss my point.. (I think) With some careful work on libraries, even an UNO can be MUCH easier to use.

-jim lee

Do I read you are volunteering to port the Python textwrap capability on arduino ? :wink:

My view is that you need to draw the line somewhere - it’s Somewhat low (the String class) in Arduino’s libraries but at the end of the day small arduino don’t have a big screen nor keyboard and it’s not the primary target use.

You could argue that the pretty print for the adafruit printer could have been added in the printer’s library

Well I actually already have written a text wrap library for the Arduino. But if I can't sell 'em on simple things like blinker & timeObj. I fear a text wrap library would be a little too "Alien tech".

My view is that a small library with the foundation of solid code can run on the Uno no problem. This would lower the learning curve for the beginners. Then, as people get more ambitious, they won't have to un-learn stuff when they got to bigger processors.

-jim lee

My view is that a small library with the foundation of solid code can run on the Uno no problem.

I guess the crux lies in the definition of small and what makes it to the library beyond what exists in Arduino.h

Uggh! You are totally right. It would just start yet another endless fight.

sigh..

-jim lee

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.