|
@@ -0,0 +1,204 @@
|
|
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
|
+
|
|
25
|
+#include <iostream>
|
|
26
|
+#include <string>
|
|
27
|
+
|
|
28
|
+#include "mpr121.h"
|
|
29
|
+
|
|
30
|
+using namespace upm;
|
|
31
|
+using namespace std;
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+MPR121::MPR121(int bus, uint8_t address)
|
|
35
|
+{
|
|
36
|
+ // setup our i2c link
|
|
37
|
+ m_i2c = mraa_i2c_init(bus);
|
|
38
|
+
|
|
39
|
+ m_addr = address;
|
|
40
|
+
|
|
41
|
+ mraa_result_t ret = mraa_i2c_address(m_i2c, m_addr);
|
|
42
|
+
|
|
43
|
+ if (ret != MRAA_SUCCESS)
|
|
44
|
+ cerr << "MPR121: Could not initialize i2c bus. " << endl;
|
|
45
|
+
|
|
46
|
+ m_buttonStates = 0;
|
|
47
|
+ m_overCurrentFault = false;
|
|
48
|
+}
|
|
49
|
+
|
|
50
|
+MPR121::~MPR121()
|
|
51
|
+{
|
|
52
|
+ mraa_i2c_stop(m_i2c);
|
|
53
|
+}
|
|
54
|
+
|
|
55
|
+mraa_result_t MPR121::writeBytes(uint8_t reg, uint8_t *buffer, unsigned int len)
|
|
56
|
+{
|
|
57
|
+ if (!len || !buffer)
|
|
58
|
+ return MRAA_SUCCESS;
|
|
59
|
+
|
|
60
|
+ // create a buffer 1 byte larger than the supplied buffer,
|
|
61
|
+ // store the register in the first byte
|
|
62
|
+ uint8_t buf2[len + 1];
|
|
63
|
+
|
|
64
|
+ buf2[0] = reg;
|
|
65
|
+
|
|
66
|
+ // copy in the buffer after the reg byte
|
|
67
|
+ for (int i=1; i<(len + 1); i++)
|
|
68
|
+ buf2[i] = buffer[i-1];
|
|
69
|
+
|
|
70
|
+ mraa_i2c_address(m_i2c, m_addr);
|
|
71
|
+
|
|
72
|
+ return mraa_i2c_write(m_i2c, buf2, len + 1);
|
|
73
|
+}
|
|
74
|
+
|
|
75
|
+void MPR121::readBytes(uint8_t reg, uint8_t *buffer, unsigned int len)
|
|
76
|
+{
|
|
77
|
+ if (!len || !buffer)
|
|
78
|
+ return;
|
|
79
|
+
|
|
80
|
+ // The usual mraa_i2c_read() does not work here, so we need to
|
|
81
|
+ // read each byte individually.
|
|
82
|
+ for (int i=0; i<len; i++)
|
|
83
|
+ buffer[i] = mraa_i2c_read_byte_data(m_i2c, reg + i);
|
|
84
|
+
|
|
85
|
+ return;
|
|
86
|
+}
|
|
87
|
+
|
|
88
|
+bool MPR121::configAN3944()
|
|
89
|
+{
|
|
90
|
+ // Configure the mpr121 chip as recommended in the AN3944 MPR121
|
|
91
|
+ // Quick Start Guide
|
|
92
|
+
|
|
93
|
+ mraa_result_t rv;
|
|
94
|
+
|
|
95
|
+ // First, turn off all electrodes by zeroing out the Electrode Configuration
|
|
96
|
+ // register.
|
|
97
|
+ // If this one fails, it's unlikely any of the others will succeed.
|
|
98
|
+ uint8_t eleConf = 0x00;
|
|
99
|
+ if ((rv = writeBytes(0x5e, &eleConf, 1)) != MRAA_SUCCESS)
|
|
100
|
+ {
|
|
101
|
+ cerr << __FUNCTION__ << ": " << __LINE__<< ": I2C write failed." << endl;
|
|
102
|
+ return false;
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ // Section A
|
|
106
|
+ // Filtering when data is greater than baseline
|
|
107
|
+ // regs 0x2b-0x2e
|
|
108
|
+ uint8_t sectA[] = { 0x01, 0x01, 0x00, 0x00 };
|
|
109
|
+ if ((rv = writeBytes(0x2b, sectA, 4)) != MRAA_SUCCESS)
|
|
110
|
+ {
|
|
111
|
+ cerr << __FUNCTION__ << ": " << __LINE__<< ": I2C write failed." << endl;
|
|
112
|
+ return false;
|
|
113
|
+ }
|
|
114
|
+
|
|
115
|
+ // Section B
|
|
116
|
+ // Filtering when data is less than baseline
|
|
117
|
+ // regs 0x2f-0x32
|
|
118
|
+ uint8_t sectB[] = { 0x01, 0x01, 0xff, 0x02 };
|
|
119
|
+ if ((rv = writeBytes(0x2f, sectB, 4)) != MRAA_SUCCESS)
|
|
120
|
+ {
|
|
121
|
+ cerr << __FUNCTION__ << ": " << __LINE__<< ": I2C write failed." << endl;
|
|
122
|
+ return false;
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+ // Section C
|
|
126
|
+ // Touch Threshold/Release registers, ELE0-ELE11
|
|
127
|
+ // regs 0x41-0x58
|
|
128
|
+ // __T_ __R_
|
|
129
|
+ uint8_t sectC[] = { 0x0f, 0x0a,
|
|
130
|
+ 0x0f, 0x0a,
|
|
131
|
+ 0x0f, 0x0a,
|
|
132
|
+ 0x0f, 0x0a,
|
|
133
|
+ 0x0f, 0x0a,
|
|
134
|
+ 0x0f, 0x0a,
|
|
135
|
+ 0x0f, 0x0a,
|
|
136
|
+ 0x0f, 0x0a,
|
|
137
|
+ 0x0f, 0x0a,
|
|
138
|
+ 0x0f, 0x0a,
|
|
139
|
+ 0x0f, 0x0a,
|
|
140
|
+ 0x0f, 0x0a };
|
|
141
|
+ if ((rv = writeBytes(0x41, sectC, 24)) != MRAA_SUCCESS)
|
|
142
|
+ {
|
|
143
|
+ cerr << __FUNCTION__ << ": " << __LINE__<< ": I2C write failed." << endl;
|
|
144
|
+ return false;
|
|
145
|
+ }
|
|
146
|
+
|
|
147
|
+ // Section D
|
|
148
|
+ // Filter configuration
|
|
149
|
+ // reg 0x5d
|
|
150
|
+ uint8_t filterConf = 0x04;
|
|
151
|
+ if ((rv = writeBytes(0x5d, &filterConf, 1)) != MRAA_SUCCESS)
|
|
152
|
+ {
|
|
153
|
+ cerr << __FUNCTION__ << ": " << __LINE__<< ": I2C write failed." << endl;
|
|
154
|
+ return false;
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+ // Section F
|
|
158
|
+ // Autoconfiguration registers
|
|
159
|
+ // regs 0x7b-0x7f
|
|
160
|
+ uint8_t sectF0 = 0x0b;
|
|
161
|
+ if ((rv = writeBytes(0x7b, §F0, 1)) != MRAA_SUCCESS)
|
|
162
|
+ {
|
|
163
|
+ cerr << __FUNCTION__ << ": " << __LINE__<< ": I2C write failed." << endl;
|
|
164
|
+ return false;
|
|
165
|
+ }
|
|
166
|
+ uint8_t sectF1[] = { 0x9c, 0x65, 0x8c };
|
|
167
|
+ if ((rv = writeBytes(0x7d, sectF1, 3)) != MRAA_SUCCESS)
|
|
168
|
+ {
|
|
169
|
+ cerr << __FUNCTION__ << ": " << __LINE__<< ": I2C write failed." << endl;
|
|
170
|
+ return false;
|
|
171
|
+ }
|
|
172
|
+
|
|
173
|
+ // Section E - this one must be set last, and switches to run mode
|
|
174
|
+ // Enable all 12 electrodes, and set a pre-calibration to avoid
|
|
175
|
+ // excessive calibration delay on startup.
|
|
176
|
+ // reg 0x5e
|
|
177
|
+ eleConf = 0x8c;
|
|
178
|
+ if ((rv = writeBytes(0x5e, &eleConf, 1)) != MRAA_SUCCESS)
|
|
179
|
+ {
|
|
180
|
+ cerr << __FUNCTION__ << ": " << __LINE__<< ": I2C write failed." << endl;
|
|
181
|
+ return false;
|
|
182
|
+ }
|
|
183
|
+
|
|
184
|
+ return true;
|
|
185
|
+}
|
|
186
|
+
|
|
187
|
+void MPR121::readButtons()
|
|
188
|
+{
|
|
189
|
+ uint8_t rv;
|
|
190
|
+ uint8_t buffer[2];
|
|
191
|
+
|
|
192
|
+ // read in the 2 bytes at register 0x00-0x01, and setup the member
|
|
193
|
+ // variables accordingly.
|
|
194
|
+
|
|
195
|
+ readBytes(0x00, buffer, 2);
|
|
196
|
+
|
|
197
|
+ m_buttonStates = (buffer[0] | ((buffer[1] & 0x1f) << 8));
|
|
198
|
+ if (buffer[1] & 0x80)
|
|
199
|
+ m_overCurrentFault = true;
|
|
200
|
+ else
|
|
201
|
+ m_overCurrentFault = false;
|
|
202
|
+
|
|
203
|
+ return;
|
|
204
|
+}
|