├── .gitignore ├── data.h ├── parser.h ├── config.h ├── README.md ├── cppm.h ├── x52.h ├── events_cppm.cpp ├── cppm.cpp ├── frsky.h ├── parser.cpp ├── Saitek-X52-PPM.ino ├── config.cpp ├── events.h ├── frsky.cpp ├── events_deadzone.cpp ├── x52.cpp ├── COPYING └── events_buttons.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /data.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Saitek X52 Arduino USB Host Shield Library. 3 | * Copyright 2016 by Thomas Buck 4 | * 5 | * Based on the USB Host Library HID Joystick example 6 | * https://www.circuitsathome.com/mcu/hid-joystick-code-sample 7 | * 8 | * This program is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License as 10 | * published by the Free Software Foundation, version 2. 11 | */ 12 | 13 | #ifndef __GAMEPAD_EVENT_DATA_H__ 14 | #define __GAMEPAD_EVENT_DATA_H__ 15 | 16 | #include 17 | 18 | class GamePadEventData { 19 | public: 20 | GamePadEventData(uint16_t v) : X(v), Y(v), Z(v), Rx(v), Ry(v), Rz(v), Slider(v) { } 21 | GamePadEventData(uint16_t x, uint16_t y, uint8_t z, uint8_t rx, uint8_t ry, 22 | uint16_t rz, uint8_t slider) 23 | : X(x), Y(y), Z(z), Rx(rx), Ry(ry), Rz(rz), Slider(slider) { } 24 | 25 | uint16_t X, Y, Rz; // 11bit, 11bit, 10bit 26 | uint8_t Z, Rx, Ry, Slider; 27 | }; 28 | 29 | #endif // __GAMEPAD_EVENT_DATA_H__ 30 | 31 | -------------------------------------------------------------------------------- /parser.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Saitek X52 Arduino USB Host Shield Library. 3 | * Copyright 2016 by Thomas Buck 4 | * 5 | * Based on the USB Host Library HID Joystick example 6 | * https://www.circuitsathome.com/mcu/hid-joystick-code-sample 7 | * 8 | * This program is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License as 10 | * published by the Free Software Foundation, version 2. 11 | */ 12 | 13 | #ifndef __PARSER_H__ 14 | #define __PARSER_H__ 15 | 16 | #include 17 | #include "data.h" 18 | 19 | #define RPT_GEMEPAD_LEN 8 20 | #define BUFFER_SIZE 16 21 | 22 | class JoystickEvents; 23 | 24 | class JoystickReportParser : public HIDReportParser { 25 | public: 26 | JoystickReportParser(JoystickEvents* evt); 27 | virtual void Parse(USBHID* hid, bool is_rpt_id, uint8_t len, uint8_t* bufPart); 28 | 29 | private: 30 | uint8_t buf[BUFFER_SIZE]; 31 | uint8_t oldPad[RPT_GEMEPAD_LEN]; 32 | uint8_t oldHat; 33 | uint64_t oldButtons; 34 | uint8_t oldMouse; 35 | GamePadEventData buffer; 36 | JoystickEvents* joyEvents; 37 | }; 38 | 39 | #endif // __PARSER_H__ 40 | 41 | -------------------------------------------------------------------------------- /config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * EEPROM configuration storage utility 3 | * Copyright 2016 by Thomas Buck 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License as 7 | * published by the Free Software Foundation, version 2. 8 | */ 9 | 10 | #ifndef __CONFIG_H__ 11 | #define __CONFIG_H__ 12 | 13 | #include 14 | 15 | #define CPPM_OUTPUT_PIN_DEFAULT 4 16 | #define CHANNEL_MINIMUM_VALUE 1000 17 | #define CHANNEL_DEFAULT_VALUE 1500 18 | #define CHANNEL_MAXIMUM_VALUE 2000 19 | #define CHANNELS_MAX 12 20 | #define DEFAULT_CHANNELS 6 21 | #define DEFAULT_FRAME_LENGTH 20000 22 | #define DEFAULT_PULSE_LENGTH 300 23 | #define DEFAULT_INVERT_STATE 0 24 | 25 | enum RxChannels { 26 | CHANNEL_ROLL = 0, 27 | CHANNEL_PITCH = 1, 28 | CHANNEL_THROTTLE = 2, 29 | CHANNEL_YAW = 3, 30 | CHANNEL_AUX1 = 4, 31 | CHANNEL_AUX2 = 5 32 | }; 33 | 34 | // Increase string number when struct changes! 35 | #define CONFIG_VERSION "USBCPPM-02" 36 | #define CONFIG_VERSION_LENGTH (sizeof(CONFIG_VERSION) - 1) 37 | 38 | struct ConfigData { 39 | uint16_t channels, frameLength, pulseLength, inverted, cppmPin; 40 | uint16_t minimum[CHANNELS_MAX]; 41 | uint16_t maximum[CHANNELS_MAX]; 42 | uint16_t invert[CHANNELS_MAX]; 43 | int16_t trim[CHANNELS_MAX]; 44 | }; 45 | #define CONFIG_DATA_LENGTH (sizeof(ConfigData)) 46 | 47 | void eepromRead(); 48 | void eepromWrite(); 49 | 50 | #endif // __CONFIG_H__ 51 | 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Arduino Saitek X52 USB CPPM converter 2 | 3 | This sketch allows connecting a [Saitek X52](http://www.saitek.com/uk/prod/x52.html) or [Saitek X52 Pro](http://www.saitek.com/uk/prod/x52pro.html) to an [Arduino](https://www.arduino.cc/en/Main/ArduinoBoardUno) with a [USB Host Shield](https://www.arduino.cc/en/Main/ArduinoUSBHostShield). It uses the [USB_Host_Shield_2.0 Library](https://github.com/felis/USB_Host_Shield_2.0). 4 | 5 | **!!Warning!!:** This software has been written for the USB Host Shield library 2.0, Version 1.2.1. There have been some changes with the library for version 1.3.0 renaming symbols used by this project. I've made the required changes, but these are _untested_! If you have any problems, I'd suggest using an older version of the library and the code in this repository. 6 | 7 | [![Multicopter flight with Saitek X52](http://img.youtube.com/vi/De_Ld6MerNo/0.jpg)](http://www.youtube.com/watch?v=De_Ld6MerNo "Multicopter flight with Saitek X52") 8 | 9 | A CPPM signal is generated on a configurable pin and can be fed into a transmitter module or directly into a flight controller. 10 | 11 | Using the Multi-Function-Display on the Joystick, every config option can be changed. These values can be stored on the EEPROM and will be loaded on every start. 12 | 13 | I'm connecting the Arduino to the [FrSky DHT module](http://www.frsky-rc.com/product/pro.php?pro_id=7) in my modified transmitter to control my Tricopter. 14 | 15 | A modified (ie. non-inverted) FrSky Host Telemetry Port (D-Port) can be connected to the hardware serial port of the Arduino so the Telemetry data (link quality and voltages) will be displayed on the Multi-Function-Display of the joystick. 16 | 17 | ## License 18 | 19 | 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. 20 | 21 | -------------------------------------------------------------------------------- /cppm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Combined-PPM signal generator 3 | * Copyright 2016 by Thomas Buck 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License as 7 | * published by the Free Software Foundation, version 2. 8 | */ 9 | 10 | #ifndef __CPPM_H__ 11 | #define __CPPM_H__ 12 | 13 | #include 14 | #include 15 | #include "config.h" 16 | 17 | ISR(TIMER1_COMPA_vect); 18 | 19 | class CPPM { 20 | public: 21 | inline static CPPM& instance() { 22 | if (!inst) { 23 | inst = new CPPM(); 24 | } 25 | return *inst; 26 | } 27 | 28 | void init(void); 29 | void copy(uint16_t* d); 30 | 31 | inline uint16_t getChannels() { return channels; } 32 | inline void setChannels(uint16_t c) { 33 | if (c > CHANNELS_MAX) 34 | c = CHANNELS_MAX; 35 | channels = c; 36 | } 37 | 38 | inline uint16_t getFrameLength() { return frameLength; } 39 | inline void setFrameLength(uint16_t fl) { frameLength = fl; } 40 | 41 | inline uint16_t getPulseLength() { return pulseLength; } 42 | inline void setPulseLength(uint16_t pl) { pulseLength = pl; } 43 | 44 | inline uint8_t getInvert() { return !onState; } 45 | inline void setInvert(uint8_t i) { onState = !i; } 46 | 47 | inline uint8_t getOutput() { return output; } 48 | void setOutput(uint8_t i); 49 | 50 | private: 51 | CPPM() : channels(DEFAULT_CHANNELS), frameLength(DEFAULT_FRAME_LENGTH), 52 | pulseLength(DEFAULT_PULSE_LENGTH), onState(!DEFAULT_INVERT_STATE), 53 | output(CPPM_OUTPUT_PIN_DEFAULT) { } 54 | CPPM(CPPM&) { } 55 | 56 | volatile uint16_t channels; 57 | volatile uint16_t frameLength; // PPM frame length in microseconds (1ms = 1000µs) 58 | volatile uint16_t pulseLength; 59 | volatile uint8_t onState; // polarity of the pulses: 1 is positive, 0 is negative 60 | volatile uint8_t output; 61 | 62 | volatile uint16_t data[CHANNELS_MAX]; 63 | volatile uint8_t state; 64 | volatile uint8_t currentChannel; 65 | volatile uint16_t calcRest; 66 | 67 | static CPPM* inst; 68 | friend void TIMER1_COMPA_vect(void); 69 | }; 70 | 71 | #endif // __CPPM_H__ 72 | 73 | -------------------------------------------------------------------------------- /x52.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Saitek X52 Arduino USB Host Shield Library. 3 | * Copyright 2016 by Thomas Buck 4 | * 5 | * Based on "x52pro-linux" by Nirenjan Krishnan 6 | * https://github.com/nirenjan/x52pro-linux 7 | * 8 | * This program is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License as 10 | * published by the Free Software Foundation, version 2. 11 | */ 12 | 13 | #ifndef __X52_H__ 14 | #define __X52_H__ 15 | 16 | #include 17 | 18 | #define X52_VENDOR_REQUEST 0x91 19 | #define X52_MFD_BRIGHTNESS 0xB1 20 | #define X52_LED_BRIGHTNESS 0xB2 21 | #define X52_MFD_CLEAR_LINE 0x08 22 | #define X52_MFD_WRITE_LINE 0x00 23 | #define X52_MFD_LINE1 0xD1 24 | #define X52_MFD_LINE2 0xD2 25 | #define X52_MFD_LINE3 0xD4 26 | #define X52_SHIFT_INDICATOR 0xFD 27 | #define X52_SHIFT_ON 0x51 28 | #define X52_SHIFT_OFF 0x50 29 | #define X52_BLINK_INDICATOR 0xB4 30 | #define X52_BLINK_ON 0x51 31 | #define X52_BLINK_OFF 0x50 32 | #define X52_DATE_DDMM 0xC4 33 | #define X52_DATE_YEAR 0xC8 34 | #define X52_TIME_CLOCK1 0xC0 35 | #define X52_OFFS_CLOCK2 0xC1 36 | #define X52_OFFS_CLOCK3 0xC2 37 | 38 | class X52 { 39 | public: 40 | X52(USB* u, USBHID* h); 41 | 42 | /* 43 | * Check if a valid PID/VID device has been found. 44 | */ 45 | void initialize(); 46 | 47 | /* 48 | * Set brightness of LEDs and MFD backlight. 49 | * Three states, 0=off, 1=dim, 2=on. 50 | */ 51 | void setLEDBrightness(uint16_t val) { sendCommand(X52_LED_BRIGHTNESS, val); } 52 | void setMFDBrightness(uint16_t val) { sendCommand(X52_MFD_BRIGHTNESS, val); } 53 | 54 | /* 55 | * Print text on the MFD lines (0 - 2). 56 | * Maximum of 16 characters per line. 57 | */ 58 | void setMFDText(uint8_t line, const char* text = " "); 59 | 60 | /* 61 | * Enable (1) or Disable(0) the MFD shift indicator. 62 | */ 63 | void setShift(uint8_t state); 64 | 65 | /* 66 | * Fast blinking of Info and Hat LEDs. 67 | * State can be 0=off or 1=on. 68 | */ 69 | void setBlink(uint8_t state); 70 | 71 | /* 72 | * Set date. The third digit seems to be broken in hardware? 73 | */ 74 | void setDate(uint8_t dd, uint8_t mm, uint8_t yy); 75 | 76 | /* 77 | * Set time. There seems to be a problem with time offsets 78 | * and the 12h/24h time format conflicting...? 79 | */ 80 | void setTime(uint8_t h, uint8_t m); 81 | void setTimeOffset(uint8_t cl, int16_t offset); 82 | 83 | private: 84 | uint8_t sendCommand(uint16_t command, uint16_t val); 85 | 86 | USB* usb; 87 | USBHID* hid; 88 | uint8_t ready; 89 | }; 90 | 91 | extern X52 x52; 92 | 93 | #endif // __X52_H__ 94 | 95 | -------------------------------------------------------------------------------- /events_cppm.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Saitek X52 Arduino USB Host Shield Library. 3 | * Copyright 2016 by Thomas Buck 4 | * 5 | * Based on the USB Host Library HID Joystick example 6 | * https://www.circuitsathome.com/mcu/hid-joystick-code-sample 7 | * 8 | * This program is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License as 10 | * published by the Free Software Foundation, version 2. 11 | */ 12 | 13 | #include 14 | #include "data.h" 15 | #include "cppm.h" 16 | #include "events.h" 17 | 18 | JoystickEventsCPPM::JoystickEventsCPPM(JoystickEvents* client) : JoystickEvents(client) { 19 | for (uint8_t i = 0; i < CHANNELS_MAX; i++) { 20 | values[i] = CHANNEL_DEFAULT_VALUE; 21 | invert[i] = 0; 22 | minimum[i] = CHANNEL_MINIMUM_VALUE; 23 | maximum[i] = CHANNEL_MAXIMUM_VALUE; 24 | trim[i] = 0; 25 | } 26 | 27 | /* 28 | * Aux switches are commonly used for arming. 29 | * Ensure we're not sending high values when 30 | * no joystick has been connected... 31 | */ 32 | values[CHANNEL_AUX1] = CHANNEL_MINIMUM_VALUE; 33 | values[CHANNEL_AUX2] = CHANNEL_MINIMUM_VALUE; 34 | 35 | CPPM::instance().copy(values); 36 | } 37 | 38 | void JoystickEventsCPPM::OnGamePadChanged(const GamePadEventData& evt) { 39 | for (uint8_t i = 0; i < (CHANNEL_AUX2 + 1); i++) { 40 | uint16_t value = ((int32_t)getJoystickAxis(evt, i)) + trim[i]; 41 | values[i] = map(value, 0, getJoystickMax(i), 42 | invert[i] ? maximum[i] : minimum[i], invert[i] ? minimum[i] : maximum[i]); 43 | } 44 | 45 | CPPM::instance().copy(values); 46 | 47 | if (client) { 48 | client->OnGamePadChanged(evt); 49 | } 50 | } 51 | 52 | uint16_t JoystickEventsCPPM::getJoystickAxis(const GamePadEventData& evt, uint8_t ch) { 53 | if (ch == CHANNEL_ROLL) { 54 | return evt.X; 55 | } else if (ch == CHANNEL_PITCH) { 56 | return evt.Y; 57 | } else if (ch == CHANNEL_THROTTLE) { 58 | return evt.Z; 59 | } else if (ch == CHANNEL_YAW) { 60 | return evt.Rz; 61 | } else if (ch == CHANNEL_AUX1) { 62 | return evt.Ry; 63 | } else if (ch == CHANNEL_AUX2) { 64 | return evt.Slider; 65 | } else if (ch == (CHANNEL_AUX2 + 1)) { 66 | return evt.Rx; 67 | } else { 68 | return 0; 69 | } 70 | } 71 | 72 | uint16_t JoystickEventsCPPM::getJoystickMax(uint8_t ch) { 73 | if (ch == CHANNEL_ROLL) { 74 | return 0x7FF; 75 | } else if (ch == CHANNEL_PITCH) { 76 | return 0x7FF; 77 | } else if (ch == CHANNEL_THROTTLE) { 78 | return 0xFF; 79 | } else if (ch == CHANNEL_YAW) { 80 | return 0x3FF; 81 | } else if (ch == CHANNEL_AUX1) { 82 | return 0xFF; 83 | } else if (ch == CHANNEL_AUX2) { 84 | return 0xFF; 85 | } else if (ch == (CHANNEL_AUX2 + 1)) { 86 | return 0xFF; 87 | } else { 88 | return 0xFF; 89 | } 90 | } 91 | 92 | -------------------------------------------------------------------------------- /cppm.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Combined-PPM signal generator 3 | * 4 | * Based on the code from: 5 | * https://quadmeup.com/generate-ppm-signal-with-arduino/ 6 | * https://github.com/DzikuVx/ppm_encoder/blob/master/ppm_encoder_source.ino 7 | * 8 | * Copyright 2016 by Thomas Buck 9 | * 10 | * This program is free software; you can redistribute it and/or 11 | * modify it under the terms of the GNU General Public License as 12 | * published by the Free Software Foundation, version 2. 13 | */ 14 | 15 | #include 16 | #include "cppm.h" 17 | 18 | //#define DEBUG_OUTPUT Serial 19 | 20 | CPPM* CPPM::inst = NULL; 21 | 22 | void CPPM::init(void) { 23 | #ifdef DEBUG_OUTPUT 24 | DEBUG_OUTPUT.println("Initializing Timer..."); 25 | #endif 26 | 27 | state = 1; 28 | currentChannel = channels; 29 | calcRest = 0; 30 | for (uint8_t i = 0; i < channels; i++) { 31 | data[i] = CHANNEL_DEFAULT_VALUE; 32 | } 33 | 34 | pinMode(output, OUTPUT); 35 | digitalWrite(output, CPPM::inst->onState ? LOW : HIGH); 36 | 37 | cli(); 38 | TCCR1A = 0; // set entire TCCR1 register to 0 39 | TCCR1B = 0; 40 | OCR1A = 100; // compare match register 41 | TCCR1B |= (1 << WGM12); // turn on CTC mode 42 | TCCR1B |= (1 << CS11); // 8 prescaler: 0,5 microseconds at 16mhz 43 | TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt 44 | sei(); 45 | } 46 | 47 | void CPPM::setOutput(uint8_t i) { 48 | digitalWrite(output, LOW); 49 | pinMode(output, INPUT); 50 | output = i; 51 | pinMode(output, OUTPUT); 52 | digitalWrite(output, CPPM::inst->onState ? LOW : HIGH); 53 | } 54 | 55 | void CPPM::copy(uint16_t* d) { 56 | #ifdef DEBUG_OUTPUT 57 | DEBUG_OUTPUT.println("New CPPM data!"); 58 | #endif 59 | 60 | cli(); 61 | for (int i = 0; i < channels; i++) { 62 | data[i] = d[i]; 63 | } 64 | sei(); 65 | } 66 | 67 | ISR(TIMER1_COMPA_vect) { 68 | if (!CPPM::inst) { 69 | return; 70 | } 71 | 72 | TCNT1 = 0; 73 | if (CPPM::inst->state) { 74 | // start pulse 75 | digitalWrite(CPPM::inst->output, CPPM::inst->onState ? HIGH : LOW); 76 | OCR1A = CPPM::inst->pulseLength << 1; 77 | CPPM::inst->state = 0; 78 | } else { 79 | // end pulse and calculate when to start the next pulse 80 | digitalWrite(CPPM::inst->output, CPPM::inst->onState ? LOW : HIGH); 81 | CPPM::inst->state = 1; 82 | if (CPPM::inst->currentChannel >= CPPM::inst->channels) { 83 | CPPM::inst->currentChannel = 0; 84 | CPPM::inst->calcRest += CPPM::inst->pulseLength; 85 | OCR1A = (CPPM::inst->frameLength - CPPM::inst->calcRest) << 1; 86 | CPPM::inst->calcRest = 0; 87 | } else { 88 | OCR1A = (CPPM::inst->data[CPPM::inst->currentChannel] - CPPM::inst->pulseLength) << 1; 89 | CPPM::inst->calcRest += CPPM::inst->data[CPPM::inst->currentChannel]; 90 | CPPM::inst->currentChannel++; 91 | } 92 | } 93 | } 94 | 95 | -------------------------------------------------------------------------------- /frsky.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FrSky Telemetry Protocol Host implementation. 3 | * Copyright 2016 by Thomas Buck 4 | * 5 | * Based on the FrSky Telemetry Protocol documentation: 6 | * http://www.frsky-rc.com/download/down.php?id=128 7 | * 8 | * This program is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License as 10 | * published by the Free Software Foundation, version 2. 11 | */ 12 | 13 | #ifndef __FRSKY_H__ 14 | #define __FRSKY_H__ 15 | 16 | #include 17 | 18 | class FrSky { 19 | public: 20 | enum AnalogValue { 21 | analog1_1 = 0, 22 | analog1_2 = 1, 23 | analog2_1 = 2, 24 | analog2_2 = 3 25 | }; 26 | 27 | enum GreaterLessThan { 28 | less = 0, 29 | greater = 1 30 | }; 31 | 32 | enum AlarmLevel { 33 | disable = 0, 34 | yellow = 1, 35 | orange = 2, 36 | red = 3 37 | }; 38 | 39 | struct AlarmThreshold { 40 | AlarmThreshold(AnalogValue i, GreaterLessThan d, AlarmLevel l, uint8_t v) 41 | : id(i), dir(d), level(l), value(v) { } 42 | 43 | AnalogValue id; 44 | GreaterLessThan dir; 45 | AlarmLevel level; 46 | uint8_t value; 47 | }; 48 | 49 | typedef void (*DataHandler)(uint8_t a1, uint8_t a2, uint8_t q1, uint8_t q2); 50 | typedef void (*AlarmThresholdHandler)(AlarmThreshold alarm); 51 | typedef void (*UserDataHandler)(const uint8_t* buf, uint8_t len); 52 | 53 | // ------------------------------------------------------------------------------- 54 | 55 | FrSky(Stream* s); 56 | 57 | void poll(); 58 | void pollAlarms(); 59 | void setAlarm(AlarmThreshold alarm); 60 | 61 | void setDataHandler(DataHandler h) { dataHandler = h; } 62 | void setAlarmThresholdHandler(AlarmThresholdHandler h) { alarmThresholdHandler = h; } 63 | void setUserDataHandler(UserDataHandler h) { userDataHandler = h; } 64 | 65 | private: 66 | void handleMessage(); 67 | void writeEscaped(uint8_t v); 68 | 69 | const static uint8_t bufferSize = 19; 70 | const static uint8_t userDataSize = 6; 71 | const static uint8_t minPacketSize = 11; 72 | const static uint8_t delimiter = 0x7E; 73 | const static uint8_t escape = 0x7D; 74 | const static uint8_t key = 0x20; 75 | const static uint8_t alarms = 4; 76 | const static uint8_t idVoltageQuality = 0xFE; 77 | const static uint8_t idUserData = 0xFD; 78 | const static uint8_t idAlarm0 = 0xFC; 79 | const static uint8_t idAlarm1 = 0xFB; 80 | const static uint8_t idAlarm2 = 0xFA; 81 | const static uint8_t idAlarm3 = 0xF9; 82 | const static uint8_t idGetAlarms = 0xF8; 83 | 84 | Stream* serial; 85 | DataHandler dataHandler; 86 | AlarmThresholdHandler alarmThresholdHandler; 87 | UserDataHandler userDataHandler; 88 | 89 | uint8_t userData[userDataSize]; 90 | uint8_t buffer[bufferSize]; 91 | uint8_t bufferIndex; 92 | }; 93 | 94 | #endif // __FRSKY_H__ 95 | 96 | -------------------------------------------------------------------------------- /parser.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Saitek X52 Arduino USB Host Shield Library. 3 | * Copyright 2016 by Thomas Buck 4 | * 5 | * Based on the USB Host Library HID Joystick example 6 | * https://www.circuitsathome.com/mcu/hid-joystick-code-sample 7 | * 8 | * This program is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License as 10 | * published by the Free Software Foundation, version 2. 11 | */ 12 | 13 | #include "events.h" 14 | #include "parser.h" 15 | 16 | //#define DEBUG_OUTPUT Serial 17 | 18 | JoystickReportParser::JoystickReportParser(JoystickEvents* evt) 19 | : joyEvents(evt), oldHat(0), oldButtons(0), oldMouse(0), buffer(0) { 20 | for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++) { 21 | oldPad[i] = 0; 22 | } 23 | } 24 | 25 | void JoystickReportParser::Parse(USBHID* hid, bool is_rpt_id, uint8_t len, uint8_t* bufPart) { 26 | // Ugly hack for too small packet size in USB Host library... 27 | if (len == 8) { 28 | // First part of buffer, store and do nothing 29 | for (uint8_t i = 0; i < 8; i++) { 30 | buf[i] = bufPart[i]; 31 | } 32 | return; 33 | } else { 34 | // Append second part, then evaluate 35 | for (uint8_t i = 0; i < len; i++) { 36 | buf[i + 8] = bufPart[i]; 37 | } 38 | } 39 | 40 | #ifdef DEBUG_OUTPUT 41 | // Dump whole USB HID packet for debugging purposes 42 | DEBUG_OUTPUT.println(""); 43 | DEBUG_OUTPUT.print("Packet: "); 44 | for (uint8_t i = 0; i < (8 + len); i++) { 45 | DEBUG_OUTPUT.print(buf[i], HEX); 46 | DEBUG_OUTPUT.print(" "); 47 | } 48 | DEBUG_OUTPUT.println(""); 49 | DEBUG_OUTPUT.println(""); 50 | #endif 51 | 52 | // Checking if there are changes in report since the method was last called 53 | bool match = true; 54 | for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++) { 55 | if (buf[i] != oldPad[i]) { 56 | match = false; 57 | break; 58 | } 59 | } 60 | 61 | // Calling Game Pad event handler 62 | if (!match && joyEvents) { 63 | buffer.X = buf[0] | ((buf[1] & 0x07) << 8); 64 | buffer.Y = ((buf[1] & 0xF8) >> 3) | ((buf[2] & 0x3F) << 5); 65 | buffer.Rz = ((buf[2] & 0xC0) >> 6) | (buf[3] << 2); 66 | buffer.Z = buf[4]; 67 | buffer.Rx = buf[5]; 68 | buffer.Ry = buf[6]; 69 | buffer.Slider = buf[7]; 70 | 71 | joyEvents->OnGamePadChanged(buffer); 72 | 73 | for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++) { 74 | oldPad[i] = buf[i]; 75 | } 76 | } 77 | 78 | // Calling Hat Switch event handler 79 | uint8_t hat = (buf[12] & 0xF0) >> 4; 80 | if (hat != oldHat && joyEvents) { 81 | joyEvents->OnHatSwitch(hat); 82 | oldHat = hat; 83 | } 84 | 85 | uint64_t buttons = buf[12] & 0x03; 86 | buttons <<= 8; 87 | buttons |= buf[11]; 88 | buttons <<= 8; 89 | buttons |= buf[10]; 90 | buttons <<= 8; 91 | buttons |= buf[9]; 92 | buttons <<= 8; 93 | buttons |= buf[8]; 94 | 95 | // Calling Button Event Handler for every button changed 96 | uint64_t changes = (buttons ^ oldButtons); 97 | if (changes) { 98 | for (uint8_t i = 0; i < 34; i++) { 99 | uint64_t mask = (1ull << i); 100 | if ((mask & changes) && joyEvents) { 101 | if (buttons & mask) { 102 | joyEvents->OnButtonDown(i); 103 | } else { 104 | joyEvents->OnButtonUp(i); 105 | } 106 | } 107 | } 108 | oldButtons = buttons; 109 | } 110 | 111 | // Calling Mouse Event Handler if state has changed 112 | if (oldMouse != buf[13] && joyEvents) { 113 | oldMouse = buf[13]; 114 | joyEvents->OnMouseMoved((buf[13] & 0xF0) >> 4, buf[13] & 0x0F); 115 | } 116 | } 117 | 118 | -------------------------------------------------------------------------------- /Saitek-X52-PPM.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * Saitek X52 Arduino USB Host Shield Library. 3 | * Copyright 2016 by Thomas Buck 4 | * 5 | * Based on the USB Host Library HID Joystick example 6 | * https://www.circuitsathome.com/mcu/hid-joystick-code-sample 7 | * 8 | * This program is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License as 10 | * published by the Free Software Foundation, version 2. 11 | */ 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include "events.h" 19 | #include "parser.h" 20 | #include "x52.h" 21 | #include "cppm.h" 22 | #include "frsky.h" 23 | #include "config.h" 24 | 25 | #define ENABLE_SERIAL_PORT 9600 26 | //#define DEBUG_OUTPUT Serial 27 | //#define DEBUG_MFD_UPTIME 28 | 29 | #define LED_STATUS_PIN 13 30 | 31 | USB usb; 32 | USBHub hub(&usb); 33 | HIDUniversal hid(&usb); 34 | X52 x52(&usb, &hid); 35 | JoystickEventsCPPM joyCPPM; 36 | JoystickEventsButtons joyButtons((JoystickEvents*)&joyCPPM); 37 | JoystickEventsDeadZone joyDeadZone((JoystickEvents*)&joyButtons); 38 | JoystickReportParser joy(&joyDeadZone); 39 | FrSky frsky(&Serial); 40 | 41 | static void stringHelper(String& a, String& b, char delim, uint8_t line) { 42 | String s = a + delim; 43 | for (uint8_t i = 0; i < (14 - a.length() - b.length()); i++) { 44 | s += ' '; 45 | } 46 | s += b + delim; 47 | x52.setMFDText(line, s.c_str()); 48 | } 49 | 50 | void statusCallback(uint8_t a1, uint8_t a2, uint8_t q1, uint8_t q2) { 51 | x52.setMFDText(0, "Telemetry Status"); 52 | 53 | uint16_t l1 = q1 * 100 / 255, l2 = q2 * 100 / 255; 54 | String link1(l1), link2(l2); 55 | stringHelper(link1, link2, '%', 1); 56 | 57 | uint32_t v1 = a1 * 330 / 255; 58 | uint32_t v1hundred = v1 / 100; 59 | uint32_t v1ten = v1 % 100; 60 | String volt1 = String(v1hundred) + '.' + (v1ten); 61 | uint32_t v2 = a2 * 330 / 255; 62 | uint32_t v2hundred = v2 / 100; 63 | uint32_t v2ten = v2 % 100; 64 | String volt2 = String(v2hundred) + '.' + (v2ten); 65 | stringHelper(volt1, volt2, 'V', 2); 66 | } 67 | 68 | void setup() { 69 | #ifdef ENABLE_SERIAL_PORT 70 | Serial.begin(ENABLE_SERIAL_PORT); 71 | #endif 72 | 73 | #ifdef DEBUG_OUTPUT 74 | DEBUG_OUTPUT.println("Start"); 75 | #endif 76 | 77 | pinMode(LED_STATUS_PIN, OUTPUT); 78 | digitalWrite(LED_STATUS_PIN, LOW); 79 | 80 | eepromRead(); 81 | 82 | if (usb.Init() == -1) { 83 | digitalWrite(LED_STATUS_PIN, HIGH); 84 | #ifdef DEBUG_OUTPUT 85 | DEBUG_OUTPUT.println("OSC did not start."); 86 | #endif 87 | } 88 | 89 | if (!hid.SetReportParser(0, &joy)) { 90 | digitalWrite(LED_STATUS_PIN, HIGH); 91 | #ifdef DEBUG_OUTPUT 92 | DEBUG_OUTPUT.println("Error adding report parser."); 93 | #endif 94 | } 95 | 96 | CPPM::instance().init(); 97 | frsky.setDataHandler(&statusCallback); 98 | wdt_enable(WDTO_1S); 99 | } 100 | 101 | void init_joystick() { 102 | x52.initialize(); 103 | 104 | x52.setMFDText(0, "Arduino X52 Host"); 105 | x52.setMFDText(1, "Made by xythobuz"); 106 | x52.setMFDText(2, "Licensed as GPL2"); 107 | 108 | // Sometimes the first message is lost, so send again 109 | if (joyButtons.getCurrentMode() == 1) { 110 | x52.setLEDBrightness(2); 111 | x52.setMFDBrightness(2); 112 | } else if (joyButtons.getCurrentMode() == 2) { 113 | x52.setLEDBrightness(1); 114 | x52.setMFDBrightness(1); 115 | } else if (joyButtons.getCurrentMode() == 3) { 116 | x52.setLEDBrightness(0); 117 | x52.setMFDBrightness(0); 118 | } 119 | } 120 | 121 | void loop() { 122 | wdt_reset(); 123 | usb.Task(); 124 | frsky.poll(); 125 | 126 | static unsigned long lastTime = 0; 127 | static uint8_t initialized = 0; 128 | if ((millis() - lastTime) >= 1000) { 129 | lastTime = millis(); 130 | if (initialized == 0) { 131 | initialized = 1; 132 | } else if (initialized == 1) { 133 | init_joystick(); 134 | initialized = 2; 135 | } 136 | 137 | #ifdef DEBUG_MFD_UPTIME 138 | String text = "Uptime: " + String(millis() / 1000) + "s"; 139 | x52.setMFDText(2, text.c_str()); 140 | #endif 141 | } 142 | } 143 | 144 | -------------------------------------------------------------------------------- /config.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * EEPROM configuration storage utility 3 | * Copyright 2016 by Thomas Buck 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License as 7 | * published by the Free Software Foundation, version 2. 8 | */ 9 | 10 | #include 11 | #include "cppm.h" 12 | #include "events.h" 13 | #include "config.h" 14 | 15 | //#define DEBUG_OUTPUT Serial 16 | 17 | static void toEEPROM(const ConfigData& data) { 18 | for (uint8_t i = 0; i < CONFIG_VERSION_LENGTH; i++) { 19 | EEPROM.write(i, CONFIG_VERSION[i]); 20 | } 21 | 22 | const uint8_t* buffer = (const uint8_t*)((const void*)&data); 23 | uint8_t sum = 0; 24 | for (uint8_t i = 0; i < CONFIG_DATA_LENGTH; i++) { 25 | EEPROM.write(CONFIG_VERSION_LENGTH + i, buffer[i]); 26 | sum ^= buffer[i]; 27 | } 28 | 29 | EEPROM.write(CONFIG_VERSION_LENGTH + CONFIG_DATA_LENGTH, sum); 30 | 31 | #ifdef DEBUG_OUTPUT 32 | DEBUG_OUTPUT.println("EEPROM write success!"); 33 | #endif 34 | } 35 | 36 | // Return 0 on valid data, otherwise header or checksum wrong 37 | static uint8_t fromEEPROM(ConfigData& data) { 38 | for (uint8_t i = 0; i < CONFIG_VERSION_LENGTH; i++) { 39 | if (EEPROM.read(i) != CONFIG_VERSION[i]) { 40 | #ifdef DEBUG_OUTPUT 41 | DEBUG_OUTPUT.println("EEPROM header wrong!"); 42 | #endif 43 | return 1; 44 | } 45 | } 46 | 47 | uint8_t* buffer = (uint8_t*)((void*)&data); 48 | uint8_t sum = 0; 49 | for (uint8_t i = 0; i < CONFIG_DATA_LENGTH; i++) { 50 | buffer[i] = EEPROM.read(CONFIG_VERSION_LENGTH + i); 51 | sum ^= buffer[i]; 52 | } 53 | 54 | uint8_t storedSum = EEPROM.read(CONFIG_VERSION_LENGTH + CONFIG_DATA_LENGTH); 55 | if (storedSum != sum) { 56 | #ifdef DEBUG_OUTPUT 57 | DEBUG_OUTPUT.println("EEPROM checksum wrong!"); 58 | #endif 59 | return 2; 60 | } 61 | 62 | #ifdef DEBUG_OUTPUT 63 | DEBUG_OUTPUT.println("EEPROM read success!"); 64 | #endif 65 | 66 | return 0; 67 | } 68 | 69 | void eepromRead() { 70 | ConfigData data; 71 | 72 | if (fromEEPROM(data) != 0) { 73 | data.channels = DEFAULT_CHANNELS; 74 | data.frameLength = DEFAULT_FRAME_LENGTH; 75 | data.pulseLength = DEFAULT_PULSE_LENGTH; 76 | data.inverted = DEFAULT_INVERT_STATE; 77 | data.cppmPin = CPPM_OUTPUT_PIN_DEFAULT; 78 | for (uint8_t i = 0; i < CHANNELS_MAX; i++) { 79 | data.invert[i] = 0; 80 | data.minimum[i] = CHANNEL_MINIMUM_VALUE; 81 | data.maximum[i] = CHANNEL_MAXIMUM_VALUE; 82 | data.trim[i] = 0; 83 | } 84 | 85 | // Should be correct for every device 86 | data.invert[CHANNEL_THROTTLE] = 1; 87 | data.invert[CHANNEL_PITCH] = 1; 88 | 89 | /* 90 | * Default values to match my personal setup. 91 | * Can be changed using the on-screen menu. 92 | */ 93 | data.minimum[CHANNEL_THROTTLE] = 1010; 94 | data.maximum[CHANNEL_THROTTLE] = 1950; 95 | data.minimum[CHANNEL_ROLL] = 1050; 96 | data.maximum[CHANNEL_ROLL] = 1950; 97 | data.minimum[CHANNEL_PITCH] = 1080; 98 | data.maximum[CHANNEL_PITCH] = 1890; 99 | data.minimum[CHANNEL_AUX1] = 990; 100 | data.maximum[CHANNEL_AUX1] = 2100; 101 | data.minimum[CHANNEL_AUX2] = 990; 102 | data.maximum[CHANNEL_AUX2] = 1990; 103 | 104 | toEEPROM(data); 105 | } 106 | 107 | CPPM::instance().setChannels(data.channels); 108 | CPPM::instance().setFrameLength(data.frameLength); 109 | CPPM::instance().setPulseLength(data.pulseLength); 110 | CPPM::instance().setInvert(data.inverted); 111 | CPPM::instance().setOutput(data.cppmPin); 112 | for (uint8_t i = 0; i < CHANNELS_MAX; i++) { 113 | joyCPPM.setInvert(i, data.invert[i]); 114 | joyCPPM.setMinimum(i, data.minimum[i]); 115 | joyCPPM.setMaximum(i, data.maximum[i]); 116 | joyCPPM.setTrim(i, data.trim[i]); 117 | } 118 | } 119 | 120 | void eepromWrite() { 121 | ConfigData data; 122 | 123 | data.channels = CPPM::instance().getChannels(); 124 | data.frameLength = CPPM::instance().getFrameLength(); 125 | data.pulseLength = CPPM::instance().getPulseLength(); 126 | data.inverted = CPPM::instance().getInvert(); 127 | data.cppmPin = CPPM::instance().getOutput(); 128 | for (uint8_t i = 0; i < CHANNELS_MAX; i++) { 129 | data.invert[i] = joyCPPM.getInvert(i); 130 | data.minimum[i] = joyCPPM.getMinimum(i); 131 | data.maximum[i] = joyCPPM.getMaximum(i); 132 | data.trim[i] = joyCPPM.getTrim(i); 133 | } 134 | 135 | toEEPROM(data); 136 | } 137 | 138 | -------------------------------------------------------------------------------- /events.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Saitek X52 Arduino USB Host Shield Library. 3 | * Copyright 2016 by Thomas Buck 4 | * 5 | * Based on the USB Host Library HID Joystick example 6 | * https://www.circuitsathome.com/mcu/hid-joystick-code-sample 7 | * 8 | * This program is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License as 10 | * published by the Free Software Foundation, version 2. 11 | */ 12 | 13 | #ifndef __JOYSTICK_EVENTS_H__ 14 | #define __JOYSTICK_EVENTS_H__ 15 | 16 | #include 17 | #include "config.h" 18 | 19 | class GamePadEventData; 20 | 21 | class JoystickEvents { 22 | public: 23 | JoystickEvents(JoystickEvents* _client = 0) : client(_client) { } 24 | virtual void OnGamePadChanged(const GamePadEventData& evt) { if (client) client->OnGamePadChanged(evt); } 25 | virtual void OnHatSwitch(uint8_t hat) { if(client) client->OnHatSwitch(hat); } 26 | virtual void OnButtonUp(uint8_t but_id) { if(client) client->OnButtonUp(but_id); } 27 | virtual void OnButtonDown(uint8_t but_id) { if(client) client->OnButtonDown(but_id); } 28 | virtual void OnMouseMoved(uint8_t x, uint8_t y) { if (client) client->OnMouseMoved(x, y); } 29 | 30 | protected: 31 | JoystickEvents* client; 32 | }; 33 | 34 | class JoystickEventsDeadZone : public JoystickEvents { 35 | public: 36 | JoystickEventsDeadZone(JoystickEvents* client = 0) : JoystickEvents(client) { } 37 | virtual void OnGamePadChanged(const GamePadEventData& evt); 38 | virtual void OnMouseMoved(uint8_t x, uint8_t y); 39 | 40 | private: 41 | const static GamePadEventData deadZone; 42 | const static uint8_t deadZoneMouseX, deadZoneMouseY; 43 | 44 | const static GamePadEventData centerValue; 45 | const static uint8_t centerMouseX, centerMouseY; 46 | }; 47 | 48 | class JoystickEventsCPPM : public JoystickEvents { 49 | public: 50 | JoystickEventsCPPM(JoystickEvents* client = 0); 51 | virtual void OnGamePadChanged(const GamePadEventData& evt); 52 | 53 | uint8_t getInvert(uint8_t ch) { 54 | if (ch < CHANNELS_MAX) return invert[ch]; 55 | else return 0; 56 | } 57 | 58 | void setInvert(uint8_t ch, uint8_t i) { 59 | if (ch < CHANNELS_MAX) invert[ch] = i; 60 | } 61 | 62 | uint16_t getMinimum(uint8_t ch) { 63 | if (ch < CHANNELS_MAX) return minimum[ch]; 64 | else return 0; 65 | } 66 | 67 | void setMinimum(uint8_t ch, uint16_t i) { 68 | if (ch < CHANNELS_MAX) minimum[ch] = i; 69 | } 70 | 71 | uint16_t getMaximum(uint8_t ch) { 72 | if (ch < CHANNELS_MAX) return maximum[ch]; 73 | else return 0; 74 | } 75 | 76 | void setMaximum(uint8_t ch, uint16_t i) { 77 | if (ch < CHANNELS_MAX) maximum[ch] = i; 78 | } 79 | 80 | int16_t getTrim(uint8_t ch) { 81 | if (ch < CHANNELS_MAX) return trim[ch]; 82 | else return 0; 83 | } 84 | 85 | void setTrim(uint8_t ch, int16_t i) { 86 | if (ch < CHANNELS_MAX) trim[ch] = i; 87 | } 88 | 89 | private: 90 | uint16_t getJoystickAxis(const GamePadEventData& evt, uint8_t ch); 91 | uint16_t getJoystickMax(uint8_t ch); 92 | 93 | uint16_t values[CHANNELS_MAX]; 94 | uint8_t invert[CHANNELS_MAX]; 95 | uint16_t minimum[CHANNELS_MAX]; 96 | uint16_t maximum[CHANNELS_MAX]; 97 | int16_t trim[CHANNELS_MAX]; 98 | }; 99 | 100 | class JoystickEventsButtons : public JoystickEvents { 101 | public: 102 | JoystickEventsButtons(JoystickEvents* client = 0); 103 | virtual void OnButtonDown(uint8_t but_id); 104 | 105 | uint8_t getCurrentMode() { return currentMode; } 106 | 107 | private: 108 | enum MenuState { 109 | NONE = 0, 110 | MAINMENU, 111 | CPPMMENU, 112 | TRIMAXISMENU, 113 | TRIMENDPOINTMENU, 114 | INVERTAXISMENU, 115 | 116 | STATES_EDIT, 117 | EDIT_CHANNELS, 118 | EDIT_FRAME_LENGTH, 119 | EDIT_PULSE_LENGTH, 120 | EDIT_INVERT, 121 | EDIT_CPPM_PIN, 122 | EDIT_MIN_ROLL, 123 | EDIT_MAX_ROLL, 124 | EDIT_MIN_PITCH, 125 | EDIT_MAX_PITCH, 126 | EDIT_MIN_YAW, 127 | EDIT_MAX_YAW, 128 | EDIT_MIN_THROTTLE, 129 | EDIT_MAX_THROTTLE, 130 | EDIT_MIN_AUX1, 131 | EDIT_MAX_AUX1, 132 | EDIT_MIN_AUX2, 133 | EDIT_MAX_AUX2, 134 | EDIT_INVERT_ROLL, 135 | EDIT_INVERT_PITCH, 136 | EDIT_INVERT_YAW, 137 | EDIT_INVERT_THROTTLE, 138 | EDIT_INVERT_AUX1, 139 | EDIT_INVERT_AUX2, 140 | 141 | STATES_EDIT_SIGNED, 142 | EDIT_TRIM_ROLL, 143 | EDIT_TRIM_PITCH, 144 | EDIT_TRIM_YAW, 145 | EDIT_TRIM_THROTTLE, 146 | EDIT_TRIM_AUX1, 147 | EDIT_TRIM_AUX2, 148 | 149 | STATES_MAX 150 | }; 151 | 152 | void printMenu(); 153 | void menuHelper(uint8_t count, const char** menu, const char* title); 154 | void printValue(uint16_t min, uint16_t max, const char* title); 155 | void printSignedValue(int16_t min, int16_t max, const char* title); 156 | 157 | MenuState state; 158 | uint8_t index; 159 | uint16_t value; 160 | int16_t signedValue; 161 | uint8_t currentMode; 162 | }; 163 | 164 | extern JoystickEventsCPPM joyCPPM; 165 | extern JoystickEventsButtons joyButtons; 166 | extern JoystickEventsDeadZone joyDeadZone; 167 | 168 | #endif // __JOYSTICK_EVENTS_H__ 169 | 170 | -------------------------------------------------------------------------------- /frsky.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * FrSky Telemetry Protocol Host implementation. 3 | * Copyright 2016 by Thomas Buck 4 | * 5 | * Based on the FrSky Telemetry Protocol documentation: 6 | * http://www.frsky-rc.com/download/down.php?id=128 7 | * 8 | * This program is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License as 10 | * published by the Free Software Foundation, version 2. 11 | */ 12 | 13 | #include "frsky.h" 14 | 15 | //#define DEBUG_OUTPUT Serial 16 | 17 | FrSky::FrSky(Stream* s) : serial(s), dataHandler(0), 18 | alarmThresholdHandler(0), userDataHandler(0), bufferIndex(0) { 19 | for (uint8_t i = 0; i < userDataSize; i++) { 20 | userData[i] = 0; 21 | } 22 | 23 | for (uint8_t i = 0; i < bufferSize; i++) { 24 | buffer[i] = 0; 25 | } 26 | } 27 | 28 | void FrSky::poll() { 29 | if ((!serial) || (!serial->available())) { 30 | return; 31 | } 32 | 33 | uint8_t c = serial->read(); 34 | if (c == delimiter) { 35 | #ifdef DEBUG_OUTPUT 36 | DEBUG_OUTPUT.print("Got delimiter at "); 37 | DEBUG_OUTPUT.println(bufferIndex); 38 | #endif 39 | if (bufferIndex < (minPacketSize - 1)) { 40 | #ifdef DEBUG_OUTPUT 41 | DEBUG_OUTPUT.print("Reset to 0: "); 42 | DEBUG_OUTPUT.print(bufferIndex); 43 | DEBUG_OUTPUT.print(" ! <= "); 44 | DEBUG_OUTPUT.println(minPacketSize - 1); 45 | #endif 46 | bufferIndex = 0; 47 | } 48 | if (bufferIndex >= bufferSize) { 49 | #ifdef DEBUG_OUTPUT 50 | DEBUG_OUTPUT.print("too large: "); 51 | DEBUG_OUTPUT.print(bufferIndex); 52 | DEBUG_OUTPUT.print(" / "); 53 | DEBUG_OUTPUT.println(bufferSize); 54 | #endif 55 | bufferIndex = bufferSize - 1; 56 | } 57 | buffer[bufferIndex++] = c; 58 | if (bufferIndex >= minPacketSize) { 59 | #ifdef DEBUG_OUTPUT 60 | DEBUG_OUTPUT.println("Handling..."); 61 | #endif 62 | handleMessage(); 63 | bufferIndex = 0; 64 | } 65 | } else if ((bufferIndex > 0) && (bufferIndex < bufferSize)) { 66 | buffer[bufferIndex++] = c; 67 | #ifdef DEBUG_OUTPUT 68 | DEBUG_OUTPUT.print("Got "); 69 | DEBUG_OUTPUT.print(c); 70 | DEBUG_OUTPUT.print(" at "); 71 | DEBUG_OUTPUT.println(bufferIndex - 1); 72 | #endif 73 | } else { 74 | #ifdef DEBUG_OUTPUT 75 | DEBUG_OUTPUT.print("Invalid: "); 76 | DEBUG_OUTPUT.print(bufferIndex); 77 | DEBUG_OUTPUT.print(" / "); 78 | DEBUG_OUTPUT.println(bufferSize); 79 | #endif 80 | } 81 | } 82 | 83 | void FrSky::pollAlarms() { 84 | serial->write(delimiter); 85 | writeEscaped(idGetAlarms); 86 | for (uint8_t i = 0; i < 8; i++) { 87 | writeEscaped(0); 88 | } 89 | serial->write(delimiter); 90 | } 91 | 92 | void FrSky::setAlarm(AlarmThreshold alarm) { 93 | uint8_t id = (alarm.id == analog1_1) ? idAlarm0 94 | : ((alarm.id == analog1_2) ? idAlarm1 95 | : ((alarm.id == analog2_1) ? idAlarm2 : idAlarm3)); 96 | serial->write(delimiter); 97 | writeEscaped(id); 98 | writeEscaped(alarm.value); 99 | writeEscaped(alarm.dir); 100 | writeEscaped(alarm.level); 101 | for (uint8_t i = 0; i < 5; i++) { 102 | writeEscaped(0); 103 | } 104 | serial->write(delimiter); 105 | } 106 | 107 | void FrSky::writeEscaped(uint8_t v) { 108 | if ((v == delimiter) || (v == escape)) { 109 | v ^= key; 110 | serial->write(escape); 111 | } 112 | serial->write(v); 113 | } 114 | 115 | void FrSky::handleMessage() { 116 | #ifdef DEBUG_OUTPUT 117 | DEBUG_OUTPUT.println("FrSky::handleMessage()"); 118 | for (uint8_t i = 0; i < bufferIndex; i++) { 119 | DEBUG_OUTPUT.print(buffer[i], HEX); 120 | DEBUG_OUTPUT.print(" "); 121 | } 122 | DEBUG_OUTPUT.println(); 123 | #endif 124 | 125 | if ((buffer[0] != delimiter) || (buffer[bufferIndex - 1] != delimiter)) { 126 | #ifdef DEBUG_OUTPUT 127 | DEBUG_OUTPUT.println("invalid packet begin/end!"); 128 | #endif 129 | return; 130 | } 131 | 132 | // Fix escaped bytes 133 | for (uint8_t i = 0; i < (bufferIndex - 1); i++) { 134 | if (buffer[i] == escape) { 135 | buffer[i] = buffer[i + 1] ^ key; 136 | for (uint8_t j = i + 1; j < (bufferIndex - 1); j++) { 137 | buffer[j] = buffer[j + 1]; 138 | } 139 | bufferIndex--; 140 | } 141 | } 142 | 143 | if (buffer[1] == idVoltageQuality) { 144 | if (dataHandler) { 145 | dataHandler(buffer[2], buffer[3], buffer[4], buffer[5]); 146 | } 147 | } else if (buffer[1] == idUserData) { 148 | uint8_t len = buffer[2]; 149 | if (len > userDataSize) { 150 | len = userDataSize; 151 | } 152 | for (uint8_t i = 0; i < len; i++) { 153 | userData[i] = buffer[i + 4]; 154 | } 155 | if ((len > 0) && (userDataHandler)) { 156 | userDataHandler(userData, len); 157 | } 158 | } else if ((buffer[1] == idAlarm0) || (buffer[1] == idAlarm1) 159 | || (buffer[1] == idAlarm2) || (buffer[1] == idAlarm3)) { 160 | AnalogValue v = (buffer[1] == idAlarm0) ? analog1_1 161 | : ((buffer[1] == idAlarm1) ? analog1_2 162 | : ((buffer[1] == idAlarm2) ? analog2_1 : analog2_2)); 163 | if (alarmThresholdHandler) { 164 | alarmThresholdHandler(AlarmThreshold(v, (GreaterLessThan)buffer[3], 165 | (AlarmLevel)buffer[4], buffer[2])); 166 | } 167 | } else { 168 | #ifdef DEBUG_OUTPUT 169 | DEBUG_OUTPUT.print("Unexpected ID: "); 170 | DEBUG_OUTPUT.println(buffer[1], HEX); 171 | #endif 172 | } 173 | } 174 | 175 | -------------------------------------------------------------------------------- /events_deadzone.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Saitek X52 Arduino USB Host Shield Library. 3 | * Copyright 2016 by Thomas Buck 4 | * 5 | * Based on the USB Host Library HID Joystick example 6 | * https://www.circuitsathome.com/mcu/hid-joystick-code-sample 7 | * 8 | * This program is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License as 10 | * published by the Free Software Foundation, version 2. 11 | */ 12 | 13 | #include "data.h" 14 | #include "events.h" 15 | 16 | //#define DEBUG_OUTPUT_RAW Serial 17 | //#define DEBUG_OUTPUT Serial 18 | 19 | /* 20 | * Deadzone around the axis center, in both directions. 21 | * You can try to counteract a faulty Yaw-Potentiometer 22 | * with this, but it won't be enough in extreme cases. 23 | * X, Y, Z, Rx, Ry, Rz, Slider 24 | */ 25 | const GamePadEventData JoystickEventsDeadZone::deadZone( 26 | 4, 4, 2, 2, 5, 25, 2 27 | ); 28 | const uint8_t JoystickEventsDeadZone::deadZoneMouseX = 1; 29 | const uint8_t JoystickEventsDeadZone::deadZoneMouseY = 1; 30 | 31 | /* 32 | * Absolute values of the axis center. Deadzone will be applied 33 | * in both directions around these values. 34 | * X 11bit, Y 11bit, Z 8bit, Rx 8bit, Ry 8bit, Rz 10bit, Slider 8bit 35 | */ 36 | const GamePadEventData JoystickEventsDeadZone::centerValue( 37 | 0x3FF, 0x3FF, 0x7F, 0x7F, 0x7F, 0x1FF, 0x7F 38 | ); 39 | const uint8_t JoystickEventsDeadZone::centerMouseX = 0x07; 40 | const uint8_t JoystickEventsDeadZone::centerMouseY = 0x07; 41 | 42 | void JoystickEventsDeadZone::OnGamePadChanged(const GamePadEventData& evt) { 43 | #ifdef DEBUG_OUTPUT_RAW 44 | DEBUG_OUTPUT_RAW.print("Raw X: "); 45 | DEBUG_OUTPUT_RAW.print(evt.X, HEX); 46 | DEBUG_OUTPUT_RAW.print(" Y: "); 47 | DEBUG_OUTPUT_RAW.print(evt.Y, HEX); 48 | DEBUG_OUTPUT_RAW.print(" Z: "); 49 | DEBUG_OUTPUT_RAW.print(evt.Z, HEX); 50 | DEBUG_OUTPUT_RAW.print(" Rx: "); 51 | DEBUG_OUTPUT_RAW.print(evt.Rx, HEX); 52 | DEBUG_OUTPUT_RAW.print(" Ry: "); 53 | DEBUG_OUTPUT_RAW.print(evt.Ry, HEX); 54 | DEBUG_OUTPUT_RAW.print(" Rz: "); 55 | DEBUG_OUTPUT_RAW.print(evt.Rz, HEX); 56 | DEBUG_OUTPUT_RAW.print(" S: "); 57 | DEBUG_OUTPUT_RAW.println(evt.Slider, HEX); 58 | #endif 59 | 60 | GamePadEventData newData = centerValue; 61 | 62 | #ifdef DEBUG_OUTPUT 63 | uint8_t updated = 0; 64 | #endif 65 | 66 | if ((evt.X > (centerValue.X + deadZone.X)) 67 | || (evt.X < (centerValue.X - deadZone.X))) { 68 | newData.X = evt.X; 69 | 70 | #ifdef DEBUG_OUTPUT 71 | updated = 1; 72 | #endif 73 | } 74 | 75 | if ((evt.Y > (centerValue.Y + deadZone.Y)) 76 | || (evt.Y < (centerValue.Y - deadZone.Y))) { 77 | newData.Y = evt.Y; 78 | 79 | #ifdef DEBUG_OUTPUT 80 | updated = 1; 81 | #endif 82 | } 83 | 84 | if ((evt.Z > (centerValue.Z + deadZone.Z)) 85 | || (evt.Z < (centerValue.Z - deadZone.Z))) { 86 | newData.Z = evt.Z; 87 | 88 | #ifdef DEBUG_OUTPUT 89 | updated = 1; 90 | #endif 91 | } 92 | 93 | if ((evt.Rx > (centerValue.Rx + deadZone.Rx)) 94 | || (evt.Rx < (centerValue.Rx - deadZone.Rx))) { 95 | newData.Rx = evt.Rx; 96 | 97 | #ifdef DEBUG_OUTPUT 98 | updated = 1; 99 | #endif 100 | } 101 | 102 | if ((evt.Ry > (centerValue.Ry + deadZone.Ry)) 103 | || (evt.Ry < (centerValue.Ry - deadZone.Ry))) { 104 | newData.Ry = evt.Ry; 105 | 106 | #ifdef DEBUG_OUTPUT 107 | updated = 1; 108 | #endif 109 | } 110 | 111 | if ((evt.Rz > (centerValue.Rz + deadZone.Rz)) 112 | || (evt.Rz < (centerValue.Rz - deadZone.Rz))) { 113 | newData.Rz = evt.Rz; 114 | 115 | #ifdef DEBUG_OUTPUT 116 | updated = 1; 117 | #endif 118 | } 119 | 120 | if ((evt.Slider > (centerValue.Slider + deadZone.Slider)) 121 | || (evt.Slider < (centerValue.Slider - deadZone.Slider))) { 122 | newData.Slider = evt.Slider; 123 | 124 | #ifdef DEBUG_OUTPUT 125 | updated = 1; 126 | #endif 127 | } 128 | 129 | #ifdef DEBUG_OUTPUT 130 | if (updated) { 131 | DEBUG_OUTPUT.print("X: "); 132 | DEBUG_OUTPUT.print(newData.X, HEX); 133 | DEBUG_OUTPUT.print(" Y: "); 134 | DEBUG_OUTPUT.print(newData.Y, HEX); 135 | DEBUG_OUTPUT.print(" Z: "); 136 | DEBUG_OUTPUT.print(newData.Z, HEX); 137 | DEBUG_OUTPUT.print(" Rx: "); 138 | DEBUG_OUTPUT.print(newData.Rx, HEX); 139 | DEBUG_OUTPUT.print(" Ry: "); 140 | DEBUG_OUTPUT.print(newData.Ry, HEX); 141 | DEBUG_OUTPUT.print(" Rz: "); 142 | DEBUG_OUTPUT.print(newData.Rz, HEX); 143 | DEBUG_OUTPUT.print(" S: "); 144 | DEBUG_OUTPUT.println(newData.Slider, HEX); 145 | } 146 | #endif 147 | 148 | if (client) { 149 | client->OnGamePadChanged(newData); 150 | } 151 | } 152 | 153 | void JoystickEventsDeadZone::OnMouseMoved(uint8_t x, uint8_t y) { 154 | #ifdef DEBUG_OUTPUT_RAW 155 | DEBUG_OUTPUT_RAW.print("Raw Mouse X: "); 156 | DEBUG_OUTPUT_RAW.print(x, HEX); 157 | DEBUG_OUTPUT_RAW.print("\tY: "); 158 | DEBUG_OUTPUT_RAW.println(y, HEX); 159 | #endif 160 | 161 | uint8_t newX = centerMouseX; 162 | uint8_t newY = centerMouseY; 163 | 164 | #ifdef DEBUG_OUTPUT 165 | uint8_t updated = 0; 166 | #endif 167 | 168 | if ((x > (centerMouseX + deadZoneMouseX)) 169 | || (x < (centerMouseX - deadZoneMouseX))) { 170 | newX = x; 171 | 172 | #ifdef DEBUG_OUTPUT 173 | updated = 1; 174 | #endif 175 | } 176 | 177 | if ((y > (centerMouseY + deadZoneMouseY)) 178 | || (y < (centerMouseY - deadZoneMouseY))) { 179 | newY = y; 180 | 181 | #ifdef DEBUG_OUTPUT 182 | updated = 1; 183 | #endif 184 | } 185 | 186 | #ifdef DEBUG_OUTPUT 187 | if (updated) { 188 | DEBUG_OUTPUT.print("Mouse X: "); 189 | DEBUG_OUTPUT.print(newX, HEX); 190 | DEBUG_OUTPUT.print("\tY: "); 191 | DEBUG_OUTPUT.println(newY, HEX); 192 | } 193 | #endif 194 | 195 | if (client) { 196 | client->OnMouseMoved(x, y); 197 | } 198 | } 199 | 200 | -------------------------------------------------------------------------------- /x52.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Saitek X52 Arduino USB Host Shield Library. 3 | * Copyright 2016 by Thomas Buck 4 | * 5 | * Based on "x52pro-linux" by Nirenjan Krishnan 6 | * https://github.com/nirenjan/x52pro-linux 7 | * 8 | * This program is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License as 10 | * published by the Free Software Foundation, version 2. 11 | */ 12 | 13 | #include "x52.h" 14 | 15 | //#define DEBUG_OUTPUT Serial 16 | 17 | #define TIME_24H_FORMAT 18 | 19 | X52::X52(USB* u, USBHID* h) : usb(u), hid(h), ready(0) { } 20 | 21 | void X52::initialize() { 22 | ready = 0; 23 | 24 | if ((!usb) || (!hid)) { 25 | return; 26 | } 27 | 28 | // Get the USB device descriptor so we can check for a valid PID/VID 29 | uint8_t buf[sizeof (USB_DEVICE_DESCRIPTOR)]; 30 | USB_DEVICE_DESCRIPTOR* udd = reinterpret_cast(buf); 31 | uint8_t ret = usb->getDevDescr(hid->GetAddress(), 0, sizeof(USB_DEVICE_DESCRIPTOR), (uint8_t*)buf); 32 | if (ret) { 33 | #ifdef DEBUG_OUTPUT 34 | DEBUG_OUTPUT.print("Error getting descriptor: "); 35 | DEBUG_OUTPUT.println(ret, DEC); 36 | #endif 37 | } 38 | 39 | uint16_t vid = udd->idVendor; 40 | uint16_t pid = udd->idProduct; 41 | 42 | #ifdef DEBUG_OUTPUT 43 | DEBUG_OUTPUT.print("VID: "); 44 | DEBUG_OUTPUT.print(vid, DEC); 45 | DEBUG_OUTPUT.print(" PID: "); 46 | DEBUG_OUTPUT.println(pid, DEC); 47 | #endif 48 | 49 | if ((vid == 0x06A3) && (pid == 0x0255)) { 50 | // Saitek X52 51 | ready = 1; 52 | } else if ((vid == 0x06A3) && (pid == 0x0762)) { 53 | // Saitek X52 Pro 54 | ready = 1; 55 | } else { 56 | #ifdef DEBUG_OUTPUT 57 | DEBUG_OUTPUT.println("No valid VID/PID found!"); 58 | #endif 59 | } 60 | } 61 | 62 | void X52::setTime(uint8_t h, uint8_t m) { 63 | uint8_t ret = sendCommand(X52_TIME_CLOCK1, m | ((h & 0x7F) << 8) 64 | #ifdef TIME_24H_FORMAT 65 | | (1 << 15) 66 | #endif 67 | ); 68 | if (ret) { 69 | #ifdef DEBUG_OUTPUT 70 | DEBUG_OUTPUT.print("Error setting time: "); 71 | DEBUG_OUTPUT.println(ret, DEC); 72 | #endif 73 | } 74 | } 75 | 76 | void X52::setTimeOffset(uint8_t cl, int16_t offset) { 77 | if (offset < -1023) { 78 | offset = -1023; 79 | } 80 | 81 | if (offset > 1023) { 82 | offset = 1023; 83 | } 84 | 85 | uint8_t negative = 0; 86 | if (offset < 0) { 87 | negative = 1; 88 | offset = -offset; 89 | } 90 | 91 | uint8_t ret = sendCommand(cl ? X52_OFFS_CLOCK3 : X52_OFFS_CLOCK2, 92 | (negative << 10) | (offset & 0x03FF) 93 | #ifdef TIME_24H_FORMAT 94 | | (1 << 15) 95 | #endif 96 | ); 97 | if (ret) { 98 | #ifdef DEBUG_OUTPUT 99 | DEBUG_OUTPUT.print("Error setting offset: "); 100 | DEBUG_OUTPUT.println(ret, DEC); 101 | #endif 102 | } 103 | } 104 | 105 | void X52::setDate(uint8_t dd, uint8_t mm, uint8_t yy) { 106 | uint8_t ret = sendCommand(X52_DATE_DDMM, dd | (mm << 8)); 107 | if (!ret) { 108 | ret = sendCommand(X52_DATE_YEAR, yy); 109 | } 110 | if (ret) { 111 | #ifdef DEBUG_OUTPUT 112 | DEBUG_OUTPUT.print("Error setting date: "); 113 | DEBUG_OUTPUT.println(ret, DEC); 114 | #endif 115 | } 116 | } 117 | 118 | void X52::setBlink(uint8_t state) { 119 | uint8_t ret = sendCommand(X52_BLINK_INDICATOR, state ? X52_BLINK_ON : X52_BLINK_OFF); 120 | if (ret != 0) { 121 | #ifdef DEBUG_OUTPUT 122 | DEBUG_OUTPUT.print("Error setting blink: "); 123 | DEBUG_OUTPUT.println(ret, DEC); 124 | #endif 125 | } 126 | } 127 | 128 | void X52::setShift(uint8_t state) { 129 | uint8_t ret = sendCommand(X52_SHIFT_INDICATOR, state ? X52_SHIFT_ON : X52_SHIFT_OFF); 130 | if (ret != 0) { 131 | #ifdef DEBUG_OUTPUT 132 | DEBUG_OUTPUT.print("Error setting shift: "); 133 | DEBUG_OUTPUT.println(ret, DEC); 134 | #endif 135 | } 136 | } 137 | 138 | void X52::setMFDText(uint8_t line, const char* text) { 139 | const static uint16_t lines[3] = { 140 | X52_MFD_LINE1, 141 | X52_MFD_LINE2, 142 | X52_MFD_LINE3 143 | }; 144 | 145 | if (line >= 3) { 146 | #ifdef DEBUG_OUTPUT 147 | DEBUG_OUTPUT.print("Invalid line: "); 148 | DEBUG_OUTPUT.println(line, DEC); 149 | #endif 150 | return; 151 | } 152 | 153 | uint8_t ret = sendCommand(X52_MFD_CLEAR_LINE | lines[line], 0); 154 | if (ret) { 155 | #ifdef DEBUG_OUTPUT 156 | DEBUG_OUTPUT.print("Error clearing line: "); 157 | DEBUG_OUTPUT.println(ret, DEC); 158 | #endif 159 | return; 160 | } 161 | 162 | for (uint8_t i = 0; i < 16; i += 2) { 163 | if (text[i] == '\0') { 164 | break; 165 | } 166 | 167 | uint16_t value = text[i]; 168 | 169 | if (text[i + 1] != '\0') { 170 | value |= text[i + 1] << 8; 171 | } 172 | 173 | ret = sendCommand(X52_MFD_WRITE_LINE | lines[line], value); 174 | if (ret) { 175 | #ifdef DEBUG_OUTPUT 176 | DEBUG_OUTPUT.print("Error writing to line: "); 177 | DEBUG_OUTPUT.println(ret, DEC); 178 | #endif 179 | return; 180 | } 181 | 182 | if (text[i + 1] == '\0') { 183 | break; 184 | } 185 | } 186 | } 187 | 188 | uint8_t X52::sendCommand(uint16_t command, uint16_t val) { 189 | if (!ready) { 190 | #ifdef DEBUG_OUTPUT 191 | DEBUG_OUTPUT.println("Invalid state!"); 192 | #endif 193 | return 23; 194 | } 195 | 196 | if ((!usb) || (!hid)) { 197 | #ifdef DEBUG_OUTPUT 198 | DEBUG_OUTPUT.println("Invalid objects!"); 199 | #endif 200 | return 42; 201 | } 202 | 203 | const uint8_t valLo = (val & 0x00FF); 204 | const uint8_t valHi = (val & 0xFF00) >> 8; 205 | 206 | #ifdef DEBUG_OUTPUT 207 | DEBUG_OUTPUT.println("Sending X52 Ctrl-Req..."); 208 | #endif 209 | 210 | uint8_t ret = usb->ctrlReq(hid->GetAddress(), 0, 211 | USB_SETUP_TYPE_VENDOR | USB_SETUP_RECIPIENT_DEVICE, 212 | X52_VENDOR_REQUEST, valLo, valHi, command, 213 | 0, 0, NULL, NULL); 214 | if (ret != 0) { 215 | #ifdef DEBUG_OUTPUT 216 | DEBUG_OUTPUT.print("Ctrl-Req Error Code: "); 217 | DEBUG_OUTPUT.println(val, DEC); 218 | #endif 219 | } 220 | 221 | return ret; 222 | } 223 | 224 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /events_buttons.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Saitek X52 Arduino USB Host Shield Library. 3 | * Copyright 2016 by Thomas Buck 4 | * 5 | * Based on the USB Host Library HID Joystick example 6 | * https://www.circuitsathome.com/mcu/hid-joystick-code-sample 7 | * 8 | * This program is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License as 10 | * published by the Free Software Foundation, version 2. 11 | */ 12 | 13 | #include 14 | #include "data.h" 15 | #include "x52.h" 16 | #include "cppm.h" 17 | #include "events.h" 18 | #include "config.h" 19 | 20 | //#define DEBUG_OUTPUT Serial 21 | //#define DEBUG_BUTTON_MFD 22 | 23 | #define MENU_BUTTON_ENTER_1 29 24 | #define MENU_BUTTON_ENTER_2 26 25 | #define MENU_BUTTON_DOWN 27 26 | #define MENU_BUTTON_UP 28 27 | 28 | #define MODE_BUTTON_GREEN 23 29 | #define MODE_BUTTON_YELLOW 24 30 | #define MODE_BUTTON_RED 25 31 | 32 | #define MIN_FRAME_LENGTH 10000 33 | #define MAX_FRAME_LENGTH 30000 34 | #define MIN_PULSE_LENGTH 100 35 | #define MAX_PULSE_LENGTH 1000 36 | #define MIN_LOW_ENDPOINT 500 37 | #define MAX_LOW_ENDPOINT 1500 38 | #define MIN_HIGH_ENDPOINT 1500 39 | #define MAX_HIGH_ENDPOINT 2500 40 | #define MIN_TRIM -100 41 | #define MAX_TRIM 100 42 | 43 | void statusCallback(uint8_t a1, uint8_t a2, uint8_t q1, uint8_t q2); 44 | 45 | JoystickEventsButtons::JoystickEventsButtons(JoystickEvents* client) 46 | : JoystickEvents(client), state(NONE), index(0), 47 | value(0), signedValue(0), currentMode(0) { } 48 | 49 | void JoystickEventsButtons::printMenu() { 50 | static const char* mainMenu[] = { 51 | "Status", "Trim Axis", "Trim Endpoint", "Invert Axis", "CPPM Config", 52 | "Save EEPROM", "Load EEPROM" 53 | }; 54 | static const uint8_t mainMenuCount = sizeof(mainMenu) / sizeof(mainMenu[0]); 55 | 56 | static const char* cppmMenu[] = { 57 | "Channels", "Frame Length", "Pulse Length", "Invert", "Output Pin", "Main Menu" 58 | }; 59 | static const uint8_t cppmMenuCount = sizeof(cppmMenu) / sizeof(cppmMenu[0]); 60 | 61 | static const char* axisMenu[] = { 62 | "Roll", "Pitch", "Yaw", "Throttle", "Aux1", "Aux2", "Main Menu" 63 | }; 64 | static const uint8_t axisMenuCount = sizeof(axisMenu) / sizeof(axisMenu[0]); 65 | 66 | static const char* endpointMenu[] = { 67 | "Roll Min", "Roll Max", "Pitch Min", "Pitch Max", "Yaw Min", "Yaw Max", 68 | "Throttle Min", "Throttle Max", "Aux1 Min", "Aux1 Max", "Aux2 Min", 69 | "Aux2 Max", "Main Menu" 70 | }; 71 | static const uint8_t endpointMenuCount = sizeof(endpointMenu) / sizeof(endpointMenu[0]); 72 | 73 | if (state == NONE) { 74 | statusCallback(0, 0, 0, 0); 75 | } else if (state == MAINMENU) { 76 | menuHelper(mainMenuCount, mainMenu, "Main Menu"); 77 | } else if (state == CPPMMENU) { 78 | menuHelper(cppmMenuCount, cppmMenu, "CPPM Config Menu"); 79 | } else if (state == TRIMAXISMENU) { 80 | menuHelper(axisMenuCount, axisMenu, "Trim Axis Menu"); 81 | } else if (state == TRIMENDPOINTMENU) { 82 | menuHelper(endpointMenuCount, endpointMenu, "Trim Endpoints"); 83 | } else if (state == INVERTAXISMENU) { 84 | menuHelper(axisMenuCount, axisMenu, "Invert Axis Menu"); 85 | } else if (state == EDIT_CHANNELS) { 86 | printValue(4, CHANNELS_MAX, cppmMenu[0]); 87 | } else if (state == EDIT_FRAME_LENGTH) { 88 | printValue(MIN_FRAME_LENGTH, MAX_FRAME_LENGTH, cppmMenu[1]); 89 | } else if (state == EDIT_PULSE_LENGTH) { 90 | printValue(MIN_PULSE_LENGTH, MAX_PULSE_LENGTH, cppmMenu[2]); 91 | } else if (state == EDIT_INVERT) { 92 | printValue(0, 1, cppmMenu[3]); 93 | } else if (state == EDIT_CPPM_PIN) { 94 | printValue(0, 13, cppmMenu[4]); 95 | } else if ((state >= EDIT_INVERT_ROLL) && (state <= EDIT_INVERT_AUX2)) { 96 | uint8_t index = state - EDIT_INVERT_ROLL; 97 | printValue(0, 1, (String("Invert ") + axisMenu[index]).c_str()); 98 | } else if ((state >= EDIT_MIN_ROLL) && (state <= EDIT_MAX_AUX2)) { 99 | uint8_t index = state - EDIT_MIN_ROLL; 100 | if (index & 0x01) { 101 | printValue(MIN_HIGH_ENDPOINT, MAX_HIGH_ENDPOINT, endpointMenu[index]); 102 | } else { 103 | printValue(MIN_LOW_ENDPOINT, MAX_LOW_ENDPOINT, endpointMenu[index]); 104 | } 105 | } else if ((state >= EDIT_TRIM_ROLL) && (state <= EDIT_TRIM_AUX2)) { 106 | uint8_t index = state - EDIT_TRIM_ROLL; 107 | printSignedValue(MIN_TRIM, MAX_TRIM, (String("Trim ") + axisMenu[index]).c_str()); 108 | } 109 | } 110 | 111 | void JoystickEventsButtons::OnButtonDown(uint8_t but_id) { 112 | #ifdef DEBUG_BUTTON_MFD 113 | String text = "Button " + String(but_id) + " down"; 114 | x52.setMFDText(1, text.c_str()); 115 | #endif 116 | 117 | if (but_id == MODE_BUTTON_GREEN) { 118 | x52.setLEDBrightness(2); 119 | x52.setMFDBrightness(2); 120 | currentMode = 1; 121 | } else if (but_id == MODE_BUTTON_YELLOW) { 122 | x52.setLEDBrightness(1); 123 | x52.setMFDBrightness(1); 124 | currentMode = 2; 125 | } else if (but_id == MODE_BUTTON_RED) { 126 | x52.setLEDBrightness(0); 127 | x52.setMFDBrightness(0); 128 | currentMode = 3; 129 | } else if ((but_id == MENU_BUTTON_ENTER_1) || (but_id == MENU_BUTTON_ENTER_2)) { 130 | if (state == NONE) { 131 | state = MAINMENU; 132 | index = 0; 133 | } else if (state == MAINMENU) { 134 | if (index == 0) { 135 | state = NONE; 136 | } else if (index == 1) { 137 | state = TRIMAXISMENU; 138 | index = 0; 139 | } else if (index == 2) { 140 | state = TRIMENDPOINTMENU; 141 | index = 0; 142 | } else if (index == 3) { 143 | state = INVERTAXISMENU; 144 | index = 0; 145 | } else if (index == 4) { 146 | state = CPPMMENU; 147 | index = 0; 148 | } else if (index == 5) { 149 | eepromWrite(); 150 | index = 0; 151 | } else if (index == 6) { 152 | eepromRead(); 153 | index = 0; 154 | } 155 | } else if (state == TRIMAXISMENU) { 156 | if (index == 0) { 157 | state = EDIT_TRIM_ROLL; 158 | signedValue = joyCPPM.getTrim(CHANNEL_ROLL); 159 | } else if (index == 1) { 160 | state = EDIT_TRIM_PITCH; 161 | signedValue = joyCPPM.getTrim(CHANNEL_PITCH); 162 | } else if (index == 2) { 163 | state = EDIT_TRIM_YAW; 164 | signedValue = joyCPPM.getTrim(CHANNEL_YAW); 165 | } else if (index == 3) { 166 | state = EDIT_TRIM_THROTTLE; 167 | signedValue = joyCPPM.getTrim(CHANNEL_THROTTLE); 168 | } else if (index == 4) { 169 | state = EDIT_TRIM_AUX1; 170 | signedValue = joyCPPM.getTrim(CHANNEL_AUX1); 171 | } else if (index == 5) { 172 | state = EDIT_TRIM_AUX2; 173 | signedValue = joyCPPM.getTrim(CHANNEL_AUX2); 174 | } else if (index == 6) { 175 | state = MAINMENU; 176 | index = 0; 177 | } 178 | } else if (state == TRIMENDPOINTMENU) { 179 | if (index == 0) { 180 | state = EDIT_MIN_ROLL; 181 | value = joyCPPM.getMinimum(CHANNEL_ROLL); 182 | } else if (index == 1) { 183 | state = EDIT_MAX_ROLL; 184 | value = joyCPPM.getMaximum(CHANNEL_ROLL); 185 | } else if (index == 2) { 186 | state = EDIT_MIN_PITCH; 187 | value = joyCPPM.getMinimum(CHANNEL_PITCH); 188 | } else if (index == 3) { 189 | state = EDIT_MAX_PITCH; 190 | value = joyCPPM.getMaximum(CHANNEL_PITCH); 191 | } else if (index == 4) { 192 | state = EDIT_MIN_YAW; 193 | value = joyCPPM.getMinimum(CHANNEL_YAW); 194 | } else if (index == 5) { 195 | state = EDIT_MAX_YAW; 196 | value = joyCPPM.getMaximum(CHANNEL_YAW); 197 | } else if (index == 6) { 198 | state = EDIT_MIN_THROTTLE; 199 | value = joyCPPM.getMinimum(CHANNEL_THROTTLE); 200 | } else if (index == 7) { 201 | state = EDIT_MAX_THROTTLE; 202 | value = joyCPPM.getMaximum(CHANNEL_THROTTLE); 203 | } else if (index == 8) { 204 | state = EDIT_MIN_AUX1; 205 | value = joyCPPM.getMinimum(CHANNEL_AUX1); 206 | } else if (index == 9) { 207 | state = EDIT_MAX_AUX1; 208 | value = joyCPPM.getMaximum(CHANNEL_AUX1); 209 | } else if (index == 10) { 210 | state = EDIT_MIN_AUX2; 211 | value = joyCPPM.getMinimum(CHANNEL_AUX2); 212 | } else if (index == 11) { 213 | state = EDIT_MAX_AUX2; 214 | value = joyCPPM.getMaximum(CHANNEL_AUX2); 215 | } else if (index == 12) { 216 | state = EDIT_MIN_ROLL; 217 | state = MAINMENU; 218 | index = 0; 219 | } 220 | } else if (state == INVERTAXISMENU) { 221 | if (index == 0) { 222 | state = EDIT_INVERT_ROLL; 223 | value = joyCPPM.getInvert(CHANNEL_ROLL); 224 | } else if (index == 1) { 225 | state = EDIT_INVERT_PITCH; 226 | value = joyCPPM.getInvert(CHANNEL_PITCH); 227 | } else if (index == 2) { 228 | state = EDIT_INVERT_YAW; 229 | value = joyCPPM.getInvert(CHANNEL_YAW); 230 | } else if (index == 3) { 231 | state = EDIT_INVERT_THROTTLE; 232 | value = joyCPPM.getInvert(CHANNEL_THROTTLE); 233 | } else if (index == 4) { 234 | state = EDIT_INVERT_AUX1; 235 | value = joyCPPM.getInvert(CHANNEL_AUX1); 236 | } else if (index == 5) { 237 | state = EDIT_INVERT_AUX2; 238 | value = joyCPPM.getInvert(CHANNEL_AUX2); 239 | } else if (index == 6) { 240 | state = MAINMENU; 241 | index = 0; 242 | } 243 | } else if (state == CPPMMENU) { 244 | if (index == 0) { 245 | state = EDIT_CHANNELS; 246 | value = CPPM::instance().getChannels(); 247 | } else if (index == 1) { 248 | state = EDIT_FRAME_LENGTH; 249 | value = CPPM::instance().getFrameLength(); 250 | } else if (index == 2) { 251 | state = EDIT_PULSE_LENGTH; 252 | value = CPPM::instance().getPulseLength(); 253 | } else if (index == 3) { 254 | state = EDIT_INVERT; 255 | value = CPPM::instance().getInvert(); 256 | } else if (index == 4) { 257 | state = EDIT_CPPM_PIN; 258 | value = CPPM::instance().getOutput(); 259 | } else if (index == 5) { 260 | state = MAINMENU; 261 | index = 0; 262 | } 263 | } else if (state == EDIT_CHANNELS) { 264 | CPPM::instance().setChannels(value); 265 | state = CPPMMENU; 266 | } else if (state == EDIT_FRAME_LENGTH) { 267 | CPPM::instance().setFrameLength(value); 268 | state = CPPMMENU; 269 | } else if (state == EDIT_PULSE_LENGTH) { 270 | CPPM::instance().setPulseLength(value); 271 | state = CPPMMENU; 272 | } else if (state == EDIT_INVERT) { 273 | CPPM::instance().setInvert(value); 274 | state = CPPMMENU; 275 | } else if (state == EDIT_CPPM_PIN) { 276 | CPPM::instance().setOutput(value); 277 | state = CPPMMENU; 278 | } else if (state == EDIT_INVERT_ROLL) { 279 | joyCPPM.setInvert(CHANNEL_ROLL, value); 280 | state = INVERTAXISMENU; 281 | } else if (state == EDIT_INVERT_PITCH) { 282 | joyCPPM.setInvert(CHANNEL_PITCH, value); 283 | state = INVERTAXISMENU; 284 | } else if (state == EDIT_INVERT_YAW) { 285 | joyCPPM.setInvert(CHANNEL_YAW, value); 286 | state = INVERTAXISMENU; 287 | } else if (state == EDIT_INVERT_THROTTLE) { 288 | joyCPPM.setInvert(CHANNEL_THROTTLE, value); 289 | state = INVERTAXISMENU; 290 | } else if (state == EDIT_INVERT_AUX1) { 291 | joyCPPM.setInvert(CHANNEL_AUX1, value); 292 | state = INVERTAXISMENU; 293 | } else if (state == EDIT_INVERT_AUX2) { 294 | joyCPPM.setInvert(CHANNEL_AUX2, value); 295 | state = INVERTAXISMENU; 296 | } else if (state == EDIT_MIN_ROLL) { 297 | joyCPPM.setMinimum(CHANNEL_ROLL, value); 298 | state = TRIMENDPOINTMENU; 299 | } else if (state == EDIT_MAX_ROLL) { 300 | joyCPPM.setMaximum(CHANNEL_ROLL, value); 301 | state = TRIMENDPOINTMENU; 302 | } else if (state == EDIT_MIN_PITCH) { 303 | joyCPPM.setMinimum(CHANNEL_PITCH, value); 304 | state = TRIMENDPOINTMENU; 305 | } else if (state == EDIT_MAX_PITCH) { 306 | joyCPPM.setMaximum(CHANNEL_PITCH, value); 307 | state = TRIMENDPOINTMENU; 308 | } else if (state == EDIT_MIN_YAW) { 309 | joyCPPM.setMinimum(CHANNEL_YAW, value); 310 | state = TRIMENDPOINTMENU; 311 | } else if (state == EDIT_MAX_YAW) { 312 | joyCPPM.setMaximum(CHANNEL_YAW, value); 313 | state = TRIMENDPOINTMENU; 314 | } else if (state == EDIT_MIN_THROTTLE) { 315 | joyCPPM.setMinimum(CHANNEL_THROTTLE, value); 316 | state = TRIMENDPOINTMENU; 317 | } else if (state == EDIT_MAX_THROTTLE) { 318 | joyCPPM.setMaximum(CHANNEL_THROTTLE, value); 319 | state = TRIMENDPOINTMENU; 320 | } else if (state == EDIT_MIN_AUX1) { 321 | joyCPPM.setMinimum(CHANNEL_AUX1, value); 322 | state = TRIMENDPOINTMENU; 323 | } else if (state == EDIT_MAX_AUX1) { 324 | joyCPPM.setMaximum(CHANNEL_AUX1, value); 325 | state = TRIMENDPOINTMENU; 326 | } else if (state == EDIT_MIN_AUX2) { 327 | joyCPPM.setMinimum(CHANNEL_AUX2, value); 328 | state = TRIMENDPOINTMENU; 329 | } else if (state == EDIT_MAX_AUX2) { 330 | joyCPPM.setMaximum(CHANNEL_AUX2, value); 331 | state = TRIMENDPOINTMENU; 332 | } else if (state == EDIT_TRIM_ROLL) { 333 | joyCPPM.setTrim(CHANNEL_ROLL, signedValue); 334 | state = TRIMAXISMENU; 335 | } else if (state == EDIT_TRIM_PITCH) { 336 | joyCPPM.setTrim(CHANNEL_PITCH, signedValue); 337 | state = TRIMAXISMENU; 338 | } else if (state == EDIT_TRIM_YAW) { 339 | joyCPPM.setTrim(CHANNEL_YAW, signedValue); 340 | state = TRIMAXISMENU; 341 | } else if (state == EDIT_TRIM_THROTTLE) { 342 | joyCPPM.setTrim(CHANNEL_THROTTLE, signedValue); 343 | state = TRIMAXISMENU; 344 | } else if (state == EDIT_TRIM_AUX1) { 345 | joyCPPM.setTrim(CHANNEL_AUX1, signedValue); 346 | state = TRIMAXISMENU; 347 | } else if (state == EDIT_TRIM_AUX2) { 348 | joyCPPM.setTrim(CHANNEL_AUX2, signedValue); 349 | state = TRIMAXISMENU; 350 | } 351 | printMenu(); 352 | } else if (but_id == MENU_BUTTON_DOWN) { 353 | if ((state > STATES_EDIT) && (state < STATES_EDIT_SIGNED)) { 354 | if (value > 0) { 355 | value--; 356 | } 357 | } else if (state > STATES_EDIT_SIGNED) { 358 | if (signedValue > -32767) { 359 | signedValue--; 360 | } 361 | } else if (state != NONE) { 362 | index++; 363 | } 364 | printMenu(); 365 | } else if (but_id == MENU_BUTTON_UP) { 366 | if ((state > STATES_EDIT) && (state < STATES_EDIT_SIGNED)) { 367 | if (value < 0xFFFF) { 368 | value++; 369 | } 370 | } else if (state > STATES_EDIT_SIGNED) { 371 | if (signedValue < 32767) { 372 | signedValue++; 373 | } 374 | } else if (state != NONE) { 375 | if (index > 0) { 376 | index--; 377 | } 378 | } 379 | printMenu(); 380 | } 381 | 382 | if (client) { 383 | client->OnButtonDown(but_id); 384 | } 385 | } 386 | 387 | void JoystickEventsButtons::menuHelper(uint8_t count, const char** menu, const char* title) { 388 | if (index >= count) { 389 | index = count - 1; 390 | } 391 | 392 | uint8_t start = 0, line = 0; 393 | if (index > 1) { 394 | start = index - 1; 395 | } 396 | 397 | uint8_t end = start + 2; 398 | if (index == 0) { 399 | x52.setMFDText(0, title); 400 | line = 1; 401 | end = start + 1; 402 | } 403 | 404 | if (end >= count) { 405 | end = count - 1; 406 | } 407 | 408 | for (uint8_t i = start; i <= end; i++) { 409 | String tmp = (index == i) ? "-> " : " "; 410 | x52.setMFDText(line++, (tmp + menu[i]).c_str()); 411 | } 412 | 413 | if (line == 2) { 414 | x52.setMFDText(2); 415 | } 416 | 417 | #ifdef DEBUG_OUTPUT 418 | DEBUG_OUTPUT.print("menuHelper() state="); 419 | DEBUG_OUTPUT.print(state); 420 | DEBUG_OUTPUT.print(" index="); 421 | DEBUG_OUTPUT.print(index); 422 | DEBUG_OUTPUT.print(" start="); 423 | DEBUG_OUTPUT.print(start); 424 | DEBUG_OUTPUT.print(" end="); 425 | DEBUG_OUTPUT.println(end); 426 | #endif 427 | } 428 | 429 | void JoystickEventsButtons::printValue(uint16_t min, uint16_t max, const char* title) { 430 | #ifdef DEBUG_OUTPUT 431 | DEBUG_OUTPUT.print("printValue() state="); 432 | DEBUG_OUTPUT.print(state); 433 | DEBUG_OUTPUT.print(" index="); 434 | DEBUG_OUTPUT.print(index); 435 | DEBUG_OUTPUT.print(" min="); 436 | DEBUG_OUTPUT.print(min); 437 | DEBUG_OUTPUT.print(" max="); 438 | DEBUG_OUTPUT.println(max); 439 | #endif 440 | 441 | if (value < min) { 442 | value = min; 443 | } 444 | if (value > max) { 445 | value = max; 446 | } 447 | 448 | x52.setMFDText(0, (String(title) + ":").c_str()); 449 | x52.setMFDText(1, String(value).c_str()); 450 | x52.setMFDText(2, "Press OK to save"); 451 | } 452 | 453 | void JoystickEventsButtons::printSignedValue(int16_t min, int16_t max, const char* title) { 454 | #ifdef DEBUG_OUTPUT 455 | DEBUG_OUTPUT.print("printSignedValue() state="); 456 | DEBUG_OUTPUT.print(state); 457 | DEBUG_OUTPUT.print(" index="); 458 | DEBUG_OUTPUT.print(index); 459 | DEBUG_OUTPUT.print(" min="); 460 | DEBUG_OUTPUT.print(min); 461 | DEBUG_OUTPUT.print(" max="); 462 | DEBUG_OUTPUT.println(max); 463 | #endif 464 | 465 | if (signedValue < min) { 466 | signedValue = min; 467 | } 468 | if (signedValue > max) { 469 | signedValue = max; 470 | } 471 | 472 | x52.setMFDText(0, (String(title) + ":").c_str()); 473 | x52.setMFDText(1, String(signedValue).c_str()); 474 | x52.setMFDText(2, "Press OK to save"); 475 | } 476 | 477 | --------------------------------------------------------------------------------