I need to adapt the following code, written for a Teensy 2.0 (from here http://forum.il2sturmovik.com/topic/179 … entry46472 ), to run on a Mega:
/* USB FLCS Grip
You must select Joystick from the "Tools > USB Type" menu
*/
// Buttons are muxed into shift registers, use the SPI protocol to read them
#include <SPI.h>
const int slaveSelectPin = 0;
unsigned int buttonInputs1; // data read from SPI
unsigned int buttonInputs2;
unsigned int buttonInputs3;
// Use some macros to clean things up
#define S3 !(buttonInputs1 & 0x80) /* Pinky Switch */
#define TG1 !(buttonInputs1 & 0x40) /* Trigger 1 */
#define TG2 !(buttonInputs1 & 0x20) /* Trigger 2 */
#define S1 !(buttonInputs1 & 0x10) /* Nose Wheel Steering */
#define S4 !(buttonInputs1 & 0x08) /* Paddle Switch */
#define S2 !(buttonInputs1 & 0x04) /* Pickle */
#define H1D !(buttonInputs2 & 0x80) /* Trim */
#define H1R !(buttonInputs2 & 0x40)
#define H1U !(buttonInputs2 & 0x20)
#define H1L !(buttonInputs2 & 0x10)
#define H4U !(buttonInputs2 & 0x08) /* CMS */
#define H4L !(buttonInputs2 & 0x04)
#define H4D !(buttonInputs2 & 0x02)
#define H4R !(buttonInputs2 & 0x01)
#define H3D !(buttonInputs3 & 0x80) /* DMS */
#define H3R !(buttonInputs3 & 0x40)
#define H3U !(buttonInputs3 & 0x20)
#define H3L !(buttonInputs3 & 0x10)
#define H2D !(buttonInputs3 & 0x08) /* TMS */
#define H2R !(buttonInputs3 & 0x04)
#define H2U !(buttonInputs3 & 0x02)
#define H2L !(buttonInputs3 & 0x01)
// setup() runs once on boot
void setup() {
// set the slaveSelectPin as an output:
pinMode (slaveSelectPin, OUTPUT);
// start the SPI library:
SPI.begin();
// configure the joystick to manual send mode. This gives precise
// control over when the computer receives updates, but it does
// require you to manually call Joystick.send_now().
Joystick.useManualSend(true);
}
// loop() runs for as long as power is applied
void loop() {
// take the SS pin low to select the chip
digitalWrite(slaveSelectPin,LOW);
// send a value of 0 to read the SPI bytes
buttonInputs1 = SPI.transfer(0x00);
buttonInputs2 = SPI.transfer(0x00);
buttonInputs3 = SPI.transfer(0x00);
// take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin,HIGH);
// Write to joystick buttons
Joystick.button(1, TG1);
Joystick.button(2, S2);
Joystick.button(3, S3);
Joystick.button(4, S4);
Joystick.button(5, S1);
Joystick.button(6, TG2);
Joystick.button(7, H2U);
Joystick.button(8, H2R);
Joystick.button(9, H2D);
Joystick.button(10, H2L);
Joystick.button(11, H3U);
Joystick.button(12, H3R);
Joystick.button(13, H3D);
Joystick.button(14, H3L);
Joystick.button(15, H4U);
Joystick.button(16, H4R);
Joystick.button(17, H4D);
Joystick.button(18, H4L);
//Joystick.button(19, H1U);
//Joystick.button(20, H1R);
//Joystick.button(21, H1D);
//Joystick.button(22, H1L);
// Determine Joystick Hat Position
int angle = -1;
if (H1U) {
if (H1R) {
angle = 45;
} else if (H1L) {
angle = 315;
} else {
angle = 0;
}
} else if (H1D) {
if (H1R) {
angle = 135;
} else if (H1L) {
angle = 225;
} else {
angle = 180;
}
} else if (H1R) {
angle = 90;
} else if (H1L) {
angle = 270;
}
Joystick.hat(angle);
// Because setup configured the Joystick manual send,
// the computer does not see any of the changes yet.
// This send_now() transmits everything all at once.
Joystick.send_now();
}
Comparing the pinouts of the Teensy 2.0 https://www.pjrc.com/teensy/pinout.html and the Mega http://arduino.cc/en/Main/arduinoBoardMega , I can see the latter uses pins 50-53 for the SPI (MISO, MOSI, SCK, SS), whereas the Teensy uses Pins 0-3 (SS, SCLK, MOSI, MISO).
The only pin assignment I can see in the code is for SS:
const int slaveSelectPin = 0;
so do I just need to change this to
const int slaveSelectPin = 53;
for the Mega? I guess it’s going to need some more code to run as a USB Joystick, as Arduino IDE doesn’t have “Tools > USB Type” to set to “Joystick”, so can anyone help out with that as well please? Alternatively, I could just run it as a USB Serial device (i.e. on a COM port) or via an Ethernet Shield and use some bridge software on the PC to convert the data into something suitable for my flight sims but I need an easy way to test that the joystick buttons/hats are sending signals before I get to that stage, maybe using the Serial Monitor?