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.

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