Shop OBEX P1 Docs P2 Docs Learn Events
Quick Byte: Colorful Terminal Output with ANSI Escape Sequences — Parallax Forums

Quick Byte: Colorful Terminal Output with ANSI Escape Sequences

We're starting to venture into the capabilities of FlexProp, with this example.

https://www.parallax.com/colorful-terminal-output-with-ansi-escape-sequences

Ken Gracey

Comments

  • Cluso99Cluso99 Posts: 18,069

    Fantastic!
    Another great object by Jon B)

  • JonnyMacJonnyMac Posts: 9,162
    edited 2021-02-06 20:45

    Thanks, Ray. It's an incredibly simple object, and I hope it inspires others to craft simple objects, too. An object doesn't have to be complicated to be useful.

  • Cluso99Cluso99 Posts: 18,069

    Ken,
    I noticed a couple of things when trying this out.

    1. The Quick Bytes page for downloading doesn't have a backward pointer to go back to the Quick Bytes page (same topic). I know it opened a new tab but I missed that initially. Even though the other tab is still open, a link to take this one back would be helpful - yes, then you would have two tabs open with the same page.
    2. To get the demo working I needed to change the baud to 115_200 from 230_400. I mention this because I'm not sure what Jon is using but this became a P1 standard although a lot of us are using 2_000_000 on P2.
    con { terminal }
    
    '  BR_TERM  = 230_400                                            ' terminal baud rate
      BR_TERM  = 115_200                                            ' terminal baud rate
    
    1. To get the demo working with TeraTerm I had to set it up and then click File|Disconnect to let go of the port, then on pnut (guess the same for PropTool) run and download, then back to TeraTerm and click File|NewConection. This takes some time to do, so item 4 is required. Maybe there's a better way - I rarely use TeraTerm.
    2. Add "waitms(10_000)" for a 10 second delay after Jon's program starts to get TeraTerm running as shown
    pub main() | k
    
      waitms(10_000)        ' wait 10s to get TeraTerm running
    
      setup()
    
  • JonnyMacJonnyMac Posts: 9,162
    edited 2021-02-06 21:34

    Ray,

    I don't know why you had to change the baud for Tera Term -- it runs at 230400 for me. I also tested with PuTTY (for Windows) and the default terminal used by FlexProp.

    With ANSI output I download to Flash using F11. If opening the terminal program doesn't reset the board, then I do a manual reset. I made a note of this in the demo code (top of listing).

    I'm not sure what Jon is using

    I settled on 230_400 for the P2.

    Long term, I am hoping that @"Jeff Martin" will update PST so that is accepts PST commands, or ANSI commands. Of course, I would like it to be cross-platform, too.

    1028 x 673 - 72K
  • Cluso99Cluso99 Posts: 18,069
    edited 2021-02-06 21:59

    Jon,

    Congratulations! Another fine example Jon.

    I just use 115_200 as a default in everything I do for P1 and a lot of P2, and lately I've been switching to 2_000_000 for P2.
    So I just thought 115_200 might be more common. As I said, I'm wasn't sure what your object's normally use, tho I now see you use 230_400 so that's fine.

    OK, I see you're using Flash. I don't - never used it yet. It's much quicker to just download to ram which is one of the great benefits of P1, and hence P2. So now I understand the reset comment.

    Yes, it would be great to see PST support a subset of ANSI.
    Clear, home, goto(x,y) and fgnd(r,g,b) and bgnd(r,g,b) would be my first preference.
    I am sure if Jeff released that section of the code someone would step up and add it in for him.
    I'll probably add this subset to my LCD driver I released yesterday.

    May I suggest you also add goto(x,y) which just calls crsr(x,y) ?
    I'm curious that crsr_xy(x,y) does x+1 and y+1 whereas crsr_x(x) and crsr_y(y) does not - I haven't coded vt52/vt100 for 40 years.
    BTW while looking at your jm_ansi.spin2 I noticed that you missed coding "pub bgnd_rgb(r, g, b)"

    Ray

  • BTW while looking at your jm_ansi.spin2 I noticed that you missed coding "pub bgnd_rgb(r, g, b)"

    Whoops, meant to delete that -- I found it didn't always work and I don't want to be nagged for something that I have no control over.

  • Cluso99Cluso99 Posts: 18,069
    edited 2021-02-06 22:33

    @JonnyMac said:

    BTW while looking at your jm_ansi.spin2 I noticed that you missed coding "pub bgnd_rgb(r, g, b)"

    Whoops, meant to delete that -- I found it didn't always work and I don't want to be nagged for something that I have no control over.

    Oh! Good to know.
    So I was just adding home, cls, gotoxy and fgnd, bgnd to my LCD driver. So what do you recommend to set forgeround and background colors?

    I have to do scroll and that's a PITA to do manually. It seems the scroll in the LCD chipset is broken - doesn't seem to work properly in landscape mode but fine in portrait. So I may have to keep my own text buffer :( I've done it but it's a PITA to need to redraw the whole screen when the chipset is supposed to do it.

  • So what do you recommend to set forgeround and background colors?

    The named and index colors work in the ANSI object with the three terminals I tested. I will re-test the RGB mechanism; maybe I made a mistake.

  • This is SPINning out of control.

    For you C lovers out there, I matey, here is my spin on the subject.

    ANSI.h

    enum {
        BLACK,
        RED,
        GREEN,
        YELLOW,
        BLUE,
        MAGENTA,
        CYAN,
        WHITE,
        BR_BLACK,
        BR_RED,
        BR_GREEN,
        BR_YELLOW,
        BR_BLUE,
        BR_MEGENTA,
        BR_CYAN,
        BR_WHITE
    } COLORS;
    
    #define HOME "\x1b[H"
    #define POSITION "\x1b[%d;%dH"
    #define CURSOROFF "\x1b[?25l"
    #define CURSORON  "\x1b[?25h"
    #define CURSORUP "\x1b[%dA"
    #define CURSORDOWN "\x1b[%dB"
    #define CURSORRIGHT "\x1b[%dC"
    #define CURSORLEFT "\1b[%dD"
    #define CLEAREND "\x1b[K"
    #define CLEARTO "\x1b[1K"
    #define CLEARLINE "\x1b[2K"
    #define CLEARDOWN "\x1b[0J"
    #define CLEARSCREEN "\x1b[2J"
    #define NORMAL "\x1b[0m"
    #define BOLD "\x1b[1m"
    #define FAINT "\x1b[2m"
    #define UNDERLINE "\x1b[4m"
    #define BLINK "\x1b[5m"
    #define REVERSE "\x1b[7m"
    #define INVISABLE "\0x1b[8m"
    #define FOREGROUND "\x1b[3%1dm"
    #define BACKGROUND "\x1b[4%1dm"
    #define FOREGROUNDB "\x1b[9%1dm"
    #define BACKGROUNDB "\x1b[10%1dm"
    #define FOREGROUNDNORMAL "\x1b[39m"
    #define BACKGROUNDNORMAL "\x1b[49m"
    

    To use this include file:

        printf(HOME);
        printf(CLEARSCREEN);
        printf(POSITION,10,10);
    
        printf(BACKGROUND,BLUE);
        printf("Testing one, two, three");
        printf(BACKGROUNDNORMAL);
        printf(POSITION,20,10);
        printf(FOREGROUND,RED);
        printf("Another Test");
    
    

    Actually, no code was written to do any of this...

    Mike

  • JonnyMacJonnyMac Posts: 9,162
    edited 2021-02-07 19:53

    Quick Bytes is about helping new programmers get started. If you're going to chime in, why not translate the Spin demo so that new C programmers can compare and learn from the two languages?

    FWIW, "ANSI.h" seems already to be defined in the C world, so maybe that's not a good name for the C header you suggest. I'm not much of a C programmer, but with a big cup of coffee and a couple of references on my desk, I managed to create a work-alike demo of my Spin program that is topic of this thread. I tested the code with FlexProp on Windows.

  • dgatelydgately Posts: 1,631
    edited 2021-02-07 21:23

    I hate to bare bad news, but... The ansi sample code is "just a little" off (so far, on macOS' Terminal) as it is not returning to column 0 on each line. Definitely using -T in flexprop's command options.


    Used minicom (within the Terminal app) as well, and got the same results.

  • You might try changing LF to CR for the Mac. The code, as presented, works in PuTTY, Tera Term, and the PropTool terminal under Windows. In PuTTY and Tera Term I have it add CR to LF and vice-versa.

    1061 x 708 - 92K
    1009 x 654 - 91K
    1579 x 872 - 114K
  • Cluso99Cluso99 Posts: 18,069

    Yes, it's a function of interpretation of and . There if often a setting for these.

    can be interpreted as just return to the beginning of the line, or return to beginning of line and advance the line.
    can be interpreted as advance the line, while returning to the beginning of the line is again dependant/optional.
    Then when you use you may get a double advance line leaving a blank line between.
    And to even add to this, sometimes if you perform a the does a return and new line, and the software will skip the following . As an adjunct to this, if when writing characters to the screen, you overflow off toe right edge, then the next character will be in the leftmost position of the next line. Some terminals record this fact, and if a , or follows it will be ignored. Makes for some interesting coding. ANd we haven't resolved any of this in more than 40 years!!!

Sign In or Register to comment.