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.

57 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. #pragma once
  6. #include "BaseSensor.h"
  7. class I2CSensor : public BaseSensor {
  8. public:
  9. void setAddress(unsigned char address) {
  10. if (_address == address) return;
  11. _address = address;
  12. _dirty = true;
  13. }
  14. unsigned char getAddress() {
  15. return _address;
  16. }
  17. protected:
  18. // Specific for I2C sensors
  19. unsigned char _begin_i2c(unsigned char address, size_t size, unsigned char * addresses) {
  20. // Check if we should release a previously locked address
  21. if (_previous_address != address) {
  22. i2cReleaseLock(_previous_address);
  23. }
  24. // If we have already an address, check it is not locked
  25. if (address && !i2cGetLock(address)) {
  26. _error = SENSOR_ERROR_I2C;
  27. // If we don't have an address...
  28. } else {
  29. // Trigger auto-discover
  30. address = i2cFindAndLock(size, addresses);
  31. // If still nothing exit with error
  32. if (address == 0) _error = SENSOR_ERROR_I2C;
  33. }
  34. _previous_address = address;
  35. return address;
  36. }
  37. unsigned char _previous_address = 0;
  38. unsigned char _address = 0;
  39. };