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.

126 lines
3.6 KiB

6 years ago
6 years ago
6 years ago
  1. // -----------------------------------------------------------------------------
  2. // HC-SR04, SRF05, SRF06, DYP-ME007, JSN-SR04T & Parallax PING)))™
  3. // Copyright (C) 2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // Enhancements by Rui Marinho
  5. // -----------------------------------------------------------------------------
  6. #if SENSOR_SUPPORT && SONAR_SUPPORT
  7. #pragma once
  8. #include <NewPing.h>
  9. #include "BaseSensor.h"
  10. class SonarSensor : public BaseSensor {
  11. public:
  12. // ---------------------------------------------------------------------
  13. // Public
  14. // ---------------------------------------------------------------------
  15. SonarSensor() {
  16. _count = 1;
  17. _sensor_id = SENSOR_SONAR_ID;
  18. }
  19. // ---------------------------------------------------------------------
  20. // Echo pin.
  21. void setEcho(unsigned char echo) {
  22. _echo = echo;
  23. }
  24. // Number of iterations to ping in order to filter out erroneous readings
  25. // using a digital filter.
  26. void setIterations(unsigned int iterations) {
  27. _iterations = iterations;
  28. }
  29. // Max sensor distance in centimeters.
  30. void setMaxDistance(unsigned int distance) {
  31. _max_distance = distance;
  32. }
  33. // Trigger pin.
  34. void setTrigger(unsigned char trigger) {
  35. _trigger = trigger;
  36. }
  37. // ---------------------------------------------------------------------
  38. unsigned char getEcho() {
  39. return _echo;
  40. }
  41. unsigned char getTrigger() {
  42. return _trigger;
  43. }
  44. unsigned int getMaxDistance() {
  45. return _max_distance;
  46. }
  47. unsigned int getIterations() {
  48. return _iterations;
  49. }
  50. // ---------------------------------------------------------------------
  51. // Sensor API
  52. // ---------------------------------------------------------------------
  53. // Initialization method, must be idempotent
  54. void begin() {
  55. _sonar = new NewPing(getTrigger(), getEcho(), getMaxDistance());
  56. _ready = true;
  57. }
  58. // Descriptive name of the sensor
  59. String description() {
  60. char buffer[23];
  61. snprintf(buffer, sizeof(buffer), "Sonar @ GPIO(%u, %u)", _trigger, _echo);
  62. return String(buffer);
  63. }
  64. // Descriptive name of the slot # index
  65. String description(unsigned char index) {
  66. return description();
  67. };
  68. // Address of the sensor (it could be the GPIO or I2C address)
  69. String address(unsigned char index) {
  70. return String(_trigger);
  71. }
  72. // Type for slot # index
  73. unsigned char type(unsigned char index) {
  74. if (index == 0) return MAGNITUDE_DISTANCE;
  75. return MAGNITUDE_NONE;
  76. }
  77. // Current value for slot # index
  78. double value(unsigned char index) {
  79. if (index != 0) return 0;
  80. if (getIterations() > 0) {
  81. return NewPing::convert_cm(_sonar->ping_median(getIterations())) / 100.0;
  82. }
  83. return _sonar->ping_cm() / 100.0;
  84. }
  85. protected:
  86. // ---------------------------------------------------------------------
  87. // Protected
  88. // ---------------------------------------------------------------------
  89. unsigned char _trigger;
  90. unsigned char _echo;
  91. unsigned int _max_distance;
  92. unsigned int _iterations;
  93. NewPing * _sonar = NULL;
  94. };
  95. #endif // SENSOR_SUPPORT && SONAR_SUPPORT