Class library for a 4x4 keypad connected to a MCP23008 I2C IO Expander.

Committer:
grantphillips
Date:
Tue Apr 05 19:04:04 2016 +0000
Revision:
3:7d6b9684595c
Parent:
2:e5ae44d8ac5b
v1.00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
grantphillips 0:6f83aeb11f9a 1 /* MCP23008_Keypad Library v1.0
grantphillips 0:6f83aeb11f9a 2 * Copyright (c) 2016 Grant Phillips
grantphillips 0:6f83aeb11f9a 3 * grant.phillips@nmmu.ac.za
grantphillips 0:6f83aeb11f9a 4 *
grantphillips 3:7d6b9684595c 5 * This is a modified version of Riaan Ehlers' library which was written for the
grantphillips 3:7d6b9684595c 6 * Microchip PIC18 series microcontrollers.
grantphillips 0:6f83aeb11f9a 7 *
grantphillips 0:6f83aeb11f9a 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
grantphillips 0:6f83aeb11f9a 9 * of this software and associated documentation files (the "Software"), to deal
grantphillips 0:6f83aeb11f9a 10 * in the Software without restriction, including without limitation the rights
grantphillips 0:6f83aeb11f9a 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
grantphillips 0:6f83aeb11f9a 12 * copies of the Software, and to permit persons to whom the Software is
grantphillips 0:6f83aeb11f9a 13 * furnished to do so, subject to the following conditions:
grantphillips 0:6f83aeb11f9a 14 *
grantphillips 0:6f83aeb11f9a 15 * The above copyright notice and this permission notice shall be included in
grantphillips 0:6f83aeb11f9a 16 * all copies or substantial portions of the Software.
grantphillips 0:6f83aeb11f9a 17 *
grantphillips 0:6f83aeb11f9a 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
grantphillips 0:6f83aeb11f9a 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
grantphillips 0:6f83aeb11f9a 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
grantphillips 0:6f83aeb11f9a 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
grantphillips 0:6f83aeb11f9a 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
grantphillips 0:6f83aeb11f9a 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
grantphillips 0:6f83aeb11f9a 24 * THE SOFTWARE.
grantphillips 0:6f83aeb11f9a 25 */
grantphillips 0:6f83aeb11f9a 26
grantphillips 0:6f83aeb11f9a 27 #ifndef MCP23008_Keypad_H
grantphillips 0:6f83aeb11f9a 28 #define MCP23008_Keypad_H
grantphillips 0:6f83aeb11f9a 29
grantphillips 0:6f83aeb11f9a 30 #include "mbed.h"
grantphillips 0:6f83aeb11f9a 31
grantphillips 0:6f83aeb11f9a 32 /** Class library for a 4x4 keypad connected to a MCP23008 I2C IO Expander.
grantphillips 0:6f83aeb11f9a 33 * The Keypad is assumed to be connected to the MCP23008 as follows:
grantphillips 0:6f83aeb11f9a 34 *
grantphillips 2:e5ae44d8ac5b 35 * GP7 -> ROW4; GP6 -> ROW3; GP5 -> ROW2; GP4 -> ROW1; GP3 -> COL4; GP2 -> COL3; GP1 -> COL2; GP0 -> COL1
grantphillips 0:6f83aeb11f9a 36 *
grantphillips 0:6f83aeb11f9a 37 * Example:
grantphillips 0:6f83aeb11f9a 38 * @code
grantphillips 0:6f83aeb11f9a 39 * #include "mbed.h"
grantphillips 0:6f83aeb11f9a 40 * #include "MCP23008_Keypad.h"
grantphillips 0:6f83aeb11f9a 41 *
grantphillips 0:6f83aeb11f9a 42 * MCP23008_Keypad keypad(PB_9, PB_6, 0x42);
grantphillips 0:6f83aeb11f9a 43 *
grantphillips 0:6f83aeb11f9a 44 * int main() {
grantphillips 0:6f83aeb11f9a 45 * char rcv;
grantphillips 0:6f83aeb11f9a 46 * int released = 1;
grantphillips 0:6f83aeb11f9a 47 *
grantphillips 0:6f83aeb11f9a 48 * while(1){
grantphillips 0:6f83aeb11f9a 49 * rcv = keypad.ReadKey(); //read the current key pressed
grantphillips 0:6f83aeb11f9a 50 *
grantphillips 0:6f83aeb11f9a 51 * if(rcv == '\0')
grantphillips 0:6f83aeb11f9a 52 * released = 1; //set the flag when all keys are released
grantphillips 0:6f83aeb11f9a 53 *
grantphillips 0:6f83aeb11f9a 54 * if((rcv != '\0') && (released == 1)) { //if a key is pressed AND previous key was released
grantphillips 0:6f83aeb11f9a 55 * printf("%c\n", rcv);
grantphillips 0:6f83aeb11f9a 56 * released = 0; //clear the flag to indicate that key is still pressed
grantphillips 0:6f83aeb11f9a 57 * }
grantphillips 0:6f83aeb11f9a 58 * }
grantphillips 0:6f83aeb11f9a 59 * }
grantphillips 0:6f83aeb11f9a 60 * @endcode
grantphillips 0:6f83aeb11f9a 61 */
grantphillips 0:6f83aeb11f9a 62
grantphillips 0:6f83aeb11f9a 63 class MCP23008_Keypad {
grantphillips 0:6f83aeb11f9a 64 public:
grantphillips 0:6f83aeb11f9a 65 /** Create a MCP23008_Keypad object.
grantphillips 0:6f83aeb11f9a 66 * @param SDApin I2C compatible pin used for connecting to the MCP23008's SDA pin
grantphillips 0:6f83aeb11f9a 67 * @param SCLpin I2C compatible pin used for connecting to the MCP23008's SCL pin
grantphillips 0:6f83aeb11f9a 68 * @param MCP23008Address The I2C address of the MCP23008 to which the Keypad is connected.
grantphillips 0:6f83aeb11f9a 69 */
grantphillips 0:6f83aeb11f9a 70 MCP23008_Keypad(PinName SDApin, PinName SCLpin, int MCP23008Address);
grantphillips 0:6f83aeb11f9a 71
grantphillips 0:6f83aeb11f9a 72 /** Returns which key is currently is pressed.
grantphillips 0:6f83aeb11f9a 73 * @param
grantphillips 0:6f83aeb11f9a 74 * None
grantphillips 0:6f83aeb11f9a 75 * @return
grantphillips 0:6f83aeb11f9a 76 * The character representation of the key being pressed. Returns '\0' if no key is pressed.
grantphillips 0:6f83aeb11f9a 77 */
grantphillips 0:6f83aeb11f9a 78 char ReadKey();
grantphillips 0:6f83aeb11f9a 79
grantphillips 0:6f83aeb11f9a 80
grantphillips 0:6f83aeb11f9a 81 private:
grantphillips 0:6f83aeb11f9a 82 I2C i2c;
grantphillips 0:6f83aeb11f9a 83 int _addr;
grantphillips 0:6f83aeb11f9a 84 };
grantphillips 0:6f83aeb11f9a 85
grantphillips 0:6f83aeb11f9a 86 #endif