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
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
64 {
66 != i2c_.TransmitBlocking(config_.dev_addr, data, size, 10);
67 }
68
71 {
73 != i2c_.ReceiveBlocking(config_.dev_addr, data, size, 10);
74 }
75
76 private:
77 I2CHandle i2c_;
78 Config config_;
79};
80
81
86template <typename Transport>
87class Mpr121
88{
89 public:
90 Mpr121() {}
92
105
107 {
108 OK = 0,
109 ERR
110 };
111
116 {
117 config_ = config;
118
119 SetTransportErr(transport_.Init(config_.transport_config));
120
121 // soft reset
123 System::Delay(1);
124
126
127 // this doesn't work for some reason...
128 // uint8_t c = ReadRegister8(MPR121_CONFIG2);
129
130 // if(c != 0x24)
131 // return ERR;
132
138
143
147
149 WriteRegister(MPR121_CONFIG1, 0x10); // default, 16uA charge current
150 WriteRegister(MPR121_CONFIG2, 0x20); // 0.5uS encoding, 1ms period
151
152 // autoconfig
153 // WriteRegister(MPR121_AUTOCONFIG0, 0x0B);
154
155 // correct values for Vdd = 3.3V
156 // WriteRegister(MPR121_UPLIMIT, 200); // ((Vdd - 0.7)/Vdd) * 256
157 // WriteRegister(MPR121_TARGETLIMIT, 180); // UPLIMIT * 0.9
158 // WriteRegister(MPR121_LOWLIMIT, 130); // UPLIMIT * 0.65
159 // autoconfig
160
161 // enable X electrodes and start MPR121
163 = 0x80
164 + 12; // 5 bits for baseline tracking & proximity disabled + X
165 // amount of electrodes running (12)
167 ECR_SETTING); // start with above ECR setting
168
169 return GetTransportErr();
170 }
171
178 {
179 // set all thresholds (the same)
180 for(uint8_t i = 0; i < 12; i++)
181 {
184 }
185 }
186
187
196 {
197 if(t > 12)
198 return 0;
199 return ReadRegister16(MPR121_FILTDATA_0L + t * 2);
200 }
201
209 {
210 if(t > 12)
211 return 0;
213 return (bl << 2);
214 }
215
222 {
224 return t & 0x0FFF;
225 }
226
232 {
234 SetTransportErr(transport_.Write(&reg, 1));
235 SetTransportErr(transport_.Read(&buff, 1));
236
237 return buff;
238 }
239
245 {
247 SetTransportErr(transport_.Write(&reg, 1));
248 SetTransportErr(transport_.Read((uint8_t *)&buff, 2));
249
250 return buff;
251 }
252
258 {
259 // MPR121 must be put in Stop Mode to write to most registers
260 bool stop_required = true;
261
262 // first get the current set value of the MPR121_ECR register
264 uint8_t buff[2] = {ecr_reg, 0x00};
265
266 SetTransportErr(transport_.Write(buff, 1));
267
269 SetTransportErr(transport_.Read(&ecr_backup, 1));
270 if((reg == MPR121_ECR) || ((0x73 <= reg) && (reg <= 0x7A)))
271 {
272 stop_required = false;
273 }
274
275 if(stop_required)
276 {
277 // clear this register to set stop mode
278 SetTransportErr(transport_.Write(buff, 2));
279 }
280
281 buff[0] = reg;
282 buff[1] = value;
283 SetTransportErr(transport_.Write(buff, 2));
284
285 if(stop_required)
286 {
287 // write back the previous set ECR settings
288 buff[0] = ecr_reg;
289 buff[1] = ecr_backup;
290 SetTransportErr(transport_.Write(buff, 2));
291 }
292 }
293
336
337 private:
338 Config config_;
339 Transport transport_;
340 bool transport_error_;
341
343 void SetTransportErr(bool err) { transport_error_ |= err; }
344
346 Result GetTransportErr()
347 {
348 Result ret = transport_error_ ? ERR : OK;
349 transport_error_ = false;
350 return ret;
351 }
352
353}; // class
354
356
359} // namespace daisy
360#endif
Definition i2c.h:26
Result Init(const Config &config)
Result ReceiveBlocking(uint16_t address, uint8_t *data, uint16_t size, uint32_t timeout)
Result TransmitBlocking(uint16_t address, uint8_t *data, uint16_t size, uint32_t timeout)
Definition leddriver.h:33
void Init(I2CHandle i2c, const uint8_t(&addresses)[numDrivers], DmaBuffer dma_buffer_a, DmaBuffer dma_buffer_b, Pin oe_pin=Pin(PORTX, 0))
Definition leddriver.h:65
Device support for MPR121 12x Capacitive Touch Sensor.
Definition mpr121.h:88
void WriteRegister(uint8_t reg, uint8_t value)
Definition mpr121.h:257
Mpr121()
Definition mpr121.h:90
~Mpr121()
Definition mpr121.h:91
uint16_t FilteredData(uint8_t t)
Definition mpr121.h:195
uint8_t ReadRegister8(uint8_t reg)
Definition mpr121.h:231
uint16_t BaselineData(uint8_t t)
Definition mpr121.h:208
void SetThresholds(uint8_t touch, uint8_t release)
Definition mpr121.h:177
Result
Definition mpr121.h:107
@ OK
Definition mpr121.h:108
@ ERR
Definition mpr121.h:109
uint16_t Touched()
Definition mpr121.h:221
Result Init(Config config)
Definition mpr121.h:115
RegMap
Definition mpr121.h:296
@ MPR121_NHDT
Definition mpr121.h:310
@ MPR121_DEBOUNCE
Definition mpr121.h:316
@ MPR121_MHDR
Definition mpr121.h:302
@ MPR121_GPIOCLR
Definition mpr121.h:331
@ MPR121_GPIODIR
Definition mpr121.h:328
@ MPR121_FDLF
Definition mpr121.h:309
@ MPR121_SOFTRESET
Definition mpr121.h:334
@ MPR121_FDLR
Definition mpr121.h:305
@ MPR121_NHDR
Definition mpr121.h:303
@ MPR121_TARGETLIMIT
Definition mpr121.h:326
@ MPR121_TOUCHTH_0
Definition mpr121.h:314
@ MPR121_NHDF
Definition mpr121.h:307
@ MPR121_NCLR
Definition mpr121.h:304
@ MPR121_TOUCHSTATUS_H
Definition mpr121.h:298
@ MPR121_AUTOCONFIG0
Definition mpr121.h:322
@ MPR121_FILTDATA_0L
Definition mpr121.h:299
@ MPR121_GPIOEN
Definition mpr121.h:329
@ MPR121_MHDF
Definition mpr121.h:306
@ MPR121_FDLT
Definition mpr121.h:312
@ MPR121_LOWLIMIT
Definition mpr121.h:325
@ MPR121_RELEASETH_0
Definition mpr121.h:315
@ MPR121_NCLF
Definition mpr121.h:308
@ MPR121_AUTOCONFIG1
Definition mpr121.h:323
@ MPR121_CHARGETIME_1
Definition mpr121.h:320
@ MPR121_BASELINE_0
Definition mpr121.h:301
@ MPR121_CONFIG2
Definition mpr121.h:318
@ MPR121_CONFIG1
Definition mpr121.h:317
@ MPR121_GPIOSET
Definition mpr121.h:330
@ MPR121_ECR
Definition mpr121.h:321
@ MPR121_UPLIMIT
Definition mpr121.h:324
@ MPR121_FILTDATA_0H
Definition mpr121.h:300
@ MPR121_TOUCHSTATUS_L
Definition mpr121.h:297
@ MPR121_GPIOTOGGLE
Definition mpr121.h:332
@ MPR121_NCLT
Definition mpr121.h:311
@ MPR121_CHARGECURR_0
Definition mpr121.h:319
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 Read(uint8_t *data, uint16_t size)
Definition mpr121.h:70
~Mpr121I2CTransport()
Definition mpr121.h:21
bool Write(uint8_t *data, uint16_t size)
Definition mpr121.h:63
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
Speed
Definition i2c.h:51
Peripheral
Definition i2c.h:40
Definition mpr121.h:94
uint8_t release_threshold
Definition mpr121.h:97
Transport::Config transport_config
Definition mpr121.h:95
uint8_t touch_threshold
Definition mpr121.h:96
Config()
Definition mpr121.h:99
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