joystick interface now works with arbitrary joystick axes

code now scans all available axes for changes, then selects the first
two as X and Y, rather than relying on joysticks to have X and Y at 0
and 1.
This commit is contained in:
Charles Mangin 2014-02-05 15:02:22 -05:00
parent 3ca006d483
commit f7138bd1bc
4 changed files with 97 additions and 49 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -2,11 +2,9 @@
#include <SPI.h>
// set pin 4 as the slave select for the digital pot:
const int slaveSelectPin = A0; //4;
//const float foo = .21;
// set pin A0 as the slave select for the digital pot:
const int slaveSelectPin = A0;
const int TrimPin = A3;
const int Butt0Pin = A4;
const int Butt1Pin = A5;
@ -41,17 +39,12 @@ void setup()
{
// set the slaveSelectPin as an output:
pinMode (slaveSelectPin, OUTPUT);
pinMode (Butt0Pin, OUTPUT);
pinMode (Butt1Pin, OUTPUT);
// initialize SPI:
SPI.begin();
Serial.begin( 115200 );
Serial.println("Start");
@ -63,41 +56,36 @@ void setup()
if (!Hid.SetReportParser(0, &Joy))
ErrorMessage<uint8_t>(PSTR("SetReportParser"), 1 );
}
void loop()
{
Usb.Task();
int channelX = 5;
int channelY = 3;
float foo = float(analogRead(TrimPin))/1023.000;
float calibrate = float(analogRead(TrimPin))/1023.000;
// need to determine if x or x2 is giving values
int joyX = JoyEvents.X;
int channel = 5;
digitalPotWrite(channel, joyX * foo);
digitalPotWrite(channel - 1, joyX * foo);
// need to determine if y or y2 is giving values
int joyY = JoyEvents.Y;
channel = 3;
digitalPotWrite(channel, joyY * foo);
digitalPotWrite(channel - 1, joyY * foo);
// write the values to the POTs
digitalPotWrite(channelX, joyX * calibrate);
digitalPotWrite(channelX - 1, joyX * calibrate);
digitalPotWrite(channelY, joyY * calibrate);
digitalPotWrite(channelY - 1, joyY * calibrate);
// write the buttons
digitalWrite(Butt0Pin,JoyEvents.Butt0);
digitalWrite(Butt1Pin,JoyEvents.Butt1);
}
void digitalPotWrite(int address, int value) {
// take the SS pin low to select the chip:
digitalWrite(slaveSelectPin,LOW);

View File

@ -1,12 +1,7 @@
#include "hidjoystickrptparser.h"
JoystickReportParser::JoystickReportParser(JoystickEvents *evt) :
joyEvents(evt),
oldHat(0xDE),
oldButtons(0)
{
for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++)
@ -41,7 +36,8 @@ void JoystickReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t
// Calling Button Event Handler for every button changed
if (changes)
{
for (uint8_t i=0; i<0x0C; i++)
// for (uint8_t i=0; i<0x0C; i++) // 12 buttons 0x0C == 12
for (uint8_t i=0; i<0x0F; i++) // up to 16 buttons
{
uint16_t mask = (0x0001 << i);
@ -57,18 +53,71 @@ void JoystickReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t
void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt)
{
// got to find a better way to make an array from this structured vars
axes[0] = evt->axis0;
axes[1] = evt->axis1;
axes[2] = evt->axis2;
axes[3] = evt->axis3;
axes[4] = evt->axis4;
X = evt->X;
Y = evt->Y;
Serial.print("X: ");
Serial.print(X);
//PrintHex<uint8_t>(evt->X, 0x80);
Serial.print("\tY: ");
Serial.print(Y);
// PrintHex<uint8_t>(evt->Y, 0x80);
Serial.println("");
if(initialized) {
for(int i =0; i<5 ; i++ ){
if (axes[i] < axesMin[i] ) {
axesMin[i] = axes[i];
}
if (axes[i] > axesMax[i]) {
axesMax[i] = axes[i];
}
axesDelta[i] = axesMax[i] - axesMin[i];
}
for(int j =0; j<5 ; j++ ){
if(axesDelta[j] > 0) {
X = axes[j];
Y = axes[j+1];
break;
}
}
} else {
for(int i =0; i<5 ; i++ ){
axesMin[i] = axes[i];
axesMax[i] = axes[i];
}
initialized = true;
// don't bother with the first set of data, as joystick initializes.
}
Serial.print(X);
Serial.print(" ");
Serial.println(Y);
/*
keep track of axes 0 - 4 (5 axes)
min and max values. max-min = range
first axis with range, and first +1 are X and Y
*/
}
@ -76,11 +125,11 @@ void JoystickEvents::OnButtonUp(uint8_t but_id)
{
Serial.print("Up: ");
Serial.println(but_id);
if(but_id == 1) {
if(but_id % 2 == 0) { // all even numbered buttons = button 0
Butt0 = false;
}
if(but_id == 2) {
if(but_id % 2 == 1) { // all odd buttons = button 1
Butt1 = false;
}
@ -92,11 +141,11 @@ void JoystickEvents::OnButtonDn(uint8_t but_id)
Serial.println(but_id);
if(but_id == 1) {
if(but_id % 2 == 0) { // all even numbered buttons = button 0
Butt0 = true;
}
if(but_id == 2) {
if(but_id % 2 == 1) { // all odd buttons = button 1
Butt1 = true;
}
}

View File

@ -23,7 +23,7 @@
struct GamePadEventData
{
uint8_t X, Y;
uint8_t axis0, axis1, axis2, axis3, axis4; // read first 5 axes
};
class JoystickEvents
@ -36,6 +36,18 @@ public:
uint8_t X;
uint8_t Y;
int axes[5];
int axesMin[5];
int axesMax[5];
int axesDelta[5];
int activeX;
int activeY;
//arrays of min and max values
// var with which axis is X/Y
boolean initialized;
boolean Butt0;
boolean Butt1;
@ -48,7 +60,6 @@ class JoystickReportParser : public HIDReportParser
JoystickEvents *joyEvents;
uint8_t oldPad[RPT_GEMEPAD_LEN];
uint8_t oldHat;
uint16_t oldButtons;
public: