Explorar o código

KBF-11 implement Ultrasonic support

Bence Balint %!s(int64=3) %!d(string=hai) anos
pai
achega
25598a91ef

+ 1 - 0
CMakeLists.txt

@@ -5,6 +5,7 @@ idf_component_register(
         "src/kbf.cpp"
         "src/adc.cpp"
         "src/driver/lcd.cpp"
+        "src/driver/ultrasonic.cpp"
         "src/gpio.cpp"
         "src/http/client.cpp"
         "src/http/common.cpp"

+ 49 - 0
include/kbf/driver/ultrasonic.h

@@ -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

+ 10 - 0
src/driver/ultrasonic.cpp

@@ -0,0 +1,10 @@
+#include "kbf/driver/ultrasonic.h"
+
+uint32_t kbf::driver::Ultrasonic::read(int maxDistanceCm) {
+    uint32_t  distance;
+    esp_err_t err = ultrasonic_measure_cm(&sensor, maxDistanceCm, &distance);
+    if (err != ESP_OK) {
+        return err;
+    }
+    return distance;
+}

+ 1 - 0
test/CMakeLists.txt

@@ -1,6 +1,7 @@
 idf_component_register(
         SRC_DIRS
         "."
+        "demo"
         "driver"
         "esp-idf-lib"
         "rtos"

+ 35 - 0
test/demo/test_ultrasonic_lcd.cpp

@@ -0,0 +1,35 @@
+#include <kbf.h>
+#include <kbf/driver/lcd.h>
+#include <kbf/driver/ultrasonic.h>
+
+#include <unity.h>
+
+using namespace kbf;
+
+void clearOutput(driver::LCD &lcd) {
+    lcd.move(0, 1);
+    lcd << "                ";
+    lcd.move(0, 1);
+}
+
+[[noreturn]] TEST_CASE("ultrasonic on LCD", "[kbf_demo]") {
+    auto ultrasonic = driver::Ultrasonic(5, 18);
+    auto lcd        = driver::LCD(32, 33, 25, 26, 27, 13);
+
+    lcd << "distance: ";
+
+    int lastDistance = 0x200;
+    while (true) {
+        int distance = ultrasonic.read();
+        if (distance != lastDistance) {
+            clearOutput(lcd);
+            if (distance >= 0x200) {
+                lcd << "idk lol";
+            } else {
+                lcd << distance << "cm";
+            }
+            lastDistance = distance;
+        }
+        kbf::sleep(300);
+    }
+}

+ 20 - 0
test/driver/test_ultrasonic.cpp

@@ -0,0 +1,20 @@
+#include "kbf.h"
+#include "kbf/driver/ultrasonic.h"
+
+#include <iostream>
+
+#include <unity.h>
+
+using namespace kbf;
+using std::cout;
+using std::endl;
+
+[[noreturn]] TEST_CASE("ultrasonic", "[kbf_ultrasonic]") {
+    auto ultrasonic = driver::Ultrasonic(5, 18);
+
+    while (true) {
+        cout << "distance: " << ultrasonic.read() << "cm" << endl;
+        kbf::sleep(30);
+    }
+}
+