Terminal code for Home Cursor

hi i'm trying to get my Mega2560 to send a VT100 code for Home Cursor. I'm using a Putty terminal.

int x;
void setup() {
  Serial.begin(9600);
}
void loop() {
Serial.write(13);Serial.write(10);
delay(500);
Serial.print("word");
if(!(x++ %3)) { Serial.write(27); Serial.write("H"); Serial.print("x"); }
}

this is what i get:
...
word
wordx
word
word
wordx
word
word
wordx
...

and yeah i did search for this topic and honest-to-God it's nowhere to be found.

Now here is nowhere:
https://www.vt100.net/

Do you really need to home cursor, or just return to the beginning of the line? '\r' will do that without advancing to the next line. But it won't erase any characters you've written.

yes that's why i included the Serial.write(10) in my code. The output shows that putty understands 10 and 13. What i'm really asking is, why doesn't the standard code Serial.write(27); Serial.write("H"); work? Why doesn't the cursor go home?

Decimal 10 is line feed, not carriage return. CR is decimal 13. The escape character method was partly designed to hide that to make it more foolproof.

The manual of VT100 commands that I found has the following

Cursor Functions:

 *  ESC [ pn A        cursor up pn times - stop at top
 *  ESC [ pn B        cursor down pn times - stop at bottom
 *  ESC [ pn C        cursor right pn times - stop at far right
 *  ESC [ pn D        cursor left pn times - stop at far left
 *  ESC [ pl ; pc H   set cursor position - pl Line, pc Column
 *  ESC [ H           set cursor home
 *  ESC [ pl ; pc f   set cursor position - pl Line, pc Column
 *  ESC [ f           set cursor home
 *  ESC D             cursor down - at bottom of region, scroll up
 *  ESC M             cursor up - at top of region, scroll down
 *  ESC E             next line (same as CR LF)
 *  ESC 7             save cursor position(char attr,char set,org)
 *  ESC 8             restore position (char attr,char set,origin)

Exactly which cursor function are you trying to use by sending ESC H

did you possibly mean,

Serial.write('H');

?

:flushed: Damn that was it it's supposed to be 27 "[H"
that's why the question hasn't come up. I'm the 1st one who's dumb enough to ask.
much obliged amigo

I would use "\x1B[H" if it is correct. For some reason my VT100 code doesn't include the home command.

Never mind, you got there

well now so do i :+1:

Some more for you:

// control functions
const char VT_RESET[] = "\x1Bc";

// erasure
const char VT_CLS[] = "\x1B[2J";
const char VT_CL_EOL[] = "\x1B[0K";
const char VT_TESTFILL[] = "\x1B#8";

// character attributes
const char VT_NORMAL[] = "\x1B[m";
const char VT_REVERSE[] = "\x1B[7m";

// colours
const char VT_DEFAULT_COLOUR[] = "\x1B[39m";

const char VT_RED[] = "\x1B[31m";
const char VT_GREEN[] = "\x1B[32m";
const char VT_YELLOW[] = "\x1B[33m";
const char VT_BLUE[] = "\x1B[34m";
const char VT_MAGENTA[] = "\x1B[35m";
const char VT_CYAN[] = "\x1B[36m";
const char VT_GREY[] = "\x1B[37m";

const char VT_BACK_DEFAULT_COLOUR[] = "\x1B[49m";

const char VT_BACK_RED[] = "\x1B[41m";
const char VT_BACK_GREEN[] = "\x1B[42m";
const char VT_BACK_YELLOW[] = "\x1B[43m";
const char VT_BACK_BLUE[] = "\x1B[44m";
const char VT_BACK_MAGENTA[] = "\x1B[45m";
const char VT_BACK_CYAN[] = "\x1B[46m";
const char VT_BACK_GREY[] = "\x1B[47m";

// save and restore cursor
const char VT_SAVE_CUR[] = "\x1B7";
const char VT_RESTORE_CUR[] = "\x1B8";

// cursor hide
const char VT_INVISIBLE_CUR[] = "\x1B[?25l";
const char VT_VISIBLE_CUR[] = "\x1B[?25h";

// change character sets
const char VT_CHARSET_INIT[] = "\x1B(B\x1B)0";

// change character set to DEC Special Graphics and back
const char VT_CHARSET_DSG[] = "\x1B(0";
const char VT_CHARSET_DEFAULT[] = "\x1B(B";

I guess I need to update that. Darn, as if I don't have other things to finish...

Example use:

Serial.print(VT_CLS);

YEAH i can definitely use that! i'll make it into an extra h file and include it. THANX
i'll update and repost it.

I have a 99% complete VT100 terminal server library in dev. But it went on the back burner. Maybe it's time to release it. IIRC I thought it should include something like CURSES, that slowed me down and froze the project.

well there are guys who've made their own terminal monitors, don't know if any of em do curses though. That's a big job

As big as full TEK4010 vector graphic terminal emulation support? :slight_smile: Okay, actually I admit, it wasn't that hard. There was a lot of undocumented stuff to deal with, though...

yeah hard is a relative term. I sure couldn't do it

I think full curses wouldn't fit in the AVR anyway. So it would be a subset, like a few other libraries... that makes it a little easier too. With the TEK4010 thing, all the heavy lifting is done by the Tera Term application. The control codes are not that complex, similar to VT commands. What took ages to make work, the cursor position request (i.e. mouse). The timing and sequence was weird. The original purpose of it was just to make snazzy menus in Tera Term and the project got bigger than that. The ASCII terminal part of it is both nostalgic, because I've programmed on a VT100, and useful because you can make great control menus in it. Also serial can be accessed remotely if needed.

you mean timing and sequence of the request itself?

Yes, I looked back just now, it has been several years since I wrote it. It's a rather complicated state machine. I'm reluctant to release it until it's been properly updated and polished.

it sure is and i wish i could help with it. i have written something though which i think might work for cursor position.

int setPos(int x, int y) {
  int VT_CUR_POS_X_Y[14] = "\x1B["; // y;xH";
  int q = 4;
  if(y/100) {
    VT_CUR_POS_X_Y[q++] = y/100+48;   // ascii("0") = 48
    VT_CUR_POS_X_Y[q++] = (y%100)/10+48;
    VT_CUR_POS_X_Y[q++] = y%10+48;
  }
  else if(y/10) {
    VT_CUR_POS_X_Y[q++] = y/10+48;
    VT_CUR_POS_X_Y[q++] = y%10+48;
  }
  else VT_CUR_POS_X_Y[q++] = y%10+48;
  VT_CUR_POS_X_Y[q++] = ";"

  if(x/100) {
    VT_CUR_POS_X_Y[q++] = x/100+48;
    VT_CUR_POS_X_Y[q++] = (x%100)/10+48;
    VT_CUR_POS_X_Y[q++] = x%10+48;
  }
  else if(x/10) {
    VT_CUR_POS_X_Y[q++] = x/10+48; 
    VT_CUR_POS_X_Y[q++] = x%10+48;
  }
  else VT_CUR_POS_X_Y[q++] = x%10+48;

  VT_CUR_POS_X_Y[q++] = "H"	
  return VT_CUR_POS_X_Y[14]
}