Display sensor status in raw command

This commit is contained in:
whowechina 2024-03-23 18:50:41 +08:00
parent 77f67fbaad
commit dd2018ed1a
7 changed files with 35 additions and 13 deletions

Binary file not shown.

View File

@ -361,8 +361,13 @@ static void print_raw_zones(const char *title, const uint16_t *raw, int num)
static void handle_raw()
{
printf("Touch raw readings:\n");
printf(" |__1__|__2__|__3__|__4__|__5__|__6__|__7__|__8__|\n");
const uint16_t *raw = touch_raw();
printf(" Sensor: 0: %s, 1: %s 2: %s\n",
touch_sensor_ok(0) ? "OK" : "ERR",
touch_sensor_ok(1) ? "OK" : "ERR",
touch_sensor_ok(2) ? "OK" : "ERR");
printf(" |__1__|__2__|__3__|__4__|__5__|__6__|__7__|__8__|\n");
print_raw_zones("A", raw, 8);
print_raw_zones("B", raw + 8, 8);
print_raw_zones("C", raw + 16, 2);

View File

@ -123,7 +123,7 @@ void mpr121_init(uint8_t i2c_addr)
// (Auto configure result) alone.
// I want to max out sensitivity, I don't care linearity
const uint8_t usl = (3.3 - 0.1) / 3.3 * 256;
const uint8_t usl = 255; //(3.3 - 0.0) / 3.3 * 256;
write_reg(i2c_addr, 0x7D, usl),
write_reg(i2c_addr, 0x7E, usl * 0.65),
write_reg(i2c_addr, 0x7F, usl * 0.9);
@ -133,21 +133,26 @@ void mpr121_init(uint8_t i2c_addr)
#define ABS(x) ((x) < 0 ? -(x) : (x))
static void mpr121_read_many(uint8_t addr, uint8_t reg, uint8_t *buf, size_t n)
static bool mpr121_read_many(uint8_t addr, uint8_t reg, uint8_t *buf, size_t n)
{
i2c_write_blocking_until(I2C_PORT, addr, &reg, 1, true,
time_us_64() + IO_TIMEOUT_US);
i2c_read_blocking_until(I2C_PORT, addr, buf, n, false,
time_us_64() + IO_TIMEOUT_US * n / 2);
int bytes = i2c_read_blocking_until(I2C_PORT, addr, buf, n, false,
time_us_64() + IO_TIMEOUT_US * n / 2);
return bytes == n;
}
static void mpr121_read_many16(uint8_t addr, uint8_t reg, uint16_t *buf, size_t n)
static bool mpr121_read_many16(uint8_t addr, uint8_t reg, uint16_t *buf, size_t n)
{
uint8_t vals[n * 2];
mpr121_read_many(addr, reg, vals, n * 2);
if (!mpr121_read_many(addr, reg, vals, n * 2)){
return false;
}
for (int i = 0; i < n; i++) {
buf[i] = (vals[i * 2 + 1] << 8) | vals[i * 2];
}
return true;
}
uint16_t mpr121_touched(uint8_t addr)
@ -157,9 +162,9 @@ uint16_t mpr121_touched(uint8_t addr)
return touched;
}
void mpr121_raw(uint8_t addr, uint16_t *raw, int num)
bool mpr121_raw(uint8_t addr, uint16_t *raw, int num)
{
mpr121_read_many16(addr, MPR121_ELECTRODE_FILTERED_DATA_REG, raw, num);
return mpr121_read_many16(addr, MPR121_ELECTRODE_FILTERED_DATA_REG, raw, num);
}
static uint8_t mpr121_stop(uint8_t addr)

View File

@ -10,7 +10,7 @@
void mpr121_init(uint8_t addr);
uint16_t mpr121_touched(uint8_t addr);
void mpr121_raw(uint8_t addr, uint16_t *raw, int num);
bool mpr121_raw(uint8_t addr, uint16_t *raw, int num);
void mpr121_filter(uint8_t addr, uint8_t ffi, uint8_t sfi, uint8_t esi);
void mpr121_sense(uint8_t addr, int8_t sense, int8_t *sense_keys, int num);
void mpr121_debounce(uint8_t addr, uint8_t touch, uint8_t release);

View File

@ -88,13 +88,23 @@ void touch_update()
touch_stat();
}
static bool sensor_ok[3];
bool touch_sensor_ok(unsigned i)
{
if (i < 3) {
return sensor_ok[i];
}
return false;
}
const uint16_t *touch_raw()
{
static uint16_t readout[36];
uint16_t buf[36];
mpr121_raw(MPR121_ADDR, buf, 12);
mpr121_raw(MPR121_ADDR + 1, buf + 12, 12);
mpr121_raw(MPR121_ADDR + 2, buf + 24, 10);
for (int i = 0; i < 3; i++) {
sensor_ok[i] = mpr121_raw(MPR121_ADDR + i, buf + i * 12, 12);
}
for (int i = 0; i < 34; i++) {
readout[touch_map[i]] = buf[i];

View File

@ -15,6 +15,8 @@ bool touch_touched(unsigned key);
uint64_t touch_touchmap();
const uint16_t *touch_raw();
bool touch_sensor_ok(unsigned i);
void touch_update_config();
unsigned touch_count(unsigned key);
void touch_reset_stat();