keypadc.h

#include <keypadc.h>

The keypadc library is used for quickly polling the status of the keys in the keypad. It supports multi-key presses, and can be used for extremely responsive input compared to OS routines such as os_GetCSC().

Key Detection

Handling key detection properly is a major requirement for a lot of programs. Detecting a hold can be rather straightforward, but things like a press, release, can sometimes be rather confusing at first.

Detecting a Single Press

When a key is first pressed, we only want to trigger on that event. The following code sets up a simple rising edge detector to catch this.

bool key, prevkey;
key = kb_Data[1] == kb_2nd;

if (key && !prevkey) {
    // ...
}
prevkey = key;

Detecting a Single Release

When a key is released we only want to trigger on that event. The following code sets up a simple falling edge detector to catch this.

bool key, prevkey;
key = kb_Data[1] == kb_2nd;

if (!key && prevkey) {
    // ...
}
prevkey = key;

Getting GetCSC Codes with keypadc

This code returns the same keycodes that os_GetCSC() produces.

// code by jacobly
uint8_t get_single_key_pressed(void) {
    static uint8_t last_key;
    uint8_t only_key = 0;
    kb_Scan();
    for (uint8_t key = 1, group = 7; group; --group) {
        for (uint8_t mask = 1; mask; mask <<= 1, ++key) {
            if (kb_Data[group] & mask) {
                if (only_key) {
                    last_key = 0;
                    return 0;
                } else {
                    only_key = key;
                }
            }
        }
    }
    if (only_key == last_key) {
        return 0;
    }
    last_key = only_key;
    return only_key;
}

API Documentation

keypadc is a simple direct-input keypad library.

Author

Matt “MateoConLechuga” Waltz

Author

Shaun “Merthsoft” McFall

Defines

kb_SetMode(mode)

Sets the keypad scanning mode.

See also

kb_scan_mode_t

kb_GetMode()

Gets the keypad scanning mode.

See also

kb_scan_mode_t

kb_EnableInt

Enabled keypad interrupt signals.

kb_IntAcknowledge

Acknowledege keypad interrupt signals.

kb_IntStatus

Status of keypad interrupt signals.

kb_Config

Configuration of keypad controller.

kb_IsDown(lkey)

Checks if a key is pressed.

This uses the long key type (kb_lkey_t), which includes the group as well. It can be used in place of reading directly from kb_Data.

Long key types have the same name as the normal key types, but are prefixed with kb_Key* rather than kb_*.

It does not call kb_Scan() internally, so you have to do so before calling this if you want the keyboard data to get updated.

kb_Data

Keypad Data registers.

Offset

Bit 0

Bit 1

Bit 2

Bit 3

Bit 4

Bit 5

Bit 6

Bit 7

1

kb_Graph

kb_Trace

kb_Zoom

kb_Window

kb_Fenetre

kb_Yequ

kb_Fx

kb_2nd

kb_Mode

kb_Del

kb_Suppr

2

kb_Sto

kb_Ln

kb_Log

kb_Square

kb_Recip

kb_TglExact

kb_Math

kb_Alpha

3

kb_0

kb_1

kb_4

kb_7

kb_Comma

kb_Sin

kb_Trig

kb_Apps

kb_Matrice

kb_GraphVar

4

kb_DecPnt

kb_2

kb_5

kb_8

kb_LParen

kb_Cos

kb_Resol

kb_Prgm

kb_Stat

5

kb_Chs

kb_3

kb_6

kb_9

kb_RParen

kb_Tan

kb_Frac

kb_Vars

6

kb_Enter

kb_Add

kb_Sub

kb_Mul

kb_Div

kb_Power

kb_Clear

kb_Annul

7

kb_Down

kb_Left

kb_Right

kb_Up

These data registers can be indexed as a normal array. For example, to check the status of the ‘2nd’ key:

if (kb_Data[1] & kb_2nd) {
    ...
}

Likewise, you can test keys with the following general format:

if (kb_Data[group] & kb_Name){
    ...
}

See also

kb_On

kb_On

ON key signal.

Note

Scanning mode does not matter.

Note

The ON key is not wired as part of the normal key matrix. The ON key must be checked separately from the other keys with kb_On:

int main(void) {
    kb_DisableOnLatch();
    ...
    if (kb_On) {
        ...
    }
}

kb_EnableOnLatch()

Causes kb_On to latch when the ON is key pressed: once the ON key is pressed, kb_On will remain true until reset with kb_ClearOnLatch().

This may be useful if you want to check the ON key occasionally, for example as a break signal.

Warning

This defaults to whatever the last program set this to, so be sure to set it explicitly.

kb_DisableOnLatch()

Disables ON key latching behavior.

See also

kb_EnableOnLatch

Warning

This defaults to whatever the last program set this to, so be sure to set it explicitly.

kb_ClearOnLatch()

When ON key latching has been enabled with kb_EnableOnLatch(), this will reset kb_On back to false (assuming the user is no longer pressing ON).

See also

kb_EnableOnLatch

Note

This may persist between program runs, so be to sure to disable as needed.

Typedefs

typedef uint8_t kb_key_t

Normal key type.

typedef uint16_t kb_lkey_t

Long key type.

Enums

enum kb_scan_mode_t

Different available scanning modes.

Values:

enumerator MODE_0_IDLE = 0

Keypad scanning is idle.

enumerator MODE_1_INDISCRIMINATE

Indiscriminate key detection.

Data registers are invalid, but when any key is pressed, interrupt KB_MODE_1_PRESS is set (and cannot be cleared until the key is released).

enumerator MODE_2_SINGLE

Single scan.

The keypad is scanned once, and then the mode returns to MODE_0_IDLE.

enumerator MODE_3_CONTINUOUS

Continuous scan.

When scanning completes, it just starts over again after a delay.

enum kb_int_signal_t

Different available interrupt signals.

Values:

enumerator KB_SCAN_COMPLETE = 1

Interrupt set when keypad is done scanning.

enumerator KB_DATA_CHANGED = 2

Interrupt set when data is changed on a press or a release.

enumerator KB_MODE_1_PRESS = 4

Interrupt set when a key is pressed in MODE_1_INDISCRIMINATE.

Functions

void kb_Scan(void)

Scans the keyboard to update data values.

Note

Disables interrupts.

kb_key_t kb_ScanGroup(uint8_t row)

Scans the given keyboard row and returns the row value.

Note

Disables interrupts.

Parameters

row[in] Row to scan.

uint8_t kb_AnyKey(void)

Scans the keyboard quickly to tell if any key was pressed.

Note

Disables interrupts.

void kb_Reset(void)

Resets the keyboard before returning to the OS.

Note

Only use if the keyboard timers or number of rows have been modified.