To use this file copy and paste this:    // #URL-lib "http://pin1.org/forthlib/flb/I2C/bv4219-term.fth"   into BV Terminal 3 or here to download.

// Example of using the BV4219 as a terminal, this will also // respond to some VT100 escape codes // Go word is 'term'

// REQUIRES: // #URL-lib "http://pin1.org/forthlib/flb/General/soft1.flb" sid=0 // #URL-lib "http://pin1.org/forthlib/flb/General/pinsel.flb" sid=101 // #URL-Lib "http://pin1.org/forthlib/flb/I2C/i2c.flb" sid=102 // #URL-lib "http://pin1.org/forthlib/flb/I2C/bv4219.flb" sid=103

// CONSTANTS: // set this to the device address &42 constant dev_address


Full Contents of File

// Example of using the BV4219 as a terminal, this will also
// respond to some VT100 escape codes
// Go word is 'term'

// REQUIRES:
// #URL-lib "http://pin1.org/forthlib/flb/General/soft1.flb" sid=0
// #URL-lib "http://pin1.org/forthlib/flb/General/pinsel.flb" sid=101
// #URL-Lib "http://pin1.org/forthlib/flb/I2C/i2c.flb" sid=102
// #URL-lib "http://pin1.org/forthlib/flb/I2C/bv4219.flb" sid=103

// CONSTANTS:
// set this to the device address
&42  constant  dev_address

// utilities
: getnum ( from-key -- number, non-num )
v$ buf  16
int# bcount  0
        begin
                key  dup  48  57  between
        while       
                %@  buf  bcount  +  c!
                1  +>  bcount
        repeat       
        0  %@  buf  bcount  +  c!  // terminating 0
        %@  buf  ?number  drop  // the -1
;       
// VT100 escape code interpretation
: vt100
int p1  p2  p3  p4
        key  [char]  [  <>  if  drop  escape  then
        getnum  =>  p1  =>  p2  // is ; or command
        p2  [char]  ;  =  if  getnum  =>  p3  =>  p4  then
        p4  [char]  H  =  if  p1  p3  a25  then
        p2  [char]  A  =  if  a28  p1  -  0  max  a8  then  // cursor up
        p2  [char]  J  =  if  p1  2  =  if  a5  then  then  // cls
;       

// check for correct address and if device is present
: device?
        dev_address  4219-init
        if
                400000  i2speed  ;  // set fast speed
        else
                ."  Can't  initialise  device"
                abort
        then
;                       

: term
int curr_pos
        device?  50  ms
        a5  50  ms  // clear screen
        begin       
            key
            dup  27  =  if 
                drop  vt100
            else   
                dup  3  =  if  abort  then  // finish
                dup  13  =  if  // cr
                                a28  7  = 
                                if
                                        0  0  a25
                                else
                                        a28  1+  0  a25
                                then         
                        then                 
                dup  8  =  if  // bs
                        a27  1-  6  / 1- 0 max => curr_pos
                        a28  curr_pos  a25      50  ms
                        32  a20    50  ms  // space
                        a28  curr_pos  a25    50  ms       
                        then
                a20
            then   
        again       
;