To use this file copy and paste this:    // #URL-lib "http://pin1.org/forthlib/flb/I2C/BV4218.flb"   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 library file that can be // extended using your own code. This file is intended as a library // but can be used on its own for debugging purposes // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

// HISTORY: // Nov 2008 * changed deva to device // * added new bvsys.flb library // Dec 2008 * removed most delays following new 1.a version

// 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=103 // #URL-Lib "http://pin1.org/forthlib/flb/I2C/bvsys.flb"

// CONSTANTS: // device variable is in bvsys.lib


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 library file that can be
// extended using your own code. This file is intended as a library
// but can be used on its own for debugging purposes
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

// HISTORY:
// Nov 2008 * changed deva to device
// * added new bvsys.flb library
// Dec 2008 * removed most delays following new 1.a version

// 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=103
// #URL-Lib "http://pin1.org/forthlib/flb/I2C/bvsys.flb"

// CONSTANTS:
// device variable is in bvsys.lib

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Special note on LCD displays and timimg.
// LCD are slow devices and must be given time to respond to
// a command or data. The device will use clock stretching where possible
// but raw commands still need an apropriate delay
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// ------------ LCD Display ---------------------------
//
: LCDcmd ( cmd -- )   device  s  1  sb  sb    p  ;  // send command
: LCDdata ( -- )   device  s  2  sb  ;          // finish with stop
: LCDemit ( c -- )   device  s  2  sb  sb  p  ;  // sends ASCII char to display
: 4218-default ( -- )   66  bv-init  ;

// Types a string to the display
// addr is a string to send to the display
// at the current cursor location
: stype2 { addr --- }
        LCDdata                  // prep i2c
        addr  255  +          // end
        addr                      // start
        do
                i  c@  0= 
                if 
                        leave
                else
                        i  c@  sb         
                then
        next       
        p                  // stop when complete
        addr  length  for    next  // proportional to size of message
; 

// various useful commands, delays are required for the LCD to
// respond, the user should supply these as they vary from disply to
// display
: lc1  LCDcmd  2  ms  ;          // clear display
: lh2  LCDcmd    ;        // home cursor but done clear displat
: l2&c0  LCDcmd  ;        // line 2 *** not for all displays
: ln ( num -- )   <#  #s  #>  stype2  ;  // display number
: lco&e  LCDcmd  ;  // cursor on
: lcf&c  LCDcmd  ;  // cursor off
: blight ( 1|0 --- )   device  s  3  sb  sb  p  ;  // back light hi/lo

// change the start up message
: start-m { string --- }
        device  s  6  sb  device  1+  rs  rl  p          // get message area
        device  s  &91  sb  sb            // set up eeprom write
        string  dup  length  +  string
        do
                i  c@  sb          // write bytes to eeprom
        loop
        0  sb                // terminator
        p                      // stop
;               

// ----------------- key pad ---------------------------------
// gets number of kesys in buffer
: kn ( -- n ) device  s  &10  sb  device  1+  rs  rl  p  ; 
: kg ( -- n )   device  s  &11  sb  device  1+  rs  rl  p  ;    // get last key pressed

// ( n --- ) Gets n number of keys in buffer
: kg# ( n -- n... )   device  s  &11  sb  device  1+  rs 
            1-  for  rn  next  rl  p  ;
// returns 1 if key is being pressed
: kd  ( -- n )   device  s  &13  sb  device  1+  rs  rl  p  ; 
: kc  ( -- )   device  s  &14  sb  p  ;    // clear key buffer
: ks  device  s  &15  sb  device  1+  rs  rl  p  ;    // scan code
: db@ ( -- n ) device  s  &90  sb  2  sb  device  1+  rs  rl  p  ;  // debound fetch
: db! ( n -- ) device  s  &91  sb  2  sb  sb  p  ;  // debounce store
// dumpkey buffer
: kbufdevice  s  &18  sb  device  1+  rs  15  for  rn  ph.  next  rl  ph.  p  ; 
: kmapdevice  s  &12  sb  device  1+  rs  rl  p  ;    // gets ke map start address

// maps correct keys, must be done AFTER factory reset
// as it uses the returned values to determin the eeprom
// addresses stores values to eepron so only needs doing once
// TERMINAL VERSION
: key-map-t
        cr  ."  press  keys  in  turn  1,2,  etc."
        12  0 
        do
                cr  ."  press  key  "  i  .  space
                begin  50  ms  kn  until    // wait for key
                kg            // get eeprom value
                kmap  +    // points to correct location
                device  s  &91  sb  sb  i  sb  p      // write eeprom wi correct value
                300  ms            // user delay
        loop
;               

// maps correct keys, must be done AFTER factory reset
// as it uses the returned values to determin the eeprom
// addresses stores values to eepron so only needs doing once
// LCD VERSION (2 x 16 line display)
: key-map-l
        4218-default  -1  <>  if  ."  Cant  start  4218"  abort  then
        lc  s"  press  keys  in"  stype2
        l2  s"  turn  1,2,  etc."  stype2
        2000  ms
        12  0
        do
                lc        // clear display
                s"  press  key  "  stype2  i  ln  // tell user what to do
                begin  50  ms  kn  until    // wait for key
                kg            // get key, eeprom value
                kmap  +    // points to correct location
                device  s  &91  sb  sb  i  sb  p      // write eeprom wi correct value
                300  ms            // user delay
        loop
        lc  s"  Keys  set"  stype2
;