Changeset 32:22525b9438b5 in xplra for src/client


Ignore:
Timestamp:
02/09/13 10:28:16 (11 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
hg-Phase:
(<MercurialRepository 3 'hg:/home/ivaradi/xplane/hg/xplra' '/'>, 'public')
Message:

Implemented the setting of single datarefs

File:
1 edited

Legend:

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

    r31 r32  
    155155        """Get the value of the byte array dataref with the given name as a
    156156        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)
    158196
    159197    def disconnect(self):
     
    211249            return arr
    212250
     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
    213283    def _writeU8(self, x):
    214284        """Write the given value as an unsigned, 8-bit value."""
Note: See TracChangeset for help on using the changeset viewer.