Beispiel #1
0
    def distance(self, distance_unit=CM):
        """
        Measure distance to closest object.

        :param distance_unit: The units to measure the distance in. Optional, default Srf08.CM.
        :type distance_unit: Srf08.CM, Srf08.INCH or Srf08.MS.
        :return: The distance in specified units.
        :rtype: int
        :raise: IoTPy_ThingError
        """
        if distance_unit not in (Srf08.CM, Srf08.INCH, Srf08.MS):
            errmsg("Wrong units for distance, should be 'c' or 'i' or 'm'.")
            raise IoTPy_ThingError(
                "Wrong units for distance, should be 'c' or 'i' or 'm'.")
        try:
            self.interface.transaction(self.address, Srf08.CMD + distance_unit,
                                       0)
            sleep(0.08)
            distance = unpack(
                '>H',
                self.interface.transaction(self.address, Srf08.RESULT,
                                           2)[:2])[0]
        except IoTPy_ThingError:
            raise IoTPy_ThingError("srf08 - distance reading error.")
        return distance
Beispiel #2
0
    def _read_raw(self, command, regaddr, regcount):
        self.interface.transaction(self.address, '\0', 0)
        self.interface.transaction(self.address,
                                   command + regaddr + chr(regcount), 0)
        sleep(0.002)
        buf, err = self.interface.transaction(self.address, '', regcount + 4)

        if err:
            raise IoTPy_ThingError("AM2321 reading error.")

        crc = unpack('<H', buf[-2:])[0]
        if crc != self._am_crc16(buf[:-2]):
            raise IoTPy_ThingError("AM2321 CRC error.")

        return buf[2:-2]
Beispiel #3
0
 def distance(self, distance_unit=CM):
     if distance_unit not in (Srf08.CM, Srf08.INCH, Srf08.MS):
         errmsg("Wrong units for distance, should be 'c' or 'i' or 'm'.")
         raise IoTPy_ThingError(
             "Wrong units for distance, should be 'c' or 'i' or 'm'.")
     try:
         self.interface.transaction(self.address, Srf08.CMD + distance_unit,
                                    0)
         sleep(0.08)
         distance = unpack(
             '>H',
             self.interface.transaction(self.address, Srf08.RESULT,
                                        2)[:2])[0]
     except IoTPy_ThingError:
         raise IoTPy_ThingError("srf08 - distance reading error.")
     return distance
Beispiel #4
0
    def transaction(self, address, write_data, read_length, ignore_error=False):
        """
        Perform I2C data transaction.

        I2C data transaction consists of (optional) write transaction, followed by (optional) read transaction.

        :param address: I2C device address.
        :type address: int
        :param write_data: A byte sequence to be transmitted. If write_data is None, no write transaction will be executed.
        :type write_data: str
        :param read_length: A number of bytes to be received. If read_length is 0, no read trasaction will be executed.
        :type read_length: int
        :param ignore_error:
        :type ignore_error: bool
        :return: Received data or I2C communication error code.
        :rtype: str or int
        :raise: IoTPy_APIError, IoTPy_ThingError
        """
        try:
            result = self.board.decode_sfp(self.board.uper_io(1, self.board.encode_sfp(41, [address, write_data, read_length])))
        except IoTPy_APIError:
            errmsg("UPER API: I2C bus not connected.")
            raise IoTPy_IOError("I2C bus not connected.")
        if type(result[1][0]) == IntType and not ignore_error:
            errmsg("UPER Interface: I2C device with address %#x returned error code %#x.", address, result[1][0])
            raise IoTPy_ThingError("I2C slave reading error.")
        else:
            return result[1][0]
Beispiel #5
0
 def humidity(self):
     try:
         self.interface.transaction(Si7020.ADDRESS, Si7020.RH_HOLD, 0)
         result_raw = self.interface.transaction(Si7020.ADDRESS, '', 2)
         result_integer = unpack('>H', result_raw[:2])[0]
         rh = (125.0 * result_integer) / 65536 - 6
     except IoTPy_ThingError:
         raise IoTPy_ThingError("Si7020 - humidity reading error.")
     return rh
Beispiel #6
0
 def temperature(self):
     try:
         self.interface.transaction(Si7020.ADDRESS, Si7020.TEMP_HOLD, 0)
         result_raw = self.interface.transaction(Si7020.ADDRESS, '', 2)
         result_integer = unpack('>H', result_raw[:2])[0]
         temp = (175.72 * result_integer) / 65536 - 46.85
     except IoTPy_ThingError:
         raise IoTPy_ThingError("Si7020 - temperature reading error.")
     return temp
Beispiel #7
0
 def transaction(self, address, write_data, read_length, ignore_error=False):
     try:
         result = self.board.decode_sfp(self.board.uper_io(1, self.board.encode_sfp(41, [address, write_data, read_length])))
     except IoTPy_APIError:
         errmsg("UPER API: I2C bus not connected.")
         raise IoTPy_IOError("I2C bus not connected.")
     if type(result[1][0]) == IntType and not ignore_error:
         errmsg("UPER Interface: I2C device with address %#x returned error code %#x.", address, result[1][0] )
         raise IoTPy_ThingError("I2C slave reading error.")
     else:
         return result[1][0]
Beispiel #8
0
 def light(self):
     try:
         self.interface.transaction(self.address, Srf08.CMD + Srf08.CM, 0)
         sleep(0.08)
         light = unpack(
             '>B',
             self.interface.transaction(self.address, Srf08.LIGHT,
                                        1)[:1])[0]
     except IoTPy_ThingError:
         raise IoTPy_ThingError("srf08 - distance reading error.")
     return light
Beispiel #9
0
    def temperature(self):
        """
        Read and return temperature.

        :return: Temperature in celsius.
        :rtype: int
        :raise: IoTPy_ThingError
        """
        try:
            result_raw = self.interface.transaction(self.address, '\x00', 2)
            result_integer = unpack('>H', result_raw)[0]
            temperature = result_integer / 256.0
        except IoTPy_ThingError:
            raise IoTPy_ThingError("LM75 - temperature reading error.")
        return temperature
Beispiel #10
0
    def analog(self):
        """
        Read, convert and return analog sensor value.

        :return: Analog ADC value
        :rtype: int
        :raise: IoTPy_ThingError
        """
        try:
            result_raw = self.interface.transaction(self.address,
                                                    SI70xx_CMD_ANALOG, 2)
            result_integer = unpack('>H', result_raw[:2])[0]
            return result_integer
        except IoTPy_ThingError:
            raise IoTPy_ThingError("Si7020 - analog reading error.")
Beispiel #11
0
    def humidity(self):
        """
        Read, convert and return humidity.

        :return: Humidity value in percent.
        :rtype: float
        :raise: IoTPy_ThingError
        """
        try:
            result_raw = self.interface.transaction(self.address,
                                                    SI70xx_CMD_RH_HOLD, 2)
            result_integer = unpack('>H', result_raw[:2])[0]
            rh = (125.0 * result_integer) / 65536 - 6
        except IoTPy_ThingError:
            raise IoTPy_ThingError("Si7020 - humidity reading error.")
        return rh
Beispiel #12
0
    def temperature(self):
        """
        Read, convert and return temperature.

        :return: Temperature value in celsius.
        :rtype: float
        :raise: IoTPy_ThingError
        """
        try:
            result_raw = self.interface.transaction(self.address,
                                                    SI70xx_CMD_TEMP_HOLD, 2)
            result_integer = unpack('>H', result_raw[:2])[0]
            temp = (175.72 * result_integer) / 65536 - 46.85
        except IoTPy_ThingError:
            raise IoTPy_ThingError("Si7020 - temperature reading error.")
        return temp
Beispiel #13
0
    def light(self):
        """
        Measure light intensity.

        :return: Light intensity in relative units.
        :rtype: int
        :raise: IoTPy_ThingError
        """
        try:
            self.interface.transaction(self.address, Srf08.CMD + Srf08.CM, 0)
            sleep(0.08)
            light = unpack(
                '>B',
                self.interface.transaction(self.address, Srf08.LIGHT,
                                           1)[:1])[0]
        except IoTPy_ThingError:
            raise IoTPy_ThingError("srf08 - distance reading error.")
        return light