[英]Bluetooth Arduino Gamepad
我正在嘗試使用RN-42XV藍牙芯片和arduino制作具有2個模擬搖桿和16個按鈕的自定義藍牙控制器,可以與android和Windows pc一起使用。 到目前為止,我已經成功地將arduino leonardo編碼為具有2個模擬搖桿和16個按鈕的游戲手柄,但是我無法使arduino與我的RN-42XV藍牙一起工作。 RN-42XV連接到我的電腦,並顯示為帶有2個模擬搖桿和16個按鈕的游戲手柄,但是當我按下arduino上的按鈕時,它不會向RN-42注冊。 我使用的代碼如下,請提供幫助。
#include "Joystick2.h"
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#define BLUETOOTH_SERIAL_RATE 9600
//Buttons defined according to https://www.w3.org/TR/gamepad/
#define BUTTON_0 0
#define BUTTON_1 1
#define BUTTON_2 2
#define BUTTON_3 3
#define SHOULDER_LEFT 4
#define SHOULDER_RIGHT 5
#define TRIGGER_LEFT 6
#define TRIGGER_RIGHT 7
#define START 8
#define SELECT 9
#define THUMB_LEFT 10
#define THUMB_RIGHT 11
#define HAT_UP 12
#define HAT_DOWN 13
#define HAT_LEFT 14
#define HAT_RIGHT 15
#define HOME 16
#define x1 A1
#define y1 A0
#define x2 A3 // Pin 20
#define y2 A2 // Pin 19
void setup() {
Serial.begin(BLUETOOTH_SERIAL_RATE);
delay(500);
// Initialize Button Pins - comment out any unused
// pinMode(0, INPUT_PULLUP);
// pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
// pinMode(14, INPUT_PULLUP);
// pinMode(15, INPUT_PULLUP);
// pinMode(16, INPUT_PULLUP);
// pinMode(18, INPUT_PULLUP);
// pinMode(19, INPUT_PULLUP);
// pinMode(20, INPUT_PULLUP);
// pinMode(21, INPUT_PULLUP);
// Initialize Joystick Library
Joystick[0].begin();
Joystick[1].begin();
}
//Edit button to pin mapping according to your hardware - use -1 if unused
int pinToButtonMap[20][2] = {{BUTTON_0, 9},
{BUTTON_1, 8},
{BUTTON_2, 7},
{BUTTON_3, 6},
{SHOULDER_LEFT, 4},
{SHOULDER_RIGHT, 5},
{TRIGGER_LEFT, -1},
{TRIGGER_RIGHT, -1},
{START, 3},
{SELECT, 2},
{THUMB_LEFT, 10},
{THUMB_RIGHT, -1},
{HAT_UP, -1},
{THUMB_LEFT, -1},
{HAT_UP, -1},
{HAT_DOWN, -1},
{HAT_LEFT, -1},
{HAT_RIGHT, -1},
{HOME, -1}};
int btnState[20][2] = {{BUTTON_0, 0},
{BUTTON_1, 0},
{BUTTON_2, 0},
{BUTTON_3, 0},
{SHOULDER_LEFT, 0},
{SHOULDER_RIGHT, 0},
{TRIGGER_LEFT, 0},
{TRIGGER_RIGHT, 0},
{START, 0},
{SELECT, 0},
{THUMB_LEFT, 0},
{THUMB_RIGHT, 0},
{HAT_UP, 0},
{THUMB_LEFT, 0},
{HAT_UP, 0},
{HAT_DOWN, 0},
{HAT_LEFT, 0},
{HAT_RIGHT, 0},
{HOME, 0}};
void loop() {
for (int i = 0; i < 20; i++) {
if(pinToButtonMap[i][1] > -1) {
int currentState = !digitalRead(pinToButtonMap[i][1]);
if (currentState != btnState[i][1]) { // Detect button state change
Joystick[0].setButton(i, currentState);
Joystick[1].setButton(i, currentState);
btnState[i][1] = currentState;
}
}
}
Joystick[0].setXAxis(analogRead(x1));
Joystick[0].setYAxis(analogRead(y1));
Joystick[1].setXAxis(analogRead(x2));
Joystick[1].setYAxis(analogRead(y2));
delay(50);
}
void currentState(uint32_t btnState, int8_t x1, int8_t y1, int8_t x2, int8_t
y2)
{
//write the header part for RN42
Serial.write((uint8_t)0xFD); //start byte
Serial.write((uint8_t)0x06); //length of the descriptor
//gampad positions and buttons
//on a gamepad there typically is two analog joysticks one is typically
used to
//indicate x/y position and one is for z/rotation.
Serial.write((uint8_t)x1 & 0xFF); //value between -127 to 127 indicating
the x postition
Serial.write((uint8_t)y1 & 0xFF); //value between -127 to 127 indicating
the y postition
Serial.write((uint8_t)x2 & 0xFF); //value between -127 to 127 indicating
the z postition
Serial.write((uint8_t)y2 & 0xFF); //value between -127 to 127 indicating
the rotation postition
//one bit for each button pressed there can be a total of 16 buttons one
byte in each
//set the bit to show a button pressed and clear the bit to indicate not
pressed
uint8_t btnState1 = btnState & 0xFF;
uint8_t btnState2 = (btnState >> 8) & 0xFF;
Serial.write((uint8_t)btnState1);
Serial.write((uint8_t)btnState2);
}
您與RN-42XV用於HID游戲桿報告的報告描述符的格式不匹配。 受支持的HID報告的格式在《藍牙數據模塊命令參考和高級信息用戶指南》中進行了描述:
http://ww1.microchip.com/downloads/en/DeviceDoc/bluetooth_cr_UG-v1.0r.pdf
第5.3.3節介紹了操縱桿數據的原始報告格式:
字節0:0xFD(指示要遵循的原始HID報告)
字節1:0x06(報告長度)
字節2:按鈕0-7的狀態
字節3:按鈕8-15的狀態
字節4:軸X1
字節5:軸Y1
字節6:軸X2
字節7:軸Y2
您的currentState方法正在編寫:
字節0:0xFD(良好)
字節1:0x06(良好)
字節2:x1和0xFF(應為按鈕0-7)
字節3:y1和0xFF(應為按鈕8-15)
字節4:x2和0xFF(應為軸X1)
字節5:y2和0xFF(應為軸Y1)
字節6:btnState和0xFF(應為Axis X2)
字節7:(btnState >> 8)和0xFF(應為軸Y2)
我認為,如果您重新安排在軸狀態之前寫入按鈕狀態的方法,它將起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.