소스 검색

KBF-9 add ultrasonic test

Bence Balint 3 년 전
부모
커밋
64c21e0200
2개의 변경된 파일56개의 추가작업 그리고 0개의 파일을 삭제
  1. 1 0
      CMakeLists.txt
  2. 55 0
      test/esp-idf-lib/test_ultrasonic.cpp

+ 1 - 0
CMakeLists.txt

@@ -28,4 +28,5 @@ idf_component_register(
         "hd44780"
         "nvs_flash"
         "spiffs"
+        "ultrasonic"
 )

+ 55 - 0
test/esp-idf-lib/test_ultrasonic.cpp

@@ -0,0 +1,55 @@
+#include <cstdio>
+#include <freertos/FreeRTOS.h>
+#include <freertos/task.h>
+#include <ultrasonic.h>
+
+#include <unity.h>
+#include <kbf.h>
+
+#define MAX_DISTANCE_CM 200
+
+[[noreturn]] void ultrasonic_test(void *)
+{
+    ultrasonic_sensor_t sensor = {
+            .trigger_pin = GPIO_NUM_5,
+            .echo_pin = GPIO_NUM_18
+    };
+
+    ultrasonic_init(&sensor);
+
+    while (true)
+    {
+        uint32_t distance;
+        esp_err_t res = ultrasonic_measure_cm(&sensor, MAX_DISTANCE_CM, &distance);
+        if (res != ESP_OK)
+        {
+            printf("Error: ");
+            switch (res)
+            {
+                case ESP_ERR_ULTRASONIC_PING:
+                    printf("Cannot ping (device is in invalid state)\n");
+                    break;
+                case ESP_ERR_ULTRASONIC_PING_TIMEOUT:
+                    printf("Ping timeout (no device found)\n");
+                    break;
+                case ESP_ERR_ULTRASONIC_ECHO_TIMEOUT:
+                    printf("Echo timeout (i.e. distance too big)\n");
+                    break;
+                default:
+                    printf("%d\n", res);
+            }
+        }
+        else
+            printf("Distance: %d cm\n", distance);
+
+        vTaskDelay(500 / portTICK_PERIOD_MS);
+    }
+}
+
+[[noreturn]] TEST_CASE("ultrasonic", "[esp-idf-lib]") {
+    xTaskCreate(ultrasonic_test, "ultrasonic_test", configMINIMAL_STACK_SIZE * 3, nullptr, 5, nullptr);
+
+    while (true) {
+        kbf::sleep(1000);
+    }
+}