A sample library to get the MPR121-based touch board from SparkFun up and running. This code is based on the sample Arduino code from SparkFun but is ported to C++ for mbed. The Mbed will require 4.7K pull-up resistors on the i2c SCL and SDA lines to communicate with the board. The IRQ line does not. The example code has the SDA on P28, SCL on P27 and IRQ on P26

Dependencies:   mbed

Committer:
abuckton
Date:
Mon Feb 28 12:20:18 2011 +0000
Revision:
1:d1837531c318
Parent:
0:2e5b82508aea
Cleaned up copyright, attributed original source

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abuckton 0:2e5b82508aea 1 /*
abuckton 1:d1837531c318 2 Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)
abuckton 1:d1837531c318 3
abuckton 1:d1837531c318 4
abuckton 1:d1837531c318 5 Permission is hereby granted, free of charge, to any person obtaining a copy
abuckton 1:d1837531c318 6 of this software and associated documentation files (the "Software"), to deal
abuckton 1:d1837531c318 7 in the Software without restriction, including without limitation the rights
abuckton 1:d1837531c318 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
abuckton 1:d1837531c318 9 copies of the Software, and to permit persons to whom the Software is
abuckton 1:d1837531c318 10 furnished to do so, subject to the following conditions:
abuckton 1:d1837531c318 11
abuckton 1:d1837531c318 12 The above copyright notice and this permission notice shall be included in
abuckton 1:d1837531c318 13 all copies or substantial portions of the Software.
abuckton 1:d1837531c318 14
abuckton 1:d1837531c318 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
abuckton 1:d1837531c318 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
abuckton 1:d1837531c318 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
abuckton 1:d1837531c318 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
abuckton 1:d1837531c318 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
abuckton 1:d1837531c318 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
abuckton 1:d1837531c318 21 THE SOFTWARE.
abuckton 1:d1837531c318 22
abuckton 1:d1837531c318 23 Parts written by Jim Lindblom of Sparkfun
abuckton 1:d1837531c318 24 Ported to mbed by A.Buckton, Feb 2011
abuckton 0:2e5b82508aea 25 */
abuckton 0:2e5b82508aea 26
abuckton 0:2e5b82508aea 27 #ifndef MPR121_H
abuckton 0:2e5b82508aea 28 #define MPR121_H
abuckton 0:2e5b82508aea 29
abuckton 0:2e5b82508aea 30 //using namespace std;
abuckton 0:2e5b82508aea 31
abuckton 0:2e5b82508aea 32 class Mpr121
abuckton 0:2e5b82508aea 33 {
abuckton 0:2e5b82508aea 34
abuckton 0:2e5b82508aea 35 public:
abuckton 0:2e5b82508aea 36 // i2c Addresses, bit-shifted
abuckton 0:2e5b82508aea 37 enum Address { ADD_VSS = 0xb4,// ADD->VSS = 0x5a <-wiring on Sparkfun board
abuckton 0:2e5b82508aea 38 ADD_VDD = 0xb6,// ADD->VDD = 0x5b
abuckton 0:2e5b82508aea 39 ADD_SCL = 0xb8,// ADD->SDA = 0x5c
abuckton 0:2e5b82508aea 40 ADD_SDA = 0xba // ADD->SCL = 0x5d
abuckton 0:2e5b82508aea 41 };
abuckton 0:2e5b82508aea 42
abuckton 0:2e5b82508aea 43 // Real initialiser, takes the i2c address of the device.
abuckton 0:2e5b82508aea 44 Mpr121(I2C *i2c, Address i2cAddress);
abuckton 0:2e5b82508aea 45
abuckton 0:2e5b82508aea 46 bool getProximityMode();
abuckton 0:2e5b82508aea 47
abuckton 0:2e5b82508aea 48 void setProximityMode(bool mode);
abuckton 0:2e5b82508aea 49
abuckton 0:2e5b82508aea 50 int readTouchData();
abuckton 0:2e5b82508aea 51
abuckton 0:2e5b82508aea 52 unsigned char read(int key);
abuckton 0:2e5b82508aea 53
abuckton 0:2e5b82508aea 54 int write(int address, unsigned char value);
abuckton 0:2e5b82508aea 55 int writeMany(int start, unsigned char* dataSet, int length);
abuckton 0:2e5b82508aea 56
abuckton 0:2e5b82508aea 57 void setElectrodeThreshold(int electrodeId, unsigned char touchThreshold, unsigned char releaseThreshold);
abuckton 0:2e5b82508aea 58
abuckton 0:2e5b82508aea 59 protected:
abuckton 0:2e5b82508aea 60 // Configures the MPR with standard settings. This is permitted to be overwritten by sub-classes.
abuckton 0:2e5b82508aea 61 void configureSettings();
abuckton 0:2e5b82508aea 62
abuckton 0:2e5b82508aea 63 private:
abuckton 0:2e5b82508aea 64 // The I2C bus instance.
abuckton 0:2e5b82508aea 65 I2C *i2c;
abuckton 0:2e5b82508aea 66
abuckton 0:2e5b82508aea 67 // i2c address of this mpr121
abuckton 0:2e5b82508aea 68 Address address;
abuckton 0:2e5b82508aea 69 };
abuckton 0:2e5b82508aea 70
abuckton 0:2e5b82508aea 71
abuckton 0:2e5b82508aea 72 // MPR121 Register Defines
abuckton 0:2e5b82508aea 73 #define MHD_R 0x2B
abuckton 0:2e5b82508aea 74 #define NHD_R 0x2C
abuckton 0:2e5b82508aea 75 #define NCL_R 0x2D
abuckton 0:2e5b82508aea 76 #define FDL_R 0x2E
abuckton 0:2e5b82508aea 77 #define MHD_F 0x2F
abuckton 0:2e5b82508aea 78 #define NHD_F 0x30
abuckton 0:2e5b82508aea 79 #define NCL_F 0x31
abuckton 0:2e5b82508aea 80 #define FDL_F 0x32
abuckton 0:2e5b82508aea 81 #define NHDT 0x33
abuckton 0:2e5b82508aea 82 #define NCLT 0x34
abuckton 0:2e5b82508aea 83 #define FDLT 0x35
abuckton 0:2e5b82508aea 84 // Proximity sensing controls
abuckton 0:2e5b82508aea 85 #define MHDPROXR 0x36
abuckton 0:2e5b82508aea 86 #define NHDPROXR 0x37
abuckton 0:2e5b82508aea 87 #define NCLPROXR 0x38
abuckton 0:2e5b82508aea 88 #define FDLPROXR 0x39
abuckton 0:2e5b82508aea 89 #define MHDPROXF 0x3A
abuckton 0:2e5b82508aea 90 #define NHDPROXF 0x3B
abuckton 0:2e5b82508aea 91 #define NCLPROXF 0x3C
abuckton 0:2e5b82508aea 92 #define FDLPROXF 0x3D
abuckton 0:2e5b82508aea 93 #define NHDPROXT 0x3E
abuckton 0:2e5b82508aea 94 #define NCLPROXT 0x3F
abuckton 0:2e5b82508aea 95 #define FDLPROXT 0x40
abuckton 0:2e5b82508aea 96 // Electrode Touch/Release thresholds
abuckton 0:2e5b82508aea 97 #define ELE0_T 0x41
abuckton 0:2e5b82508aea 98 #define ELE0_R 0x42
abuckton 0:2e5b82508aea 99 #define ELE1_T 0x43
abuckton 0:2e5b82508aea 100 #define ELE1_R 0x44
abuckton 0:2e5b82508aea 101 #define ELE2_T 0x45
abuckton 0:2e5b82508aea 102 #define ELE2_R 0x46
abuckton 0:2e5b82508aea 103 #define ELE3_T 0x47
abuckton 0:2e5b82508aea 104 #define ELE3_R 0x48
abuckton 0:2e5b82508aea 105 #define ELE4_T 0x49
abuckton 0:2e5b82508aea 106 #define ELE4_R 0x4A
abuckton 0:2e5b82508aea 107 #define ELE5_T 0x4B
abuckton 0:2e5b82508aea 108 #define ELE5_R 0x4C
abuckton 0:2e5b82508aea 109 #define ELE6_T 0x4D
abuckton 0:2e5b82508aea 110 #define ELE6_R 0x4E
abuckton 0:2e5b82508aea 111 #define ELE7_T 0x4F
abuckton 0:2e5b82508aea 112 #define ELE7_R 0x50
abuckton 0:2e5b82508aea 113 #define ELE8_T 0x51
abuckton 0:2e5b82508aea 114 #define ELE8_R 0x52
abuckton 0:2e5b82508aea 115 #define ELE9_T 0x53
abuckton 0:2e5b82508aea 116 #define ELE9_R 0x54
abuckton 0:2e5b82508aea 117 #define ELE10_T 0x55
abuckton 0:2e5b82508aea 118 #define ELE10_R 0x56
abuckton 0:2e5b82508aea 119 #define ELE11_T 0x57
abuckton 0:2e5b82508aea 120 #define ELE11_R 0x58
abuckton 0:2e5b82508aea 121 // Proximity Touch/Release thresholds
abuckton 0:2e5b82508aea 122 #define EPROXTTH 0x59
abuckton 0:2e5b82508aea 123 #define EPROXRTH 0x5A
abuckton 0:2e5b82508aea 124 // Debounce configuration
abuckton 0:2e5b82508aea 125 #define DEB_CFG 0x5B
abuckton 0:2e5b82508aea 126 // AFE- Analogue Front End configuration
abuckton 0:2e5b82508aea 127 #define AFE_CFG 0x5C
abuckton 0:2e5b82508aea 128 // Filter configuration
abuckton 0:2e5b82508aea 129 #define FIL_CFG 0x5D
abuckton 0:2e5b82508aea 130 // Electrode configuration - transistions to "active mode"
abuckton 0:2e5b82508aea 131 #define ELE_CFG 0x5E
abuckton 0:2e5b82508aea 132
abuckton 0:2e5b82508aea 133 #define GPIO_CTRL0 0x73
abuckton 0:2e5b82508aea 134 #define GPIO_CTRL1 0x74
abuckton 0:2e5b82508aea 135 #define GPIO_DATA 0x75
abuckton 0:2e5b82508aea 136 #define GPIO_DIR 0x76
abuckton 0:2e5b82508aea 137 #define GPIO_EN 0x77
abuckton 0:2e5b82508aea 138 #define GPIO_SET 0x78
abuckton 0:2e5b82508aea 139 #define GPIO_CLEAR 0x79
abuckton 0:2e5b82508aea 140 #define GPIO_TOGGLE 0x7A
abuckton 0:2e5b82508aea 141 // Auto configration registers
abuckton 0:2e5b82508aea 142 #define AUTO_CFG_0 0x7B
abuckton 0:2e5b82508aea 143 #define AUTO_CFG_U 0x7D
abuckton 0:2e5b82508aea 144 #define AUTO_CFG_L 0x7E
abuckton 0:2e5b82508aea 145 #define AUTO_CFG_T 0x7F
abuckton 0:2e5b82508aea 146
abuckton 0:2e5b82508aea 147 // Threshold defaults
abuckton 0:2e5b82508aea 148 // Electrode touch threshold
abuckton 0:2e5b82508aea 149 #define E_THR_T 0x0F
abuckton 0:2e5b82508aea 150 // Electrode release threshold
abuckton 0:2e5b82508aea 151 #define E_THR_R 0x0A
abuckton 0:2e5b82508aea 152 // Prox touch threshold
abuckton 0:2e5b82508aea 153 #define PROX_THR_T 0x02
abuckton 0:2e5b82508aea 154 // Prox release threshold
abuckton 0:2e5b82508aea 155 #define PROX_THR_R 0x02
abuckton 0:2e5b82508aea 156
abuckton 0:2e5b82508aea 157 #endif