ANSI Cursor Control from Forth

ANSI graphics in the kitty terminal emulator

When lacking a bit-mapped graphics interface, one old standby is ANSI cursor control, which allows 8 colors, with cursor positioning assuming a monospace font. It could allow you to do limited graphics and forms over a UART connection, e.g., the serial connection to a microcontroller.

An entertaining rabbit trail: Here is Emacs running the pong game in an X11 window (i.e., normal graphics):

Emacs Pong using normal X11 graphics

But if you run Emacs in no-window mode (i.e., “terminal” mode) it looks like it falls back to control codes (ANSI escape codes…?)

Emacs Pong using terminal graphics (control codes, I presume)

Emacs was designed originally to be run in terminal environments which used control codes — bit-mapped graphics were later grafted on to Emacs. This allows Emacs to be run from a Gnu/Linux virtual console (e.g., CTRL-ALT-F2).

Anyway, here is a Forth interface to some of the ANSI escape codes, specifically line,column cursor positioning, and setting a graphics mode (character attributes like color and bold/underline).

\ Copyright 2020 Christopher Howard

\ ansi-cursor.fs

\ Licensed under the Apache License, Version 2.0 (the "License");
\ you may not use this file except in compliance with the License.
\ You may obtain a copy of the License at

\     http://www.apache.org/licenses/LICENSE-2.0

\ Unless required by applicable law or agreed to in writing, software
\ distributed under the License is distributed on an "AS IS" BASIS,
\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\ See the License for the specific language governing permissions and
\ limitations under the License.

\ http://ascii-table.com/ansi-escape-sequences.php

\ GFX mode values

30 constant black-fg
31 constant red-fg
32 constant green-fg
33 constant yellow-fg
34 constant blue-fg
35 constant magenta-fg
36 constant cyan-fg
37 constant white-fg

40 constant black-bg
41 constant red-bg
42 constant green-bg
43 constant yellow-bg
44 constant blue-bg
45 constant magenta-bg
46 constant cyan-bg
47 constant white-bg

0 constant attributes-off
1 constant bold
4 constant underscore
5 constant blink
7 constant reverse
8 constant concealed

: <esc> 0x1b emit ;

: <[> [char] [ emit ;

: .| abs 0 <# #s #> type ;

: <;> [char] ; emit ;

: position-cursor ( line col -- ) <esc> <[> swap .| <;> .| [char] H emit ;

: erase-display <esc> <[> 2 .| [char] J emit ;

: set-gfx-mode ( gfx-mode-value -- )
    <esc> <[> .| [char] m emit ;

And demo example code:

\ Copyright 2020 Christopher Howard

\ demo.fs

\ Licensed under the Apache License, Version 2.0 (the "License");
\ you may not use this file except in compliance with the License.
\ You may obtain a copy of the License at

\     http://www.apache.org/licenses/LICENSE-2.0

\ Unless required by applicable law or agreed to in writing, software
\ distributed under the License is distributed on an "AS IS" BASIS,
\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\ See the License for the specific language governing permissions and
\ limitations under the License.

require ansi-cursor.fs

: exp-scale ( n prescaled-range scaled-range -- n )
    rot dup * * swap dup * / ;

: cab-dist ( x y xc yc -- n ) 2 pick - abs >r swap drop - abs r> + ;

: demo-scale ( x y xc yx unscaled-range )
    >r cab-dist r> 8 exp-scale 7 min ;

: demo attributes-off set-gfx-mode erase-display
    80 0 do
        40 0 do
            i j 2dup position-cursor
            20 40 70 demo-scale black-bg + set-gfx-mode
            space
        loop
    loop
    attributes-off set-gfx-mode
    20 31 position-cursor ." * ansi-cursor demo *"
    40 0 position-cursor
;
Advertisement

1 thought on “ANSI Cursor Control from Forth”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s