use mbed os

Dependents:   Seeed_Grove_I2C_Touch_Example

Fork of MPR121 by Sam Grove

Committer:
tulanthoar
Date:
Thu Jun 29 15:56:48 2017 +0000
Revision:
8:870059a44bfc
Parent:
5:3934358ec2b7
Child:
9:8cb5ce483be3
Child:
10:fb2d2454fea4
Child:
13:185ada085785
use mbed-os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 0:42add775212a 1 /**
sam_grove 0:42add775212a 2 * @file MPR121.h
sam_grove 0:42add775212a 3 * @brief Device driver - MPR121 capactiive touch IC
sam_grove 0:42add775212a 4 * @author sam grove
sam_grove 0:42add775212a 5 * @version 1.0
sam_grove 0:42add775212a 6 * @see http://cache.freescale.com/files/sensors/doc/data_sheet/MPR121.pdf
sam_grove 0:42add775212a 7 *
sam_grove 0:42add775212a 8 * Copyright (c) 2013
sam_grove 0:42add775212a 9 *
sam_grove 0:42add775212a 10 * Licensed under the Apache License, Version 2.0 (the "License");
sam_grove 0:42add775212a 11 * you may not use this file except in compliance with the License.
sam_grove 0:42add775212a 12 * You may obtain a copy of the License at
sam_grove 0:42add775212a 13 *
sam_grove 0:42add775212a 14 * http://www.apache.org/licenses/LICENSE-2.0
sam_grove 0:42add775212a 15 *
sam_grove 0:42add775212a 16 * Unless required by applicable law or agreed to in writing, software
sam_grove 0:42add775212a 17 * distributed under the License is distributed on an "AS IS" BASIS,
sam_grove 0:42add775212a 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sam_grove 0:42add775212a 19 * See the License for the specific language governing permissions and
sam_grove 0:42add775212a 20 * limitations under the License.
sam_grove 0:42add775212a 21 */
sam_grove 0:42add775212a 22
sam_grove 0:42add775212a 23 #ifndef MPR121_H
sam_grove 0:42add775212a 24 #define MPR121_H
sam_grove 0:42add775212a 25
sam_grove 0:42add775212a 26 #include "mbed.h"
tulanthoar 8:870059a44bfc 27 #include "rtos.h"
sam_grove 0:42add775212a 28
sam_grove 3:828260f21de6 29 /** Using the Sparkfun SEN-10250 BoB
sam_grove 0:42add775212a 30 *
sam_grove 0:42add775212a 31 * Example:
sam_grove 0:42add775212a 32 * @code
sam_grove 3:828260f21de6 33 * #include "mbed.h"
sam_grove 3:828260f21de6 34 * #include "MPR121.h"
sam_grove 3:828260f21de6 35 *
sam_grove 3:828260f21de6 36 * Serial pc(USBTX, USBRX);
sam_grove 3:828260f21de6 37 * DigitalOut myled(LED1);
sam_grove 3:828260f21de6 38 *
sam_grove 3:828260f21de6 39 * #if defined TARGET_LPC1768 || TARGET_LPC11U24
sam_grove 3:828260f21de6 40 * I2C i2c(p28, p27);
sam_grove 3:828260f21de6 41 * InterruptIn irq(p26);
sam_grove 3:828260f21de6 42 * MPR121 touch_pad(i2c, irq, MPR121::ADDR_VSS);
sam_grove 3:828260f21de6 43 *
sam_grove 3:828260f21de6 44 * #elif defined TARGET_KL25Z
sam_grove 3:828260f21de6 45 * I2C i2c(PTC9, PTC8);
sam_grove 3:828260f21de6 46 * InterruptIn irq(PTA5);
sam_grove 3:828260f21de6 47 * MPR121 touch_pad(i2c, irq, MPR121::ADDR_VSS);
sam_grove 3:828260f21de6 48 *
sam_grove 3:828260f21de6 49 * #else
sam_grove 3:828260f21de6 50 * #error TARGET NOT TESTED
sam_grove 3:828260f21de6 51 * #endif
sam_grove 3:828260f21de6 52 *
sam_grove 3:828260f21de6 53 * int main()
sam_grove 3:828260f21de6 54 * {
sam_grove 3:828260f21de6 55 * touch_pad.init();
sam_grove 3:828260f21de6 56 * touch_pad.enable();
sam_grove 3:828260f21de6 57 *
sam_grove 3:828260f21de6 58 * while(1)
sam_grove 3:828260f21de6 59 * {
sam_grove 3:828260f21de6 60 * if(touch_pad.isPressed())
sam_grove 3:828260f21de6 61 * {
sam_grove 3:828260f21de6 62 * uint16_t button_val = touch_pad.buttonPressed();
sam_grove 3:828260f21de6 63 * printf("button = 0x%04x\n", button_val);
sam_grove 3:828260f21de6 64 * myled = (button_val>0) ? 1 : 0;
sam_grove 3:828260f21de6 65 * }
sam_grove 3:828260f21de6 66 * }
sam_grove 3:828260f21de6 67 * }
sam_grove 0:42add775212a 68 * @endcode
sam_grove 0:42add775212a 69 */
sam_grove 0:42add775212a 70
sam_grove 0:42add775212a 71 /**
sam_grove 0:42add775212a 72 * @class MPR121
sam_grove 3:828260f21de6 73 * @brief API for the MPR121 capacitive touch IC
sam_grove 0:42add775212a 74 */
sam_grove 0:42add775212a 75 class MPR121
sam_grove 0:42add775212a 76 {
sam_grove 0:42add775212a 77 private:
sam_grove 0:42add775212a 78
sam_grove 0:42add775212a 79 I2C *_i2c;
sam_grove 0:42add775212a 80 InterruptIn *_irq;
sam_grove 0:42add775212a 81 uint8_t _i2c_addr;
sam_grove 0:42add775212a 82 volatile uint16_t _button;
sam_grove 0:42add775212a 83 volatile uint32_t _button_has_changed;
sam_grove 0:42add775212a 84
sam_grove 0:42add775212a 85 /** The interrupt handler for the IRQ pin
sam_grove 0:42add775212a 86 */
sam_grove 0:42add775212a 87 void handler(void);
sam_grove 0:42add775212a 88
sam_grove 0:42add775212a 89 public:
sam_grove 0:42add775212a 90
sam_grove 0:42add775212a 91 /**
sam_grove 0:42add775212a 92 * @enum MPR121_ADDR
sam_grove 0:42add775212a 93 * @brief Possible terminations for the ADDR pin
sam_grove 0:42add775212a 94 */
sam_grove 0:42add775212a 95 enum MPR121_ADDR
sam_grove 0:42add775212a 96 {
sam_grove 0:42add775212a 97 ADDR_VSS = 0x5A, /*!< ADDR connected to VSS */
sam_grove 0:42add775212a 98 ADDR_VDD, /*!< ADDR connected to VDD */
sam_grove 0:42add775212a 99 ADDR_SCL, /*!< ADDR connected to SDA */
sam_grove 0:42add775212a 100 ADDR_SDA /*!< ADDR connected to SCL */
sam_grove 0:42add775212a 101 };
sam_grove 0:42add775212a 102
sam_grove 0:42add775212a 103 /**
sam_grove 0:42add775212a 104 * @enum MPR121_REGISTER
sam_grove 0:42add775212a 105 * @brief The device register map
sam_grove 0:42add775212a 106 */
sam_grove 0:42add775212a 107 enum MPR121_REGISTER
sam_grove 0:42add775212a 108 {
sam_grove 0:42add775212a 109 ELE0_7_STAT = 0x00,
sam_grove 0:42add775212a 110 ELE8_11_STAT, ELE0_7_OOR_STAT, ELE8_11_OOR_STAT, EFD0LB, EFD0HB,
sam_grove 0:42add775212a 111 EFD1LB, EFD1HB, EFD2LB, EFD2HB, EFD3LB, EFD3HB, EFD4LB, EFD4HB, EFD5LB, EFD5HB,
sam_grove 0:42add775212a 112
sam_grove 0:42add775212a 113 EFD6LB = 0x10,
sam_grove 0:42add775212a 114 EFD6HB, EFD7LB, EFD7HB, EFD8LB, EFD8HB, EFD9LB, EFD9HB, EFD10LB,
sam_grove 0:42add775212a 115 EFD10HB, EFD11LB, EFD11HB, EFDPROXLB, EFDPROXHB, E0BV, E1BV,
sam_grove 0:42add775212a 116
sam_grove 0:42add775212a 117 E2BV = 0x20,
sam_grove 0:42add775212a 118 E3BV, E4BV, E5BV, E6BV, E7BV, E8BV, E9BV, E10BV, E11BV, EPROXBV,
sam_grove 0:42add775212a 119 MHDR, NHDR, NCLR, FDLR, MHDF,
sam_grove 0:42add775212a 120
sam_grove 0:42add775212a 121 NHDF = 0x30,
sam_grove 0:42add775212a 122 NCLF, FDLF, NHDT, NCLT, FDLT, MHDPROXR, NHDPROXR, NCLPROXR,
sam_grove 0:42add775212a 123 FDLPROXR, MHDPROXF, NHDPROXF, NCLPROXF, FDLPROXF, NHDPROXT, NCLPROXT,
sam_grove 0:42add775212a 124
sam_grove 0:42add775212a 125 FDLPROXT = 0x40,
sam_grove 0:42add775212a 126 E0TTH, E0RTH, E1TTH, E1RTH, E2TTH, E2RTH, E3TTH, E3RTH,
sam_grove 0:42add775212a 127 E4TTH, E4RTH, E5TTH, E5RTH, E6TTH, E6RTH, E7TTH,
sam_grove 0:42add775212a 128
sam_grove 0:42add775212a 129 E7RTH = 0x50,
sam_grove 0:42add775212a 130 E8TTH, E8RTH, E9TTH, E9RTH, E10TTH, E10RTH, E11TTH, E11RTH,
sam_grove 0:42add775212a 131 EPROXTTH, EPROXRTH, DT_DR, CDC_CONFIG, CDT_CONFIG, ECR, CDC0,
sam_grove 0:42add775212a 132
sam_grove 0:42add775212a 133 CDC1 = 0x60,
sam_grove 0:42add775212a 134 CDC2, CDC3, CDC4, CDC5, CDC6, CDC7, CDC8, CDC9, CDC10, CDC11, CDCPROX, CDT0_CDT1,
sam_grove 0:42add775212a 135 CDT2_CDT3, CDT4_CDT5, CDT6_CDT7,
sam_grove 0:42add775212a 136
sam_grove 0:42add775212a 137 CDT8_CDT9 = 0x70,
sam_grove 0:42add775212a 138 CDT10_CDT11, CDTPROX, GPIO_CTRL0, GPIO_CTRL1, GPIO_DATA, GPIO_DIR, GPIO_EN, GPIO_SET,
sam_grove 0:42add775212a 139 GPIO_CLR, GPIO_TOGGLE, AUTO_CFG0, AUTO_CFG1, USL, LSL, TL,
sam_grove 0:42add775212a 140
sam_grove 0:42add775212a 141 SRST = 0x80
sam_grove 0:42add775212a 142 };
sam_grove 0:42add775212a 143
sam_grove 0:42add775212a 144 /** Create the MPR121 object
sam_grove 1:cee45334b36a 145 * @param i2c - A defined I2C object
sam_grove 1:cee45334b36a 146 * @param pin - A defined InterruptIn object
sam_grove 1:cee45334b36a 147 * @param i2c_addr - Connection of the address line
sam_grove 0:42add775212a 148 */
sam_grove 0:42add775212a 149 MPR121(I2C &i2c, InterruptIn &pin, MPR121_ADDR i2c_addr);
sam_grove 0:42add775212a 150
sam_grove 5:3934358ec2b7 151 /** Create the MPR121 object
sam_grove 5:3934358ec2b7 152 * @param i2c - A defined I2C object
sam_grove 5:3934358ec2b7 153 * @param i2c_addr - Connection of the address line
sam_grove 5:3934358ec2b7 154 */
sam_grove 5:3934358ec2b7 155 MPR121(I2C &i2c, MPR121_ADDR i2c_addr);
sam_grove 5:3934358ec2b7 156
sam_grove 3:828260f21de6 157 /** Clear state variables and initilize the dependant objects
sam_grove 0:42add775212a 158 */
sam_grove 0:42add775212a 159 void init(void);
sam_grove 0:42add775212a 160
sam_grove 0:42add775212a 161 /** Allow the IC to run and collect user input
sam_grove 0:42add775212a 162 */
sam_grove 0:42add775212a 163 void enable(void);
sam_grove 0:42add775212a 164
sam_grove 0:42add775212a 165 /** Stop the IC and put into low power mode
sam_grove 0:42add775212a 166 */
sam_grove 0:42add775212a 167 void disable(void);
sam_grove 0:42add775212a 168
sam_grove 0:42add775212a 169 /** Determine if a new button press event occured
sam_grove 0:42add775212a 170 * Upon calling the state is cleared until another press is detected
sam_grove 0:42add775212a 171 * @return 1 if a press has been detected since the last call, 0 otherwise
sam_grove 0:42add775212a 172 */
sam_grove 0:42add775212a 173 uint32_t isPressed(void);
sam_grove 0:42add775212a 174
sam_grove 0:42add775212a 175 /** Get the electrode status (ELE12 ... ELE0 -> b15 xxx b11 ... b0
sam_grove 0:42add775212a 176 * The buttons are bit mapped. ELE0 = b0 ... ELE11 = b11 b12 ... b15 undefined
sam_grove 0:42add775212a 177 * @return The state of all buttons
sam_grove 0:42add775212a 178 */
sam_grove 0:42add775212a 179 uint16_t buttonPressed(void);
sam_grove 0:42add775212a 180
sam_grove 0:42add775212a 181 /** print the register map and values to the console
sam_grove 3:828260f21de6 182 * @param obj - a Serial object that prints to a console
sam_grove 0:42add775212a 183 */
sam_grove 3:828260f21de6 184 void registerDump(Serial &obj) const;
sam_grove 0:42add775212a 185
sam_grove 5:3934358ec2b7 186 /** print the register map and values to the console
sam_grove 5:3934358ec2b7 187 */
sam_grove 5:3934358ec2b7 188 void registerDump(void) const;
sam_grove 5:3934358ec2b7 189
sam_grove 0:42add775212a 190 /** Write to a register (exposed for debugging reasons)
sam_grove 0:42add775212a 191 * Note: most writes are only valid in stop mode
sam_grove 1:cee45334b36a 192 * @param reg - The register to be written
sam_grove 1:cee45334b36a 193 * @param data - The data to be written
sam_grove 0:42add775212a 194 */
sam_grove 3:828260f21de6 195 void writeRegister(MPR121_REGISTER const reg, uint8_t const data) const;
sam_grove 0:42add775212a 196
sam_grove 0:42add775212a 197 /** Read from a register (exposed for debugging reasons)
sam_grove 1:cee45334b36a 198 * @param reg - The register to read from
sam_grove 0:42add775212a 199 * @return The register contents
sam_grove 0:42add775212a 200 */
sam_grove 3:828260f21de6 201 uint8_t readRegister(MPR121_REGISTER const reg) const;
sam_grove 0:42add775212a 202
sam_grove 0:42add775212a 203 };
sam_grove 0:42add775212a 204
sam_grove 0:42add775212a 205 #endif