DarkElvenAngel
Posts: 1756
Joined: Tue Mar 20, 2018 9:53 pm

Writing control characters to screen

Thu Jun 10, 2021 7:01 pm

Hello Everyone,

This is an odd question I know but I'm assuming there is a standard way to do this?

For example if I press <Backspace> the cursor will move back one space but it I want to write the character for backspace instead how is this handled? (I'm thinking over a serial link) My current thought is to preceded the control character with <Esc> this works fine but what is the standard way to do this?

This is for my PT52 project on the Pico

User avatar
rpdom
Posts: 18881
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Writing control characters to screen

Thu Jun 10, 2021 7:41 pm

I know that pressing Ctrl-V first works in some situations.

Like typing echo "fish then Ctrl+V then Ctrl-M then chips" and Enter will print "fish", then do a CR and print "chips" over the top.
Unreadable squiggle

DarkElvenAngel
Posts: 1756
Joined: Tue Mar 20, 2018 9:53 pm

Re: Writing control characters to screen

Fri Jun 11, 2021 3:13 am

rpdom wrote:
Thu Jun 10, 2021 7:41 pm
I know that pressing Ctrl-V first works in some situations.

Like typing echo "fish then Ctrl+V then Ctrl-M then chips" and Enter will print "fish", then do a CR and print "chips" over the top.
I gave that a try and it does work but not in the way I was thinking maybe it's a bash thing but I got echo "fish^Mchips" For my use case I was thinking it would have a musical note not ^M. I was think like a card game would want to display suits for the cards as an example.

swampdog
Posts: 731
Joined: Fri Dec 04, 2015 11:22 am

Re: Writing control characters to screen

Fri Jun 11, 2021 5:15 am

I can't help with the pico but in a normal terminal..

Code: Select all

foo@pi18:~ $ echo "foo^Mbar"
bar
foo@pi18:~ $ echo "foo^Mbar" | hexdump -Cv
00000000  66 6f 6f 0d 62 61 72 0a                           |foo.bar.|
00000008
foo@pi18:~ $ echo -n "foo^Mbar" | hexdump -Cv
00000000  66 6f 6f 0d 62 61 72                              |foo.bar|
00000007
..noting that ctrl-V is esc-v when in 'nano'.

Trouble is, it depends on the terminal capabilities. Consider this..

Code: Select all

foo@foo:~$ cat c.c
#include <stdio.h>

static const char       ESC=27;
static const char       CH='A', /*up*/
                        CD='B', /*down*/
                        CR='C', /*right*/
                        CL='D', /*left*/
                        HO='H', /*home*/
                        CE='J', /*clear*/
                        MV='Y'; /*move*/

int
main()
{
 printf("%c%c%c%c%s",ESC,HO,ESC,CE,"abc");
 fflush(stdout);
 printf("%c%c%s",ESC,CD,"def");
 printf("%c%c%c%c%s",ESC,MV,' '+2,' '+10,"ghi");
 fflush(stdout);
 putchar('\n');
 return 0;
..it will not behave until it has the expected terminal (in this case VT52). Thus to see it work..

Code: Select all

$ sudo apt-get install xterm
$ xterm -ti vt52 -tn vt52
$ echo $TERM
vt52
..and run the above within that..

Code: Select all

abc
   def
          ghi
foo@foo:~$
I picked VT52 because it's about the simplest to implement yourself but there is nothing stopping you inventing your own if both ends of the link are talking to your "apps".

DarkElvenAngel
Posts: 1756
Joined: Tue Mar 20, 2018 9:53 pm

Re: Writing control characters to screen

Fri Jun 11, 2021 5:22 am

swampdog wrote:
Fri Jun 11, 2021 5:15 am
I can't help with the pico but in a normal terminal..

Code: Select all

foo@pi18:~ $ echo "foo^Mbar"
bar
foo@pi18:~ $ echo "foo^Mbar" | hexdump -Cv
00000000  66 6f 6f 0d 62 61 72 0a                           |foo.bar.|
00000008
foo@pi18:~ $ echo -n "foo^Mbar" | hexdump -Cv
00000000  66 6f 6f 0d 62 61 72                              |foo.bar|
00000007
..noting that ctrl-V is esc-v when in 'nano'.

Trouble is, it depends on the terminal capabilities. Consider this..

Code: Select all

foo@foo:~$ cat c.c
#include <stdio.h>

static const char       ESC=27;
static const char       CH='A', /*up*/
                        CD='B', /*down*/
                        CR='C', /*right*/
                        CL='D', /*left*/
                        HO='H', /*home*/
                        CE='J', /*clear*/
                        MV='Y'; /*move*/

int
main()
{
 printf("%c%c%c%c%s",ESC,HO,ESC,CE,"abc");
 fflush(stdout);
 printf("%c%c%s",ESC,CD,"def");
 printf("%c%c%c%c%s",ESC,MV,' '+2,' '+10,"ghi");
 fflush(stdout);
 putchar('\n');
 return 0;
..it will not behave until it has the expected terminal (in this case VT52). Thus to see it work..

Code: Select all

$ sudo apt-get install xterm
$ xterm -ti vt52 -tn vt52
$ echo $TERM
vt52
..and run the above within that..

Code: Select all

abc
   def
          ghi
foo@foo:~$
I picked VT52 because it's about the simplest to implement yourself but there is nothing stopping you inventing your own if both ends of the link are talking to your "apps".
I have to try that my Pico project emulates a VT52.

I have set up if you send ESC and then send another control character the glyph will be displayed and it will not do it's function.

Return to “General programming discussion”