1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-05 06:50:20 +00:00
CLK/OSBindings/Qt/timer.cpp

45 lines
1.1 KiB
C++
Raw Normal View History

#include "timer.h"
#include "../../ClockReceiver/TimeTypes.hpp"
#include <algorithm>
#include <QDebug>
Timer::Timer(QObject *parent) : QObject(parent) {}
void Timer::startWithMachine(MachineTypes::TimedMachine *machine, std::mutex *machineMutex) {
this->machine = machine;
this->machineMutex = machineMutex;
thread.start();
thread.performAsync([this] {
// Set up the emulation timer. Bluffer's guide: the QTimer will post an
// event to an event loop. QThread is a thread with an event loop.
// My class, Timer, will be wired up to receive the QTimer's events.
2020-06-15 03:38:44 +00:00
timer = std::make_unique<QTimer>();
timer->setInterval(1);
connect(timer.get(), &QTimer::timeout, this, &Timer::tick, Qt::DirectConnection);
timer->start();
});
}
void Timer::tick() {
const auto now = Time::nanos_now();
const auto duration = std::min(now - lastTickNanos, int64_t(500'000'000));
lastTickNanos = now;
2020-06-06 03:06:28 +00:00
std::lock_guard lock_guard(*machineMutex);
machine->run_for(double(duration) / 1e9);
2022-07-12 15:03:58 +00:00
machine->flush_output(MachineTypes::TimedMachine::Output::All);
}
Timer::~Timer() {
thread.performAsync([this] {
if(timer) {
timer->stop();
}
});
2020-06-15 03:38:44 +00:00
thread.stop();
}