Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/Keypad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <Keypad.h>

// <<constructor>> Allows custom keymap, pin configuration, and keypad sizes.
Keypad::Keypad(char *userKeymap, byte *row, byte *col, byte numRows, byte numCols) {
Keypad::Keypad(char *userKeymap, uint8_t *row, uint8_t *col, uint8_t numRows, uint8_t numCols) {
rowPins = row;
columnPins = col;
sizeKpd.rows = numRows;
Expand Down Expand Up @@ -82,15 +82,15 @@ bool Keypad::getKeys() {
// Private : Hardware scan
void Keypad::scanKeys() {
// Re-intialize the row pins. Allows sharing these pins with other hardware.
for (byte r=0; r<sizeKpd.rows; r++) {
for (uint8_t r=0; r<sizeKpd.rows; r++) {
pin_mode(rowPins[r],INPUT_PULLUP);
}

// bitMap stores ALL the keys that are being pressed.
for (byte c=0; c<sizeKpd.columns; c++) {
for (uint8_t c=0; c<sizeKpd.columns; c++) {
pin_mode(columnPins[c],OUTPUT);
pin_write(columnPins[c], LOW); // Begin column pulse output.
for (byte r=0; r<sizeKpd.rows; r++) {
for (uint8_t r=0; r<sizeKpd.rows; r++) {
bitWrite(bitMap[r], c, !pin_read(rowPins[r])); // keypress is active low so invert to high.
}
// Set pin to high impedance input. Effectively ends column pulse.
Expand All @@ -105,7 +105,7 @@ bool Keypad::updateList() {
bool anyActivity = false;

// Delete any IDLE keys
for (byte i=0; i<LIST_MAX; i++) {
for (uint8_t i=0; i<LIST_MAX; i++) {
if (key[i].kstate==IDLE) {
key[i].kchar = NO_KEY;
key[i].kcode = -1;
Expand All @@ -114,8 +114,8 @@ bool Keypad::updateList() {
}

// Add new keys to empty slots in the key list.
for (byte r=0; r<sizeKpd.rows; r++) {
for (byte c=0; c<sizeKpd.columns; c++) {
for (uint8_t r=0; r<sizeKpd.rows; r++) {
for (uint8_t c=0; c<sizeKpd.columns; c++) {
boolean button = bitRead(bitMap[r],c);
char keyChar = keymap[r * sizeKpd.columns + c];
int keyCode = r * sizeKpd.columns + c;
Expand All @@ -126,7 +126,7 @@ bool Keypad::updateList() {
}
// Key is NOT on the list so add it.
if ((idx == -1) && button) {
for (byte i=0; i<LIST_MAX; i++) {
for (uint8_t i=0; i<LIST_MAX; i++) {
if (key[i].kchar==NO_KEY) { // Find an empty slot or don't add key to list.
key[i].kchar = keyChar;
key[i].kcode = keyCode;
Expand All @@ -140,7 +140,7 @@ bool Keypad::updateList() {
}

// Report if the user changed the state of any key.
for (byte i=0; i<LIST_MAX; i++) {
for (uint8_t i=0; i<LIST_MAX; i++) {
if (key[i].stateChanged) anyActivity = true;
}

Expand All @@ -149,7 +149,7 @@ bool Keypad::updateList() {

// Private
// This function is a state machine but is also used for debouncing the keys.
void Keypad::nextKeyState(byte idx, boolean button) {
void Keypad::nextKeyState(uint8_t idx, boolean button) {
key[idx].stateChanged = false;

switch (key[idx].kstate) {
Expand All @@ -176,7 +176,7 @@ void Keypad::nextKeyState(byte idx, boolean button) {

// New in 2.1
bool Keypad::isPressed(char keyChar) {
for (byte i=0; i<LIST_MAX; i++) {
for (uint8_t i=0; i<LIST_MAX; i++) {
if ( key[i].kchar == keyChar ) {
if ( (key[i].kstate == PRESSED) && key[i].stateChanged )
return true;
Expand All @@ -188,7 +188,7 @@ bool Keypad::isPressed(char keyChar) {
// Search by character for a key in the list of active keys.
// Returns -1 if not found or the index into the list of active keys.
int Keypad::findInList (char keyChar) {
for (byte i=0; i<LIST_MAX; i++) {
for (uint8_t i=0; i<LIST_MAX; i++) {
if (key[i].kchar == keyChar) {
return i;
}
Expand All @@ -199,7 +199,7 @@ int Keypad::findInList (char keyChar) {
// Search by code for a key in the list of active keys.
// Returns -1 if not found or the index into the list of active keys.
int Keypad::findInList (int keyCode) {
for (byte i=0; i<LIST_MAX; i++) {
for (uint8_t i=0; i<LIST_MAX; i++) {
if (key[i].kcode == keyCode) {
return i;
}
Expand Down Expand Up @@ -227,7 +227,7 @@ bool Keypad::keyStateChanged() {

// The number of keys on the key list, key[LIST_MAX], equals the number
// of bytes in the key list divided by the number of bytes in a Key object.
byte Keypad::numKeys() {
uint8_t Keypad::numKeys() {
return sizeof(key)/sizeof(Key);
}

Expand All @@ -244,7 +244,7 @@ void Keypad::addEventListener(void (*listener)(char)){
keypadEventListener = listener;
}

void Keypad::transitionTo(byte idx, KeyState nextState) {
void Keypad::transitionTo(uint8_t idx, KeyState nextState) {
key[idx].kstate = nextState;
key[idx].stateChanged = true;

Expand Down
22 changes: 11 additions & 11 deletions src/Keypad.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ typedef unsigned long ulong;
// Made changes according to this post http://arduino.cc/forum/index.php?topic=58337.0
// by Nick Gammon. Thanks for the input Nick. It actually saved 78 bytes for me. :)
typedef struct {
byte rows;
byte columns;
uint8_t rows;
uint8_t columns;
} KeypadSize;

#define LIST_MAX 10 // Max number of keys on the active list.
Expand All @@ -75,11 +75,11 @@ typedef struct {
class Keypad : public Key {
public:

Keypad(char *userKeymap, byte *row, byte *col, byte numRows, byte numCols);
Keypad(char *userKeymap, uint8_t *row, uint8_t *col, uint8_t numRows, uint8_t numCols);

virtual void pin_mode(byte pinNum, byte mode) { pinMode(pinNum, mode); }
virtual void pin_write(byte pinNum, boolean level) { digitalWrite(pinNum, level); }
virtual int pin_read(byte pinNum) { return digitalRead(pinNum); }
virtual void pin_mode(uint8_t pinNum, uint8_t mode) { pinMode(pinNum, mode); }
virtual void pin_write(uint8_t pinNum, boolean level) { digitalWrite(pinNum, level); }
virtual int pin_read(uint8_t pinNum) { return digitalRead(pinNum); }

uint bitMap[MAPSIZE]; // 10 row x 16 column array of bits. Except Due which has 32 columns.
Key key[LIST_MAX];
Expand All @@ -97,22 +97,22 @@ class Keypad : public Key {
int findInList(int keyCode);
char waitForKey();
bool keyStateChanged();
byte numKeys();
uint8_t numKeys();

private:
unsigned long startTime;
char *keymap;
byte *rowPins;
byte *columnPins;
uint8_t *rowPins;
uint8_t *columnPins;
KeypadSize sizeKpd;
uint debounceTime;
uint holdTime;
bool single_key;

void scanKeys();
bool updateList();
void nextKeyState(byte n, boolean button);
void transitionTo(byte n, KeyState nextState);
void nextKeyState(uint8_t n, boolean button);
void transitionTo(uint8_t n, KeyState nextState);
void (*keypadEventListener)(char);
};

Expand Down