Serial LCD Plus - Reading a Key Pad


' ReadKey.BS2
'
' Reads a key using the LCD+ keypad interface.  If the key is
' numeric, the program beeps that number of times with "0" being defined
' as 10.  If the key in not numeric or no key is depressed within 10 secs
' an "Invalid" message is displayed on the LCD.
'
' Note that KeyPadModes is set for Delayed Response, LCD echo, Key Beeps
'
' copyright, Peter H. Anderson, June, '00

   INV_9600 CON $4000 + 84

   TX_PIN CON 8
   RX_PIN CON 9

   N Var Byte
   Key Var Byte
   NumBeeps Var Byte
   BeepSpaceTime Var Byte

Main:
   BeepSpaceTime = 250
   SerOut TX_PIN, INV_9600, 10, [$18, %10000101]
	' Keypad modes:  Delay Response, LCD Echo, Key Beeps
   GoSub LCDInit
Again:
   GoSub LCDClear
   GoSub LCDSetCursorHome
   SerOut TX_PIN, INV_9600, 10, ["Go", CR]
   GoSub ReadKey
   Debug ? Key
   If (Key < "0") OR (Key > "9") Then Invalid
      NumBeeps = Key - "0"
      GoSub LCDMultBeep
      GoTo Again
Invalid:
   SerOut TX_PIN, INV_9600, 10, ["Invalid", CR]
   GoTo Again

   END

ReadKey:

   Serin RX_PIN, INV_9600, 10000, Fail, [Key]
   Return

Fail:
   Key = $ff
   Return


LCDMultBeep:
   If NumBeeps <> 0 Then LCDMultBeep1
     NumBeeps = 10		' if 0 then make it 10
LCDMultBeep1:
   For N = 1 to NumBeeps
      GoSub LCDBeep
      Pause BeepSpaceTime
   Next
   Return

LCDBeep:
   SerOut TX_PIN, INV_9600, 10, [$07]
   Return

LCDInit:
   SerOut TX_PIN, INV_9600, 10, [$02, $80, $03, $60, $04, $00, $10, $00]
' Set BackLight Instensity to $80 - $02, $80
' Contrast to $60 - $03, $60
' CursorStyle Hidden - $04, $00
' Cursor Home - $10, $00
   SerOut TX_PIN, INV_9600, 10, [$17, 100, $0c, $0e]
' Beep Freq 100 - $17, 100
' Clear LCD - $0c
' Backlight on - $0e

   Return

LCDSetCursorHome:
   SerOut TX_PIN, INV_9600, 10, [$01]
   Return

LCDClear:
   SerOut TX_PIN, INV_9600, 10, [$0c]		' Clear LCD+
   Return