Adapt Teensy code to read joystick

I’ve got a Thrustmaster F16 FLCS stick, which I want to wire to an Arudino to convert it into a USB stick.

I found this post with code for doing it with a Teensy board http://forum.il2sturmovik.com/topic/179 … entry46472

and this code for using a Leonardo as a USB joystick http://www.imaginaryindustries.com/blog/?p=80 which I’ve tested on my Pro Micro and that works fine and sends random data to the Windows joystick control panel.

I wonder if anyone would be able to help me adapt and merge the two sets of code, to read both the buttons via SPI and the two pots on analog inputs for the X and Y stick axes?

I don’t really know where to start but in USBAPI.h, the main joystick-related code seems to be:

//	Joystick
//  Implemented in HID.cpp
//  The list of parameters here needs to match the implementation in HID.cpp


typedef struct JoyState 		// Pretty self explanitory. Simple state to store all the joystick parameters
{
	uint8_t		xAxis;
	uint8_t		yAxis;
	uint8_t		zAxis;

	uint8_t		xRotAxis;
	uint8_t		yRotAxis;
	uint8_t		zRotAxis;

	uint8_t		throttle;
	uint8_t		rudder;

	uint8_t		hatSw1;
	uint8_t		hatSw2;

	uint32_t	buttons;		// 32 general buttons

} JoyState_t;

class Joystick_
{
public:
	Joystick_();

	void setState(JoyState_t *joySt);

};
extern Joystick_ Joystick;

and in HID.cpp:

//	Joystick
//  Usage: Joystick.move(inputs go here)
//
//  The report data format must match the one defined in the descriptor exactly
//  or it either won't work, or the pc will make a mess of unpacking the data
//

Joystick_::Joystick_()
{
}


#define joyBytes 13 		// should be equivalent to sizeof(JoyState_t)

void Joystick_::setState(JoyState_t *joySt)
{
	uint8_t data[joyBytes];
	uint32_t buttonTmp;
	buttonTmp = joySt->buttons;

	data[0] = buttonTmp & 0xFF;		// Break 32 bit button-state out into 4 bytes, to send over USB
	buttonTmp >>= 8;
	data[1] = buttonTmp & 0xFF;
	buttonTmp >>= 8;
	data[2] = buttonTmp & 0xFF;
	buttonTmp >>= 8;
	data[3] = buttonTmp & 0xFF;

	data[4] = joySt->throttle;		// Throttle
	data[5] = joySt->rudder;		// Steering

	data[6] = (joySt->hatSw2 << 4) | joySt->hatSw1;		// Pack hat-switch states into a single byte

	data[7] = joySt->xAxis;		// X axis
	data[8] = joySt->yAxis;		// Y axis
	data[9] = joySt->zAxis;		// Z axis
	data[10] = joySt->xRotAxis;		// rX axis
	data[11] = joySt->yRotAxis;		// rY axis
	data[12] = joySt->zRotAxis;		// rZ axis

	//HID_SendReport(Report number, array of values in same order as HID descriptor, length)
	HID_SendReport(3, data, joyBytes);
	// The joystick is specified as using report 3 in the descriptor. That's where the "3" comes from
}