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.

99 lines
4.0 KiB

  1. // -----------------------------------------------------------------------------
  2. // Abstract I2C sensor class (other sensor classes extend this class)
  3. // Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && ( I2C_SUPPORT || EMON_ANALOG_SUPPORT )
  6. #if I2C_USE_BRZO
  7. #define I2C_TRANS_SUCCESS 0 // All i2c commands were executed without errors
  8. #define I2C_TRANS_ERROR_BUS_NOT_FREE 1 // Bus not free, i.e. either SDA or SCL is low
  9. #define I2C_TRANS_ERROR_NACK_WRITE 2 // Not ACK ("NACK") by slave during write:
  10. // Either the slave did not respond to the given slave address; or the slave did not ACK a byte transferred by the master.
  11. #define I2C_TRANS_ERROR_NACK_READ 4 // Not ACK ("NACK") by slave during read,
  12. // i.e. slave did not respond to the given slave address
  13. #define I2C_TRANS_ERROR_CLOCK 8 // Clock Stretching by slave exceeded maximum clock stretching time. Most probably, there is a bus stall now!
  14. #define I2C_TRANS_ERROR_READ_NULL 16 // Read was called with 0 bytes to be read by the master. Command not sent to the slave, since this could yield to a bus stall
  15. #define I2C_TRANS_ERROR_TIMEOUT 32 // ACK Polling timeout exceeded
  16. #else // Wire
  17. #define I2C_TRANS_SUCCESS 0 // success
  18. #define I2C_TRANS_ERROR_BUFFER_OVERLOW 1 // data too long to fit in transmit buffer
  19. #define I2C_TRANS_ERROR_NACK_ADDRESS 2 // received NACK on transmit of address
  20. #define I2C_TRANS_ERROR_NACK_DATA 3 // received NACK on transmit of data
  21. #define I2C_TRANS_ERROR_OTHER 4 // other error
  22. #endif
  23. #pragma once
  24. #include "BaseSensor.h"
  25. // TODO: Must inherit from I2CSensor<>, not just I2CSensor. Even with default value :(
  26. // Perhaps I2CSensor should be alias for I2CSensorBase?
  27. template <typename T = BaseSensor>
  28. class I2CSensor : public T {
  29. public:
  30. void setAddress(unsigned char address) {
  31. if (_address == address) return;
  32. _address = address;
  33. T::_dirty = true;
  34. }
  35. unsigned char getAddress() {
  36. return _address;
  37. }
  38. // Descriptive name of the slot # index
  39. String slot(unsigned char index) {
  40. return static_cast<T*>(this)->description();
  41. };
  42. // Address of the sensor (it could be the GPIO or I2C address)
  43. String address(unsigned char index) {
  44. char buffer[5];
  45. snprintf(buffer, sizeof(buffer), "0x%02X", _address);
  46. return String(buffer);
  47. }
  48. protected:
  49. // Specific for I2C sensors
  50. unsigned char _begin_i2c(unsigned char address, size_t size, unsigned char * addresses) {
  51. // If we have already locked this address for this sensor quit
  52. if ((address > 0) && (address == _previous_address)) {
  53. return _previous_address;
  54. }
  55. // Check if we should release a previously locked address
  56. if ((_previous_address > 0) && (_previous_address != address)) {
  57. i2cReleaseLock(_previous_address);
  58. _previous_address = 0;
  59. }
  60. // If requesting a specific address, try to ger a lock to it
  61. if ((0 < address) && i2cGetLock(address)) {
  62. _previous_address = address;
  63. return _previous_address;
  64. }
  65. // If everything else fails, perform an auto-discover
  66. _previous_address = i2cFindAndLock(size, addresses);
  67. // Flag error
  68. if (0 == _previous_address) {
  69. T::_error = SENSOR_ERROR_I2C;
  70. }
  71. return _previous_address;
  72. }
  73. unsigned char _previous_address = 0;
  74. unsigned char _address = 0;
  75. };
  76. #endif // SENSOR_SUPPORT && I2C_SUPPORT