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
119 changes: 119 additions & 0 deletions Keypad_I2C.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
||
|| @file Keypad_I2C.h
|| @version 2.0 - PCF8575 support added by Paul Williamson
|| @author G. D. (Joe) Young, ptw
|| @contact "G. D. (Joe) Young" <jyoung@islandnet.com>
||
|| @description
|| | Keypad_I2C provides an interface for using matrix keypads that
|| | are attached with I2C port expanders. It supports multiple keypads,
|| | user selectable pins, and user defined keymaps.
|| #
||
|| @license
|| | This library is free software; you can redistribute it and/or
|| | modify it under the terms of the GNU Lesser General Public
|| | License as published by the Free Software Foundation; version
|| | 2.1 of the License.
|| |
|| | This library is distributed in the hope that it will be useful,
|| | but WITHOUT ANY WARRANTY; without even the implied warranty of
|| | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|| | Lesser General Public License for more details.
|| |
|| | You should have received a copy of the GNU Lesser General Public
|| | License along with this library; if not, write to the Free Software
|| | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|| #
||
*/

#include "Keypad_I2C.h"

// Let the user define a keymap - assume the same row/column count as defined in constructor
void Keypad_I2C::begin(char *userKeymap) {
Keypad::begin(userKeymap);
TwoWire::begin();
pinState = pinState_set( );
}


// Initialize I2C
void Keypad_I2C::begin(void) {
TwoWire::begin();
// pinState = 0xff;
pinState = pinState_set( );
}

// Initialize I2C
void Keypad_I2C::begin(byte address) {
i2caddr = address;
TwoWire::begin(address);
// pinState = 0xff;
pinState = pinState_set( );
}

// Initialize I2C
void Keypad_I2C::begin(int address) {
i2caddr = address;
TwoWire::begin(address);
// pinState = 0xff;
pinState = pinState_set( );
}


void Keypad_I2C::pin_write(byte pinNum, boolean level) {
word mask = 1<<pinNum;
if( level == HIGH ) {
pinState |= mask;
} else {
pinState &= ~mask;
}
port_write( pinState );
} // I2CxWrite( )


int Keypad_I2C::pin_read(byte pinNum) {
word mask = 0x1<<pinNum;
TwoWire::requestFrom((int)i2caddr, (int)i2cwidth);
word pinVal = TwoWire::read( );
if (i2cwidth > 1) {
pinVal |= TwoWire::read( ) << 8;
}
pinVal &= mask;
if( pinVal == mask ) {
return 1;
} else {
return 0;
}
}

void Keypad_I2C::port_write( word i2cportval ) {
TwoWire::beginTransmission((int)i2caddr);
TwoWire::write( i2cportval & 0x00FF);
if (i2cwidth > 1) {
TwoWire::write( i2cportval >> 8 );
}
TwoWire::endTransmission();
pinState = i2cportval;
} // port_write( )

word Keypad_I2C::pinState_set( ) {
TwoWire::requestFrom( (int)i2caddr, (int)i2cwidth );
pinState = TwoWire::read( );
if (i2cwidth > 1) {
pinState |= TwoWire::read( ) << 8;
}
return pinState;
} // set_pinState( )


/*
|| @changelog
|| |
|| | 2.0 2013-08-31 - Paul Williamson : Added i2cwidth parameter for PCF8575 support
|| |
|| | 1.0 2012-07-12 - Joe Young : Initial Release
|| #
*/
85 changes: 85 additions & 0 deletions Keypad_I2C.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
||
|| @file Keypad_I2C.h
|| @version 2.0 - PCF8575 support added by Paul Williamson
|| @author G. D. (Joe) Young, ptw
|| @contact "G. D. (Joe) Young" <jyoung@islandnet.com>
||
|| @description
|| | Keypad_I2C provides an interface for using matrix keypads that
|| | are attached to I2C port expanders. It supports multiple keypads,
|| | user selectable pins, and user defined keymaps.
|| #
||
|| @license
|| | This library is free software; you can redistribute it and/or
|| | modify it under the terms of the GNU Lesser General Public
|| | License as published by the Free Software Foundation; version
|| | 2.1 of the License.
|| |
|| | This library is distributed in the hope that it will be useful,
|| | but WITHOUT ANY WARRANTY; without even the implied warranty of
|| | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|| | Lesser General Public License for more details.
|| |
|| | You should have received a copy of the GNU Lesser General Public
|| | License along with this library; if not, write to the Free Software
|| | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|| #
||
*/

#ifndef KEYPAD_I2C_H
#define KEYPAD_I2C_H

#include "Keypad.h"
#include "Wire.h"

#define PCF8574 1 // PCF8574 I/O expander device is 1 byte wide
#define PCF8575 2 // PCF8575 I/O expander device is 2 bytes wide

class Keypad_I2C : public Keypad, public TwoWire {
public:
Keypad_I2C(char* userKeymap, byte* row, byte* col, byte numRows, byte numCols, byte address, byte width = 1) :
Keypad(userKeymap, row, col, numRows, numCols) { i2caddr = address; i2cwidth = width;}


// Keypad function
void begin(char *userKeymap);
// Wire function
void begin(void);
// Wire function
void begin(byte address);
// Wire function
void begin(int address);

void pin_mode(byte pinNum, byte mode) {}
void pin_write(byte pinNum, boolean level);
int pin_read(byte pinNum);
// read initial value for pinState
word pinState_set( );
// write a whole byte or word (depending on the port expander chip) to i2c port
void port_write( word i2cportval );

private:
// I2C device address
byte i2caddr;
// I2C port expander device width in bytes (1 for 8574, 2 for 8575)
byte i2cwidth;
// I2C pin_write state persistant storage
// least significant byte is used for 8-bit port expanders
word pinState;
};



#endif // KEYPAD_I2C_H

/*
|| @changelog
|| |
|| | 2.0 2013-08-31 - Paul Williamson : Added i2cwidth parameter for PCF8575 support
|| |
|| | 1.0 2012-07-12 - Joe Young : Initial Release
|| #
*/
Binary file added docs/I2CKeypadDatasheet.pdf
Binary file not shown.
Binary file added docs/PCF8574.pdf
Binary file not shown.
Binary file added docs/PCF8575C.pdf
Binary file not shown.
Binary file added docs/PCF8575C_Breakout-v10.pdf
Binary file not shown.
Binary file added docs/pcf8575.pdf
Binary file not shown.
Binary file added docs/usingKeypad_I2C.pdf
Binary file not shown.
44 changes: 44 additions & 0 deletions examples/CustomKeypad_I2C/CustomKeypad_I2C.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
Use with I2C i/o G. D. (Joe) Young Feb 28/12
*/
#include <Keypad_I2C.h>
#include <Keypad.h> // GDY120705
#include <Wire.h>

#define I2CADDR 0x21

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'0','1','2','3'},
{'4','5','6','7'},
{'8','9','A','B'},
{'C','D','E','F'}
};
byte rowPins[ROWS] = {3, 2, 1, 0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad_I2C customKeypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS, I2CADDR);

void setup(){
// Wire.begin( );
customKeypad.begin( ); // GDY120705
Serial.begin(9600);
}

void loop(){
char customKey = customKeypad.getKey();

if (customKey != NO_KEY){
Serial.println(customKey);
}
}
Loading