Figure 38 Using the keyboard module

ROWS

B3 B2 B1 BO

COLUMNS

(Input port)

7

8

9

Fl

4

5

6

F2

1

2

3

F3

*

0

#

Scan Codes

7

8

9

Fl

4

5

6

F2

1

2

3

F3

1 *

0

#

- Scan Code

Once multitasking has started, the keyboard will be scanned at the rate defined by KEY_SCAN_TASK_DLY. At this point, your application task (typically some type of user interface) will call one of the four keyboard module services: KeyGetKey (), KeyHit (), KeyFlush. (), or KeyGetKeyDownTime ().

In the following code, the user interface task calls KeyGetKey () by specifying a timeout of 0. In this case, the user interface will be suspended until a key is pressed. When a key is pressed, KeyGetKey () returns the scan code of the key pressed. For example, if you pressed the 8 key, the scan code returned by KeyGetKey () would be 14 (see Figure 3.8).

void UserlFTask (void *data) {

INT8U key;

key = KeyGetKey(0); /* Wait for user input (no timeout) */

You can map scan codes to anything you want by defining a lookup table:

char UserKeyMapTbl[] = {

'A' ,

/*

F4

key

*/

/*

#

key

*/

'O',

/*

0

key

*/

' * 1

/*

*

key

*/

'B' ,

/*

F3

key

*/

'3 ' ,

/*

3

key

*/

'2' ,

/*

2

key

*/

'1' ,

/*

1

key

*/

'C ,

/*

F2

key

*/

'6' ,

/*

6

key

*/

'5' ,

/*

5

key

*/

'4' ,

/*

4

key

*/

'D' ,

/*

Fl

key

*/

' 9 ',

/*

9

key

*/

'8' ,

/*

8

key

*/

,7,

/*

7

key

The user interface code would now look as shown following this paragraph. With UserKeyMapTbl [ ], the 8 key would now be returned to your application as ASCII 8 or, 1 8 ', the # would be returned as ASCII ' # ', etc.

void UserlFTask (void *data) {

INT8U code; char key;

code = KeyGetKey(O);

key = UserKeyMapTbl[code];

One of the disadvantages of the user interface code shown previously is that the user interface code is suspended until a key is pressed. If your user interface also needs to display run-time information, you can run the user interface code at a regular rate and poll the keyboard module:

void UserlFTask (void *data) {

INT8U code; char key;

code = KeyGetKey(O); key = UserKeyMapTbl[code]; switch (key) {

/* User interface display functions */

Chapter 3: Keyboards —119

Was this article helpful?

0 0

Post a comment