/* * Saitek X52 Arduino USB Host Shield Library. * Copyright 2016 by Thomas Buck * * Based on the USB Host Library HID Joystick example * https://www.circuitsathome.com/mcu/hid-joystick-code-sample * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation, version 2. */ #include #include "data.h" #include "events.h" //#define DEBUG_OUTPUT_RAW //#define DEBUG_OUTPUT // X, Y, Z, Rx, Ry, Rz, Slider const GamePadEventData JoystickEventsDeadZone::deadZone( 4, 4, 2, 2, 5, 25, 2 ); const uint8_t JoystickEventsDeadZone::deadZoneMouseX = 1; const uint8_t JoystickEventsDeadZone::deadZoneMouseY = 1; // X, Y, Z, Rx, Ry, Rz, Slider const GamePadEventData JoystickEventsDeadZone::centerValue( 0x3FF, 0x3FF, 0x7F, 0x7F, 0x7F, 0x1FF, 0x7F ); const uint8_t JoystickEventsDeadZone::centerMouseX = 0x07; const uint8_t JoystickEventsDeadZone::centerMouseY = 0x07; void JoystickEventsDeadZone::OnGamePadChanged(const GamePadEventData& evt) { #ifdef DEBUG_OUTPUT_RAW Serial.print("X: "); PrintHex (evt.X, 0x80); Serial.print(" Y: "); PrintHex (evt.Y, 0x80); Serial.print(" Z: "); PrintHex (evt.Z, 0x80); Serial.print(" Rx: "); PrintHex (evt.Rx, 0x80); Serial.print(" Ry: "); PrintHex (evt.Ry, 0x80); Serial.print(" Rz: "); PrintHex (evt.Rz, 0x80); Serial.print(" S: "); PrintHex (evt.Slider, 0x80); Serial.println(""); #endif GamePadEventData newData = centerValue; #ifdef DEBUG_OUTPUT uint8_t updated = 0; #endif if ((evt.X > (centerValue.X + deadZone.X)) || (evt.X < (centerValue.X - deadZone.X))) { newData.X = evt.X; #ifdef DEBUG_OUTPUT updated = 1; #endif } if ((evt.Y > (centerValue.Y + deadZone.Y)) || (evt.Y < (centerValue.Y - deadZone.Y))) { newData.Y = evt.Y; #ifdef DEBUG_OUTPUT updated = 1; #endif } if ((evt.Z > (centerValue.Z + deadZone.Z)) || (evt.Z < (centerValue.Z - deadZone.Z))) { newData.Z = evt.Z; #ifdef DEBUG_OUTPUT updated = 1; #endif } if ((evt.Rx > (centerValue.Rx + deadZone.Rx)) || (evt.Rx < (centerValue.Rx - deadZone.Rx))) { newData.Rx = evt.Rx; #ifdef DEBUG_OUTPUT updated = 1; #endif } if ((evt.Ry > (centerValue.Ry + deadZone.Ry)) || (evt.Ry < (centerValue.Ry - deadZone.Ry))) { newData.Ry = evt.Ry; #ifdef DEBUG_OUTPUT updated = 1; #endif } if ((evt.Rz > (centerValue.Rz + deadZone.Rz)) || (evt.Rz < (centerValue.Rz - deadZone.Rz))) { newData.Rz = evt.Rz; #ifdef DEBUG_OUTPUT updated = 1; #endif } if ((evt.Slider > (centerValue.Slider + deadZone.Slider)) || (evt.Slider < (centerValue.Slider - deadZone.Slider))) { newData.Slider = evt.Slider; #ifdef DEBUG_OUTPUT updated = 1; #endif } #ifdef DEBUG_OUTPUT if (updated) { Serial.print("X: "); PrintHex (newData.X, 0x80); Serial.print(" Y: "); PrintHex (newData.Y, 0x80); Serial.print(" Z: "); PrintHex (newData.Z, 0x80); Serial.print(" Rx: "); PrintHex (newData.Rx, 0x80); Serial.print(" Ry: "); PrintHex (newData.Ry, 0x80); Serial.print(" Rz: "); PrintHex (newData.Rz, 0x80); Serial.print(" S: "); PrintHex (newData.Slider, 0x80); Serial.println(""); } #endif if (client) { client->OnGamePadChanged(newData); } } void JoystickEventsDeadZone::OnMouseMoved(uint8_t x, uint8_t y) { #ifdef DEBUG_OUTPUT_RAW Serial.print("Mouse X: "); PrintHex(x, 0x80); Serial.print("\tY: "); PrintHex(y, 0x80); Serial.println(""); #endif uint8_t newX = centerMouseX; uint8_t newY = centerMouseY; #ifdef DEBUG_OUTPUT uint8_t updated = 0; #endif if ((x > (centerMouseX + deadZoneMouseX)) || (x < (centerMouseX - deadZoneMouseX))) { newX = x; #ifdef DEBUG_OUTPUT updated = 1; #endif } if ((y > (centerMouseY + deadZoneMouseY)) || (y < (centerMouseY - deadZoneMouseY))) { newY = y; #ifdef DEBUG_OUTPUT updated = 1; #endif } #ifdef DEBUG_OUTPUT if (updated) { Serial.print("Mouse X: "); PrintHex(newX, 0x80); Serial.print("\tY: "); PrintHex(newY, 0x80); Serial.println(""); } #endif if (client) { client->OnMouseMoved(x, y); } }