Merge branch 'simu-switch' into develop

Resolves #52
This commit is contained in:
Jason Watkins
2015-05-01 15:21:45 -07:00
8 changed files with 128 additions and 42 deletions

View File

@@ -264,9 +264,17 @@ int setCONN(XPCSocket* sock, unsigned short port)
int pauseSim(XPCSocket sock, char pause) int pauseSim(XPCSocket sock, char pause)
{ {
// Validte input
if (pause < 0 || pause > 2)
{
printError("pauseSim", "Invalid argument: %i", pause);
return;
}
// Setup command // Setup command
char buffer[6] = "SIMU"; char buffer[6] = "SIMU";
buffer[5] = pause == 0 ? 0 : 1; buffer[5] = pause;
// Send command // Send command
if (sendUDP(sock, buffer, 6) < 0) if (sendUDP(sock, buffer, 6) < 0)
{ {

View File

@@ -214,6 +214,26 @@ public class XPlaneConnect implements AutoCloseable
sendUDP(msg); sendUDP(msg);
} }
/**
* Pauses, unpauses, or switches the pause state of X-Plane.
*
* @param pause {@code 1} to pause the simulator, {@code 0} to unpause, or {@code 2} to switch.
* @throws IllegalArgumentException If the values of {@code pause} is not a valid command.
* @throws IOException If the command cannot be sent.
*/
public void pauseSim(int pause) throws IOException
{
if(pause < 0 || pause > 2)
{
throw new IllegalArgumentException("pause must be a value in the range [0, 2].");
}
// S I M U LEN VAL
byte[] msg = {0x53, 0x49, 0x4D, 0x55, 0x00, 0x00};
msg[5] = (byte)pause;
sendUDP(msg);
}
/** /**
* Requests a single dref value from X-Plane. * Requests a single dref value from X-Plane.
* *

View File

@@ -29,7 +29,7 @@ if ~exist('socket', 'var')
end end
%% Validate input %% Validate input
pause = logical(pause); pause = int32(pause);
%% Send command %% Send command
socket.pauseSim(pause); socket.pauseSim(pause);

View File

@@ -81,11 +81,11 @@ class XPlaneConnect(object):
Args: Args:
pause: True to pause the simulation; False to resume. pause: True to pause the simulation; False to resume.
''' '''
pause_val = 0 pause = int(pause)
if pause: if pause < 0 or pause > 2:
pause_val = 1 raise ValueError("Invalid argument for pause command.")
buffer = struct.pack("<4sxB", "SIMU", pause_val) buffer = struct.pack("<4sxB", "SIMU", pause)
self.sendUDP(buffer) self.sendUDP(buffer)
# X-Plane UDP Data # X-Plane UDP Data

View File

@@ -833,56 +833,63 @@ int sendWYPTTest()
int pauseTest() // pauseSim test int pauseTest() // pauseSim test
{ {
// Initialize // Setup
// Note: Always run this test to the end so that the sim ends up unpaused in the // Note: Always run this test to the end so that the sim ends up unpaused in the
// case where commands are working but reading results isn't. // case where commands are working but reading results isn't.
int result = 0; int result = 0;
char* drefs[100] = char* dref = "sim/operation/override/override_planepath";
{ int size = 20;
"sim/operation/override/override_planepath" float data[20];
};
float* data[100];
int sizes[100];
XPCSocket sock = openUDP(IP); XPCSocket sock = openUDP(IP);
// Setup
for (int i = 0; i < 100; i++)
{
data[i] = (float*)malloc(40 * sizeof(float));
sizes[i] = 40;
}
// Execute // Execute
pauseSim(sock, 1); pauseSim(sock, 0);
result = getDREF(sock, drefs[0], data[0], sizes); result = getDREF(sock, dref, data, &size);
// Test // Test
if (result < 0) if (result < 0)
{ {
result = -1; result = -1;
} }
if (data[0][0] != 1) else if (data[0] != 0)
{ {
result = -2; result = -2;
} }
if (result == 0) if (result == 0)
{ {
// Execute 2 // Execute 2
pauseSim(sock, 0); pauseSim(sock, 2);
result = getDREF(sock, drefs[0], data[0], sizes); result = getDREF(sock, dref, data, &size);
// Test 2 // Test 2
if (result < 0) if (result < 0)
{ {
result = -3; result = -3;
} }
if (data[0][0] != 0) else if (data[0] != 1)
{ {
result = -4; result = -4;
} }
} }
if (result == 0)
{
// Execute 3
pauseSim(sock, 0);
result = getDREF(sock, dref, data, &size);
// Test 2
if (result < 0)
{
result = -5;
}
else if (data[0] != 0)
{
result = -6;
}
}
// Close // Close
closeUDP(sock); closeUDP(sock);

View File

@@ -208,12 +208,30 @@ public class XPlaneConnectTest
{ {
xpc.pauseSim(true); xpc.pauseSim(true);
float[] result = xpc.getDREF(dref); float[] result = xpc.getDREF(dref);
//assertEquals(1, result.length); //TODO: Why is this result 20 elements long in Java? (It's only one in MATLAB) assertEquals(20, result.length);
assertEquals(1, result[0], 1e-4); assertEquals(1, result[0], 1e-4);
xpc.pauseSim(false); xpc.pauseSim(false);
result = xpc.getDREF(dref); result = xpc.getDREF(dref);
//assertEquals(1, result.length); assertEquals(20, result.length);
assertEquals(0, result[0], 1e-4);
}
}
@Test
public void testPauseSim_Switch() throws IOException
{
String dref = "sim/operation/override/override_planepath";
try(XPlaneConnect xpc = new XPlaneConnect())
{
xpc.pauseSim(true);
float[] result = xpc.getDREF(dref);
assertEquals(20, result.length);
assertEquals(1, result[0], 1e-4);
xpc.pauseSim(2);
result = xpc.getDREF(dref);
assertEquals(20, result.length);
assertEquals(0, result[0], 1e-4); assertEquals(0, result[0], 1e-4);
} }
} }

View File

@@ -161,6 +161,16 @@ class XPCTests(unittest.TestCase):
expected = 0.0 expected = 0.0
do_test() do_test()
# Test 3
value = 1
expected = 1.0
do_test()
# Test 4
value = 2
expected = 0.0
do_test()
def test_sendCTRL(self): def test_sendCTRL(self):
# Setup # Setup

View File

@@ -496,28 +496,51 @@ namespace XPC
void MessageHandlers::HandleSimu(Message& msg) void MessageHandlers::HandleSimu(Message& msg)
{ {
// Update log // Update log
#if LOG_VERBOSITY > 0 #if LOG_VERBOSITY > 1
Log::FormatLine("[SIMU] Message Received (Conn %i)", connection.id); Log::FormatLine("[SIMU] Message Received (Conn %i)", connection.id);
#endif #endif
const unsigned char* buffer = msg.GetBuffer(); char v = msg.GetBuffer()[5];
if (v < 0 || v > 2)
// Set DREF
int value[20];
for (int i = 0; i < 20; ++i)
{ {
value[i] = buffer[5]; #if LOG_VERBOSITY > 0
Log::FormatLine("[SIMU] ERROR: Invalid argument: %i", v);
return;
#endif
} }
DataManager::Set(DREF::Pause, value, 20);
int value[20];
#if LOG_VERBOSITY > 2 if (v == 2)
if (buffer[5] == 0)
{ {
Log::FormatLine("[SIMU] Simulation Resumed (Conn %i)", connection.id); DataManager::GetIntArray(DREF::Pause, value, 20);
for (int i = 0; i < 20; ++i)
{
value[i] = value[i] ? 0 : 1;
}
} }
else else
{ {
Log::FormatLine("[SIMU] Simulation Paused (Conn %i)", connection.id); for (int i = 0; i < 20; ++i)
{
value[i] = v;
}
}
// Set DREF
DataManager::Set(DREF::Pause, value, 20);
#if LOG_VERBOSITY > 2
switch (v)
{
case 0:
Log::WriteLine("[SIMU] Simulation Resumed");
break;
case 1:
Log::WriteLine("[SIMU] Simulation Paused");
break;
case 2:
Log::WriteLine("[SIMU] Simulation switched.");
break;
} }
#endif #endif
} }