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.

79 lines
2.3 KiB

  1. // -----------------------------------------------------------------------------
  2. // SI1145 Sensor over I2C
  3. // Copyright (C) 2020 by @HilverinkJ (https://github.com/HilverinkJ)
  4. // Based on https://github.com/xoseperez/espurna/issues/2192#issuecomment-603430308
  5. // -----------------------------------------------------------------------------
  6. #if SENSOR_SUPPORT && SI1145_SUPPORT
  7. #pragma once
  8. #include <Adafruit_SI1145.h>
  9. #include "I2CSensor.h"
  10. class SI1145Sensor : public I2CSensor<> {
  11. public:
  12. SI1145Sensor() {
  13. _count = 1;
  14. _sensor_id = SENSOR_SI1145_ID;
  15. _si1145 = new Adafruit_SI1145();
  16. }
  17. void begin() {
  18. static unsigned char addresses[1] = { SI1145_ADDRESS };
  19. _address = _begin_i2c(_address, sizeof(addresses), addresses);
  20. if (_address == 0) return;
  21. if (!_si1145->begin()) {
  22. _ready = false;
  23. return;
  24. }
  25. // Adafruit library never sets any errors
  26. _error = SENSOR_ERROR_OK;
  27. _ready = true;
  28. }
  29. // ---------------------------------------------------------------------
  30. // Sensor API
  31. // ---------------------------------------------------------------------
  32. // Descriptive name of the sensor
  33. String description() {
  34. char buffer[25];
  35. snprintf(buffer, sizeof(buffer), "SI1145 @ I2C (0x%02X)", _address);
  36. return String(buffer);
  37. }
  38. // Descriptive name of the slot # index
  39. String description(unsigned char index) {
  40. return description();
  41. };
  42. // Type for slot # index
  43. unsigned char type(unsigned char index) {
  44. if (index == 0) return MAGNITUDE_UVI;
  45. return MAGNITUDE_NONE;
  46. }
  47. // Pre-read hook (usually to populate registers with up-to-date data)
  48. void pre() {
  49. _uvi = _si1145->readUV() / 100.0;
  50. }
  51. // Current value for slot # index
  52. double value(unsigned char index) {
  53. if (index == 0) return _uvi;
  54. return 0.0;
  55. }
  56. protected:
  57. Adafruit_SI1145 * _si1145 = nullptr;
  58. double _uvi = 0.0;
  59. };
  60. #endif // SENSOR_SUPPORT && SI1145_SUPPORT