|
@@ -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);
|
|
|
+ }
|
|
|
+}
|