Start of a microbit mpr121 library

Dependents:   microbitmpr121-example

Committer:
owenbrotherwood
Date:
Mon Jan 16 08:46:27 2017 +0000
Revision:
0:fb4572fc4901
Child:
1:f6fed00a3ff2
Start

Who changed what in which revision?

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