設計函數:(0,500ms) 返回 0,(500,1000ms) 返回 1,以此類推。
int getValue() {
static auto start = chrono::steady_clock::now();
auto now = chrono::steady_clock::now();
auto ms = chrono::duration_cast(now - start).count();
return static_cast(ms / 500) % 2; // 0,1,0,1... 或擴展更多值
}
int getValueAt(chrono::steady_clock::time_point tp, chrono::steady_clock::time_point base) {
auto ms = chrono::duration_cast(tp - base).count();
if (ms < 0) return -1;
return static_cast(ms / 500); // 0,1,2,3...
}
// Qt 示例
QTimer *timer = new QTimer(this);
int state = 0;
connect(timer, &QTimer::timeout, [&]{
state = (state + 1) % 2; // 或更複雜邏輯
emit valueChanged(state);
});
timer->start(500);