libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
mpr121.h
Go to the documentation of this file.
1#pragma once
2#ifndef DSY_MPR121_H
3#define DSY_MPR121_H
4
5// The default I2C address
6#define MPR121_I2CADDR_DEFAULT 0x5A
7#define MPR121_TOUCH_THRESHOLD_DEFAULT 12
8#define MPR121_RELEASE_THRESHOLD_DEFAULT 6
9
10namespace daisy
11{
18{
19 public:
22
45
47 inline bool Init(Config config)
48 {
49 config_ = config;
50
51 I2CHandle::Config i2c_conf;
53 i2c_conf.periph = config.periph;
54 i2c_conf.speed = config.speed;
55
56 i2c_conf.pin_config.scl = config.scl;
57 i2c_conf.pin_config.sda = config.sda;
58
59 return I2CHandle::Result::OK != i2c_.Init(i2c_conf);
60 }
61
63 bool ReadDataAtAddress(uint8_t address, uint8_t *data, uint16_t size)
64 {
66 != i2c_.ReadDataAtAddress(
67 config_.dev_addr, address, 1, data, size, 10);
68 }
69
71 bool WriteDataAtAddress(uint8_t address, uint8_t *data, uint16_t size)
72 {
74 != i2c_.WriteDataAtAddress(
75 config_.dev_addr, address, 1, data, size, 10);
76 }
77
78 private:
79 I2CHandle i2c_;
80 Config config_;
81};
82
83
88template <typename Transport>
89class Mpr121
90{
91 public:
92 Mpr121() {}
94
107
109 {
110 OK = 0,
111 ERR
112 };
113
118 {
119 config_ = config;
120
121 SetTransportErr(transport_.Init(config_.transport_config));
122
123 // soft reset
125 System::Delay(1);
126
128
129 uint8_t c = ReadRegister8(MPR121_CONFIG2);
130
131 if(c != 0x24)
132 return ERR;
133
139
144
148
150 WriteRegister(MPR121_CONFIG1, 0x10); // default, 16uA charge current
151 WriteRegister(MPR121_CONFIG2, 0x20); // 0.5uS encoding, 1ms period
152
153 // autoconfig
154 // WriteRegister(MPR121_AUTOCONFIG0, 0x0B);
155
156 // correct values for Vdd = 3.3V
157 // WriteRegister(MPR121_UPLIMIT, 200); // ((Vdd - 0.7)/Vdd) * 256
158 // WriteRegister(MPR121_TARGETLIMIT, 180); // UPLIMIT * 0.9
159 // WriteRegister(MPR121_LOWLIMIT, 130); // UPLIMIT * 0.65
160 // autoconfig
161
162 // enable X electrodes and start MPR121
163 uint8_t ECR_SETTING
164 = 0x80
165 + 12; // enable baseline tracking (10) & disable proximity (00) + X
166 // amount of electrodes running (12)
168 ECR_SETTING); // start with above ECR setting
169
170 return GetTransportErr();
171 }
172
178 void SetThresholds(uint8_t touch, uint8_t release)
179 {
180 // set all thresholds (the same)
181 for(uint8_t i = 0; i < 12; i++)
182 {
183 WriteRegister(MPR121_TOUCHTH_0 + 2 * i, touch);
184 WriteRegister(MPR121_RELEASETH_0 + 2 * i, release);
185 }
186 }
187
188
196 uint16_t FilteredData(uint8_t t)
197 {
198 if(t > 12)
199 return 0;
200 return ReadRegister16(MPR121_FILTDATA_0L + t * 2);
201 }
202
209 uint16_t BaselineData(uint8_t t)
210 {
211 if(t > 12)
212 return 0;
213 uint16_t bl = ReadRegister8(MPR121_BASELINE_0 + t);
214 return (bl << 2);
215 }
216
222 uint16_t Touched()
223 {
225 return t & 0x0FFF;
226 }
227
232 uint8_t ReadRegister8(uint8_t reg)
233 {
234 uint8_t buff;
235 SetTransportErr(transport_.ReadDataAtAddress(reg, &buff, 1));
236
237 return buff;
238 }
239
244 uint16_t ReadRegister16(uint8_t reg)
245 {
246 uint16_t buff;
247 SetTransportErr(
248 transport_.ReadDataAtAddress(reg, (uint8_t *)(&buff), 2));
249
250 return buff;
251 }
252
257 void WriteRegister(uint8_t reg, uint8_t value)
258 {
259 // MPR121 must be put in Stop Mode to write to most registers
260 bool stop_required = true;
261 if((reg == MPR121_ECR) || ((0x73 <= reg) && (reg <= 0x7A)))
262 {
263 stop_required = false;
264 }
265
266 uint8_t ecr_backup;
267
268 if(stop_required)
269 {
270 ecr_backup = ReadRegister8(MPR121_ECR);
271 uint8_t zero = 0x00;
272 // clear this register to set stop mode
273 SetTransportErr(
274 transport_.WriteDataAtAddress(MPR121_ECR, &zero, 1));
275 }
276
277 SetTransportErr(transport_.WriteDataAtAddress(reg, &value, 1));
278
279 if(stop_required)
280 {
281 // write back the previous set ECR settings
282 SetTransportErr(
283 transport_.WriteDataAtAddress(MPR121_ECR, &ecr_backup, 1));
284 }
285 }
286
329
330 private:
331 Config config_;
332 Transport transport_;
333 bool transport_error_;
334
336 void SetTransportErr(bool err) { transport_error_ |= err; }
337
339 Result GetTransportErr()
340 {
341 Result ret = transport_error_ ? ERR : OK;
342 transport_error_ = false;
343 return ret;
344 }
345
346}; // class
347
349
352} // namespace daisy
353#endif
Definition i2c.h:26
Result ReadDataAtAddress(uint16_t address, uint16_t mem_address, uint16_t mem_address_size, uint8_t *data, uint16_t data_size, uint32_t timeout)
Result Init(const Config &config)
Result WriteDataAtAddress(uint16_t address, uint16_t mem_address, uint16_t mem_address_size, uint8_t *data, uint16_t data_size, uint32_t timeout)
Device support for MPR121 12x Capacitive Touch Sensor.
Definition mpr121.h:90
void WriteRegister(uint8_t reg, uint8_t value)
Definition mpr121.h:257
Mpr121()
Definition mpr121.h:92
~Mpr121()
Definition mpr121.h:93
uint16_t FilteredData(uint8_t t)
Definition mpr121.h:196
uint8_t ReadRegister8(uint8_t reg)
Definition mpr121.h:232
uint16_t BaselineData(uint8_t t)
Definition mpr121.h:209
void SetThresholds(uint8_t touch, uint8_t release)
Definition mpr121.h:178
Result
Definition mpr121.h:109
@ OK
Definition mpr121.h:110
@ ERR
Definition mpr121.h:111
uint16_t Touched()
Definition mpr121.h:222
Result Init(Config config)
Definition mpr121.h:117
RegMap
Definition mpr121.h:289
@ MPR121_NHDT
Definition mpr121.h:303
@ MPR121_DEBOUNCE
Definition mpr121.h:309
@ MPR121_MHDR
Definition mpr121.h:295
@ MPR121_GPIOCLR
Definition mpr121.h:324
@ MPR121_GPIODIR
Definition mpr121.h:321
@ MPR121_FDLF
Definition mpr121.h:302
@ MPR121_SOFTRESET
Definition mpr121.h:327
@ MPR121_FDLR
Definition mpr121.h:298
@ MPR121_NHDR
Definition mpr121.h:296
@ MPR121_TARGETLIMIT
Definition mpr121.h:319
@ MPR121_TOUCHTH_0
Definition mpr121.h:307
@ MPR121_NHDF
Definition mpr121.h:300
@ MPR121_NCLR
Definition mpr121.h:297
@ MPR121_TOUCHSTATUS_H
Definition mpr121.h:291
@ MPR121_AUTOCONFIG0
Definition mpr121.h:315
@ MPR121_FILTDATA_0L
Definition mpr121.h:292
@ MPR121_GPIOEN
Definition mpr121.h:322
@ MPR121_MHDF
Definition mpr121.h:299
@ MPR121_FDLT
Definition mpr121.h:305
@ MPR121_LOWLIMIT
Definition mpr121.h:318
@ MPR121_RELEASETH_0
Definition mpr121.h:308
@ MPR121_NCLF
Definition mpr121.h:301
@ MPR121_AUTOCONFIG1
Definition mpr121.h:316
@ MPR121_CHARGETIME_1
Definition mpr121.h:313
@ MPR121_BASELINE_0
Definition mpr121.h:294
@ MPR121_CONFIG2
Definition mpr121.h:311
@ MPR121_CONFIG1
Definition mpr121.h:310
@ MPR121_GPIOSET
Definition mpr121.h:323
@ MPR121_ECR
Definition mpr121.h:314
@ MPR121_UPLIMIT
Definition mpr121.h:317
@ MPR121_FILTDATA_0H
Definition mpr121.h:293
@ MPR121_TOUCHSTATUS_L
Definition mpr121.h:290
@ MPR121_GPIOTOGGLE
Definition mpr121.h:325
@ MPR121_NCLT
Definition mpr121.h:304
@ MPR121_CHARGECURR_0
Definition mpr121.h:312
uint16_t ReadRegister16(uint8_t reg)
Definition mpr121.h:244
Definition mpr121.h:18
Mpr121I2CTransport()
Definition mpr121.h:20
bool Init(Config config)
Definition mpr121.h:47
bool ReadDataAtAddress(uint8_t address, uint8_t *data, uint16_t size)
Definition mpr121.h:63
bool WriteDataAtAddress(uint8_t address, uint8_t *data, uint16_t size)
Definition mpr121.h:71
~Mpr121I2CTransport()
Definition mpr121.h:21
static void Delay(uint32_t delay_ms)
#define MPR121_TOUCH_THRESHOLD_DEFAULT
default touch threshold value
Definition mpr121.h:7
#define MPR121_I2CADDR_DEFAULT
default I2C address
Definition mpr121.h:6
#define MPR121_RELEASE_THRESHOLD_DEFAULT
default relese threshold value
Definition mpr121.h:8
Hardware defines and helpers for daisy field platform.
Definition index.h:2
@ PORTB
Definition daisy_core.h:178
Definition i2c.h:30
Mode
Definition i2c.h:33
struct daisy::I2CHandle::Config::@15 pin_config
Mode mode
Definition i2c.h:65
Pin scl
Definition i2c.h:60
Speed
Definition i2c.h:51
Speed speed
Definition i2c.h:64
Peripheral periph
Definition i2c.h:57
Peripheral
Definition i2c.h:40
Pin sda
Definition i2c.h:61
Definition mpr121.h:96
uint8_t release_threshold
Definition mpr121.h:99
Transport::Config transport_config
Definition mpr121.h:97
uint8_t touch_threshold
Definition mpr121.h:98
Config()
Definition mpr121.h:101
Definition mpr121.h:24
Pin sda
Definition mpr121.h:28
I2CHandle::Config::Peripheral periph
Definition mpr121.h:25
Pin scl
Definition mpr121.h:27
Config()
Definition mpr121.h:34
I2CHandle::Config::Mode mode
Definition mpr121.h:30
uint8_t dev_addr
Definition mpr121.h:32
I2CHandle::Config::Speed speed
Definition mpr121.h:26
representation of hardware port/pin combination
Definition daisy_core.h:193