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

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // I2-Terminal // This is intended to be loaded as a stand alone file so that it can // be used for debugging and the like. It still needs more work // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

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

// CONSTANTS: integer device // global to hold current device address integer i2debug // if set to 1 then text accompanies commands


Full Contents of File

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// I2-Terminal
// This is intended to be loaded as a stand alone file so that it can
// be used for debugging and the like. It still needs more work
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

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

// CONSTANTS:
integer  device    // global to hold current device address
integer  i2debug  // if set to 1 then text accompanies commands

// A - process simply change the current address
// ( value --- )
: Aproc
        1+  ?number 
        if
                =>  device
        else
                ."  Error  number  should  follow  address  thus  A&70  "
        then               
;
       
// S - process - The following each process the relevant I2C commands
// by extracting the number following and sending it to the I2C bus
// this command will ser address if number follows
// ( device-address --- )
: Sproc
        dup  length  1  >
        if 
                1+  ?number  drop  =>  device 
        then
        device  S                // start command
        i2debug  if  device  cr  ."  start  address  "  ph.  then
;       
     
// ( ---)
: Pproc
        P              // stop command
        i2debug  if  cr  ."  Stop  "  then
;       

// ( --- )
: RSproc
        device  1+  RS        // restart
        i2debug  if  cr  ."  re-start  address  "  device  1+  ph.  then 
;       

// ( device-address --- reads... )
: Rproc
        1+  ?number
        if
                dup  1  =
                if
                        drop
                        RL            // read last
                        i2debug  if  cr  ."  Last  read  "  .  then
                else       
                        i2debug
                        if
                                1-  for  cr  ."  read  "  RN  .  next
                                cr  ."  Last  read  "  RL  .
                        else
                                1-  for  RN  next  RL
                        then               
                then       
        else
                ."  Error  -  syntax  RLn"  cr
        then
;                       

// ============= Public interface for terminal ====================
// The string is a line of I2C commands, this takes the next token
// and if it is a number sends this directly to the i2c but, if not
// it will send it to the apropriate handler
: <0>i2-parse { string --- }
int count  adr

        begin
                bl  string  count  +  tab    >r    // parse next
                =>  adr                  // save address
                r>  dup  1+  +>  count  // for next time
        while
                adr  ?number
                if
                        dup  SB            // send byte
                        i2debug  if  cr  ."  send  byte  "  .  else  drop  then
                else
                        adr  toupper  c@
                        case
                        [char]  A  of  adr  Aproc  endof
                        [char]  S  of  adr  Sproc  endof
                        [char]  P  of  Pproc  endof
                        [char]  R  of  RSproc  endof
                        [char]  G  of  adr  Rproc  endof
                        otherwise  ."  unrecognised  command"
                        endcase       
                then
        repeat               
;

: i2-t-help
    cr  ."  I2  Term  Usage  "
    cr  ."  Example  to  send  2  bytes  to  I2C" 
    cr  ."  with  address  of  &42"
    cr  ."  s&42  2  55  p"
    cr  ."  S[nn]  =  start  with  optional  address"
    cr  ."  nn  =  number  on  its  own  will  be  sent  to  i2c"
    cr  ."  Ann  =  change  the  current  address  to  nn"
    cr  ."  P  =  send  stop  condition"
    cr  ."  R  =  restart  condition  uses  address  +  1"
    cr  ."  Gnn  =  gets  number  of  bytes  form  i2c"
    cr
    cr  ."  Example  to  send  value  20  then" 
    cr  ."  read  5  bytes  to  current  address"
    cr  ."  s  20  r  g5  p"
    cr
;   

// I2c terminal, finish with E
: <0>i2-term
        cr  ."  End  session  with  E  "
        i2-t-help
        i2-init
        1  =>  i2debug
        begin
                cr  ."  Current  address  set  to  "  device  ph.
                cr  ."  >"
                here  80  accept  // retuns number of chars
                0  <>
                if
                        here  c@  [char]  E  =  if  escape  then  // terminates loop
                        here  i2-parse 
                then                           
        again
; 

// #currentSID sid=0