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