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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP23008_Keypad.h Source File

MCP23008_Keypad.h

00001 /* MCP23008_Keypad Library v1.0
00002  * Copyright (c) 2016 Grant Phillips
00003  * grant.phillips@nmmu.ac.za
00004  *
00005  * This is a modified version of Riaan Ehlers' library which was written for the 
00006  * Microchip PIC18 series microcontrollers.
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy
00009  * of this software and associated documentation files (the "Software"), to deal
00010  * in the Software without restriction, including without limitation the rights
00011  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012  * copies of the Software, and to permit persons to whom the Software is
00013  * furnished to do so, subject to the following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included in
00016  * all copies or substantial portions of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024  * THE SOFTWARE.
00025  */
00026  
00027 #ifndef MCP23008_Keypad_H
00028 #define MCP23008_Keypad_H
00029  
00030 #include "mbed.h"
00031  
00032 /** Class library for a 4x4 keypad connected to a MCP23008 I2C IO Expander.
00033  * The Keypad is assumed to be connected to the MCP23008 as follows:
00034  *
00035  * GP7 -> ROW4; GP6 -> ROW3; GP5 -> ROW2; GP4 -> ROW1; GP3 -> COL4; GP2 -> COL3; GP1 -> COL2; GP0 -> COL1
00036  *
00037  * Example:
00038  * @code
00039  * #include "mbed.h"
00040  * #include "MCP23008_Keypad.h"
00041  *
00042  * MCP23008_Keypad keypad(PB_9, PB_6, 0x42);
00043  *
00044  * int main() {
00045  *     char rcv;
00046  *     int released = 1;
00047  *
00048  *     while(1){
00049  *         rcv = keypad.ReadKey();                 //read the current key pressed
00050  *
00051  *         if(rcv == '\0')
00052  *             released = 1;                       //set the flag when all keys are released
00053  *            
00054  *         if((rcv != '\0') && (released == 1)) {  //if a key is pressed AND previous key was released
00055  *             printf("%c\n", rcv);            
00056  *             released = 0;                       //clear the flag to indicate that key is still pressed
00057  *         }
00058  *     }
00059  * }
00060  * @endcode
00061  */
00062  
00063 class MCP23008_Keypad {
00064   public:
00065     /** Create a MCP23008_Keypad object.
00066     * @param SDApin I2C compatible pin used for connecting to the MCP23008's SDA pin
00067     * @param SCLpin I2C compatible pin used for connecting to the MCP23008's SCL pin
00068     * @param MCP23008Address The I2C address of the MCP23008 to which the Keypad is connected.
00069     */
00070     MCP23008_Keypad(PinName SDApin, PinName SCLpin, int MCP23008Address);
00071     
00072     /** Returns which key is currently is pressed.
00073     * @param 
00074     *     None
00075     * @return 
00076     *     The character representation of the key being pressed.  Returns '\0' if no key is pressed.
00077     */
00078     char ReadKey();
00079     
00080  
00081   private:
00082     I2C i2c;
00083     int _addr;
00084 };
00085  
00086 #endif