Change Teensy HID Capabilities

Status
Not open for further replies.

SimonWakley

Active member
I have a single axis joystick I have wired to a Teensy 3.2 and would like it to coexist with an XBox style Game Pad, but they both return the first 5 Joystick values and so conflict. If I could remove them from the standard teensy joystick capabilities that would solve the issue. Does anyone have code examples to do this. Do I have to use Raw HID? At the moment I have hacked the receiver to make it work but that is not elegant.

it also has 3 buttons, but I have offset them to be after the gamepad buttons

At the moment i just selected Joystick as recommended in the sample I used:
Thanks,
Simon

/* Complete USB Joystick Example
Teensy becomes a USB joystick with 16 or 32 buttons and 6 axis input

You must select Joystick from the "Tools > USB Type" menu

Pushbuttons should be connected between the digital pins and ground.
Potentiometers should be connected to analog inputs 0 to 5.

This example code is in the public domain.
*/

// Configure the number of buttons. Be careful not
// to use a pin for both a digital button and analog
// axis. The pullup resistor will interfere with
// the analog voltage.
const int numButtons = 3; // Only using 3 atm

int led = 13;
int on = 0;
void setup()
{
// you can print to the serial monitor while the joystick is active!
Serial.begin(9600);

pinMode(led, OUTPUT);

// 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);

for (int i = 0; i < numButtons; i++)
{
pinMode(i, INPUT_PULLUP);
}

Serial.println("Begin Complete Joystick Test");
}

byte allButtons[numButtons];
byte prevButtons[numButtons];
int angle = 0;
int count = -256;
void loop()
{

Joystick.X(512);
Joystick.Y(512);
Joystick.Z(512);
Joystick.sliderLeft(512);
Joystick.sliderRight(512);

// Not used by the Game Pad
Joystick.Zrotate(analogRead(0));

if (count >= 256)
{
if (on)
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
else
digitalWrite(led, LOW); // turn the LED on (HIGH is the voltage level)

on = !on;
count = -256;
}


// read digital pins and use them for the buttons
// Just using 3 Inputs for now
for (int i = 0; i < numButtons; i++)
{
if (digitalRead(i))
{
// when a pin reads high, the button is not pressed
// the pullup resistor creates the "on" signal
allButtons = 0;
}
else
{
// when a pin reads low, the button is connecting to ground.
allButtons = 1;
}
// Offset to write to the 12,13 and 14th button
// Button 10 & 11 is used by pushing down on the XBox joysticks
Joystick.button(i + 11, allButtons);
}

// 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();

// check to see if any button changed since last time
boolean anyChange = false;
for (int i = 0; i < numButtons; i++)
{
if (allButtons != prevButtons)
anyChange = true;
prevButtons = allButtons;
}

// if any button changed, print them to the serial monitor
if (anyChange)
{
Serial.print("Buttons: ");
for (int i = 0; i < numButtons; i++)
{
Serial.print(allButtons, DEC);
}
Serial.println();
}

// a brief delay, so this runs "only" 20 times per second
delay(50);
}
 
Status
Not open for further replies.
Back
Top