Changeset 32:22525b9438b5 in xplra
- Timestamp:
- 02/09/13 10:28:16 (12 years ago)
- Branch:
- default
- Phase:
- public
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
.hgignore
r11 r32 11 11 ltmain.sh 12 12 m4/.*\.m4 13 .*\.pyc -
src/client/python/xplra.py
r31 r32 155 155 """Get the value of the byte array dataref with the given name as a 156 156 string.""" 157 return self.getByteArray(name, offset = offset).tostring() 157 s = "" 158 for b in self.getByteArray(name, offset = offset): 159 if b==0: break 160 s += chr(b) 161 return s 162 163 def setInt(self, name, value): 164 """Set the value of the integer dataref with the given name.""" 165 self._setSingle(name, TYPE_INT, value) 166 167 def setFloat(self, name, value): 168 """Set the value of the float dataref with the given name.""" 169 self._setSingle(name, TYPE_FLOAT, value) 170 171 def setDouble(self, name, value): 172 """Set the value of the double dataref with the given name.""" 173 self._setSingle(name, TYPE_DOUBLE, value) 174 175 def setFloatArray(self, name, value, offset = 0): 176 """Set the value of the float array dataref with the given name.""" 177 self._setSingle(name, TYPE_FLOAT_ARRAY, value, offset = offset) 178 179 def setIntArray(self, name, value, offset = 0): 180 """Set the value of the integer array dataref with the given name.""" 181 self._setSingle(name, TYPE_INT_ARRAY, value, offset = offset) 182 183 def setByteArray(self, name, value, offset = 0): 184 """Set the value of the byte array dataref with the given name.""" 185 self._setSingle(name, TYPE_BYTE_ARRAY, value, offset = offset) 186 187 def setString(self, name, value, length, offset = 0): 188 """Set the value of the byte array dataref with the given name from a 189 string. 190 191 The string will be padded with 0's so that its length is the given 192 one.""" 193 value = [ord(c) for c in value[:length]] 194 value += [0] * (length - len(value)) 195 self.setByteArray(name, value, offset) 158 196 159 197 def disconnect(self): … … 211 249 return arr 212 250 251 def _setSingle(self, name, type, value, offset = None): 252 """Set the single value of the given name and type.""" 253 self._writeU8(COMMAND_SET_SINGLE) 254 self._writeString(name) 255 self._writeU8(type) 256 if offset is not None: 257 self._writeS32(len(value)) 258 self._writeS32(offset) 259 260 self._writeValue(type, value) 261 self._flush() 262 self._checkResult() 263 264 def _writeValue(self, type, value): 265 """Write a value of the given type.""" 266 if type==TYPE_INT: 267 self._writeS32(int(value)) 268 elif type==TYPE_FLOAT: 269 self._writeFloat(float(value)) 270 elif type==TYPE_DOUBLE: 271 self._writeDouble(float(value)) 272 else: 273 arr = None 274 if type==TYPE_FLOAT_ARRAY: 275 arr = array.array("f") 276 elif type==TYPE_INT_ARRAY: 277 arr = array.array("i") 278 elif type==TYPE_BYTE_ARRAY: 279 arr = array.array("B") 280 arr.fromlist(value) 281 self._stream.write(arr.tostring()) 282 213 283 def _writeU8(self, x): 214 284 """Write the given value as an unsigned, 8-bit value.""" -
test/basictest.py
r31 r32 51 51 print "Querying the aircraft's description as a string, with an offset of 3..." 52 52 result2 = xplane.getString("sim/aircraft/view/acf_descrip", offset = 3); 53 print "Got: '" + result2 + "'" 53 print "Got: '" + result2 + "' (" + `len(result2)` + ")" 54 print result2=="h 8 Q400" 54 55 print 55 56 … … 64 65 print 65 66 67 print "Setting the number of the engines to", numEngines + 1 68 xplane.setInt("sim/aircraft/engine/acf_num_engines", numEngines + 1) 69 numEngines = xplane.getInt("sim/aircraft/engine/acf_num_engines") 70 print "The new number of engines:", numEngines 71 print 72 73 acfElevUp = xplane.getFloat("sim/aircraft/controls/acf_elev_up") 74 print "Setting the aircraft elevator up control from %f to %f..." % \ 75 (acfElevUp, acfElevUp + 15.0) 76 xplane.setFloat("sim/aircraft/controls/acf_elev_up", acfElevUp + 15.0) 77 acfElevUp = xplane.getFloat("sim/aircraft/controls/acf_elev_up") 78 print "The aircraft elevator up control set to", acfElevUp 79 print 80 81 localX = xplane.getDouble("sim/flightmodel/position/local_x") 82 print "Setting the aircraft's local X-coordinate from %f to %f..." % \ 83 (localX, localX + 15.0) 84 xplane.setDouble("sim/flightmodel/position/local_x", localX + 15.0) 85 localX = xplane.getDouble("sim/flightmodel/position/local_x") 86 print "The aircraft's local X-coordinate set to", localX 87 print 88 89 numBlades = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades").tolist() 90 print "Setting the number of blades" 91 print " from:", numBlades 92 numBlades = [n+2.5 for n in numBlades] 93 print " to:", numBlades 94 xplane.setFloatArray("sim/aircraft/prop/acf_num_blades", numBlades) 95 96 numBlades = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades").tolist() 97 print "The result:", numBlades 98 print 99 100 batteryArrayOn = xplane.getIntArray("sim/cockpit/electrical/battery_array_on").tolist() 101 print "Setting the batteries" 102 print " from:", batteryArrayOn 103 batteryArrayOn = [1 if b==0 else 0 for b in batteryArrayOn] 104 print " to:", batteryArrayOn 105 xplane.setIntArray("sim/cockpit/electrical/battery_array_on", batteryArrayOn) 106 batteryArrayOn = xplane.getIntArray("sim/cockpit/electrical/battery_array_on").tolist() 107 print "The result:", batteryArrayOn 108 print 109 110 tailNum = [0] * 40 111 tailNum[0] = ord('H') 112 tailNum[1] = ord('A') 113 tailNum[2] = ord('-') 114 tailNum[3] = ord('V') 115 tailNum[4] = ord('A') 116 tailNum[5] = ord('I') 117 118 print "Setting the tail number to %s as a byte array..." % (tailNum,) 119 xplane.setByteArray("sim/aircraft/view/acf_tailnum", tailNum) 120 print "The tail number is:", xplane.getString("sim/aircraft/view/acf_tailnum") 121 print 122 123 tailNum1 = "VAIS" 124 print "Setting the tail number to " + tailNum1 + " as a string..." 125 xplane.setString("sim/aircraft/view/acf_tailnum", tailNum1, 40) 126 print "The tail number is:", xplane.getString("sim/aircraft/view/acf_tailnum") 127 print 128 129 66 130 except Exception as e: 67 131 print ">>>>>>>>>>>>>>>>>>>>>> Exception caught:", str(e)
Note:
See TracChangeset
for help on using the changeset viewer.