Cleric-K / vJoySerialFeeder

Feed Virtual Joystick driver with data from a serial port
GNU General Public License v3.0
252 stars 55 forks source link

Issues with Arduino Mega #38

Closed CubicVirtuoso closed 2 years ago

CubicVirtuoso commented 3 years ago

Hey, sorry if this is a really basic question, but I'm having some issues with simple push buttons working on the Arduino Mega.

I have nothing connected to any analog pins, and nothing connected to Digital Bitmapped keys. My buttons are simply connected to Pins 40-51.

Firstly, I found that in the Ardunio sketch I believe I need to define analog pins even if I don't have anything connected. So I have the following definitions:

define NUM_ANALOG_INPUTS 16

byte analogPins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

define NUM_DIGITAL_INPUTS 12

byte digitalPins[] = {40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};

define NUM_DIGITAL_BITMAPPED_INPUTS 0

byte digitalBitmappedPins[] = {};

I've uploaded the sketch to the Arduino and connected to the Arduino in vJoySerialFeeder.

At the bottom it says I have 37 channels, which to me doesn't make sense? Shouldn't I have 28 Channels?

I believe Channel 1-16 are the analog channels and they are showing an input of 1023. Like I said I have nothing connected to the analog inputs on the Arduino anyway.

So I believe Channel 17 is Pin40 on the Arduino correct?

Input displays 0 and won't toggle if I push the button connected to the Arduino.

What am I doing wrong?

Cleric-K commented 3 years ago

Hi, I'm not sure why the number of channels is wrong. Anyway if you are not using the analog channels you should change the code to:

#define NUM_ANALOG_INPUTS 0
byte analogPins[] = {};
#define NUM_DIGITAL_INPUTS 12
byte digitalPins[] = {40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
#define NUM_DIGITAL_BITMAPPED_INPUTS 0
byte digitalBitmappedPins[] = {};

But I would recommend to use the bitmapped inputs like this:

#define NUM_ANALOG_INPUTS 0
byte analogPins[] = {};
#define NUM_DIGITAL_INPUTS 0
byte digitalPins[] = {};
#define NUM_DIGITAL_BITMAPPED_INPUTS 12
byte digitalBitmappedPins[] = {40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};

This will allow all 12 buttons to be packed in just one channel (2 bytes/16bits). Otherwise, with every frame you are sending 12 16bit channels of which really only 1 bit has any meaning, so it's a waste.

CubicVirtuoso commented 3 years ago

Thanks for the insight. I've reconfigured with your suggestion but still having issues with these momentary buttons.

Firstly when I upload the script I get the following warnings:

C:\Users\William\Desktop\vjoy-serialfeeder\Joystick\Joystick.ino:15:0: warning: "NUM_ANALOG_INPUTS" redefined

define NUM_ANALOG_INPUTS 0

In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:258:0, from sketch\Joystick.ino.cpp:1: C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\mega/pins_arduino.h:29:0: note: this is the location of the previous definition

define NUM_ANALOG_INPUTS 16

Secondly, in the vJoySerialFeeder App I now show only one channel detected, so I configured a bit-mapped button. Tied it to channel 1 and here are my settings for each bit: image

Nothing seems to be working, I'm not getting any feedback or seeing any button response in the vJoy Game controller.

Cleric-K commented 3 years ago

Oh, I see now. I had no idea that NUM_ANALOG_INPUTS is actually #define that is used in Arduino code itself =O I'll have to update the sketch to use another name. Please replace every instance of NUM_ANALOG_INPUTS inside the .ino file with something else.

About the the other problem: I completely forgot that recently there was update of vjsf that is probably the reason for this.

Some time ago it turned out that IBUS uses only 12bits (0-2047) for each channel. This leaves 4bits free from every 16. Thus FlySky is using the 4bits of three consecutive channels to pack another 12-bit channel. For this reason if you have 3 channels (2 bytes/16-bit each) in the frame, you can pack inside one additional 12-bit channel (3 times the 4 unused bits).

This is the reason that you got 37 channels instead of 28. 28/3 = 9.333... So with 28 16bit values you can pack 9 additional channels, giving the total of 37.

Newer AFHDS3 tranismitters pack these additional channels. Within the standard 14 channels they pack 4 additional for total of 18. That's why I've implemented this in vjsf, but also made it possible to use the old behavior that uses the full 16-bits for each channel. You have to click the Setup button next to the IBUS protocol selector and there choose 16-bit channels.

I had to document this but I completely forgot about all this.