├── LICENSE ├── README.md ├── in_use.png ├── inc └── vjoyinterface.h ├── lib ├── amd64 │ ├── vJoyInterface.dll │ ├── vJoyInterface.lib │ └── vJoyInterface.pdb ├── vJoyInterface.dll ├── vJoyInterface.lib └── vJoyInterface.pdb ├── mDjiController.zip └── src ├── Joystick.cpp ├── Joystick.h ├── Main.cpp ├── Serial.cpp └── Serial.h /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | mDjiController 2 | =============== 3 | - Connect your DJI Remote Controller to your PC and use it to play simulators. 4 | - Currently confirmed working controllers: DJI Phantom 2 5 | - For Mavic Mini or a Python interface, take a look at [justin97530/miniDjiController](https://github.com/justin97530/miniDjiController) 6 | - For DJI Mini 2 or a Python interface, take a look at [usatenko/DjiMini2RCasJoystick](https://github.com/usatenko/DjiMini2RCasJoystick) 7 | - For DJI Phantom 3 take a look at [mishavoloshchuk/mDjiController](https://github.com/mishavoloshchuk/mDjiController) 8 | - For DJI Mavic 3 (RC-N1) take a look at [IvanYaky/DJI_RC-N1_SIMULATOR_FLY_DCL](https://github.com/IvanYaky/DJI_RC-N1_SIMULATOR_FLY_DCL) 9 | ----------------------------------------------------------------------------- 10 | 11 | 12 | This is a program that connects to your DJI Phantom Remote Controller (RC), 13 | reads the stick positions and tells Windows that position. A simplified, wannabe driver. 14 | 15 | In use: 16 | ![](https://raw.githubusercontent.com/Matsemann/mDjiController/master/in_use.png) 17 | 18 | Here I'm flying a DJI Phantom in HELI-X5 using mDjiController. Open up the image in big version to see the controller configuration. 19 | 20 | Installation / Usage 21 | ------------ 22 | 23 | ### For a thorough guide on how to get started, see this blog post: [Using a Phantom 2 DJI Controller in AirSim](https://timebutt.github.io/static/using-a-phantom-dji-controller-in-airsim/) 24 | 25 | ### UPDATE: Slaterbbx has added more features to this, you should also check out his fork of this here: https://github.com/slaterbbx/mDjiController 26 | 27 | In order for it to work, the original drivers from DJI must be installed, and something called vJoy. 28 | 29 | * Download mDjiController.zip from above ( https://github.com/Matsemann/mDjiController/blob/master/mDjiController.zip?raw=true ) 30 | * Download DJI WIN Driver Installer from here: http://www.dji.com/product/phantom-2/download 31 | * Download vJoy from here: http://vjoystick.sourceforge.net/site/index.php/download-a-install/72-download 32 | 33 | * Install both. 34 | * Then connect your RC to your computer via USB and turn it on. 35 | 36 | * Run mDjiController.exe, and select the correct COM port. By default you should try writing "3". 37 | * For vJoy, write 1 unless you use vJoy already and have different configurations. 38 | 39 | Then open a simulator and calibrate the controller. 40 | 41 | How does it work? 42 | ----------------- 43 | 44 | * It is possible to ask the controller for its status when it's connected. So this program continuously 45 | reads the status. It connects through COM. The output from the controller is just a list of numbers, 46 | but sampling many enough such lists it's easy to see a pattern for which number means what. The biggest issue 47 | was that the numbers are little endian encoded and uses two's complement, so it took some time to understand how 48 | each number behaved. 49 | 50 | * vJoy is a virtual joystick that can be installed on your computer. Windows think it's a normal joystick. mDjiController 51 | takes the stick positions from the controller and tells Windows that this virtual joystick has the same positions. 52 | 53 | 54 | TROUBLESHOOTING 55 | --------------- 56 | * If something doesn't work, make sure you have enabled logging, it may tell you the error. 57 | 58 | * COM can't connect 59 | Try with other numbers instead of 3. Open up device-manager. 60 | You should see a category named "Ports (COM & LPT) and then a device named "DJI USB Virtual COM (COMX)" 61 | The X should be the number you should use. 62 | If there is nothing there, the driver from DJI is not installed correctly. 63 | 64 | If you have any of the DJI Assistant Software installed, make sure they are not running, as this program 65 | cannot connect then. 66 | 67 | * vJoy can't connect 68 | The error message should tell you if it's installed or not. Install the vJoy driver if it's not there. 69 | 70 | * Not all sticks or buttons work 71 | *If they look correct in the log:* 72 | Your vJoy configuration may be wrong. Open "Configure vJoy" and reset configuration 1, or make a new one 73 | and tell mDjiController.exe to use that configuration. 74 | *If they are NOT correct in the log:* 75 | Your controller sends in a different format, so you need to figure that out, edit the code and recompile. 76 | (A bit advanced) 77 | 78 | 79 | -------------------------------------------------------------------------------- /in_use.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Matsemann/mDjiController/e3ba0dcdb71d8f65874c06b678a50d68eb8bde3f/in_use.png -------------------------------------------------------------------------------- /inc/vjoyinterface.h: -------------------------------------------------------------------------------- 1 | // The following ifdef block is the standard way of creating macros which make exporting 2 | // from a DLL simpler. All files within this DLL are compiled with the VJOYINTERFACE_EXPORTS 3 | // symbol defined on the command line. this symbol should not be defined on any project 4 | // that uses this DLL. This way any other project whose source files include this file see 5 | // VJOYINTERFACE_API functions as being imported from a DLL, whereas this DLL sees symbols 6 | // defined with this macro as being exported. 7 | #ifdef VJOYINTERFACE_EXPORTS 8 | #define VJOYINTERFACE_API __declspec(dllexport) 9 | #else 10 | #define VJOYINTERFACE_API __declspec(dllimport) 11 | #endif 12 | 13 | ///////////////////////////// vJoy device (collection) status //////////////////////////////////////////// 14 | #ifndef VJDSTAT 15 | #define VJDSTAT 16 | enum VjdStat /* Declares an enumeration data type called BOOLEAN */ 17 | { 18 | VJD_STAT_OWN, // The vJoy Device is owned by this application. 19 | VJD_STAT_FREE, // The vJoy Device is NOT owned by any application (including this one). 20 | VJD_STAT_BUSY, // The vJoy Device is owned by another application. It cannot be acquired by this application. 21 | VJD_STAT_MISS, // The vJoy Device is missing. It either does not exist or the driver is down. 22 | VJD_STAT_UNKN // Unknown 23 | }; 24 | #endif 25 | extern "C" { 26 | ///////////////////////////// vJoy device (collection) Control interface ///////////////////////////////// 27 | /* 28 | These functions allow writing feeders and other applications that interface with vJoy 29 | It is assumed that only one vJoy top-device (= Raw PDO) exists. 30 | This top-level device can have up to 16 siblings (=top-level Reports/collections) 31 | Each sibling is refered to as a "vJoy Device" and is attributed a unique Report ID (Range: 1-16). 32 | 33 | Naming convetion: 34 | VJD = vJoy Device 35 | rID = Report ID 36 | */ 37 | 38 | ///// General driver data 39 | VJOYINTERFACE_API SHORT __cdecl GetvJoyVersion(void); 40 | VJOYINTERFACE_API BOOL __cdecl vJoyEnabled(void); 41 | VJOYINTERFACE_API PVOID __cdecl GetvJoyProductString(void); 42 | VJOYINTERFACE_API PVOID __cdecl GetvJoyManufacturerString(void); 43 | VJOYINTERFACE_API PVOID __cdecl GetvJoySerialNumberString(void); 44 | 45 | 46 | ///// vJoy Device properties 47 | VJOYINTERFACE_API int __cdecl GetVJDButtonNumber(UINT rID); // Get the number of buttons defined in the specified VDJ 48 | VJOYINTERFACE_API int __cdecl GetVJDDiscPovNumber(UINT rID); // Get the number of descrete-type POV hats defined in the specified VDJ 49 | VJOYINTERFACE_API int __cdecl GetVJDContPovNumber(UINT rID); // Get the number of descrete-type POV hats defined in the specified VDJ 50 | VJOYINTERFACE_API BOOL __cdecl GetVJDAxisExist(UINT rID, UINT Axis); // Test if given axis defined in the specified VDJ 51 | VJOYINTERFACE_API BOOL __cdecl GetVJDAxisMax(UINT rID, UINT Axis, LONG * Max); // Get logical Maximum value for a given axis defined in the specified VDJ 52 | VJOYINTERFACE_API BOOL __cdecl GetVJDAxisMin(UINT rID, UINT Axis, LONG * Min); // Get logical Minimum value for a given axis defined in the specified VDJ 53 | 54 | ///// Write access to vJoy Device - Basic 55 | VJOYINTERFACE_API BOOL __cdecl AcquireVJD(UINT rID); // Acquire the specified vJoy Device. 56 | VJOYINTERFACE_API VOID __cdecl RelinquishVJD(UINT rID); // Relinquish the specified vJoy Device. 57 | VJOYINTERFACE_API BOOL __cdecl UpdateVJD(UINT rID, PVOID pData); // Update the position data of the specified vJoy Device. 58 | VJOYINTERFACE_API enum VjdStat __cdecl GetVJDStatus(UINT rID); // Get the status of the specified vJoy Device. 59 | 60 | ///// Write access to vJoy Device - Modifyiers 61 | // This group of functions modify the current value of the position data 62 | // They replace the need to create a structure of position data then call UpdateVJD 63 | 64 | //// Reset functions 65 | VJOYINTERFACE_API BOOL __cdecl ResetVJD(UINT rID); // Reset all controls to predefined values in the specified VDJ 66 | VJOYINTERFACE_API VOID __cdecl ResetAll(void); // Reset all controls to predefined values in all VDJ 67 | VJOYINTERFACE_API BOOL __cdecl ResetButtons(UINT rID); // Reset all buttons (To 0) in the specified VDJ 68 | VJOYINTERFACE_API BOOL __cdecl ResetPovs(UINT rID); // Reset all POV Switches (To -1) in the specified VDJ 69 | 70 | // Write data 71 | VJOYINTERFACE_API BOOL __cdecl SetAxis(LONG Value, UINT rID, UINT Axis); // Write Value to a given axis defined in the specified VDJ 72 | VJOYINTERFACE_API BOOL __cdecl SetBtn(BOOL Value, UINT rID, UCHAR nBtn); // Write Value to a given button defined in the specified VDJ 73 | VJOYINTERFACE_API BOOL __cdecl SetDiscPov(int Value, UINT rID, UCHAR nPov); // Write Value to a given descrete POV defined in the specified VDJ 74 | VJOYINTERFACE_API BOOL __cdecl SetContPov(DWORD Value, UINT rID, UCHAR nPov); // Write Value to a given continuous POV defined in the specified VDJ 75 | 76 | } // extern "C" -------------------------------------------------------------------------------- /lib/amd64/vJoyInterface.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Matsemann/mDjiController/e3ba0dcdb71d8f65874c06b678a50d68eb8bde3f/lib/amd64/vJoyInterface.dll -------------------------------------------------------------------------------- /lib/amd64/vJoyInterface.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Matsemann/mDjiController/e3ba0dcdb71d8f65874c06b678a50d68eb8bde3f/lib/amd64/vJoyInterface.lib -------------------------------------------------------------------------------- /lib/amd64/vJoyInterface.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Matsemann/mDjiController/e3ba0dcdb71d8f65874c06b678a50d68eb8bde3f/lib/amd64/vJoyInterface.pdb -------------------------------------------------------------------------------- /lib/vJoyInterface.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Matsemann/mDjiController/e3ba0dcdb71d8f65874c06b678a50d68eb8bde3f/lib/vJoyInterface.dll -------------------------------------------------------------------------------- /lib/vJoyInterface.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Matsemann/mDjiController/e3ba0dcdb71d8f65874c06b678a50d68eb8bde3f/lib/vJoyInterface.lib -------------------------------------------------------------------------------- /lib/vJoyInterface.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Matsemann/mDjiController/e3ba0dcdb71d8f65874c06b678a50d68eb8bde3f/lib/vJoyInterface.pdb -------------------------------------------------------------------------------- /mDjiController.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Matsemann/mDjiController/e3ba0dcdb71d8f65874c06b678a50d68eb8bde3f/mDjiController.zip -------------------------------------------------------------------------------- /src/Joystick.cpp: -------------------------------------------------------------------------------- 1 | #include "Joystick.h" 2 | #include "vjoyinterface.h" 3 | 4 | Joystick::Joystick(int id, int logging) 5 | { 6 | interfaceId = id; 7 | if (logging == 0) { 8 | log = true; 9 | } 10 | 11 | printf("Creating vJoy connection\n"); 12 | 13 | if (!vJoyEnabled()) { 14 | printf("vJoy driver not enabled: Failed Getting vJoy attributes.\n"); 15 | return; 16 | } else { 17 | printf("Driver found:\nVendor:%S, Product:%S, Ver:%S\n", TEXT(GetvJoyManufacturerString()), TEXT(GetvJoyProductString()), TEXT(GetvJoySerialNumberString())); 18 | }; 19 | 20 | VjdStat status = GetVJDStatus(interfaceId); 21 | switch (status) 22 | { 23 | case VJD_STAT_OWN: 24 | printf("vJoy Device %d is already owned by this feeder\n", interfaceId); 25 | break; 26 | case VJD_STAT_FREE: 27 | printf("vJoy Device %d is free\n", interfaceId); 28 | break; 29 | case VJD_STAT_BUSY: 30 | printf("vJoy Device %d is already owned by another feeder\nCannot continue\n", interfaceId); 31 | return; 32 | case VJD_STAT_MISS: 33 | printf("vJoy Device %d is not installed or disabled\nCannot continue\n", interfaceId); 34 | return; 35 | default: 36 | printf("vJoy Device %d general error\nCannot continue\n", interfaceId); 37 | return; 38 | }; 39 | 40 | if (!AcquireVJD(interfaceId)) { 41 | printf("Failed to acquire vJoy Interface number %d\n", interfaceId); 42 | return; 43 | } 44 | else { 45 | ResetVJD(interfaceId); 46 | connected = true; 47 | printf("Acquired vJoy Interface number %d\n", interfaceId); 48 | } 49 | } 50 | 51 | void Joystick::update(int l_hor, int l_ver, int r_hor, int r_ver, int lever_left, int lever_right, int camera) 52 | { 53 | // Values must be between 0x0 and 0x8000 54 | // The values from the controller are between -1000 and 1000, so we just scale them between 0 and 16000 (maximum range in vJoyMonitor) 55 | l_hor += 1000; 56 | l_ver += 1000; 57 | r_hor += 1000; 58 | r_ver += 1000; 59 | camera -= 1000; 60 | 61 | l_hor *= 16; 62 | l_ver *= 16; 63 | r_hor *= 16; 64 | r_ver *= 16; 65 | camera *= -16; // negative multiplication to make the value feel natural (left = negative | right = positive) 66 | 67 | // Read values from left lever 68 | // -780 = 'OFF' position | 0 = 'CL' position | 780 = 'HL' position 69 | if (lever_left == -780) { 70 | button_1 = true; 71 | button_2 = false; 72 | button_3 = false; 73 | } 74 | else if (lever_left == 780) { 75 | button_1 = false; 76 | button_2 = false; 77 | button_3 = true; 78 | } // else remain as before, sometimes this value is a bit weird 79 | else if (lever_left == 0) { 80 | button_1 = false; 81 | button_2 = true; 82 | button_3 = false; 83 | } 84 | 85 | // Read values from right lever 86 | // -780 = 'GPS' position | 0 = 'ATTI' position | 780 = 'ATTI' position 87 | if (lever_right == -780) { 88 | button_4 = true; 89 | button_5 = false; 90 | button_6 = false; 91 | } 92 | else if (lever_right == 780) { 93 | button_4 = false; 94 | button_5 = false; 95 | button_6 = true; 96 | } // else remain as before, sometimes this value is a bit weird 97 | else { 98 | button_4 = false; 99 | button_5 = true; 100 | button_6 = false; 101 | } 102 | 103 | if (log) 104 | printf("L vert: %-5d | L hori: %-5d | R vert: %-5d | R hori: %-5d | btn1: %-d | btn2: %-d | btn3: %-d | btn4: %-d | btn5: %-d | btn6: %-d | camera: %-d\n", l_ver, l_hor, r_ver, r_hor, button_1, button_2, button_3, button_4, button_5, button_6, camera); 105 | 106 | // Send stick values to vJoy 107 | SetAxis(l_hor, interfaceId, HID_USAGE_X); 108 | SetAxis(l_ver, interfaceId, HID_USAGE_Y); 109 | SetAxis(r_hor, interfaceId, HID_USAGE_Z); 110 | SetAxis(r_ver, interfaceId, HID_USAGE_RX); 111 | SetAxis(camera, interfaceId, HID_USAGE_RY); 112 | 113 | // Send button values to vJoy 114 | SetBtn(button_1, interfaceId, 1); 115 | SetBtn(button_2, interfaceId, 2); 116 | SetBtn(button_3, interfaceId, 3); 117 | SetBtn(button_4, interfaceId, 4); 118 | SetBtn(button_5, interfaceId, 5); 119 | SetBtn(button_6, interfaceId, 6); 120 | } 121 | 122 | Joystick::~Joystick() 123 | { 124 | RelinquishVJD(interfaceId); 125 | printf("Disconnected from joystick\n"); 126 | } -------------------------------------------------------------------------------- /src/Joystick.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #define HID_USAGE_X 0x30 7 | #define HID_USAGE_Y 0x31 8 | #define HID_USAGE_Z 0x32 9 | #define HID_USAGE_RX 0x33 10 | #define HID_USAGE_RY 0x34 11 | #define HID_USAGE_RZ 0x35 12 | 13 | class Joystick 14 | { 15 | private: 16 | bool button_1, button_2, button_3, button_4, button_5, button_6; 17 | bool connected; 18 | unsigned int interfaceId; 19 | bool log = false; 20 | public: 21 | Joystick(int interfaceId, int logging); 22 | ~Joystick(); 23 | void update(int l_hor, int l_ver, int r_hor, int r_ver, int toggle_left, int toggle_right, int camera); 24 | bool isConnected() { return connected; } 25 | }; -------------------------------------------------------------------------------- /src/Main.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "Serial.h" 8 | #include "Joystick.h" 9 | 10 | 11 | 12 | short littleEndiansToShort(int first, int second) { 13 | if (first < 0) { 14 | first = 256 + first; 15 | } 16 | 17 | short combined = second << 8 | first; 18 | return combined; 19 | 20 | } 21 | 22 | void run(char* portName, int stickId, int logging) { 23 | 24 | char initData[] = { 0x55, 0xaa, 0x55, 0xaa, 0x1e, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x80, 0x00, 0x04, 0x04, 0x74, 0x94, 0x35, 0x00, 0xd8, 0xc0, 0x41, 0x00, 0x30, 0xf6, 0x08, 0x00, 0x00, 0xf6, 0x69, 0x9c, 0x01, 0xe8}; 25 | char pingData[] = { 0x55, 0xaa, 0x55, 0xaa, 0x1e, 0x00, 0x01, 0x00, 0x00, 0x1c, 0x02, 0x00, 0x80, 0x00, 0x06, 0x01, 0x28, 0x97, 0xae, 0x03, 0x28, 0x36, 0xa4, 0x03, 0x28, 0x36, 0xa4, 0x03, 0xab, 0xa7, 0x30, 0x00, 0x03, 0x53}; 26 | 27 | char incomingData[256] = ""; 28 | int dataLength = 256; 29 | int readResult = 0; 30 | bool shouldRun = true; 31 | 32 | 33 | Joystick j(stickId, logging); 34 | if (!j.isConnected()) { 35 | printf("Couldn't connect to vJoy, quitting...\n"); 36 | return; 37 | } 38 | printf("\n"); 39 | 40 | Serial s(portName); 41 | if (!s.IsConnected()) { 42 | printf("Couldn't connect to COM port, quitting...\n"); 43 | return; 44 | } 45 | 46 | printf("\nEverything is ready\n\n"); 47 | s.WriteData(initData, 34); 48 | 49 | printf("Running...\nPress ESC to quit\n"); 50 | Sleep(2000); 51 | 52 | while (s.IsConnected() && shouldRun) 53 | { 54 | s.WriteData(pingData, 34); // write this once in a while, otherwise it stops sending? :O 55 | readResult = s.ReadData(incomingData, dataLength); 56 | 57 | if (readResult == 76 && incomingData[0] == 0x55) { // probably positioning data 58 | short left_vertical = littleEndiansToShort(incomingData[39], incomingData[40]); 59 | short left_horizontal = littleEndiansToShort(incomingData[43], incomingData[44]); 60 | 61 | short right_horizontal = littleEndiansToShort(incomingData[31], incomingData[32]); 62 | short right_vertical = littleEndiansToShort(incomingData[35], incomingData[36]); 63 | 64 | short left_lever = littleEndiansToShort(incomingData[47], incomingData[48]); 65 | short right_lever = littleEndiansToShort(incomingData[51], incomingData[52]); 66 | 67 | short camera = littleEndiansToShort(incomingData[55], incomingData[56]); 68 | 69 | // update our virtual joystick 70 | j.update(left_horizontal, left_vertical, right_horizontal, right_vertical, left_lever, right_lever, camera); 71 | } 72 | 73 | 74 | if (GetAsyncKeyState(VK_ESCAPE)) { 75 | shouldRun = false; 76 | printf("\n\nDetected ESC, quitting...\n"); 77 | } 78 | 79 | Sleep(10); 80 | } 81 | 82 | } 83 | 84 | int main() { 85 | std::string in; 86 | int portNr, stickId, logging; 87 | 88 | 89 | printf("Select port number (default COM \"3\"): "); 90 | std::getline(std::cin, in); 91 | portNr = atoi(in.c_str()); 92 | 93 | if (portNr < 1) { 94 | portNr = 3; 95 | } 96 | 97 | char port[100]; 98 | sprintf_s(port, "COM%d", portNr); 99 | 100 | 101 | printf("Select vJoy configuration (default \"1\"): "); 102 | std::getline(std::cin, in); 103 | stickId = atoi(in.c_str()); 104 | 105 | if (stickId < 1) { 106 | stickId = 1; 107 | } 108 | 109 | 110 | printf("Disable logging? 1 for off (default \"0\"): "); 111 | std::getline(std::cin, in); 112 | logging = atoi(in.c_str()); 113 | 114 | if (logging != 0) { 115 | logging = 1; 116 | } 117 | 118 | printf("Starting...\n\n"); 119 | run(port, stickId, logging); 120 | printf("Closing down...\n\n"); 121 | 122 | 123 | system("pause"); 124 | return 0; 125 | } -------------------------------------------------------------------------------- /src/Serial.cpp: -------------------------------------------------------------------------------- 1 | #include "Serial.h" 2 | 3 | /* 4 | * Adopted from http://playground.arduino.cc/Interfacing/CPPWindows 5 | */ 6 | 7 | Serial::Serial(char *portName) 8 | { 9 | printf("Creating serial connection\n"); 10 | //We're not yet connected 11 | this->connected = false; 12 | 13 | //Try to connect to the given port through CreateFile 14 | this->hSerial = CreateFile(portName, 15 | GENERIC_READ | GENERIC_WRITE, 16 | 0, 17 | NULL, 18 | OPEN_EXISTING, 19 | FILE_ATTRIBUTE_NORMAL, 20 | NULL); 21 | 22 | //Check if the connection was successful 23 | if (this->hSerial == INVALID_HANDLE_VALUE) 24 | { 25 | //If not success full display an Error 26 | if (GetLastError() == ERROR_FILE_NOT_FOUND){ 27 | 28 | //Print Error if necessary 29 | printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName); 30 | 31 | } 32 | else 33 | { 34 | printf("Couldn't connect to COM port, unknown why..\n"); 35 | } 36 | } 37 | else 38 | { 39 | printf("Found serial port\n"); 40 | //If connected we try to set the comm parameters 41 | DCB dcbSerialParams = { 0 }; 42 | 43 | //Try to get the current 44 | if (!GetCommState(this->hSerial, &dcbSerialParams)) 45 | { 46 | //If impossible, show an error 47 | printf("failed to get current serial parameters!\n"); 48 | } 49 | else 50 | { 51 | //Define serial connection parameters for the Arduino board 52 | dcbSerialParams.BaudRate = 115200; 53 | dcbSerialParams.ByteSize = 8; 54 | dcbSerialParams.StopBits = ONESTOPBIT; 55 | dcbSerialParams.Parity = NOPARITY; 56 | 57 | //Set the parameters and check for their proper application 58 | if (!SetCommState(hSerial, &dcbSerialParams)) 59 | { 60 | printf("ALERT: Could not set Serial Port parameters\n"); 61 | } 62 | else 63 | { 64 | printf("Connected to serial port\n"); 65 | //If everything went fine we're connected 66 | this->connected = true; 67 | //Sleep(200); 68 | } 69 | } 70 | } 71 | 72 | } 73 | 74 | Serial::~Serial() 75 | { 76 | //Check if we are connected before trying to disconnect 77 | if (this->connected) 78 | { 79 | //We're no longer connected 80 | this->connected = false; 81 | //Close the serial handler 82 | CloseHandle(this->hSerial); 83 | printf("Closed serial connection\n"); 84 | } 85 | } 86 | 87 | int Serial::ReadData(char *buffer, unsigned int nbChar) 88 | { 89 | //Number of bytes we'll have read 90 | DWORD bytesRead; 91 | //Number of bytes we'll really ask to read 92 | unsigned int toRead; 93 | 94 | //Use the ClearCommError function to get status info on the Serial port 95 | ClearCommError(this->hSerial, &this->errors, &this->status); 96 | 97 | //Check if there is something to read 98 | if (this->status.cbInQue>0) 99 | { 100 | //If there is we check if there is enough data to read the required number 101 | //of characters, if not we'll read only the available characters to prevent 102 | //locking of the application. 103 | if (this->status.cbInQue>nbChar) 104 | { 105 | toRead = nbChar; 106 | } 107 | else 108 | { 109 | toRead = this->status.cbInQue; 110 | } 111 | 112 | //Try to read the require number of chars, and return the number of read bytes on success 113 | if (ReadFile(this->hSerial, buffer, toRead, &bytesRead, NULL) && bytesRead != 0) 114 | { 115 | return bytesRead; 116 | } 117 | 118 | } 119 | 120 | //If nothing has been read, or that an error was detected return -1 121 | return -1; 122 | 123 | } 124 | 125 | 126 | bool Serial::WriteData(char *buffer, unsigned int nbChar) 127 | { 128 | DWORD bytesSend; 129 | 130 | //Try to write the buffer on the Serial port 131 | if (!WriteFile(this->hSerial, (void *)buffer, nbChar, &bytesSend, 0)) 132 | { 133 | //In case it don't work get comm error and return false 134 | ClearCommError(this->hSerial, &this->errors, &this->status); 135 | 136 | return false; 137 | } 138 | else 139 | return true; 140 | } 141 | 142 | bool Serial::IsConnected() 143 | { 144 | //Simply return the connection status 145 | return this->connected; 146 | } -------------------------------------------------------------------------------- /src/Serial.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | /* 8 | * Adopted from http://playground.arduino.cc/Interfacing/CPPWindows 9 | */ 10 | 11 | class Serial 12 | { 13 | private: 14 | HANDLE hSerial; 15 | bool connected; 16 | COMSTAT status; 17 | DWORD errors; 18 | 19 | public: 20 | Serial(char *portName); 21 | ~Serial(); 22 | int ReadData(char *buffer, unsigned int nbChar); 23 | bool WriteData(char *buffer, unsigned int nbChar); 24 | bool IsConnected(); 25 | }; 26 | --------------------------------------------------------------------------------