1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 #include <mraa/i2c.hpp>
28 
29 #define MPR121_I2C_BUS     0
30 #define MPR121_DEFAULT_I2C_ADDR    0x5a
31 
32 namespace upm {
33   /**
34    * @brief MPR121 Touch Sensor library
35    * @defgroup mpr121 libupm-mpr121
36    * @ingroup seeed i2c touch tsk
37    */
38   /**
39    * @library mpr121
40    * @sensor mpr121
41    * @comname MPR121 Touch Sensor
42    * @altname Grove I2C Touch Sensor
43    * @type touch
44    * @man seeed
45    * @web http://www.seeedstudio.com/wiki/Grove_-_I2C_Touch_Sensor
46    * @con i2c
47    * @kit tsk
48    *
49    * @brief API for the MPR121 I2C Touch Sensor
50    *
51    * UPM module for the MPR121 touch sensor
52    *
53    * @image html mpr121.jpg
54    * @snippet mpr121.cxx Interesting
55    */
56   class MPR121 {
57   public:
58     /**
59      * MPR121 constructor
60      *
61      * @param bus I2C bus to use
62      */
63     MPR121(int bus, uint8_t address = MPR121_DEFAULT_I2C_ADDR);
64 
65     /**
66      * MPR121 destructor
67      * ~MPR121();
68      * there is no need for the destructor
69      **/
70 
71     /**
72      * Sets up a default configuration, based on Application Note 3944
73      * (AN3944):
74      * http://cache.freescale.com/files/sensors/doc/app_note/AN3944.pdf
75      *
76      * After configuration, the sensor is left in the run state.
77      *
78      * @return True if configuration is successful
79      */
80     bool configAN3944();
81 
82     /**
83      * Reads button states in the m_buttonStates member variable. Also,
84      * sets the m_overCurrentFault variable if overcurrent is detected.
85      */
86     void readButtons();
87 
88     /**
89      * Writes value(s) into registers
90      *
91      * @param reg Register location to start writing into
92      * @param buffer Buffer for data storage
93      * @param len Number of bytes to write
94      * @return mraa::Result
95      */
96     mraa::Result writeBytes(uint8_t reg, uint8_t *buffer, int len);
97 
98     /**
99      * Reads value(s) from registers
100      *
101      * @param reg Register location to start reading from
102      * @param buffer Buffer for data storage
103      * @param len Number of bytes to read
104      */
105     int readBytes(uint8_t reg, uint8_t *buffer, int len);
106 
107     /**
108      * Button states
109      */
110     uint16_t m_buttonStates;
111 
112     /**
113      * Overcurrent detected
114      */
115     bool m_overCurrentFault;
116 
117   private:
118     mraa::I2c m_i2c;
119     uint8_t m_addr;
120   };
121 }
122 
123 
124