Added playback example to Python client

This commit is contained in:
Jason Watkins
2015-07-30 13:42:07 -07:00
parent b4d1a8a980
commit 02cd59dbae
3 changed files with 82 additions and 2 deletions

View File

@@ -0,0 +1,77 @@
from time import sleep
import xpc
def record(path, interval = 0.1, duration = 60):
try:
fd = open(path, "w")
except:
print "Unable to open file."
return
count = duration / interval
if count < 1:
print "duration is less than a single frame."
return
with xpc.XPlaneConnect() as client:
print "Recording..."
for i in range(0, count):
try:
posi = client.getPOSI()
fd.write("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}\n".format(posi))
except:
continue
sleep(interval);
print "Recording Complete"
fd.close()
def playback(path, interval):
try:
fd = open(path, "r")
except:
print "Unable to open file."
return
with xpc.XPlaneConnect() as client:
print "Starting Playback..."
for line in fd:
try:
posi = [ float(x) for x in line.split(',') ]
posi = client.sendPOSI(posi)
except:
continue
sleep(interval);
print "Playback Complete"
fd.close()
def printMenu(title, opts):
print "\n+---------------------------------------------- +\n"
print "| {0:-42} |\n".format(title)
print "+---------------------------------------------- +\n"
for i in range(0,len(opts)):
print "| {0:2}. {1:-40} |\n".format(i + 1, opts[i])
print "+---------------------------------------------- +\n"
return int(raw_input("Please select and option: "))
def ex():
print "X-Plane Connect Playback Example [Version 1.2.0]\n"
print "(c) 2013-2015 United States Government as represented by the Administrator\n"
print "of the National Aeronautics and Space Administration. All Rights Reserved.\n"
mainOpts = [ "Record X-Plane", "Playback File", "Exit" ]
while True:
opt = printMenu("What would you like to do?", mainOpts)
if opt == 1:
path = raw_input("Enter save file path: ")
interval = int(raw_input("Enter interval between frames (milliseconds): "))
duration = int(raw_input("Enter duration to record for (seconds): "))
record(path, interval, duration)
elif opt == 2:
path = raw_input("Enter save file path: ")
interval = int(raw_input("Enter interval between frames (milliseconds): "))
playback(path, interval)
elif opt == 3:
return;
else:
print "Unrecognized option."

View File

@@ -5,7 +5,7 @@
<SchemaVersion>2.0</SchemaVersion> <SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>3c7a940d-17c8-4e91-882f-9bc8b1d2f54b</ProjectGuid> <ProjectGuid>3c7a940d-17c8-4e91-882f-9bc8b1d2f54b</ProjectGuid>
<ProjectHome>.</ProjectHome> <ProjectHome>.</ProjectHome>
<StartupFile>src\example.py</StartupFile> <StartupFile>src\basicExample.py</StartupFile>
<SearchPath> <SearchPath>
</SearchPath> </SearchPath>
<WorkingDirectory>.</WorkingDirectory> <WorkingDirectory>.</WorkingDirectory>
@@ -24,8 +24,11 @@
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging> <EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="src\playbackExample.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="src\xpc.py" /> <Compile Include="src\xpc.py" />
<Compile Include="src\example.py"> <Compile Include="src\basicExample.py">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
</ItemGroup> </ItemGroup>