新增 Ultrasonic 类和 TaskHelper 类,提供超声波测距功能及任务等待辅助方法

This commit is contained in:
chauyinn
2025-05-10 18:23:21 +08:00
parent 92e9fc4864
commit b9a9165c71
2 changed files with 108 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
#pragma once
#include "stm32h5xx_hal.h"
#include <cstdint>
#include <stdexcept>
class TaskHelper {
public:
template <typename Func>
static void WaitFor(Func func, int timeoutMilliseconds) {
auto start = HAL_GetTick();
while (!func()) {
uint32_t now = HAL_GetTick();
if ((now - start) > static_cast<uint32_t>(timeoutMilliseconds)) {
throw std::runtime_error("Operation timed out");
}
}
}
};