This library has been very useful for me while setting up a joystick emulator with a Teensy LC and a FrSky XM receiver so I can use my Taranis transmitter as a Windows USB game controller.
I'm mapping channels 1 through 6 to the joystick analog axes, rotations, and sliders successfully.
However, I've run in to a problem. I'm also trying to map the remaining 10 channels (7 through 16) to act like joystick buttons and set a button to be pressed whenever the input from the SBUS channel is over a threshold. I seem to get this to work for three to four buttons at a time, but whenever I try to add code for more buttons/channel readings it seems I no longer get the SBUS library read function to return true any longer. Serial.print statements still produce output in the loop function before the read call, but nothing after ever is executed and the joystick basically never updates as a result.
I'm new to C++ and Arduinos so perhaps I'm just overlooking something obvious in my code?
Code:
#include <SBUS.h>
#define SBUS_MIN 172 // minimum value SBUS input
#define SBUS_MAX 1811 // maximum value SBUS input
#define ANALOG_MIN 0 // minimum value analog joystick output
#define ANALOG_MAX 1023 // maximum value analog joystick output
#define SBUS_ON 1600; // minimum value SBUS input to determine if button should be considered pressed
SBUS sbus(Serial1);
uint16_t channels[16];
uint8_t failSafe;
uint16_t lostFrames = 0;
void setup() {
sbus.begin();
}
void loop() {
if (!sbus.read(&channels[0], &failSafe, &lostFrames)) return;
Joystick.X(mapSbusToAnalog(channels[0]));
Joystick.Y(mapSbusToAnalog(channels[1]));
Joystick.Z(mapSbusToAnalog(channels[2]));
Joystick.Zrotate(mapSbusToAnalog(channels[3]));
Joystick.sliderLeft(mapSbusToAnalog(channels[4]));
Joystick.sliderRight(mapSbusToAnalog(channels[5]));
Joystick.button(1, mapSbusToDigital(channels[6]));
Joystick.button(2, mapSbusToDigital(channels[7]));
Joystick.button(3, mapSbusToDigital(channels[8]));
// Joystick.button(4, mapSbusToDigital(channels[9]));
// Joystick.button(5, mapSbusToDigital(channels[10]));
// Joystick.button(6, mapSbusToDigital(channels[11]));
// Joystick.button(7, mapSbusToDigital(channels[12]));
// Joystick.button(8, mapSbusToDigital(channels[13]));
// Joystick.button(9, mapSbusToDigital(channels[14]));
// Joystick.button(10, mapSbusToDigital(channels[15]));
}
long mapSbusToAnalog(uint16_t& value) {
return (value - SBUS_MIN) * (ANALOG_MAX - ANALOG_MIN) / (SBUS_MAX - SBUS_MIN) + ANALOG_MIN;
}
boolean mapSbusToDigital(uint16_t& value) {
return value >= SBUS_ON;
}
Whenever I uncomment the lines that set button 4 and/or 5, it seems I never get a SBUS packet read again. Sometimes button 4 seems to not cause this, sometimes it does... I initially tried all of them and that did not work. I slowly enable lines and was testing with Serial.print debug statements to isolate the problem when I found out that I could enable 3 or 4 buttons before the problem appeared again.
Am I overloading the Teensy LC somehow? Did I maybe get a bad Teensy LC? Is this some SBUS nuance I don't understand? Is this the joystick library? Is it my bad coding? I'm not even sure where to start looking further. Any ideas would be very appreciated!