4

In order to use my GLCD + Arduino as a display for LCD4Linux, using the LCD4Linux SimpleLCD Driver which sends out a subset of VT100 codes (CR and LF).

I have managed to isolate the VT100 codes from regular text using the following code:

// include the library code:
#include <glcd.h>

// include the Fonts

#include <fonts/allFonts.h>

void setup() {
  // Initialize the GLCD 
  GLCD.Init();

  // Select the font for the default text area
  GLCD.SelectFont(System5x7);
  GLCD.ClearScreen();
  GLCD.println("Please wait:  "); // output using Print class
  GLCD.println("  System initializing"); // output using Print class

  // could also use gText string output routine
  // GLCD.Puts("Listening...\n"); 
  Serial.begin(9600);
}

void loop()
{
  char c;

  // when characters arrive over the serial port...
  if (Serial.available())
  {
    // read character from serial library buffer
    c = Serial.read();

    switch(c)
    {
    case 27:
      if(handleVT100Code())
      {
        GLCD.ClearScreen();
      }
      break;
    default:
      // display character on glcd
      GLCD.write(c); // use Print class for output 
      break;
    }
  }
}

boolean handleVT100Code()
{
  char c;

  String vt100Code;

  while(Serial.available())
  {
    c = Serial.read();

    vt100Code.concat(c);
    if(c == 'H')
    {
      break;
    }
  }

  if(vt100Code != "")
  {
    return(true);
  }

}

So for now it should only clear the screen when a VT100 code is detected (nasty, but to test things out). What happens, however, is that the VT100 codes get printed out anyway, probably because GLCD.ClearScreen() takes up too much cpu time (?). When I don't use ClearScreen, but simple println the vt100Code variable (which I return from handleVT100Code()) the code gets printed (albeit with some false positives).

What would I have to do to make my program understand these two VT100 Codes (Carriage return and Line feed). I have done quite some searches for an out of the box implementation of a simple VT100 terminal, but there seem to be none. If there is some easier way of using the GLCD with LCD4Linux, you are more than welcome to tell me.

embedded.kyle
  • 8,411
  • 2
  • 26
  • 44
peterrus
  • 189
  • 3
  • 11
  • i am struggling for 2 months now on reading from my VT100old device and display on VGA... - there is alot of VGA library like great FabGL and the other of bitluni .. though i can not see a READ from the serial to pass to the VGA lib., - did you implement your own VT100 escape code all done? .. i can use your example and replace GLCD by VGA library... can u give me some help or share ur final full code? - i have big hope on fabgl though i bought the pcb but still in shipping – hellojoseph1 Oct 01 '20 at 12:55
  • You are welcome to EE.SE - but you can't ask questions in the "Your Answers" box. Answers have to be answers. Please take the [Tour](https://electronics.stackexchange.com/tour) to learn how the site works and then you can ask your own question. – Transistor Oct 01 '20 at 13:02
  • I've converted you answer to a comment. If desired you can ask this as a separate answer and include a link to this question. – Russell McMahon Oct 01 '20 at 13:40
  • @hellojoseph1 Unfortunately peterrus has not logged onto this site for almost a year. So he MAY see this question and may not. || For a forum that supports discussion I suggest that you look at http://www.piclist.com (don't be put off by the presentation or the PIC in the name). Then visit http://mailman.mit.edu/mailman/listinfo/piclist and join the mailing list and say hello. Provide as much detail of your requirements and problems as possible. Just like here, there are people there who know almost everything (collectively), and are allowed to discuss anything conversationally. – Russell McMahon Oct 01 '20 at 13:45

2 Answers2

6

You seem to have some confusion on a couple of different levels here.

First of all, CR and LF are not "VT100 codes" as such, they're just ordinary ASCII control characters (hex 0D/decimal 13 and hex 0A/decimal 10, respectively). Actual VT100 codes are multi-byte sequences beginning with an escape (ESC) character (hex 1B/decimal 27), which is what your code is actually detecting. So, at that level, you should be handling CR and LF in the same switch() where you're detecting the ESC.

Now, as far as dealing with actual VT100 code sequences (to position the cursor, change colors, etc.) you need to know how long each sequence is, based on the characters you've received so far. A full implementation can be quite involved.

But the problem with your code is that once you recognize the ESC character, you call handleVT100Code(). This function has a while() loop, but the condition will only be true if the next character of the sequence has already been received. Given the relative speeds of the CPU and the serial line, it won't be. If not, the loop and the function exit immediately, and you end up processing those bytes in your main loop. You'll actually have to wait in handleVT100Code() for the bytes that follow the ESC.

Dave Tweed
  • 168,369
  • 17
  • 228
  • 393
  • Thanks for your response, with CR and LF I was indeed referring to for example: "[{ROW};{COLUMN}H". as for the fact that the program runs faster than the serial line can supply characters, I could add a simple delay(1) for example, but that would be a wild guess. Are there any better ways to do this? – peterrus Sep 28 '12 at 14:06
  • 1
    instead of relying on correct timing I could also use ```while(Serial.available() < 1) {};``` , this does not seem to make a difference though. – peterrus Sep 28 '12 at 14:46
  • 1
    @peterrus: Try to avoid delay() if at all possible. The processor can't do anything else while that's running. Make your program event driven rather than by time. – Transistor Oct 01 '20 at 13:00
1
boolean handleVT100Code()
{
  char c;

  String vt100Code;

  while(true)
  {
    //wait until next byte is received
    while(Serial.peek() == -1) {};

    c = Serial.read();

    vt100Code.concat(c);
    if(c == 'H')
    {
      break;
    }
  }

  if(vt100Code != "")
  {
    return(true);
  }

}

Thanks Dave for giving me the insight ;) Now I will need to implement true handling of the VT100 codes, but that should only be a matter of parsing strings.

peterrus
  • 189
  • 3
  • 11