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.

95 lines
3.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  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. #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. class I2CSensor : public BaseSensor {
  26. public:
  27. void setAddress(unsigned char address) {
  28. if (_address == address) return;
  29. _address = address;
  30. _dirty = true;
  31. }
  32. unsigned char getAddress() {
  33. return _address;
  34. }
  35. // Descriptive name of the slot # index
  36. String slot(unsigned char index) {
  37. return description();
  38. };
  39. // Address of the sensor (it could be the GPIO or I2C address)
  40. String address(unsigned char index) {
  41. char buffer[5];
  42. snprintf(buffer, sizeof(buffer), "0x%02X", _address);
  43. return String(buffer);
  44. }
  45. protected:
  46. // Specific for I2C sensors
  47. unsigned char _begin_i2c(unsigned char address, size_t size, unsigned char * addresses) {
  48. // If we have already locked this address for this sensor quit
  49. if ((address > 0) && (address == _previous_address)) {
  50. return _previous_address;
  51. }
  52. // Check if we should release a previously locked address
  53. if ((_previous_address > 0) && (_previous_address != address)) {
  54. i2cReleaseLock(_previous_address);
  55. _previous_address = 0;
  56. }
  57. // If requesting a specific address, try to ger a lock to it
  58. if ((0 < address) && i2cGetLock(address)) {
  59. _previous_address = address;
  60. return _previous_address;
  61. }
  62. // If everything else fails, perform an auto-discover
  63. _previous_address = i2cFindAndLock(size, addresses);
  64. // Flag error
  65. if (0 == _previous_address) {
  66. _error = SENSOR_ERROR_I2C;
  67. }
  68. return _previous_address;
  69. }
  70. unsigned char _previous_address = 0;
  71. unsigned char _address = 0;
  72. };
  73. #endif // SENSOR_SUPPORT && I2C_SUPPORT