diff --git a/Production/Firmware/chu_pico.uf2 b/Production/Firmware/chu_pico.uf2 index e99e6da..69bea00 100644 Binary files a/Production/Firmware/chu_pico.uf2 and b/Production/Firmware/chu_pico.uf2 differ diff --git a/firmware/src/cmd.c b/firmware/src/cmd.c index aad0723..724d424 100644 --- a/firmware/src/cmd.c +++ b/firmware/src/cmd.c @@ -88,8 +88,8 @@ static void disp_tof() static void disp_sense() { printf("[Sense]\n"); - printf(" Global: %+d, debounce (touch, release): %d, %d\n", chu_cfg->sense.global, - chu_cfg->sense.debounce_touch, chu_cfg->sense.debounce_release); + printf(" Filter: %d, %d\n", chu_cfg->sense.filter >> 4, chu_cfg->sense.filter & 0xf); + printf(" Sensitivity (global: %+d):\n", chu_cfg->sense.global); printf(" | 1| 2| 3| 4| 5| 6| 7| 8| 9|10|11|12|13|14|15|16|\n"); printf(" ---------------------------------------------------\n"); printf(" A |"); @@ -101,6 +101,8 @@ static void disp_sense() printf("%+2d|", chu_cfg->sense.keys[i * 2 + 1]); } printf("\n"); + printf(" Debounce (touch, release): %d, %d\n", + chu_cfg->sense.debounce_touch, chu_cfg->sense.debounce_release); } static void disp_hid() @@ -212,8 +214,8 @@ static int extract_non_neg_int(const char *param, int len) static void handle_tof(int argc, char *argv[]) { const char *usage = "Usage: tof [pitch]\n" - " offset: 40-255\n" - " pitch: 4-50\n"; + " offset: 40..255\n" + " pitch: 4..50\n"; if ((argc < 1) || (argc > 2)) { printf(usage); return; @@ -240,6 +242,30 @@ static void handle_tof(int argc, char *argv[]) disp_tof(); } +static void handle_filter(int argc, char *argv[]) +{ + const char *usage = "Usage: filter \n" + " first, second: 0..3\n"; + if ((argc < 2) || (argc > 2)) { + printf(usage); + return; + } + + int ffi = extract_non_neg_int(argv[0], 0); + int sfi = extract_non_neg_int(argv[1], 0); + + if ((ffi < 0) || (ffi > 3) || (sfi < 0) || (sfi > 3)) { + printf(usage); + return; + } + + chu_cfg->sense.filter = (ffi << 4) | sfi; + + slider_update_config(); + config_changed(); + disp_sense(); +} + static uint8_t *extract_key(const char *param) { int len = strlen(param); @@ -261,44 +287,58 @@ static uint8_t *extract_key(const char *param) return &chu_cfg->sense.keys[id * 2 + offset]; } +static void sense_do_op(int8_t *target, char op) +{ + if (op == '+') { + if (*target < SENSE_LIMIT_MAX) { + (*target)++; + } + } else if (op == '-') { + if (*target > SENSE_LIMIT_MIN) { + (*target)--; + } + } else if (op == '0') { + *target = 0; + } +} + static void handle_sense(int argc, char *argv[]) { - const char *usage = "Usage: sense [key] <+|-|0>\n" + const char *usage = "Usage: sense [key|*] <+|-|0>\n" "Example:\n" " >sense +\n" " >sense -\n" " >sense 1A +\n" - " >sense 13B 0\n"; + " >sense 13B -\n"; + " >sense * 0\n"; if ((argc < 1) || (argc > 2)) { printf(usage); return; } - int8_t *target = &chu_cfg->sense.global; const char *op = argv[argc - 1]; - if (argc == 2) { - target = extract_key(argv[0]); - if (!target) { - printf(usage); - return; - } - } - - if (strcmp(op, "+") == 0) { - if (*target < SENSE_LIMIT_MAX) { - (*target)++; - } - } else if (strcmp(op, "-") == 0) { - if (*target > SENSE_LIMIT_MIN) { - (*target)--; - } - } else if (strcmp(op, "0") == 0) { - *target = 0; - } else { + if ((strlen(op) != 1) || !strchr("+-0", op[0])) { printf(usage); return; } + if (argc == 1) { + sense_do_op(&chu_cfg->sense.global, op[0]); + } else { + if (strcmp(argv[0], "*") == 0) { + for (int i = 0; i < 32; i++) { + sense_do_op(&chu_cfg->sense.keys[i], op[0]); + } + } else { + uint8_t *key = extract_key(argv[0]); + if (!key) { + printf(usage); + return; + } + sense_do_op(key, op[0]); + } + } + slider_update_config(); config_changed(); disp_sense(); @@ -307,7 +347,7 @@ static void handle_sense(int argc, char *argv[]) static void handle_debounce(int argc, char *argv[]) { const char *usage = "Usage: debounce [release]\n" - " touch, release: 0-7\n"; + " touch, release: 0..7\n"; if ((argc < 1) || (argc > 2)) { printf(usage); return; @@ -354,6 +394,7 @@ void cmd_init() register_command("fps", handle_fps); register_command("hid", handle_hid); register_command("tof", handle_tof); + register_command("filter", handle_filter); register_command("sense", handle_sense); register_command("debounce", handle_debounce); register_command("save", handle_save); diff --git a/firmware/src/config.c b/firmware/src/config.c index 9e8c253..7b0dd14 100644 --- a/firmware/src/config.c +++ b/firmware/src/config.c @@ -52,6 +52,11 @@ static void config_loaded() chu_cfg->tof = default_cfg.tof; config_changed(); } + if ((chu_cfg->sense.filter & 0x0f) > 3 || + ((chu_cfg->sense.filter >> 4) & 0x0f) > 3) { + chu_cfg->sense.filter = default_cfg.sense.filter; + config_changed(); + } if ((chu_cfg->sense.global > 7) || (chu_cfg->sense.global < -7)) { chu_cfg->sense.global = default_cfg.sense.global; config_changed(); diff --git a/firmware/src/config.h b/firmware/src/config.h index 65fd2ef..15906eb 100644 --- a/firmware/src/config.h +++ b/firmware/src/config.h @@ -28,6 +28,7 @@ typedef struct __attribute__((packed)) { uint8_t pitch; } tof; struct { + int8_t filter; int8_t global; uint8_t debounce_touch; uint8_t debounce_release; diff --git a/firmware/src/mpr121.c b/firmware/src/mpr121.c index 741ab22..c4790af 100644 --- a/firmware/src/mpr121.c +++ b/firmware/src/mpr121.c @@ -165,10 +165,17 @@ static uint8_t mpr121_resume(uint8_t addr, uint8_t ecr) write_reg(addr, MPR121_ELECTRODE_CONFIG_REG, ecr); } -void mpr121_debounce(uint8_t addr, uint8_t touch, uint8_t release) +void mpr121_filter(uint8_t addr, uint8_t ffi, uint8_t sfi) { uint8_t ecr = mpr121_stop(addr); - write_reg(addr, 0x5B, (release & 0x07) << 4 | (touch & 0x07)); + + uint8_t afe = read_reg(addr, MPR121_AFE_CONFIG_REG); + write_reg(addr, MPR121_AFE_CONFIG_REG, (afe & 0x3f) | ffi << 6); + uint8_t acc = read_reg(addr, MPR121_AUTOCONFIG_CONTROL_0_REG); + write_reg(addr, MPR121_AUTOCONFIG_CONTROL_0_REG, (acc & 0x3f) | ffi << 6); + uint8_t fcr = read_reg(addr, MPR121_FILTER_CONFIG_REG); + write_reg(addr, MPR121_FILTER_CONFIG_REG, (fcr & 0xe7) | (sfi & 3) << 3); + mpr121_resume(addr, ecr); } @@ -184,3 +191,10 @@ void mpr121_sense(uint8_t addr, int8_t sense, int8_t *sense_keys) } mpr121_resume(addr, ecr); } + +void mpr121_debounce(uint8_t addr, uint8_t touch, uint8_t release) +{ + uint8_t ecr = mpr121_stop(addr); + write_reg(addr, 0x5B, (release & 0x07) << 4 | (touch & 0x07)); + mpr121_resume(addr, ecr); +} diff --git a/firmware/src/mpr121.h b/firmware/src/mpr121.h index 9972615..bf84a4c 100644 --- a/firmware/src/mpr121.h +++ b/firmware/src/mpr121.h @@ -8,8 +8,11 @@ #define MP121_H void mpr121_init(uint8_t addr); + uint16_t mpr121_touched(uint8_t addr); -void mpr121_debounce(uint8_t addr, uint8_t touch, uint8_t release); + +void mpr121_filter(uint8_t addr, uint8_t ffi, uint8_t sfi); void mpr121_sense(uint8_t addr, int8_t sense, int8_t *sense_keys); +void mpr121_debounce(uint8_t addr, uint8_t touch, uint8_t release); #endif diff --git a/firmware/src/slider.c b/firmware/src/slider.c index ce09d04..446f027 100644 --- a/firmware/src/slider.c +++ b/firmware/src/slider.c @@ -63,5 +63,7 @@ void slider_update_config() mpr121_debounce(MPR121_ADDR + m, chu_cfg->sense.debounce_touch, chu_cfg->sense.debounce_release); mpr121_sense(MPR121_ADDR + m, chu_cfg->sense.global, chu_cfg->sense.keys + m * 12); + mpr121_filter(MPR121_ADDR + m, chu_cfg->sense.filter & 0x0f, + (chu_cfg->sense.filter >> 4) & 0x0f); } }