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.

66 lines
1.8 KiB

  1. // -----------------------------------------------------------------------------
  2. // Abstract I2C sensor class (other sensor classes extend this class)
  3. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && ( I2C_SUPPORT || EMON_ANALOG_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. // Descriptive name of the slot # index
  19. String slot(unsigned char index) {
  20. return description();
  21. };
  22. protected:
  23. // Specific for I2C sensors
  24. unsigned char _begin_i2c(unsigned char address, size_t size, unsigned char * addresses) {
  25. // Check if we should release a previously locked address
  26. if (_previous_address != address) {
  27. i2cReleaseLock(_previous_address);
  28. }
  29. // If we have already an address, check it is not locked
  30. if (address && !i2cGetLock(address)) {
  31. _error = SENSOR_ERROR_I2C;
  32. // If we don't have an address...
  33. } else {
  34. // Trigger auto-discover
  35. address = i2cFindAndLock(size, addresses);
  36. // If still nothing exit with error
  37. if (address == 0) _error = SENSOR_ERROR_I2C;
  38. }
  39. _previous_address = address;
  40. return address;
  41. }
  42. unsigned char _previous_address = 0;
  43. unsigned char _address = 0;
  44. };
  45. #endif // SENSOR_SUPPORT && I2C_SUPPORT