|
@@ -0,0 +1,49 @@
|
|
|
+#ifndef KBF_ULTRASONIC_H
|
|
|
+#define KBF_ULTRASONIC_H
|
|
|
+
|
|
|
+#include <ultrasonic.h>
|
|
|
+
|
|
|
+namespace kbf::driver {
|
|
|
+ /**
|
|
|
+ * @brief Driver for ultrasonic sensors.
|
|
|
+ */
|
|
|
+ class Ultrasonic {
|
|
|
+ /** @brief Tag used for logging. */
|
|
|
+ static constexpr const char *TAG = "kbf::driver::Ultrasonic";
|
|
|
+
|
|
|
+ /** @brief Value returned by read() on error */
|
|
|
+ static const unsigned int ERROR = 0xffffffff;
|
|
|
+
|
|
|
+ public:
|
|
|
+ /**
|
|
|
+ * @brief Constructor.
|
|
|
+ *
|
|
|
+ * @param triggerPin GPIO pin connected to the trigger pin of the sensor
|
|
|
+ * @param echoPin GPIO pin connected to the echo pin of the sensor
|
|
|
+ */
|
|
|
+ template <typename T>
|
|
|
+ Ultrasonic(T triggerPin, T echoPin) : sensor{
|
|
|
+ .trigger_pin = static_cast<gpio_num_t>(triggerPin),
|
|
|
+ .echo_pin = static_cast<gpio_num_t>(echoPin),
|
|
|
+ } {
|
|
|
+ ultrasonic_init(&sensor);
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @brief Measures distance.
|
|
|
+ *
|
|
|
+ * @param maxDistanceCm maximum distance the sensor can handle, in centimeters; default is 200
|
|
|
+ * @return measured distance or
|
|
|
+ * - 0x200 ESP_ERR_ULTRASONIC_PING if previous ping hasn't ended yet
|
|
|
+ * - 0x201 ESP_ERR_ULTRASONIC_PING_TIMEOUT on ping timeout
|
|
|
+ * - 0x202 ESP_ERR_ULTRASONIC_ECHO_TIMEOUT on echo timeout
|
|
|
+ *
|
|
|
+ */
|
|
|
+ uint32_t read(int maxDistanceCm = 200);
|
|
|
+
|
|
|
+ private:
|
|
|
+ ultrasonic_sensor_t sensor;
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
+#endif //KBF_ULTRASONIC_H
|