Touch remap working.

This commit is contained in:
whowechina 2024-04-05 20:55:27 +08:00
parent 361d0315c1
commit 096e5f6948
6 changed files with 145 additions and 13 deletions

Binary file not shown.

View File

@ -24,7 +24,7 @@
#define TOUCH_MAP { E3, A2, B2, D2, E2, A1, B1, D1, E1, C2, A8, B8, \
D8, E8, A7, B7, D7, E7, A6, B6, D6, E6, A5, B5, \
D5, E5, C1, A4, B4, D4, E4, A3, B3, D3 }
D5, E5, C1, A4, B4, D4, E4, A3, B3, D3, XX, XX }
#else
#endif

View File

@ -9,6 +9,7 @@
#include "tusb.h"
#include "mpr121.h"
#include "touch.h"
#include "button.h"
#include "config.h"
@ -16,6 +17,7 @@
#include "cli.h"
#include "aime.h"
#include "nfc.h"
#define SENSE_LIMIT_MAX 9
#define SENSE_LIMIT_MIN -9
@ -66,6 +68,7 @@ static void disp_hid()
static void disp_aime()
{
printf("[AIME]\n");
printf(" NFC Module: %s\n", nfc_module_name());
printf(" Virtual AIC: %s\n", mai_cfg->aime.virtual_aic ? "ON" : "OFF");
}
@ -81,6 +84,21 @@ static void disp_gpio()
button_real_gpio(10), button_real_gpio(11));
}
static void disp_touch()
{
printf("[Touch]\n");
printf(" ADDR|_0|_1|_2|_3|_4|_5|_6|_7|_8|_9|10|11|\n");
for (int m = 0; m < 3; m++) {
printf(" %d: 0x%02x|", m, MPR121_BASE_ADDR + m);
for (int chn = 0; chn < 12; chn++) {
int key = touch_key_from_channel(m * 12 + chn);
printf("%2s|", touch_key_name(key));
}
printf("\n");
}
}
void handle_display(int argc, char *argv[])
{
const char *usage = "Usage: display [rgb|sense|hid|gpio|aime]\n";
@ -94,12 +112,13 @@ void handle_display(int argc, char *argv[])
disp_sense();
disp_hid();
disp_gpio();
disp_touch();
disp_aime();
return;
}
const char *choices[] = {"rgb", "sense", "hid", "gpio", "aime"};
switch (cli_match_prefix(choices, 5, argv[0])) {
const char *choices[] = {"rgb", "sense", "hid", "gpio", "touch", "aime"};
switch (cli_match_prefix(choices, 6, argv[0])) {
case 0:
disp_rgb();
break;
@ -113,6 +132,9 @@ void handle_display(int argc, char *argv[])
disp_gpio();
break;
case 4:
disp_touch();
break;
case 5:
disp_aime();
break;
default:
@ -456,6 +478,62 @@ static void handle_gpio(int argc, char *argv[])
disp_gpio();
}
static void detect_touch()
{
bool touched = false;
for (int i = 0; i < 34; i++) {
if (touch_touched(i)) {
touched = true;
printf("Touched: %s", touch_key_name(i));
uint8_t pad = touch_key_channel(i);
if (pad >= 0) {
printf(" (Sensor %d, Electrode %d)\n", pad / 12, pad % 12 + 1);
} else {
printf(" (nil)\n");
}
}
}
if (!touched) {
printf("No touch detected.\n");
}
}
static bool set_touch_map(int argc, char *argv[])
{
if (argc != 3) {
return false;
}
if (strlen(argv[2]) != 2) {
return false;
}
int sensor = cli_extract_non_neg_int(argv[0], 0);
int channel = cli_extract_non_neg_int(argv[1], 0);
int key = touch_key_by_name(argv[2]);
if ((sensor < 0) || (sensor > 2) ||
(channel < 0) || (channel > 11) ||
(key < 0)) {
return false;
}
touch_set_map(sensor * 12 + channel, key);
return true;
}
static void handle_touch(int argc, char *argv[])
{
const char *usage = "Usage: touch [<sensor> <channel> <key>]\n"
" sensor: 0..2\n"
" channel: 0..11\n"
" key: A1, C2, E5, etc. XX means Not Connected.)\n";
if (argc == 0) {
detect_touch();
} else if (set_touch_map(argc, argv)) {
disp_touch();
} else {
printf(usage);
}
}
static void handle_virtual(int argc, char *argv[])
{
const char *usage = "Usage: virtual <on|off>\n";
@ -491,6 +569,7 @@ void commands_init()
cli_register("whoami", handle_whoami, "Identify each com port.");
cli_register("save", handle_save, "Save config to flash.");
cli_register("gpio", handle_gpio, "Set GPIO pins for buttons.");
cli_register("touch", handle_touch, "Custimze touch mapping.");
cli_register("factory", config_factory_reset, "Reset everything to default.");
cli_register("virtual", handle_virtual, "Virtual AIC card on AIME.");
}

View File

@ -34,7 +34,7 @@ typedef struct __attribute__((packed)) {
} rgb;
struct {
uint8_t buttons[12];
uint8_t touch[34];
uint8_t touch[36];
} alt;
struct {
bool virtual_aic;

View File

@ -9,6 +9,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include "bsp/board.h"
@ -40,21 +41,69 @@ void touch_init()
memcpy(touch_map, mai_cfg->alt.touch, sizeof(touch_map));
}
const char *touch_pad_name(unsigned i)
const char *touch_key_name(unsigned key)
{
static char name[3] = { 0 };
if (i < 18) {
name[0] = "ABC"[i / 8];
name[1] = '1' + i % 8;
} else if (i < 34) {
name[0] = "DE"[(i - 18) / 8];
name[1] = '1' + (i - 18) % 8;
if (key < 18) {
name[0] = "ABC"[key / 8];
name[1] = '1' + key % 8;
} else if (key < 34) {
name[0] = "DE"[(key - 18) / 8];
name[1] = '1' + (key - 18) % 8;
} else {
return "XX";
}
return name;
}
int touch_key_by_name(const char *name)
{
if (strlen(name) != 2) {
return -1;
}
if (strcasecmp(name, "XX") == 0) {
return 255;
}
int zone = toupper(name[0]) - 'A';
int id = name[1] - '1';
if ((zone < 0) || (zone > 4) || (id < 0) || (id > 7)) {
return -1;
}
if ((zone == 2) && (id > 1)) {
return -1; // C1 and C2 only
}
const int offsets[] = { 0, 8, 16, 18, 26 };
return offsets[zone] + id;
}
int touch_key_channel(unsigned key)
{
for (int i = 0; i < 36; i++) {
if (touch_map[i] == key) {
return i;
}
}
return -1;
}
unsigned touch_key_from_channel(unsigned channel)
{
if (channel < 36) {
return touch_map[channel];
}
return 0xff;
}
void touch_set_map(unsigned sensor, unsigned key)
{
if (sensor < 36) {
touch_map[sensor] = key;
memcpy(mai_cfg->alt.touch, touch_map, sizeof(mai_cfg->alt.touch));
config_changed();
}
}
static uint64_t touch_reading;
static void remap_reading()

View File

@ -9,7 +9,7 @@
#include <stdint.h>
#include <stdbool.h>
enum touch_pads {
enum touch_keys {
A1 = 0, A2, A3, A4, A5, A6, A7, A8,
B1, B2, B3, B4, B5, B6, B7, B8,
C1, C2, D1, D2, D3, D4, D5, D6, D7, D8,
@ -17,12 +17,16 @@ enum touch_pads {
XX = 255
};
const char *touch_pad_name(unsigned i);
const char *touch_key_name(unsigned key);
int touch_key_by_name(const char *name);
int touch_key_channel(unsigned key);
unsigned touch_key_from_channel(unsigned channel);
void touch_init();
void touch_update();
bool touch_touched(unsigned key);
uint64_t touch_touchmap();
void touch_set_map(unsigned sensor, unsigned key);
const uint16_t *touch_raw();
bool touch_sensor_ok(unsigned i);