Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 22 additions & 29 deletions src/dev/mpr121.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,19 @@ class Mpr121I2CTransport
}

/** \return Did the transaction error? i.e. Return true if error, false if ok */
bool Write(uint8_t *data, uint16_t size)
bool ReadDataAtAddress(uint8_t address, uint8_t *data, uint16_t size)
{
return I2CHandle::Result::OK
!= i2c_.TransmitBlocking(config_.dev_addr, data, size, 10);
!= i2c_.ReadDataAtAddress(
config_.dev_addr, address, 1, data, size, 10);
}

/** \return Did the transaction error? i.e. Return true if error, false if ok */
bool Read(uint8_t *data, uint16_t size)
bool WriteDataAtAddress(uint8_t address, uint8_t *data, uint16_t size)
{
return I2CHandle::Result::OK
!= i2c_.ReceiveBlocking(config_.dev_addr, data, size, 10);
!= i2c_.WriteDataAtAddress(
config_.dev_addr, address, 1, data, size, 10);
}

private:
Expand Down Expand Up @@ -124,11 +126,10 @@ class Mpr121

WriteRegister(MPR121_ECR, 0x0);

// this doesn't work for some reason...
// uint8_t c = ReadRegister8(MPR121_CONFIG2);
uint8_t c = ReadRegister8(MPR121_CONFIG2);

// if(c != 0x24)
// return ERR;
if(c != 0x24)
return ERR;

SetThresholds(config_.touch_threshold, config_.release_threshold);
WriteRegister(MPR121_MHDR, 0x01);
Expand Down Expand Up @@ -161,7 +162,7 @@ class Mpr121
// enable X electrodes and start MPR121
uint8_t ECR_SETTING
= 0x80
+ 12; // 5 bits for baseline tracking & proximity disabled + X
+ 12; // enable baseline tracking (10) & disable proximity (00) + X
// amount of electrodes running (12)
WriteRegister(MPR121_ECR,
ECR_SETTING); // start with above ECR setting
Expand Down Expand Up @@ -231,8 +232,7 @@ class Mpr121
uint8_t ReadRegister8(uint8_t reg)
{
uint8_t buff;
SetTransportErr(transport_.Write(&reg, 1));
SetTransportErr(transport_.Read(&buff, 1));
SetTransportErr(transport_.ReadDataAtAddress(reg, &buff, 1));

return buff;
}
Expand All @@ -244,8 +244,8 @@ class Mpr121
uint16_t ReadRegister16(uint8_t reg)
{
uint16_t buff;
SetTransportErr(transport_.Write(&reg, 1));
SetTransportErr(transport_.Read((uint8_t *)&buff, 2));
SetTransportErr(
transport_.ReadDataAtAddress(reg, (uint8_t *)(&buff), 2));

return buff;
}
Expand All @@ -258,36 +258,29 @@ class Mpr121
{
// MPR121 must be put in Stop Mode to write to most registers
bool stop_required = true;

// first get the current set value of the MPR121_ECR register
uint8_t ecr_reg = MPR121_ECR;
uint8_t buff[2] = {ecr_reg, 0x00};

SetTransportErr(transport_.Write(buff, 1));

uint8_t ecr_backup;
SetTransportErr(transport_.Read(&ecr_backup, 1));
if((reg == MPR121_ECR) || ((0x73 <= reg) && (reg <= 0x7A)))
{
stop_required = false;
}

uint8_t ecr_backup;

if(stop_required)
{
ecr_backup = ReadRegister8(MPR121_ECR);
uint8_t zero = 0x00;
// clear this register to set stop mode
SetTransportErr(transport_.Write(buff, 2));
SetTransportErr(
transport_.WriteDataAtAddress(MPR121_ECR, &zero, 1));
}

buff[0] = reg;
buff[1] = value;
SetTransportErr(transport_.Write(buff, 2));
SetTransportErr(transport_.WriteDataAtAddress(reg, &value, 1));

if(stop_required)
{
// write back the previous set ECR settings
buff[0] = ecr_reg;
buff[1] = ecr_backup;
SetTransportErr(transport_.Write(buff, 2));
SetTransportErr(
transport_.WriteDataAtAddress(MPR121_ECR, &ecr_backup, 1));
}
}

Expand Down
Loading