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.

119 lines
3.4 KiB

6 years ago
  1. // -----------------------------------------------------------------------------
  2. // HC-SR04 Ultrasonic sensor
  3. // Copyright (C) 2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && HCSR04_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "BaseSensor.h"
  9. class HCSR04Sensor : public BaseSensor {
  10. public:
  11. // ---------------------------------------------------------------------
  12. // Public
  13. // ---------------------------------------------------------------------
  14. HCSR04Sensor(): BaseSensor() {
  15. _count = 1;
  16. _sensor_id = SENSOR_HCSR04_ID;
  17. }
  18. // ---------------------------------------------------------------------
  19. void setEcho(unsigned char echo) {
  20. _echo = echo;
  21. }
  22. void setTrigger(unsigned char trigger) {
  23. _trigger = trigger;
  24. }
  25. // ---------------------------------------------------------------------
  26. unsigned char getEcho() {
  27. return _echo;
  28. }
  29. unsigned char getTrigger() {
  30. return _trigger;
  31. }
  32. // ---------------------------------------------------------------------
  33. // Sensor API
  34. // ---------------------------------------------------------------------
  35. // Initialization method, must be idempotent
  36. void begin() {
  37. pinMode(_echo, INPUT);
  38. pinMode(_trigger, OUTPUT);
  39. digitalWrite(_trigger, LOW);
  40. _ready = true;
  41. }
  42. // Descriptive name of the sensor
  43. String description() {
  44. char buffer[24];
  45. snprintf(buffer, sizeof(buffer), "HCSR04 @ GPIO(%u, %u)", _trigger, _echo);
  46. return String(buffer);
  47. }
  48. // Descriptive name of the slot # index
  49. String slot(unsigned char index) {
  50. return description();
  51. };
  52. // Address of the sensor (it could be the GPIO or I2C address)
  53. String address(unsigned char index) {
  54. return String(_trigger);
  55. }
  56. // Type for slot # index
  57. unsigned char type(unsigned char index) {
  58. if (index == 0) return MAGNITUDE_DISTANCE;
  59. return MAGNITUDE_NONE;
  60. }
  61. // Current value for slot # index
  62. double value(unsigned char index) {
  63. if (index == 0) {
  64. // Trigger pulse
  65. digitalWrite(_trigger, HIGH);
  66. delayMicroseconds(10);
  67. digitalWrite(_trigger, LOW);
  68. // Wait for echo pulse low-high-low
  69. while ( digitalRead(_echo) == 0 ) yield();
  70. unsigned long start = micros();
  71. while ( digitalRead(_echo) == 1 ) yield();
  72. unsigned long travel_time = micros() - start;
  73. // Assuming a speed of sound of 340m/s
  74. // Dividing by 2 since it is a round trip
  75. return 340.0 * (double) travel_time / 1000000.0 / 2;
  76. }
  77. return 0;
  78. }
  79. protected:
  80. // ---------------------------------------------------------------------
  81. // Protected
  82. // ---------------------------------------------------------------------
  83. unsigned char _trigger;
  84. unsigned char _echo;
  85. };
  86. #endif // SENSOR_SUPPORT && HCSR04_SUPPORT