| 1 | /* |
| 2 | Plib based joystick server based on PLIBs js_demo.cxx |
| 3 | |
| 4 | js_server is Copyright (C) 2003 |
| 5 | by Stephen Lowry and Manuel Bessler |
| 6 | |
| 7 | PLIB - A Suite of Portable Game Libraries |
| 8 | Copyright (C) 2001 Steve Baker |
| 9 | |
| 10 | This program is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation; either version 2 of the License, or |
| 13 | (at your option) any later version. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 23 | |
| 24 | For further information visit http://plib.sourceforge.net |
| 25 | |
| 26 | */ |
| 27 | |
| 28 | #include <math.h> |
| 29 | #include <stdint.h> |
| 30 | #include <stdio.h> |
| 31 | |
| 32 | #include <chrono> |
| 33 | #include <memory> |
| 34 | #include <thread> |
| 35 | |
| 36 | #include "FlightGear_js.h" |
| 37 | |
| 38 | |
| 39 | void usage(char* progname) |
| 40 | { |
| 41 | printf("This is an UDP based remote joystick server.\n"); |
| 42 | printf("usage: %s <hostname> <port>\n", progname); |
| 43 | } |
| 44 | |
| 45 | int main(int argc, char** argv) |
| 46 | { |
| 47 | int port = 16759; |
| 48 | char* host; /* = "192.168.1.7"; */ |
| 49 | int activeaxes = 4; |
| 50 | |
| 51 | if (argc != 3) { |
| 52 | usage(argv[0]); |
| 53 | exit(1); |
| 54 | } |
| 55 | host = argv[1]; |
| 56 | port = atoi(argv[2]); |
| 57 | |
| 58 | jsInit(); |
| 59 | |
| 60 | auto js = std::make_unique<jsJoystick>(0); |
| 61 | |
| 62 | if (js->notWorking()) { |
| 63 | printf("no Joystick detected... exiting\n"); |
| 64 | exit(1); |
| 65 | } |
| 66 | printf("Joystick is \"%s\"\n", js->getName()); |
| 67 | |
| 68 | int numaxes = js->getNumAxes(); |
| 69 | auto ax = std::make_unique<float[]>(numaxes); |
| 70 | activeaxes = numaxes; |
| 71 | |
| 72 | if (numaxes > 4) { |
| 73 | printf("max 4 axes joysticks supported at the moment, however %i axes were detected\nWill only use the first 4 axes!\n", numaxes); |
| 74 | activeaxes = 4; |
| 75 | } |
| 76 | |
| 77 | // Must call this before any other net stuff |
| 78 | netInit(&argc, argv); |
| 79 | |
| 80 | netSocket c; |
| 81 | |
| 82 | if (!c.open(false)) { // open a UDP socket |
| 83 | printf("error opening socket\n"); |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | c.setBlocking(false); |
| 88 | |
| 89 | if (c.connect(host, port) == -1) { |
| 90 | printf("error connecting to %s:%d\n", host, port); |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | char packet[256] = "Hello world!"; |
| 95 | while (1) { |
| 96 | int b; |
| 97 | int len = 0; |
| 98 | int axis = 0; |
| 99 | |
| 100 | js->read(&b, ax.get()); |
| 101 | for (axis = 0; axis < activeaxes; axis++) { |
| 102 | int32_t axisvalue = (int32_t)(ax[axis] * 2147483647.0); |
| 103 | printf("axisval=%li\n", (long)axisvalue); |
| 104 | memcpy(packet + len, &axisvalue, sizeof(axisvalue)); |
| 105 | len += sizeof(axisvalue); |
| 106 | } |
| 107 | // fill emtpy values into packets when less than 4 axes |
| 108 | for (; axis < 4; axis++) { |
| 109 | int32_t axisvalue = 0; |
| 110 | memcpy(packet + len, &axisvalue, sizeof(axisvalue)); |
| 111 | len += sizeof(axisvalue); |
| 112 | } |
| 113 | |
| 114 | int32_t b_l = b; |
| 115 | memcpy(packet + len, &b_l, sizeof(b_l)); |
| 116 | len += sizeof(b_l); |
| 117 | |
| 118 | const char* termstr = "\0\0\r\n"; |
| 119 | memcpy(packet + len, termstr, 4); |
| 120 | len += 4; |
| 121 | |
| 122 | c.send(packet, len, 0); |
| 123 | |
| 124 | /* give other processes a chance */ |
| 125 | { |
| 126 | using namespace std::chrono_literals; |
| 127 | std::this_thread::sleep_for(std::chrono::milliseconds(1ms)); |
| 128 | } |
| 129 | |
| 130 | printf("."); |
| 131 | fflush(stdout); |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |