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

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // BV4218 LCD + Keypad circuit board // the same routines can be used for the BV4208 LCD, IC and // the BV4207 keypad chip. // This is a simple demonstration thst will check the keypad and // then add 2 numbers together. It has been designed for a 2 x 16 line // display but should work with lareger displays. // The technique used here is to extend the BV4218 library with a SID // of 102 and then only make the wotd GO public. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

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

// CONSTANTS: // none


Full Contents of File

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// BV4218 LCD + Keypad circuit board
// the same routines can be used for the BV4208 LCD, IC and
// the BV4207 keypad chip.
// This is a simple demonstration thst will check the keypad and
// then add 2 numbers together. It has been designed for a 2 x 16 line
// display but should work with lareger displays.
// The technique used here is to extend the BV4218 library with a SID
// of 102 and then only make the wotd GO public.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

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

// CONSTANTS:
// none

// extend BV4218 library
// #currentSID sid=102

: l2-dl2  5  ms  ;  // this display requires a delay afet moving cursor
: lh-dlh  5  ms  ;  //
: l2n ( n -- )   &c0  +  LCDcmd  5  ms  ;  // moves curse on second line to position n
: msg1s"  BV4128  Demo"  stype2  ;
: msg2s"  Display  &  Keypad"  stype2  ;
: msg3s"  Enter  2  values"  stype2  ;
: msg4s"  #  is  enter  key"  stype2  ;

// wait here for a key press and return with key that
// was pressed
: key-wait ( -- k )
    begin
        key?  abort"  terminal  key  pressed"
        kn    // goes to <>0 when keypad pressed
        1  ms    // dont overload i2c
    until
    kg        // get gey from keypad
;

// checks to see if keypad has been set up, # key should
// be value 11. Returns true if it is.
: keypadset? ( -- -1|0 )
    lc  s"  Press  the  #  key"  stype2
    kc    // clear key buffer
    key-wait
    11  =
;       

// asks the user to press the hash key that has been set a value of
// 11, if it does not return 11 then it goes through the kepad setup
: setkeypad
    keypadset?  invert
    if
        lc  s"  keys  need  seting"  // no room for another t
        1000  ms
        key-map-l
    then 
;       

// gets a decimal number form the keypad, terminated with the
// # key, echoes number to display
: get-number ( -- n )
    0          // start with 0
    begin
        key-wait    // get value pressed
        dup  11  =  if  drop  escape  then  // enter key
        dup  ln        // echo
        swap  10  *        // multiply existing by position
        +                  // add to existing
    again
;       

// sign on to display, tell user whats gong on and set up the lcd
// NOTE version k firmware onwards has command 55 so that a sucessfull
// conection to the device can be established, this assumes an older version.
: signon
    66  lcd-init        // set i2c bus
    0  blight        // trun on back light if there is one
    lc  msg1  l2-d  msg2    // sign on
    2000  ms
;

// this is the public word
: <0>go
    signon
    setkeypad      // check keypad is set up
    lc  msg3  l2-d  msg4  2000  ms  // user instructions
    // get keys and calculate sum
    lc  s"  First  Value"  stype2 
    l2-d  s"  *  "  stype2  get-number  // on stack
    // get second number
    lh-d    // home cursor but dont clear display
    s"  Second  Value"  stype2 
    6  l2n    // second line 6th position
    s"  *  "  stype2  get-number    // get second number
    12  l2n 
    s"  =  "  stype2  +  ln   
;

// #currentSID sid=102
// back to default SID