16#if DETWS_ENABLE_MPR121
22 return (uint16_t)(((uint16_t)status_lo | ((uint16_t)status_hi << 8)) & 0x0FFF);
32 return (status_hi & 0x10) != 0;
37 return (status_hi & 0x80) != 0;
42 return (uint16_t)(((uint16_t)lsb | ((uint16_t)msb << 8)) & 0x03FF);
45size_t mpr121_build_init(uint8_t *buf,
size_t cap, uint8_t n, uint8_t touch_thr, uint8_t release_thr)
50 static const uint8_t fixed[] = {
53 0x2B, 0x01, 0x2C, 0x01,
54 0x2D, 0x0E, 0x2E, 0x00,
55 0x2F, 0x01, 0x30, 0x05,
56 0x31, 0x01, 0x32, 0x00,
57 0x33, 0x00, 0x34, 0x00,
60 size_t need =
sizeof(fixed) + (
size_t)n * 4 + 8;
63 size_t i =
sizeof(fixed);
64 memcpy(buf, fixed,
sizeof(fixed));
65 for (uint8_t e = 0; e < n; e++)
67 buf[i++] = (uint8_t)(0x41 + 2 * e);
69 buf[i++] = (uint8_t)(0x42 + 2 * e);
70 buf[i++] = release_thr;
79 buf[i++] = (uint8_t)(0x80 | n);
103bool wr(uint8_t reg, uint8_t val)
105 Wire.beginTransmission(s_mpr.addr);
108 return Wire.endTransmission() == 0;
111bool rd(uint8_t reg, uint8_t *out, uint8_t n)
113 Wire.beginTransmission(s_mpr.addr);
115 if (Wire.endTransmission(
false) != 0)
117 if (Wire.requestFrom((
int)s_mpr.addr, (
int)n) != (
int)n)
119 for (uint8_t i = 0; i < n; i++)
120 out[i] = (uint8_t)Wire.read();
134 if (!wr(seq[0], seq[1]))
137 for (
size_t i = 2; i + 1 < n; i += 2)
138 if (!wr(seq[i], seq[i + 1]))
145 uint8_t s[2] = {0, 0};
155 uint8_t d[2] = {0, 0};
156 if (!rd((uint8_t)(0x04 + 2 * e), d, 2))
User-facing configuration for DeterministicESPAsyncWebServer.
#define DETWS_MPR121_I2C_ADDR
I2C address of the MPR121 (0x5A default; 0x5B/0x5C/0x5D via the ADDR pin).
#define DETWS_MPR121_RELEASE_THRESHOLD
MPR121 per-electrode release threshold (delta counts; should be below the touch threshold).
#define DETWS_MPR121_TOUCH_THRESHOLD
MPR121 per-electrode touch threshold (delta counts from baseline; NXP AN3944 suggests ~4....
The one owner of the shared I2C bus bring-up for the peripheral drivers.
void detws_i2c_begin()
Bring up the shared I2C bus on DETWS_I2C_SDA_PIN / DETWS_I2C_SCL_PIN (-1 = default).
NXP MPR121 12-channel capacitive-touch controller codec (DETWS_ENABLE_MPR121).
#define MPR121_INIT_MAX
Largest init sequence in bytes: (17 fixed + 2*12 threshold) pairs * 2 bytes.
bool mpr121_overcurrent(uint8_t status_hi)
True if the over-current flag (status bit 15) is set (wiring fault / short).
uint16_t mpr121_read_filtered(uint8_t e)
Read electrode e's 10-bit filtered capacitance value.
uint16_t mpr121_read_touched()
Read the current 12-electrode touch bitmask (0 if the device is absent).
size_t mpr121_build_init(uint8_t *buf, size_t cap, uint8_t n_electrodes, uint8_t touch_thr, uint8_t release_thr)
Build the MPR121 bring-up sequence as consecutive (register, value) byte pairs.
bool mpr121_begin(uint8_t addr)
Reset + configure the MPR121 at addr over I2C.
bool mpr121_proximity(uint8_t status_hi)
True if the proximity electrode (status bit 12) is active.
bool mpr121_is_touched(uint16_t mask, uint8_t e)
True if electrode e (0..11) is touched in a mask from mpr121_touched.
uint16_t mpr121_word10(uint8_t lsb, uint8_t msb)
Combine a little-endian LSB/MSB register pair into a 10-bit value (filtered/baseline).
uint16_t mpr121_touched(uint8_t status_lo, uint8_t status_hi)
Decode the 12-electrode touch bitmask from the two status registers (0x00 low, 0x01 high)....
#define MPR121_ELECTRODES
Sense electrodes on the MPR121 (ELE0..ELE11).