Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB

  1. // -----------------------------------------------------------------------------
  2. // Abstract I2C sensor class (other sensor classes extend this class)
  3. // Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && I2C_SUPPORT
  6. #pragma once
  7. #include "BaseSensor.h"
  8. class I2CSensor : public BaseSensor {
  9. public:
  10. void setAddress(unsigned char address) {
  11. if (_address == address) return;
  12. _address = address;
  13. _dirty = true;
  14. }
  15. unsigned char getAddress() {
  16. return _address;
  17. }
  18. protected:
  19. // Specific for I2C sensors
  20. unsigned char _begin_i2c(unsigned char address, size_t size, unsigned char * addresses) {
  21. // Check if we should release a previously locked address
  22. if (_previous_address != address) {
  23. i2cReleaseLock(_previous_address);
  24. }
  25. // If we have already an address, check it is not locked
  26. if (address && !i2cGetLock(address)) {
  27. _error = SENSOR_ERROR_I2C;
  28. // If we don't have an address...
  29. } else {
  30. // Trigger auto-discover
  31. address = i2cFindAndLock(size, addresses);
  32. // If still nothing exit with error
  33. if (address == 0) _error = SENSOR_ERROR_I2C;
  34. }
  35. _previous_address = address;
  36. return address;
  37. }
  38. unsigned char _previous_address = 0;
  39. unsigned char _address = 0;
  40. };
  41. #endif // SENSOR_SUPPORT && I2C_SUPPORT