Changeset 35:9451e75788ea in xplra


Ignore:
Timestamp:
02/09/13 13:00:42 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Implemented support for multi-dataref setting

Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/client/python/xplra.py

    r34 r35  
    131131        """Create a new multi-dataref getter for this X-Plane object."""
    132132        return MultiGetter(self)
     133
     134    def createMultiSetter(self):
     135        """Create a new multi-dataref setter for this X-Plane object."""
     136        return MultiSetter(self)
    133137
    134138    def getInt(self, name):
     
    251255            if arr is not None and length>0:
    252256                arr.fromstring(self._stream.read(length*elementSize))
    253             return arr
     257            return None if arr is None else arr.tolist()
    254258
    255259    def _setSingle(self, name, type, value, offset = None):
     
    524528            self.finalize()
    525529        type = self._dataRefs[id][1]
     530        length = self._dataRefs[id][2]
    526531        if type==TYPE_INT:
    527532            self._values[id] = int(value)
    528         elif type==TYPE_FLOAT:
     533        elif type==TYPE_FLOAT or type==TYPE_DOUBLE:
    529534            self._values[id] = float(value)
    530         elif type==TYPE_DOUBLE:
    531             self._values[id] = double(value)
    532535        elif type==TYPE_FLOAT_ARRAY:
     536            if len(value)!=length:
     537                raise ValueError("xplra.MultiBuffer: expected a list of length %d" % (length,))
    533538            self._values[id] = [float(x) for x in value]
    534539        elif type==TYPE_INT_ARRAY:
     540            if len(value)!=length:
     541                raise ValueError("xplra.MultiBuffer: expected a list of length %d" % (length,))
    535542            self._values[id] = [int(x) for x in value]
    536543        elif type==TYPE_BYTE_ARRAY:
    537             if instanceof(value, str):
    538                 self._values[id] = [ord(x) for x in value]
     544            if isinstance(value, str):
     545                lst = [ord(x) for x in value[:length]]
     546                if len(lst)<length:
     547                    lst += [0] * (length-len(lst))
     548                self._values[id] = lst
    539549            else:
     550                if len(value)!=length:
     551                    raise ValueError("xplra.MultiBuffer: expected a list of length %d" % (length,))
    540552                self._values[id] = [int(x) for x in value]
    541553
     
    578590
    579591#-------------------------------------------------------------------------------
     592
     593class MultiSetter(MultiBuffer):
     594    """Multi-dataref buffer for setting."""
     595    def __init__(self, xplane):
     596        """Construct the getter."""
     597        super(MultiSetter, self).__init__(xplane,
     598                                          COMMAND_REGISTER_SET_MULTI,
     599                                          COMMAND_UNREGISTER_SET_MULTI)
     600
     601    def _executeRegistered(self):
     602        """Execute the query if the buffer is registered."""
     603        self._xplane._writeU8(COMMAND_EXECUTE_SET_MULTI)
     604        self._xplane._writeU32(self._registeredID)
     605        for i in range(0, len(self._dataRefs)):
     606            self._xplane._writeValue(self._dataRefs[i][1], self._values[i])
     607        self._xplane._flush()
     608
     609        self._xplane._checkResult()
     610
     611    def _executeUnregistered(self):
     612        """Execute the query if the buffer is not registered."""
     613        self._xplane._writeU8(COMMAND_SET_MULTI)
     614
     615        numDataRefs = len(self._dataRefs)
     616        self._xplane._writeU32(numDataRefs)
     617
     618        for i in range(0, numDataRefs):
     619            (name, type, length, offset) = self._dataRefs[i]
     620            value = self._values[i]
     621
     622            self._xplane._writeString(name)
     623            self._xplane._writeType(type)
     624            if length is not None and offset is not None:
     625                self._xplane._writeS32(len(value))
     626                self._xplane._writeS32(offset)
     627            self._xplane._writeValue(type, value)
     628
     629        self._xplane._flush()
     630        self._xplane._checkResult()
     631
     632#-------------------------------------------------------------------------------
  • test/basictest.py

    r32 r35  
    4646        print "Querying the aircraft's description..."
    4747        result = xplane.getByteArray("sim/aircraft/view/acf_descrip")
    48         print "The description:", result.tolist()
     48        print "The description:", result
    4949        print
    5050
     
    5757        print "Querying the aircraft's engine types..."
    5858        result3 = xplane.getIntArray("sim/aircraft/prop/acf_en_type")
    59         print "Got:", result3.tolist()
     59        print "Got:", result3
    6060        print
    6161
    6262        print "Querying the aircraft's propeller directions..."
    6363        result5 = xplane.getFloatArray("sim/aircraft/prop/acf_prop_dir")
    64         print  "Got:", result5.tolist()
     64        print  "Got:", result5
    6565        print
    6666
     
    8787        print
    8888
    89         numBlades = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades").tolist()
     89        numBlades = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades")
    9090        print "Setting the number of blades"
    9191        print "    from:", numBlades
     
    9494        xplane.setFloatArray("sim/aircraft/prop/acf_num_blades",  numBlades)
    9595
    96         numBlades = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades").tolist()
     96        numBlades = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades")
    9797        print "The result:", numBlades
    9898        print
    9999
    100         batteryArrayOn = xplane.getIntArray("sim/cockpit/electrical/battery_array_on").tolist()
     100        batteryArrayOn = xplane.getIntArray("sim/cockpit/electrical/battery_array_on")
    101101        print "Setting the batteries"
    102102        print "    from:", batteryArrayOn
     
    104104        print "    to:", batteryArrayOn
    105105        xplane.setIntArray("sim/cockpit/electrical/battery_array_on", batteryArrayOn)
    106         batteryArrayOn = xplane.getIntArray("sim/cockpit/electrical/battery_array_on").tolist()
     106        batteryArrayOn = xplane.getIntArray("sim/cockpit/electrical/battery_array_on")
    107107        print "The result:", batteryArrayOn
    108108        print
  • test/multigettest.py

    r34 r35  
    9696            print "altimeter: %f, qnh: %f" % \
    9797                (getter[altimeterID], getter[qnhID]);
    98             print "NAV types:", getter[navTypesID].tolist()
    99             print "bus voltages:", getter[busVoltsID].tolist()
     98            print "NAV types:", getter[navTypesID]
     99            print "bus voltages:", getter[busVoltsID]
    100100            print
    101101            time.sleep(1.0)
Note: See TracChangeset for help on using the changeset viewer.