123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- /*
- * Saitek X52 Arduino USB Host Shield Library.
- * Copyright 2016 by Thomas Buck <xythobuz@xythobuz.de>
- *
- * 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 <Arduino.h>
- #include "data.h"
- #include "cppm.h"
- #include "events.h"
-
- JoystickEventsCPPM::JoystickEventsCPPM(JoystickEvents* client) : JoystickEvents(client) {
- for (uint8_t i = 0; i < CHANNELS_MAX; i++) {
- values[i] = CHANNEL_DEFAULT_VALUE;
- invert[i] = 0;
- minimum[i] = CHANNEL_MINIMUM_VALUE;
- maximum[i] = CHANNEL_MAXIMUM_VALUE;
- trim[i] = 0;
- }
-
- /*
- * Aux switches are commonly used for arming.
- * Ensure we're not sending high values when
- * no joystick has been connected...
- */
- values[CHANNEL_AUX1] = CHANNEL_MINIMUM_VALUE;
- values[CHANNEL_AUX2] = CHANNEL_MINIMUM_VALUE;
-
- CPPM::instance().copy(values);
- }
-
- void JoystickEventsCPPM::OnGamePadChanged(const GamePadEventData& evt) {
- // up to CHANNEL_AUX2 we are using axes
- for (uint8_t i = 0; i < (CHANNEL_AUX2 + 1); i++) {
- uint16_t value = ((int32_t)getJoystickAxis(evt, i)) + trim[i];
- values[i] = map(value, 0, getJoystickMax(i),
- invert[i] ? maximum[i] : minimum[i], invert[i] ? minimum[i] : maximum[i]);
- }
-
- CPPM::instance().copy(values);
-
- if (client) {
- client->OnGamePadChanged(evt);
- }
- }
-
- void JoystickEventsCPPM::OnButtonUp(uint8_t but_id) {
- // if you want to do something when a button is released
- // do it here...
-
- if (client) {
- client->OnButtonUp(but_id);
- }
- }
-
- void JoystickEventsCPPM::OnButtonDown(uint8_t but_id) {
- // called when a button is pressed
- if (but_id == 42) { // TODO proper button id
- // set CHANNEL_AUX3 according to button
- values[CHANNEL_AUX3] = 1000;
- } else if (but_id == 23) { // TODO proper button id
- values[CHANNEL_AUX3] = 2000;
- }
-
- CPPM::instance().copy(values);
-
- if (client) {
- client->OnButtonDown(but_id);
- }
- }
-
- uint16_t JoystickEventsCPPM::getJoystickAxis(const GamePadEventData& evt, uint8_t ch) {
- if (ch == CHANNEL_ROLL) {
- return evt.X;
- } else if (ch == CHANNEL_PITCH) {
- return evt.Y;
- } else if (ch == CHANNEL_THROTTLE) {
- return evt.Z;
- } else if (ch == CHANNEL_YAW) {
- return evt.Rz;
- } else if (ch == CHANNEL_AUX1) {
- return evt.Ry;
- } else if (ch == CHANNEL_AUX2) {
- return evt.Slider;
- } else if (ch == (CHANNEL_AUX2 + 1)) {
- return evt.Rx;
- } else {
- return 0;
- }
- }
-
- uint16_t JoystickEventsCPPM::getJoystickMax(uint8_t ch) {
- if (ch == CHANNEL_ROLL) {
- return 0x7FF;
- } else if (ch == CHANNEL_PITCH) {
- return 0x7FF;
- } else if (ch == CHANNEL_THROTTLE) {
- return 0xFF;
- } else if (ch == CHANNEL_YAW) {
- return 0x3FF;
- } else if (ch == CHANNEL_AUX1) {
- return 0xFF;
- } else if (ch == CHANNEL_AUX2) {
- return 0xFF;
- } else if (ch == (CHANNEL_AUX2 + 1)) {
- return 0xFF;
- } else {
- return 0xFF;
- }
- }
|