From 1d766dbb09b5010531d57e093796733482f101e4 Mon Sep 17 00:00:00 2001 From: Jason Watkins Date: Fri, 24 Apr 2015 09:32:46 -0700 Subject: [PATCH] Updated C example program to conform to the new client API. --- C/xpcExample/xpcExample/src/main.cpp | 102 ++++++++++++--------------- 1 file changed, 47 insertions(+), 55 deletions(-) diff --git a/C/xpcExample/xpcExample/src/main.cpp b/C/xpcExample/xpcExample/src/main.cpp index f4ba86b..325622d 100644 --- a/C/xpcExample/xpcExample/src/main.cpp +++ b/C/xpcExample/xpcExample/src/main.cpp @@ -16,38 +16,18 @@ #define sleep(n) Sleep(n * 1000) #endif -int main() { - int i, j; - struct xpcSocket sendfd, readfd; - float data[4][9] = { 0 }; - char DREF[100] = { 0 }; - char DREFArray[100][100]; - char DREFArray2[100][100]; - short DREFSizes[100]; - float *recDATA[100]; - float POSI[9] = { 0.0 }; - float CTRL[5] = { 0.0 }; - float gear; - char IP[16] = "127.0.0.1"; //IP Address of computer running X-Plane - short PORT = 49009; //xpcPlugin Receiving port (usually 49009) +int main() +{ + printf("XPlaneConnect Example Script\n- Setting up Simulation\n"); - printf("xplaneconnect Example Script\n- Setting up Simulation\n"); - - for (i = 0; i < 100; i++) { - recDATA[i] = (float *)malloc(40 * sizeof(float)); - memset(DREFArray[i], 0, 100); - memset(DREFArray2[i], 0, 100); - } - - // Open Sockets - readfd = openUDP(49055, IP, PORT); //Open socket for receiving - sendfd = openUDP(49077, IP, PORT); //Open socket for sending - - // Set up Connection - setCONN(sendfd, 49055); // Setup so data will be received on port 49055 + // Open Socket + const char* IP = "127.0.0.1"; //IP Address of computer running X-Plane + const unsigned short PORT = 49009; //xpcPlugin Receiving port (usually 49009) + XPCSocket sock = aopenUDP(IP, PORT); // Set Location/Orientation (sendPOSI) // Set Up Position Array + float POSI[9] = { 0.0 }; POSI[0] = 37.524; // Lat POSI[1] = -122.06899; // Lon POSI[2] = 2500; // Alt @@ -56,22 +36,30 @@ int main() { POSI[5] = 0; // Heading POSI[6] = 1; // Gear - sendPOSI(sendfd, 0, 7, POSI); + // Set position of the player aircraft + psendPOSI(sock, POSI, 7); + POSI[0] = 37.52465; // Move a second aircraft a bit North of us POSI[4] = 20; // Give that aircraft a bit or right roll - sendPOSI(sendfd, 1, 7, POSI); + // Set position of a multiplayer aircraft + sendPOSI(sock, POSI, 7, 1); // Set Rates (sendDATA) - - for (i = 0; i < 4; i++) { // Set array to -999 - for (j = 0; j < 9; j++) data[i][j] = -999; + float data[3][9] = { 0 }; + // Initialize data values to -998 to not overwrite values. + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 9; j++) + { + data[i][j] = -998; + } } // Set up Data Array (first item in row is item number (example: 20=position) data[0][0] = 18; // Alpha data[0][1] = 0; data[0][3] = 0; - data[1][0] = 3;//21; //Velocity + data[1][0] = 3; //Velocity data[1][1] = 130; data[1][2] = 130; data[1][3] = 130; @@ -82,21 +70,19 @@ int main() { data[2][2] = 0; data[2][3] = 0; - sendDATA(sendfd, data, 3); // Velocity/Alpha/PQR + sendDATA(sock, data, 3); - // Set CTRL + // Set throttle on the player aircraft using sendCTRL + float CTRL[5] = { 0.0 }; CTRL[3] = 0.8; // Throttle - - sendCTRL(sendfd, 4, CTRL); + psendCTRL(sock, CTRL, 5); // pauseSim - pauseSim(sendfd, 1); // Sending 1 to pause - - // Pause for 5 seconds - sleep(5); + pauseSim(sock, 1); // Sending 1 to pause + sleep(5); // Pause for 5 seconds // Unpause - pauseSim(sendfd, 0); // Sending 0 to unpause + pauseSim(sock, 0); // Sending 0 to unpause printf("- Resuming Simulation\n"); // Simulate for 10 seconds @@ -105,28 +91,34 @@ int main() { // SendDREF (Landing Gear) printf("- Stowing Landing Gear\n"); - strcpy(DREF, "sim/cockpit/switches/gear_handle_status"); // Gear handle data reference - DREFSizes[0] = strlen(DREF); - gear = 1; // Stow gear - - sendDREF(sendfd, DREF, DREFSizes[0], &gear, 1); // Set gear to stow + const char* dref = "sim/cockpit/switches/gear_handle_status"; // Gear handle data reference + float gear = 0; // Stow gear + setDREF(sock, dref, &gear, 1); // Set gear to stow // Simulate for 10 seconds sleep(10); // Check Landing gear, Pause printf("- Confirming Gear Status\n"); - strcpy(DREFArray2[0], "sim/cockpit/switches/gear_handle_status"); - strcpy(DREFArray2[1], "sim/operation/override/override_planepath"); - for (i = 0; i < 2; i++) { - DREFSizes[i] = (int)strlen(DREFArray2[i]); + const char* drefs[2] = + { + "sim/cockpit/switches/gear_handle_status", + "sim/operation/override/override_planepath" + }; + float* results[2]; + int sizes[2] = { 20, 20 }; + for (int i = 0; i < 2; ++i) + { + results[i] = (float*)malloc(20 * sizeof(float)); } - requestDREF(sendfd, readfd, DREFArray2, DREFSizes, 2, recDATA, DREFSizes); // Request 2 values + getDREFs(sock, drefs, results, 2, sizes); - if (*(recDATA[0]) == 0) { + if (results[0][0] == 0) + { printf("\tGear Stowed\n"); } - else { + else + { printf("\tERROR: Gear Stowage unsuccessful\n"); }