你好,我正在尝试创建一个 DIY HID 游戏手柄,稍后我将使用 Arduino Pro Micro 将其集成到更大的东西中,但我是 Arduino/MCU 开发/编程的新手,我正在尝试创建一个带有 4x4 按钮的游戏手柄矩阵和两个使用 Azoteq IQS570 电容式触摸控制器的 Azoteq TPS43-201A-S 电容式触控板,我不确定我是否正确编写了矩阵代码,而且我不知道如何为触控板编写代码,这里是我用代码走了多远。
#include <Keypad.h>
#include <Gamepad.h>
// Define the 4x4 button matrix
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'Y', 'X', 'B', 'A'},
{'LEFT', 'RIGHT', 'UP', 'DOWN'},
{'LB', 'RB', 'SE', 'MU'},
{'JL', 'JR', 'E1', 'E2'}
};
// Define the pin numbers for the rows and columns of the matrix
byte rowPins[ROWS] = {9, 8, 7, 6}; // Row pins are connected to Arduino pins 9, 8, 7, 6
byte colPins[COLS] = {5, 4, 3, 2}; // Column pins are connected to Arduino pins 5, 4, 3, 2
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Gamepad gp;
void setup() {
for (byte i = 0; i < ROWS; i++) {
pinMode(rowPins[i], INPUT_PULLUP);
}
for (byte i = 0; i < COLS; i++) {
pinMode(colPins[i], INPUT_PULLUP);
}
pinMode(A0, INPUT); //right stick y-axis
pinMode(A1, INPUT); //right stick x-axis
pinMode(A2, INPUT); //left stick y-axis
pinMode(A3, INPUT); //left stick x-axis
pinMode(A4, INPUT); //left trigger
pinMode(A5, INPUT); //right trigger
calibrate();
}
void loop() {
int rt, lt, lx, ly, rx, ry;
rt = analogRead(A5);
lt = analogRead(A4);
lx = analogRead(A3);
ly = analogRead(A2);
rx = analogRead(A1);
ry = analogRead(A0);
// Convert 0-1000 to -127 - 127
rt = floor((rt - rightTcenter) * multiplierRT);
lt = floor((lt - leftTcenter) * multiplierLT);
lx = floor((lx - leftXcenter) * multiplierLX);
ly = floor((ly - leftYcenter) * multiplierLY);
rx = floor((rx - rightXcenter) * multiplierRX);
ry = floor((ry - rightYcenter) * multiplierRY);
if (rt > 127) rt = 127;
if (lt > 127) lt = 127;
if (lx > 127) lx = 127;
if (ly > 127) ly = 127;
if (rx > 127) rx = 127;
if (ry > 127) ry = 127;
gp.setRightTaxis(rt);
gp.setLeftTaxis(lt);
gp.setLeftXaxis(lx);
gp.setRightXaxis(rx);
gp.setLeftYaxis(ly);
gp.setRightYaxis(ry);
char key = keypad.getKey();
if (key) {
handleButtonPress(key);
}
delay(20);
}
void handleButtonPress(char button) {
for (byte row = 0; row < ROWS; row++) {
for (byte col = 0; col < COLS; col++) {
if (button == keys[row][col]) {
if (digitalRead(rowPins[row]) == LOW && digitalRead(colPins[col]) == LOW) {
gp.setButtonState(row * COLS + col, true);
} else {
gp.setButtonState(row * COLS + col, false);
}
}
}
}
}
int rightTcenter = 500;
int leftTcenter = 500;
int rightXcenter = 500;
int rightYcenter = 500;
int leftXcenter = 500;
int leftYcenter = 500;
double multiplierRT = 0.254; //127 / 500
double multiplierLT = 0.254;
double multiplierRX = 0.254;
double multiplierRY = 0.254;
double multiplierLX = 0.254;
double multiplierLY = 0.254;
void calibrate() {
int rt, lt, lx, ly, rx, ry;
int i = 0;
while (i < 13) {
rt = analogRead(A5);
lt = analogRead(A4);
lx = analogRead(A3);
ly = analogRead(A2);
rx = analogRead(A1);
ry = analogRead(A0);
bool validRT = rt > (rightTcenter - 100) && rt < (rightTcenter + 100);
bool validLT = lt > (leftTcenter - 100) && lt < (leftTcenter + 100);
bool validLX = lx > (leftXcenter - 100) && lx < (leftXcenter + 100);
bool validLY = ly > (leftYcenter - 100) && ly < (leftYcenter + 100);
bool validRX = rx > (rightXcenter - 100) && rx < (rightXcenter + 100);
bool validRY = ry > (rightYcenter - 100) && ry < (rightYcenter + 100);
if (validRT && validLT && validLX && validLY && validRX && validRY) {
i++;
//nothing to do here!
} else
i = 0;
delay(20);
}
rightTcenter = rt;
leftTcenter = lt;
leftXcenter = lx;
leftYcenter = ly;
rightXcenter = rx;
rightYcenter = ry;
multiplierRT = (double)127 / (double)rt;
multiplierLT = (double)127 / (double)lt;
multiplierLX = (double)127 / (double)lx;
multiplierLY = (double)127 / (double)ly;
multiplierRX = (double)127 / (double)rx;
multiplierRY = (double)127 / (double)ry;
}
我尝试给每个按钮一个别针,但没有足够的别针。所以,我切换到矩阵。我还尝试在网上搜索有关 TPS43-201A-S 或 IQS570 的信息,但看起来除了两三个人(其中一个在 Reddit 上)之外,没有人知道任何事情。然而,他在 Linux 上使用触控板,而我尝试在 Arduino Pro Micro 上使用它。
您在
keys
和 matrixKeys
中不必要地重复了键名数组(小问题,不是造成麻烦的原因)
您将
char button
作为参数传递给 handleButtonPress
函数,然后在该函数内将其与(重复)matrixKeys
数组的元素进行比较。这永远不会起作用 - 你的 matrixKeys
是一个字符串指针数组,因此将它们与 char
进行比较永远不会导致匹配。
看来您也错误地使用了 Arduino Keypad 库。
文档here似乎暗示您应该使用
char
数组来初始化它,而不是像您所做的那样使用字符串指针数组。keys[ROWS][COLS]
应该用类似的东西初始化:
{
{'Y', 'X', 'B', 'A'},
{'L', 'R', 'U', 'D'},
{'1', '2', '3', '4'},
{'5', '6', '7', '8'}
};
您可以随意将
1
到 8
数字替换为对您更有意义的内容,只要它们保持单个字符即可。handleButtonPress
内部,不需要声明包含同一组 char
元素的新数组,只需引用 keys
。