Touch related command line finished

This commit is contained in:
whowechina 2023-12-09 12:33:29 +08:00
parent fb074707db
commit be41372545
7 changed files with 62 additions and 51 deletions

Binary file not shown.

View File

@ -26,6 +26,15 @@ static void disp_rgb()
mai_cfg->color.key_on, mai_cfg->color.key_off, mai_cfg->color.level);
}
static void print_sense_zone(const char *title, const uint8_t *zones, int num)
{
printf(" %s |", title);
for (int i = 0; i < num; i++) {
printf("%2d |", zones[i]);
}
printf("\n");
}
static void disp_sense()
{
printf("[Sense]\n");
@ -33,17 +42,12 @@ static void disp_sense()
(mai_cfg->sense.filter >> 4) & 0x03,
mai_cfg->sense.filter & 0x07);
printf(" Sensitivity (global: %+d):\n", mai_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 |");
for (int i = 0; i < 16; i++) {
printf("%+2d|", mai_cfg->sense.keys[i * 2]);
}
printf("\n B |");
for (int i = 0; i < 16; i++) {
printf("%+2d|", mai_cfg->sense.keys[i * 2 + 1]);
}
printf("\n");
printf(" |_1_|_2_|_3_|_4_|_5_|_6_|_7_|_8_|\n");
print_sense_zone("A", mai_cfg->sense.zones, 8);
print_sense_zone("B", mai_cfg->sense.zones + 8, 8);
print_sense_zone("C", mai_cfg->sense.zones + 16, 2);
print_sense_zone("D", mai_cfg->sense.zones + 18, 8);
print_sense_zone("E", mai_cfg->sense.zones + 26, 8);
printf(" Debounce (touch, release): %d, %d\n",
mai_cfg->sense.debounce_touch, mai_cfg->sense.debounce_release);
}
@ -154,7 +158,7 @@ static void handle_stat(int argc, char *argv[])
static void handle_hid(int argc, char *argv[])
{
const char *usage = "Usage: hid <joy|key1|key2\n";
const char *usage = "Usage: hid <joy|key1|key2>\n";
if (argc != 1) {
printf(usage);
return;
@ -220,23 +224,23 @@ static void handle_filter(int argc, char *argv[])
static uint8_t *extract_key(const char *param)
{
int len = strlen(param);
int offset;
if (toupper(param[len - 1]) == 'A') {
offset = 0;
} else if (toupper(param[len - 1]) == 'B') {
offset = 1;
} else {
if (strlen(param) != 2) {
return NULL;
}
int id = cli_extract_non_neg_int(param, len - 1) - 1;
if ((id < 0) || (id > 15)) {
int zone = param[0] - 'A';
int id = param[1] - '1';
if (zone < 0 || zone > 4 || id < 0 || id > 7) {
return NULL;
}
if ((zone == 2) && (id > 1)) {
return NULL; // C1 and C2 only
}
return &mai_cfg->sense.keys[id * 2 + offset];
const int offsets[] = { 0, 8, 16, 18, 26 };
return &mai_cfg->sense.zones[offsets[zone] + id];
}
static void sense_do_op(int8_t *target, char op)
@ -260,8 +264,8 @@ static void handle_sense(int argc, char *argv[])
"Example:\n"
" >sense +\n"
" >sense -\n"
" >sense 1A +\n"
" >sense 13B -\n";
" >sense A3 +\n"
" >sense C1 -\n";
" >sense * 0\n";
if ((argc < 1) || (argc > 2)) {
printf(usage);
@ -278,8 +282,8 @@ static void handle_sense(int argc, char *argv[])
sense_do_op(&mai_cfg->sense.global, op[0]);
} else {
if (strcmp(argv[0], "*") == 0) {
for (int i = 0; i < 32; i++) {
sense_do_op(&mai_cfg->sense.keys[i], op[0]);
for (int i = 0; i < sizeof(mai_cfg->sense.zones); i++) {
sense_do_op(&mai_cfg->sense.zones[i], op[0]);
}
} else {
uint8_t *key = extract_key(argv[0]);
@ -328,21 +332,27 @@ static void handle_debounce(int argc, char *argv[])
disp_sense();
}
static void handle_raw()
static void print_raw_zones(const char *title, const uint16_t *raw, int num)
{
printf("Key raw readings:\n");
const uint16_t *raw = touch_raw();
printf("|");
for (int i = 0; i < 16; i++) {
printf("%3d|", raw[i * 2]);
}
printf("\n|");
for (int i = 0; i < 16; i++) {
printf("%3d|", raw[i * 2 + 1]);
printf(" %s |", title);
for (int i = 0; i < num; i++) {
printf(" %3d |", raw[i]);
}
printf("\n");
}
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();
print_raw_zones("A", raw, 8);
print_raw_zones("B", raw + 8, 8);
print_raw_zones("C", raw + 16, 2);
print_raw_zones("D", raw + 18, 8);
print_raw_zones("E", raw + 26, 8);
}
static void handle_whoami()
{
const char *msg[] = {"\nThis is Command Line port.\n", "\nThis is Touch port.\n", "\nThis is LED port.\n"};

View File

@ -23,8 +23,8 @@ static mai_cfg_t default_cfg = {
.debounce_release = 2,
},
.hid = {
.joy = 1,
.nkro = 0,
.joy = 0,
.nkro = 1,
},
.rgb = {
.per_button = 1,
@ -42,23 +42,23 @@ static void config_loaded()
{
if ((mai_cfg->sense.filter & 0x0f) > 3 ||
((mai_cfg->sense.filter >> 4) & 0x0f) > 3) {
mai_cfg->sense.filter = default_cfg.sense.filter;
mai_cfg->sense = default_cfg.sense;
config_changed();
}
if (!in_range(mai_cfg->sense.global, -9, 9)) {
mai_cfg->sense.global = default_cfg.sense.global;
mai_cfg->sense = default_cfg.sense;
config_changed();
}
for (int i = 0; i < 32; i++) {
if (!in_range(mai_cfg->sense.keys[i], -9, 9)) {
mai_cfg->sense.keys[i] = default_cfg.sense.keys[i];
for (int i = 0; i < 34; i++) {
if (!in_range(mai_cfg->sense.zones[i], -9, 9)) {
mai_cfg->sense = default_cfg.sense;
config_changed();
break;
}
}
if (!in_range(mai_cfg->sense.debounce_touch, 0, 7) ||
!in_range(mai_cfg->sense.debounce_release, 0, 7)) {
mai_cfg->sense.debounce_touch = default_cfg.sense.debounce_touch;
mai_cfg->sense.debounce_release = default_cfg.sense.debounce_release;
mai_cfg->sense = default_cfg.sense;
config_changed();
}

View File

@ -20,7 +20,7 @@ typedef struct __attribute__((packed)) {
int8_t global;
uint8_t debounce_touch;
uint8_t debounce_release;
int8_t keys[32];
int8_t zones[34];
} sense;
struct {
uint8_t joy : 4;

View File

@ -189,10 +189,10 @@ void mpr121_filter(uint8_t addr, uint8_t ffi, uint8_t sfi, uint8_t esi)
mpr121_resume(addr, ecr);
}
void mpr121_sense(uint8_t addr, int8_t sense, int8_t *sense_keys)
void mpr121_sense(uint8_t addr, int8_t sense, int8_t *sense_keys, int num)
{
uint8_t ecr = mpr121_stop(addr);
for (int i = 0; i < 12; i++) {
for (int i = 0; i < num; i++) {
int8_t delta = sense + sense_keys[i];
write_reg(addr, MPR121_TOUCH_THRESHOLD_REG + i * 2,
TOUCH_THRESHOLD_BASE - delta);

View File

@ -12,7 +12,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);
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);
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);
#endif

View File

@ -134,7 +134,8 @@ void touch_update_config()
mpr121_debounce(MPR121_ADDR + m, mai_cfg->sense.debounce_touch,
mai_cfg->sense.debounce_release);
mpr121_sense(MPR121_ADDR + m, mai_cfg->sense.global,
mai_cfg->sense.keys + m * 12);
mai_cfg->sense.zones + m * 12,
m != 2 ? 12 : 10);
mpr121_filter(MPR121_ADDR + m, mai_cfg->sense.filter >> 6,
(mai_cfg->sense.filter >> 4) & 0x03,
mai_cfg->sense.filter & 0x07);