I2Cdevlib

I2C device library collection for AVR/Arduino or other C++-based MCUs

MPR121/MPR121.h
00001 // I2Cdev library collection - MPR121 I2C device class header file
00002 // Based on Freescale MPR121 datasheet rev. 2, 04/2010 and Freescale App Note 3944, rev 1 3/26/2010
00003 // 9/3/2011 by Andrew Schamp <schamp@gmail.com>
00004 //
00005 // This I2C device library is using (and submitted as a part of) Jeff Rowberg's I2Cdevlib library,
00006 // which should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
00007 //
00008 // Changelog:
00009 //     2011-09-03 - add callback support
00010 //     2011-08-20 - initial release
00011 
00012 /* ============================================
00013 I2Cdev device library code is placed under the MIT license
00014 Copyright (c) 2011 Andrew Schamp
00015 
00016 Permission is hereby granted, free of charge, to any person obtaining a copy
00017 of this software and associated documentation files (the "Software"), to deal
00018 in the Software without restriction, including without limitation the rights
00019 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00020 copies of the Software, and to permit persons to whom the Software is
00021 furnished to do so, subject to the following conditions:
00022 
00023 The above copyright notice and this permission notice shall be included in
00024 all copies or substantial portions of the Software.
00025 
00026 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00027 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00028 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00029 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00030 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00031 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00032 THE SOFTWARE.
00033 ===============================================
00034 */
00035 
00036 #ifndef _MPR121_h_
00037 #define _MPR121_h_
00038 
00039 #include <inttypes.h>
00040 
00041 // this is the 7-bit I2C address with the ADDR pin grounded
00042 #define MPR121_DEFAULT_ADDRESS 0x5A
00043 
00044 // MPR121 Registers (from data sheet)
00045 #define ELE0_ELE7_TOUCH_STATUS          0x00
00046 #define ELE8_ELE11_ELEPROX_TOUCH_STATUS 0x01
00047 
00048 #define ELE0_7_OOR_STATUS          0x02
00049 #define ELE8_11_ELEPROX_OOR_STATUS 0x03
00050 
00051 #define ELE0_FILTERED_DATA_LSB    0x04
00052 #define ELE0_FILTERED_DATA_MSB    0x05
00053 #define ELE1_FILTERED_DATA_LSB    0x06
00054 #define ELE1_FILTERED_DATA_MSB    0x07
00055 #define ELE2_FILTERED_DATA_LSB    0x08
00056 #define ELE2_FILTERED_DATA_MSB    0x09
00057 #define ELE3_FILTERED_DATA_LSB    0x0A
00058 #define ELE3_FILTERED_DATA_MSB    0x0B
00059 #define ELE4_FILTERED_DATA_LSB    0x0C
00060 #define ELE4_FILTERED_DATA_MSB    0x0D
00061 #define ELE5_FILTERED_DATA_LSB    0x0E
00062 #define ELE5_FILTERED_DATA_MSB    0x0F
00063 #define ELE6_FILTERED_DATA_LSB    0x10
00064 #define ELE6_FILTERED_DATA_MSB    0x11
00065 #define ELE7_FILTERED_DATA_LSB    0x12
00066 #define ELE7_FILTERED_DATA_MSB    0x13
00067 #define ELE8_FILTERED_DATA_LSB    0x14
00068 #define ELE8_FILTERED_DATA_MSB    0x15
00069 #define ELE9_FILTERED_DATA_LSB    0x16
00070 #define ELE9_FILTERED_DATA_MSB    0x17
00071 #define ELE10_FILTERED_DATA_LSB   0x18
00072 #define ELE10_FILTERED_DATA_MSB   0x19
00073 #define ELE11_FILTERED_DATA_LSB   0x1A
00074 #define ELE11_FILTERED_DATA_MSB   0x1B
00075 #define ELEPROX_FILTERED_DATA_LSB 0x1C
00076 #define ELEPROX_FILTERED_DATA_MSB 0x1D
00077 
00078 #define ELE0_BASELINE_VALUE    0x1E
00079 #define ELE1_BASELINE_VALUE    0x1F
00080 #define ELE2_BASELINE_VALUE    0x20
00081 #define ELE3_BASELINE_VALUE    0x21
00082 #define ELE4_BASELINE_VALUE    0x22
00083 #define ELE5_BASELINE_VALUE    0x23
00084 #define ELE6_BASELINE_VALUE    0x24
00085 #define ELE7_BASELINE_VALUE    0x25
00086 #define ELE8_BASELINE_VALUE    0x26
00087 #define ELE9_BASELINE_VALUE    0x27
00088 #define ELE10_BASELINE_VALUE   0x28
00089 #define ELE11_BASELINE_VALUE   0x29
00090 #define ELEPROX_BASELINE_VALUE 0x2A
00091 
00092 #define MHD_RISING                 0x2B
00093 #define NHD_AMOUNT_RISING          0x2C
00094 #define NCL_RISING                 0x2D
00095 #define FDL_RISING                 0x2E
00096 #define MHD_FALLING                0x2F
00097 #define NHD_AMOUNT_FALLING         0x30
00098 #define NCL_FALLING                0x31
00099 #define FDL_FALLING                0x32
00100 #define NHD_AMOUNT_TOUCHED         0x33
00101 #define NCL_TOUCHED                0x34
00102 #define FDL_TOUCHED                0x35
00103 #define ELEPROX_MHD_RISING         0x36
00104 #define ELEPROX_NHD_AMOUNT_RISING  0x37
00105 #define ELEPROX_NCL_RISING         0x38
00106 #define ELEPROX_FDL_RISING         0x39
00107 #define ELEPROX_MHD_FALLING        0x3A
00108 #define ELEPROX_NHD_AMOUNT_FALLING 0x3B
00109 #define ELEPROX_FDL_FALLING        0x3C
00110 #define ELEPROX_NHD_AMOUNT_TOUCHED 0x3E
00111 #define ELEPROX_NCL_TOUCHED        0x3F
00112 #define ELEPROX_FDL_TOUCHED        0x40
00113 
00114 #define ELE0_TOUCH_THRESHOLD       0x41
00115 #define ELE0_RELEASE_THRESHOLD     0x42
00116 #define ELE1_TOUCH_THRESHOLD       0x43
00117 #define ELE1_RELEASE_THRESHOLD     0x44
00118 #define ELE2_TOUCH_THRESHOLD       0x45
00119 #define ELE2_RELEASE_THRESHOLD     0x46
00120 #define ELE3_TOUCH_THRESHOLD       0x47
00121 #define ELE3_RELEASE_THRESHOLD     0x48
00122 #define ELE4_TOUCH_THRESHOLD       0x49
00123 #define ELE4_RELEASE_THRESHOLD     0x4A
00124 #define ELE5_TOUCH_THRESHOLD       0x4B
00125 #define ELE5_RELEASE_THRESHOLD     0x4C
00126 #define ELE6_TOUCH_THRESHOLD       0x4D
00127 #define ELE6_RELEASE_THRESHOLD     0x4E
00128 #define ELE7_TOUCH_THRESHOLD       0x4F
00129 #define ELE7_RELEASE_THRESHOLD     0x50
00130 #define ELE8_TOUCH_THRESHOLD       0x51
00131 #define ELE8_RELEASE_THRESHOLD     0x52
00132 #define ELE9_TOUCH_THRESHOLD       0x53
00133 #define ELE9_RELEASE_THRESHOLD     0x54
00134 #define ELE10_TOUCH_THRESHOLD      0x55
00135 #define ELE10_RELEASE_THRESHOLD    0x56
00136 #define ELE11_TOUCH_THRESHOLD      0x57
00137 #define ELE11_RELEASE_THRESHOLD    0x58
00138 #define ELEPROX_TOUCH_THRESHOLD    0x59
00139 #define ELEPROX_RELEASE_THRESHOLD  0x5A
00140 #define DEBOUNCE_TOUCH_AND_RELEASE 0x5B
00141 #define AFE_CONFIGURATION          0x5C
00142 
00143 #define FILTER_CONFIG    0x5D
00144 #define ELECTRODE_CONFIG 0x5E
00145 #define ELE0_CURRENT     0x5F
00146 #define ELE1_CURRENT     0x60
00147 #define ELE2_CURRENT     0x61
00148 #define ELE3_CURRENT     0x62
00149 #define ELE4_CURRENT     0x63
00150 #define ELE5_CURRENT     0x64
00151 #define ELE6_CURRENT     0x65
00152 #define ELE7_CURRENT     0x66
00153 #define ELE8_CURRENT     0x67
00154 #define ELE9_CURRENT     0x68
00155 #define ELE10_CURRENT    0x69
00156 #define ELE11_CURRENT    0x6A
00157 #define ELEPROX_CURRENT  0x6B
00158 
00159 #define ELE0_ELE1_CHARGE_TIME   0x6C
00160 #define ELE2_ELE3_CHARGE_TIME   0x6D
00161 #define ELE4_ELE5_CHARGE_TIME   0x6E
00162 #define ELE6_ELE7_CHARGE_TIME   0x6F
00163 #define ELE8_ELE9_CHARGE_TIME   0x70
00164 #define ELE10_ELE11_CHARGE_TIME 0x71
00165 #define ELEPROX_CHARGE_TIME     0x72
00166 
00167 #define GPIO_CONTROL_0           0x73
00168 #define GPIO_CONTROL_1           0x74
00169 #define GPIO_DATA                0x75
00170 #define GPIO_DIRECTION           0x76
00171 #define GPIO_ENABLE              0x77
00172 #define GPIO_SET                 0x78
00173 #define GPIO_CLEAR               0x79
00174 #define GPIO_TOGGLE              0x7A
00175 #define AUTO_CONFIG_CONTROL_0    0x7B
00176 #define AUTO_CONFIG_CONTROL_1    0x7C
00177 #define AUTO_CONFIG_USL          0x7D
00178 #define AUTO_CONFIG_LSL          0x7E
00179 #define AUTO_CONFIG_TARGET_LEVEL 0x7F
00180 
00181 // Other Constants
00182 // these are suggested values from app note 3944
00183 #define TOUCH_THRESHOLD   0x0F
00184 #define RELEASE_THRESHOLD 0x0A
00185 #define NUM_CHANNELS      12
00186 
00187 class MPR121
00188 {
00189   public:
00190     enum EventType {
00191       TOUCHED    = 0,
00192       RELEASED   = 1,
00193       NUM_EVENTS = 2
00194     };
00195   
00196     typedef void (*CallbackPtrType)(void);
00197     
00198     // constructor
00199     // takes a 7-b I2C address to use (0x5A by default, assumes addr pin grounded)
00200     MPR121(uint8_t address = MPR121_DEFAULT_ADDRESS);
00201 
00202     // write the configuration registers in accordance with the datasheet and app note 3944
00203     void initialize();
00204     
00205     // returns true if the device is responding on the I2C bus
00206     bool testConnection();
00207 
00208     // getTouchStatus returns the touch status for the given channel (0 - 11)
00209     bool getTouchStatus(uint8_t channel);
00210     // when not given a channel, returns a bitfield of all touch channels.
00211     uint16_t getTouchStatus();
00212 
00213     void setCallback(uint8_t channel, EventType event, CallbackPtrType callbackPtr);
00214     
00215     void serviceCallbacks();
00216     
00217   private:
00218     uint8_t m_devAddr; // contains the I2C address of the device
00219     CallbackPtrType m_callbackMap[NUM_CHANNELS][NUM_EVENTS];
00220     bool m_prevTouchStatus[NUM_CHANNELS];
00221     
00222 };
00223 
00224 #endif
00225 
 All Data Structures Functions Variables