sdp610Array.py 825 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import smbus
  2. import time
  3. class Spd610Array():
  4. i2cAddresses = (
  5. 0x21,
  6. 0x41,
  7. 0x61,
  8. 0x31,
  9. 0x39,
  10. 0x29,
  11. 0x35,
  12. 0x40
  13. )
  14. def __init__(self):
  15. self.bus = smbus.SMBus(0)
  16. def readValue(self, addr):
  17. try:
  18. self.bus.write_byte_data(addr, 0, 0xF1)
  19. block = self.bus.read_i2c_block_data(addr, 0, 3)
  20. value = (block[0] * 256 + block[1]) / 240.0
  21. crc = block[2]
  22. return value
  23. except OSError as e:
  24. print("ERROR: I2C SPD610 {:X}: ".format(addr), e)
  25. return None
  26. def getValues(self):
  27. values = [0] * len(self.i2cAddresses)
  28. for i in range(len(self.i2cAddresses)):
  29. values[i] = self.readValue(self.i2cAddresses[i])
  30. return values
  31. if __name__ == "__main__":
  32. sdp = Spd610Array()
  33. while True:
  34. print(sdp.getValues())
  35. time.sleep(1)