Revision: 1123 Author: jsuijs Date: Sun Jul 26 14:30:50 2009 Log: Extension of keyboard.jal, added testfile. http://code.google.com/p/jallib/source/detail?r=1123
Added: /trunk/test/external/keyboard/test_keyboard.jal Modified: /trunk/include/external/keyboard/keyboard.jal /trunk/sample/16f876a_keyboard_lcd.jal /trunk/sample/16f877a_keyboard_lcd.jal /trunk/test/board/board_16f877a_dwarf.jal ======================================= --- /dev/null +++ /trunk/test/external/keyboard/test_keyboard.jal Sun Jul 26 14:30:50 2009 @@ -0,0 +1,63 @@ +-- ------------------------------------------------------ +-- Title: Test program for keyboar +-- +-- Author: Joep Suijs, Copyright (c) 2008..2008, all rights reserved. +-- +-- Adapted-by: +-- +-- Compiler: >=2.4g +-- +-- This file is part of jallib (http://jallib.googlecode.com) +-- Released under the BSD license (http://www.opensource.org/licenses/bsd-license.php) +-- +-- Description: Testing keyboard (4x4, 3x4) +-- -- +-- This file defines a test for JALLIB testing, using a test-board +-- defined by a BOARD file . +-- +-- Sources: +-- +-- Notes: +-- setup: A keyboard 4x3 or 4x4 connected to two half ports +-- +-- ------------------------------------------------------ + +;@jallib use chipdef + +include delay +enable_digital_io() + +-- setup serial (see echo.jal for more details) +;@jallib use serial +include serial_hardware +serial_hw_init() + +include print + +;@jallib use keyboard +include keyboard -- check for schematics + +-- -------------------------- PROGRAM START---------------------------- +const byte str1[] = " - Test keyboard.jal- bytes\r\n" -- define a string +print_string(serial_hw_data, str1) -- output string + +var byte key + +forever loop + key = getkey() + print_byte_dec(serial_hw_data, key) + + serial_hw_data = " " + + key = getkey_once() + print_byte_dec(serial_hw_data, key) + + print_crlf(serial_hw_data) + delay_100ms(2) +end loop + + + + + + ======================================= --- /trunk/include/external/keyboard/keyboard.jal Sat Nov 15 09:24:23 2008 +++ /trunk/include/external/keyboard/keyboard.jal Sun Jul 26 14:30:50 2009 @@ -1,70 +1,139 @@ -- Title: keyboard, scans 4x4 keybaord and returns 0x00..0x0F is keypressed, 0x10 if none pressed -- Author: Javier MartÃnez, Copyright (c) 2003, all rights reserved. --- Adapted-by: Eur van Andel, [email protected] +-- Adapted-by: Eur van Andel, [email protected], Joep Suijs -- 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 consist of a single funtion: getkey() that scans a 4x4 matrix keyboard. --- it needs two variables: keydrive and keyscan. --- --- Adaptation: soldering your wires in the right order is a lot harder than you think. --- So keep portx_low on the rows and portx_high on the columns or vice versa. --- Connect the keyboard and press the buttons. Make a local copy of this library and --- adapt the row and column constants so pressing the buttons give the right result. - --- --- 1 2 3 A row1 (4) --- 4 5 6 B row2 (5) --- 7 8 9 C row3 (6) --- * 0 # D row4 (7) --- +-- Description: This library is used to scan a 4x3 or 4x4 keyboard. +-- To use it, you need to define three variables: +-- keydrive is the half port (_high or _low) with the cols connected. +-- keydrive_direction is the related direction register. +-- keyscan is the half port (_high or _low) with the rows connected. +-- -- +-- Example: +-- -- +-- var volatile byte keyscan is porta_low -- rows of keyboard +-- var volatile byte keydrive is portb_low -- columns of keyboard +-- var volatile byte keydrive_direction is portb_low_direction +-- include keyboard +-- 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: connect the keyboard rows to a half port (higher or lower 4 bits of a port) +-- and connect the cols to a half port. Put a pull-down resistor (10k - 47k) on each row pin. +-- 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 +-- -- +-- keyboard map (4x4, col4 is left out with 4x3 keyboard) +-- -- +-- 1 2 3 A row1 +-- 4 5 6 B row2 +-- 7 8 9 C row3 +-- * 0 # D row4 +-- -- -- col 1 2 3 4 -- -const row1 = 0b0000_0100 -- match these constants to your soldering skills -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 - +if (defined(row1) == false) then + ; use default row/col definition + const row1 = 0b0000_0001 + const row2 = 0b0000_0010 + const row3 = 0b0000_0100 + const row4 = 0b0000_1000 + const col1 = 0b0000_0001 + const col2 = 0b0000_0010 + const col3 = 0b0000_0100 + const col4 = 0b0000_1000 +end if + +-- ---------------------------------------------------------------------------- +-- getkey - return the key pressed (until it is released) +-- ---------------------------------------------------------------------------- +-- return: 0 .. 9 numeric key pressed +-- 10 * +-- 11 # +-- 12 .. 15 ??? +-- 16 no key pressed. +-- 17 error (more then one key pressed) +-- ---------------------------------------------------------------------------- function getkey() return byte is var byte scan0, scan1, scan2, scan3 - + + keydrive_direction = col1 ^ 0x0F keydrive = col1 -- LSB MSB scan0 = keyscan & 0x0F -- active col [1] [4] [7] [A] + keydrive_direction = col2 ^ 0x0F keydrive = col2 scan1 = keyscan & 0x0F -- active col [2] [5] [8] [0] + keydrive_direction = col3 ^ 0x0F keydrive = col3 scan2 = keyscan & 0x0F -- active col [3] [6] [9] [B] + keydrive_direction = col4 ^ 0x0F keydrive = col4 scan3 = keyscan & 0x0F -- active col [F] [E] [D] [C] - + keydrive_direction = all_input + if ( scan3 == 0x00 ) & ( scan2 == 0x00 ) & ( scan1 == 0x00 ) & ( scan0 == 0x00 ) then return 0x10 -- 16 - elsif scan3 == row1 then return 0x01 - elsif scan3 == row2 then return 0x04 - elsif scan3 == row3 then return 0x07 - elsif scan3 == row4 then return 0x0A - elsif scan2 == row1 then return 0x02 - elsif scan2 == row2 then return 0x05 - elsif scan2 == row3 then return 0x08 - elsif scan2 == row4 then return 0x00 - elsif scan1 == row1 then return 0x03 - elsif scan1 == row2 then return 0x06 - elsif scan1 == row3 then return 0x09 - elsif scan1 == row4 then return 0x0B - elsif scan0 == row1 then return 0x0F - elsif scan0 == row2 then return 0x0E - elsif scan0 == row3 then return 0x0D - elsif scan0 == row4 then return 0x0C + elsif scan0 == row1 then return 0x01 + elsif scan0 == row2 then return 0x04 + elsif scan0 == row3 then return 0x07 + elsif scan0 == row4 then return 0x0A + elsif scan1 == row1 then return 0x02 + elsif scan1 == row2 then return 0x05 + elsif scan1 == row3 then return 0x08 + elsif scan1 == row4 then return 0x00 + elsif scan2 == row1 then return 0x03 + elsif scan2 == row2 then return 0x06 + elsif scan2 == row3 then return 0x09 + elsif scan2 == row4 then return 0x0B + elsif scan3 == row1 then return 0x0F + elsif scan3 == row2 then return 0x0E + elsif scan3 == row3 then return 0x0D + elsif scan3 == row4 then return 0x0C end if return 0x11 -- 17 -- error end function + +var byte lastkey = 0xff + +-- ---------------------------------------------------------------------------- +-- 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 0x10 end if + + lastkey = key + return key + +end function ======================================= --- /trunk/sample/16f876a_keyboard_lcd.jal Sat May 23 02:21:52 2009 +++ /trunk/sample/16f876a_keyboard_lcd.jal Sun Jul 26 14:30:50 2009 @@ -43,10 +43,13 @@ include print +var byte dummy var volatile byte keyscan is portc_low -- rows of keyboard portc_low_direction = all_input var volatile byte keydrive is portb_low -- columns of keyboard, also LCD -- data, already output +var byte keydrive_direction = dummy -- don't let the lib control the pin direction + include keyboard -- -------------------------- PROGRAM START---------------------------- ======================================= --- /trunk/sample/16f877a_keyboard_lcd.jal Sun May 17 22:36:20 2009 +++ /trunk/sample/16f877a_keyboard_lcd.jal Sun Jul 26 14:30:50 2009 @@ -39,10 +39,12 @@ lcd_init() include print +var byte dummy var volatile byte keyscan is portd_low -- rows of keyboard portd_low_direction = all_input var volatile byte keydrive is portd_high -- columns of keyboard +var byte keydrive_direction = dummy -- don't let the lib control the pin direction portd_high_direction = all_output include keyboard -- check for schematics ======================================= --- /trunk/test/board/board_16f877a_dwarf.jal Sun Jul 5 01:42:39 2009 +++ /trunk/test/board/board_16f877a_dwarf.jal Sun Jul 26 14:30:50 2009 @@ -80,3 +80,11 @@ -- backlight control (can be variable and digital on this pin) var volatile bit lcd_bl is pin_c2 var volatile bit lcd_bl_direction is pin_c2_direction + +;@jallib section keyboard +-- setup the two half ports +-- (rows and cols on separte ports - unlikely setup for dwarf-board) +var volatile byte keyscan is porta_low -- rows of keyboard +var volatile byte keydrive is portb_low -- columns of keyboard +var volatile byte keydrive_direction is portb_low_direction + --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
