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

This commit is contained in:
2025-05-10 18:23:21 +08:00
parent f6541a5fbd
commit f87404cba6
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");
}
}
}
};