mitxela.com forum
Welcome. Please log in or register.

Problem sending correct raw scans from RN-42
nxsm Posted: 13 Nov 2018, 03:30 PM
Avatar


Member
Posts: 6
Joined: 13-November 18
Greetings,
I am currently developing a custom gamepad for mobile devices which consists of :

9 Buttons,
x2 Analog Joysticks,
Bluetooth HC05 (Flashed with RN42 firmware),
Leonardo Pro Micro Arduino ATMEGA32U4 5V 16MHz.

After accessing the Command mode via serial monitor, I managed to set the Bluetooth device as an HID gamepad type device and the rest of configurations.

The problem is that the scans that I send via Serial1.write have a limitation for 16 buttons and 2 thumbsticks, the second thumbstick is being limited to Z and X axis. D-PAD (POV) is missing as well. What I need is a full range of available buttons like in popular Playstation or Xbox controllers.

Any idea how I can do that with this module?

Cheers



Last edit by nxsm at 13 Nov 2018, 03:31 PM

-------------
[top]
DAVID Posted: 14 Nov 2018, 12:12 AM
Avatar
I love mcus

Member
Posts: 237
Joined: 10-September 17
how do i delete this comment?

Last edit by DAVID at 14 Nov 2018, 12:18 AM

-------------
[top]
DAVID Posted: 14 Nov 2018, 12:17 AM
Avatar
I love mcus

Member
Posts: 237
Joined: 10-September 17
QUOTE (nxsm)
D-PAD (POV) is missing as well. What I need is a full range of available buttons like in popular Playstation or Xbox controllers.
i have test all the buttons and it supports the D-pad.
could you maybe share your code?
here is one that i made a while ago https://github.com/theawsomeavr/arduino-bluetooth-gamepad/blob/master/arduino_bl_gamepad/arduino_bl_gamepad.ino


Last edit by DAVID at 14 Nov 2018, 12:19 AM

-------------
[top]
nxsm Posted: 14 Nov 2018, 02:29 AM
Avatar


Member
Posts: 6
Joined: 13-November 18
Hi David, Thanks for your reply.

This is the code I use to send raw HID scans from Arduino through RN-42:


static const uint8_t dPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 16, 14};

const int joystickA0 = 0; // joystick 1 connected to analog pin 0
const int joystickA1 = 1; // joystick 1 connected to analog pin 1
const int joystickA2 = 2; // joystick 2 connected to analog pin 2
const int joystickA3 = 3; // joystick 2 connected to analog pin 3

const int range = 254;
int threshold = range / 10;
int center = range / 2;

typedef struct
{
uint8_t first;
uint8_t second;
uint8_t first_old;
uint8_t second_old;
uint8_t j1x;
uint8_t j1y;
uint8_t j2x;
uint8_t j2y;
uint8_t j1x_old;
uint8_t j1y_old;
uint8_t j2x_old;
uint8_t j2y_old;
} gamepad;

gamepad gp;

void setup() {

gp.first = 0;
gp.second = 0;
gp.first_old = 0;
gp.second_old = 0;
gp.j1x = 0;
gp.j1y = 0;
gp.j2x = 0;
gp.j2y = 0;
gp.j1x_old = 0;
gp.j1y_old = 0;
gp.j2x_old = 0;
gp.j2y_old = 0;

for (uint8_t p = 0; p < sizeof(dPins); p++) {
pinMode( dPins[p] , INPUT_PULLUP);
digitalWrite( dPins[p] , HIGH);
}
Serial1.begin(9600);
delay(500); //Delay
}

void loop() {

gp.j1x = readAxis(joystickA0);
gp.j1y = readAxis(joystickA1);
gp.j2x = readAxis(joystickA2);
gp.j2y = readAxis(joystickA3);


for (uint8_t p = 0; p < sizeof(dPins); p++) {
if (digitalRead( dPins[p] ) == HIGH) {

if (p == 9 || p == 10)
{
release( p + 1 );
}
else {
press( p + 1 );
}
}
else {

if (p == 9 || p == 10)
{
press( p + 1 );
}
else {
release( p + 1 );
}

}
}

//JoystickInput(gp.first, gp.second, gp.j1x, gp.j1y, gp.j2x, gp.j2y);

if ( gp.first_old != gp.first
|| gp.second_old != gp.second
|| gp.j1x != gp.j1x_old
|| gp.j1y != gp.j1y_old
|| gp.j2x != gp.j2x_old
|| gp.j2y != gp.j2y_old )
{
gp.first = gp.first_old;
gp.second = gp.second_old;
gp.j1x_old = gp.j1x;
gp.j1y_old = gp.j1y;
gp.j2x_old = gp.j2x;
gp.j2y_old = gp.j2y;

JoystickInput(gp.first, gp.second, gp.j1x, gp.j1y, gp.j2x, gp.j2y);
}

delay(20);
}

void JoystickInput(byte ST_BTN, byte ND_BTN, byte L_X, byte L_Y, byte R_X, byte R_Y) {

Serial1.write((byte)0xFD); // HID raw report descriptor
Serial1.write((byte)0x6); // Length
Serial1.write((byte)R_X); // Right X Axis
Serial1.write((byte)R_Y); // Right Y Axis
Serial1.write((byte)L_X); // Left X Axis
Serial1.write((byte)L_Y); // Left Y Axis
Serial1.write((byte)ST_BTN & 0xFF); // first 8 buttons
Serial1.write((byte)ND_BTN & 0xFF); // second 8 buttons
}

void press(uint8_t b) {
if (b > 7) {
b = b - 7;
gp.second_old |= (int)1 << (b - 1); // second byte
}
else {
gp.first_old |= (int)1 << (b - 1); // fisrt byte
}
}
void release(uint8_t b) {
if (b > 7) {
b = b - 7;
gp.second_old &= ~((uint8_t)1 << (b - 1)); // second byte
}
else {
gp.first_old &= ~((uint8_t)1 << (b - 1)); // first byte
}
}

byte readAxis(int thisAxis) {

int reading = analogRead(thisAxis);
reading = map(reading, 0, 1023, 0, range);
int distance = reading - center;

if (abs(distance) < threshold) {
distance = 0;
}

return (byte)distance;
}


And this is gamepad recognized outputs:


(User posted image)


As you can see, there are a lot of buttons and axis including D-PAD missing. I have no idea how to add them. I will test your code ASAP and see if it works.


Last edit by nxsm at 14 Nov 2018, 02:46 AM

-------------
[top]
DAVID Posted: 14 Nov 2018, 02:53 AM
Avatar
I love mcus

Member
Posts: 237
Joined: 10-September 17
QUOTE (nxsm)
As you can see, there are a lot of buttons and axis including D-PAD missing. I have no idea how to add them. I will test your code ASAP and see if it works.
that is because of windows itself, you can try this website instead https://gamepadviewer.com/


Last edit by DAVID at 14 Nov 2018, 02:53 AM

-------------
[top]
nxsm Posted: 14 Nov 2018, 02:56 AM
Avatar


Member
Posts: 6
Joined: 13-November 18
Just tested your code and it shows the same thing.
I noticed you use different Baud rate than I do. Mine is 9600 but could that be the issue?

Edit to your previous comment: I noticed that is not only windows as I tried playing games on my android and Y Rotation is not working as well as it doesn't show in windows!

Thanks

Last edit by nxsm at 14 Nov 2018, 02:57 AM

-------------
[top]
DAVID Posted: 14 Nov 2018, 03:02 AM
Avatar
I love mcus

Member
Posts: 237
Joined: 10-September 17
so the Y axes do not work to you?

Last edit by DAVID at 14 Nov 2018, 03:03 AM

-------------
[top]
nxsm Posted: 14 Nov 2018, 03:09 AM
Avatar


Member
Posts: 6
Joined: 13-November 18
My right thumbstick is recognized as X Rotation on X-Axis and Z on Y-Axis. The 2 available outputs you can see in my screenshot. I would like to have the Y Rotation instead mapped to my right thumbstick Y-Axis. Also, I tried the website you shared and there is no D-PAD recognition as well. What buttons and axis do work for you?

Thanks again.


UPDATE: Okay, I managed to add D-PAD by cheating on the number of the button I input. It appears that if I send 12,13,14,15 it recognizes this buttons as D-PAD on gamepadviewer.com but as buttons 13, 14, 15, 16 on windows gamepad properties instead. I thought it would be possible to modify it so that it would show as D-PAD(Axis type calculation).
Is it even possible to use other Y, Z Rotation, Slider and Dial outputs?

Last edit by nxsm at 14 Nov 2018, 04:47 AM

-------------
[top]
DAVID Posted: 14 Nov 2018, 08:23 AM
Avatar
I love mcus

Member
Posts: 237
Joined: 10-September 17
QUOTE
I thought it would be possible to modify it so that it would show as D-PAD(Axis type calculation).
So you are talking about if the hc05 can show up as a d pad
Sorry for responding late i have been really busy doing homework

Last edit by DAVID at 14 Nov 2018, 08:23 AM

-------------
[top]
nxsm Posted: 14 Nov 2018, 09:05 AM
Avatar


Member
Posts: 6
Joined: 13-November 18
QUOTE (DAVID)
QUOTE
I thought it would be possible to modify it so that it would show as D-PAD(Axis type calculation).
So you are talking about if the hc05 can show up as a d pad
Sorry for responding late i have been really busy doing homework

It's no problem. Thank you for finding time to help me.
As I flashed the HC05 with RN42 now it behaves as an HID, and to be specific I flagged it as a gamepad. My question is: Would it be possible to access the extra Rotation axes (Rotation Y) and in general customize the descriptor report for this specific module? It is my first time working on Bluetooth HID. I know that I can modify the descriptors and send more than 16 buttons if using the USB cable. Isn't RN-42 converts the data in the same way, then why am I limited to this kind of presets?

-------------
[top]
DAVID Posted: 14 Nov 2018, 09:32 PM
Avatar
I love mcus

Member
Posts: 237
Joined: 10-September 17
sadly no, since the firmware for the rn42 is close source you can only use the 2 joysticks and 16 buttons

-------------
[top]
nxsm Posted: 15 Nov 2018, 12:04 AM
Avatar


Member
Posts: 6
Joined: 13-November 18
QUOTE (DAVID)
sadly no, since the firmware for the rn42 is close source you can only use the 2 joysticks and 16 buttons

I see. Thank you for clarifying that for me!
Do you know any other BT modules with open source firmware that can allow me to do this?

-------------
[top]
DAVID Posted: 15 Nov 2018, 12:44 PM
Avatar
I love mcus

Member
Posts: 237
Joined: 10-September 17
not really, only thing that i know is about those 4.0 bluetooth modules that can even support bl midi but i am not sure if they have another axis

-------------
[top]
DAVID Posted: 15 Nov 2018, 12:46 PM
Avatar
I love mcus

Member
Posts: 237
Joined: 10-September 17
if you are on pc it would be really nice testing out the usb joystick library for the atmega32u4 and if your phone supports OTG that is something great to have

Last edit by DAVID at 15 Nov 2018, 12:51 PM

-------------
[top]

Sign in to post a reply.