Created
May 17, 2015 21:04
-
-
Save expipiplus1/c47e44ab1ce0d74543ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WProgram.h> | |
#include <usb_serial.h> | |
#include <usb_joystick.h> | |
#include <joystick-input.h> | |
int main() { | |
pinMode(13, OUTPUT); | |
Joystick.useManualSend(true); | |
while (true) { | |
UpdateJoystickState(); | |
// Convert the signed representation into the unsigned representation expected by teensy | |
Joystick.axis(0, (CurrentJoystickState.Axes[0] + 0x8000) & 0xffff); | |
Joystick.axis(1, (CurrentJoystickState.Axes[1] + 0x8000) & 0xffff); | |
Joystick.axis(2, (CurrentJoystickState.Axes[2] + 0x8000) & 0xffff); | |
Joystick.axis(3, (CurrentJoystickState.Axes[3] + 0x8000) & 0xffff); | |
for(unsigned i = 0; i < JoystickState::NumButtonBytes * 8; ++i) | |
Joystick.button(i, GetJoystickButton(CurrentJoystickState, i)); | |
Joystick.send_now(); | |
if (GetJoystickButton(CurrentJoystickState, 7)){ | |
digitalWriteFast(13, HIGH); | |
} | |
else{ | |
digitalWriteFast(13, LOW); | |
} | |
} | |
} | |
///////////////////////////////////////////////////////// | |
// joystick-input.cpp | |
///////////////////////////////////////////////////////// | |
#include <joystick-input.h> | |
#include <usb_serial.h> | |
bool GetJoystickButton(const JoystickState &js, uint8_t button_index) { | |
return js.ButtonBytes[button_index / 8] & (1 << (button_index % 8)); | |
} | |
JoystickState CurrentJoystickState = {0}; | |
void DecodeJoystick(const uint8_t *buffer, JoystickState &state_out) { | |
// This is all quite nasty, todo use a stream class | |
const uint32_t magic = *reinterpret_cast<const uint32_t *>(buffer + 0); | |
const uint8_t num_axes = buffer[5]; | |
const uint8_t num_buttons_bytes = (buffer[6] + 7) / 8; | |
const int16_t *axes = reinterpret_cast<const int16_t *>(buffer + 7); | |
const uint8_t *button_bytes = | |
reinterpret_cast<const uint8_t *>(buffer + 7 + num_axes * 2); | |
memset(&state_out, 0, sizeof(state_out)); | |
for (uint32_t i = 0; i < JoystickState::NumAxes && i < num_axes; ++i) | |
state_out.Axes[i] = axes[i]; | |
for (uint32_t i = 0; | |
i < JoystickState::NumButtonBytes && i < num_buttons_bytes; ++i) | |
state_out.ButtonBytes[i] = button_bytes[i]; | |
} | |
bool UpdateJoystickState() { | |
const uint32_t buffer_size = 64; | |
static uint8_t buffer[buffer_size]; | |
static uint32_t bytes_read = 0; | |
while (Serial.available() && bytes_read < buffer_size) | |
{ | |
//buffer[bytes_read++] = Serial.read(); | |
Serial.readBytes(reinterpret_cast<char*>(buffer), buffer_size); | |
bytes_read = 64; | |
} | |
if (bytes_read != buffer_size) | |
return false; | |
bytes_read = 0; | |
DecodeJoystick(buffer, CurrentJoystickState); | |
// This runs the risk of missing events which happen for less than a frame It | |
// might be worth ORing the buttons together, to catch short presses at the | |
// expense of missing out on short releases... | |
//while (UpdateJoystickState()) { | |
//} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment