Revision: 1800
Author: wattymage
Date: Mon Mar 15 10:55:55 2010
Log: initial alpha sample
http://code.google.com/p/jallib/source/detail?r=1800
Added:
/trunk/catpad/keypad5_2mw.jal
=======================================
--- /dev/null
+++ /trunk/catpad/keypad5_2mw.jal Mon Mar 15 10:55:55 2010
@@ -0,0 +1,187 @@
+-- Title: keyboard, scans 4x4 keybaord and returns text on key, 0x00 if
none pressed
+-- 65 keypad will be pbased on this.
+-- 254 is last key no change
+-- is error
+--
+-- Author: Javier MartÃnez, Copyright (c) 2003, all rights reserved.
+-- Adapted-by: Eur van Andel, [email protected], Joep Suijs
+-- Adapted by: Michael Watterson 2010
+-- Compiler: >=2.4g
+--
+-- This file is part of jallib (http://jallib.googlecode.com)
+-- Released under the ZLIB license
(http://www.opensource.org/licenses/zlib-license.html)
+--
+-- Description: This library is used to scan a 4x3 or 4x4 keyboard.
+-- returning text "1" .. "9", "0", "*", "#", "A" .. "D"
+-- Additionally a 5th row and col add 4 menu keys Red, Green, Yellow, Blue
+-- returning "r", "g", "y" & "b"
+-- and 5 way Navipad (or 5 separate switches) Up, Left, Right, down & Enter
+-- returning "^", "<", ">", "v" and code 13 (CR key or Ctrl M)
+--
+-- To use it, you need to define four variables:
+-- keydrive is the port with the cols connected.
+-- keydrive_direction is the related direction register.
+-- keyscan is the port with the rows connected.
+-- --
+-- Example:
+-- --
+-- var volatile byte keyscan is portb -- rows of keyboard
+-- var volatile byte keyscan_direction is portb_direction
+-- var volatile byte keydrive is portd -- columns of keyboard
+-- var volatile byte keydrive_direction is portd_direction
+-- include keypad5_2
+-- Note: if you don't want keyboard.jal change the direction of your pins,
make sure all
+-- keydrive pins are set to output before you call the library and define:
+-- var volatile byte keydrive_direction is keydrive
+-- --
+-- The library has two functions to read the keyboard:
+-- getkey() returns the key pressed.
+-- getkey_once() returns a pressed key only once (and 'none', 0x10 until
the next key is pressed)
+-- --
+-- Hardware setup: See schematic
+
+-- Adaptation: soldering your wires in the right order is a lot harder
than you think.
+-- Make sure you keep the rows and cols together and connect them to right
port.
+-- You could either adapt your wiring to match the default configuration
or reconfigure the
+-- library by defining the next 8 constants before you include
keyboard.jal, like:
+-- const row1 = 0b0000_0100
+-- const row2 = 0b0000_0001
+-- const row3 = 0b0000_0010
+-- const row4 = 0b0000_1000
+--
+-- const col1 = 0b0000_1000
+-- const col2 = 0b0000_0010
+-- const col3 = 0b0000_0100
+-- const col4 = 0b0000_0001
+-- Connect the keyboard and press the buttons. Adapt the row and column
constants so
+-- pressing the buttons give the right result.
+-- Note: you need to specify all 8 constants, even when you have a 4x3
keyboard. In
+-- this case you can specify:
+-- const col4 = 0b0000_0000
+-- --
+-- --
+-- 1 2 3 A ^ row1
+-- 4 5 6 B < row2
+-- 7 8 9 C v row3
+-- * 0 # D > row4
+-- R G Y B E
+-- --
+-- col 1 2 3 4 5
+--
+
+if (defined(row1) == false) then
+ ; use default row/col definition
+ ; bit 0 and 1 not used
+ const row1 = 0b0000_0001
+ const row2 = 0b0000_0010
+ const row3 = 0b0000_0100
+ const row4 = 0b0000_1000
+ const row5 = 0b0001_0000
+ const srow1 = 0b0001_0001
+ const srow2 = 0b0001_0010
+ const srow3 = 0b0001_0100
+ const srow4 = 0b0001_1000
+
+ const col1 = 0b0000_0001
+ const col2 = 0b0000_0010
+ const col3 = 0b0000_0100
+ const col4 = 0b0000_1000
+ const col5 = 0b0001_0000
+end if
+
+--
----------------------------------------------------------------------------
+-- getkey - return the key pressed (until it is released)
+--
----------------------------------------------------------------------------
+-- return: "0 .. 9" numeric key pressed
+-- "*" *
+-- "#" #
+-- "A ..D" A .. D
+-- 0 no key pressed.
+-- 255 error (more then one key pressed)
+--
----------------------------------------------------------------------------
+function getkey() return byte is
+var byte scan1, scan2, scan3, scan4, scan5
+var byte oldScanDir, oldDriveDir
+-- we may be sharing ports with Graphics LCD, so save & restore direction
+-- Shift keyscan right by 2 as B0 and B1 used for I2C EEPROM
+ oldDriveDir = keydrive_direction
+ oldScanDir = keyScan_direction
+ keyscan_direction = all_input
+ -- LSB MSB
+ keydrive_direction = ! byte(col1)
+ keydrive = col1
+ scan1 = (keyscan >> 2) & 0x1F -- active col [1] [4] [7] [*] [r]
+ keydrive_direction = ! byte(col2)
+ keydrive = col2
+ scan2 = (keyscan >> 2) & 0x1F -- active col [2] [5] [8] [0] [g]
+ keydrive_direction = ! byte(col3)
+ keydrive = col3
+ scan3 = (keyscan >> 2) & 0x1F -- active col [3] [6] [9] [#] [y]
+ keydrive_direction = ! byte(col4)
+ keydrive = col4
+ scan4 = (keyscan >> 2) & 0x1F -- active col [A] [B] [C] [D] [b]
+ keydrive_direction = ! byte(col5)
+ keydrive = col5
+ scan5 = (keyscan >> 2) & 0x1F -- active col [^] [<] [v] [>] [e]
+
+ keydrive_direction = oldDriveDir
+ keyscan_direction = oldScanDir
+
+ if ( scan4 == 0x00 ) &( scan3 == 0x00 ) & ( scan2 == 0x00 ) &
+ ( scan1 == 0x00 ) & ( scan5 == 0x00 ) then return 0x0 -- null
+
+ elsif scan1 == row1 then return "1"
+ elsif scan1 == row2 then return "4"
+ elsif scan1 == row3 then return "7"
+ elsif scan1 == row4 then return "*"
+ elsif scan1 == row5 then return "r"
+
+ elsif scan2 == row1 then return "2"
+ elsif scan2 == row2 then return "5"
+ elsif scan2 == row3 then return "8"
+ elsif scan2 == row4 then return "0"
+ elsif scan2 == row5 then return "g"
+
+ elsif scan3 == row1 then return "3"
+ elsif scan3 == row2 then return "6"
+ elsif scan3 == row3 then return "9"
+ elsif scan3 == row4 then return "#"
+ elsif scan3 == row5 then return "y"
+
+ elsif scan4 == row1 then return "A"
+ elsif scan4 == row2 then return "B"
+ elsif scan4 == row3 then return "C"
+ elsif scan4 == row4 then return "D"
+ elsif scan4 == row5 then return "b"
+
+ elsif scan5 == row1 then return 1 -- Ctrl A (Esc A is ANSI Up )
+ elsif scan5 == row2 then return 2 -- Ctrl B (Esc A is ANSI Down )
+ elsif scan5 == row3 then return 3 -- Ctrl C (Esc A is ANSI Right )
+ elsif scan5 == row4 then return 4 -- Ctrl D (Esc A is ANSI Left )
+ elsif scan5 == row5 then return 13 -- CR key or Enter, Ctrl M
+ elsif scan5 == srow1 then return "^"
+ elsif scan5 == srow2 then return "v"
+ elsif scan5 == srow3 then return ">"
+ elsif scan5 == srow4 then return "<"
+ end if
+
+ return 255 -- error
+
+end function
+
+var byte lastkey = 254
+
+--
----------------------------------------------------------------------------
+-- getkey_once - return the value of a key pressed only once
+--
----------------------------------------------------------------------------
+-- returns: see getkey()
+--
----------------------------------------------------------------------------
+function getkey_once() return byte is
+
+ var byte key = getkey()
+
+ if (key == lastkey) then return 254 end if
+ lastkey = key
+ return key
+
+end function
--
You received this message because you are subscribed to the Google Groups
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jallib?hl=en.