Add command REALTIME?

This commit is contained in:
Joris van Rantwijk 2026-02-12 20:37:26 +01:00
parent 9687b65b6f
commit 4eb6ada765
1 changed files with 34 additions and 0 deletions

View File

@ -12,6 +12,7 @@
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include <algorithm>
#include <chrono>
#include <fstream>
@ -776,6 +777,38 @@ private:
return std::to_string(timestamp);
}
/** Handle command REALTIME? */
std::string qry_realtime(CommandEnvironment env)
{
struct timespec tp1, tp2, tp3;
uint64_t ts1, ts2, ts3, ts4;
// Read approximately simultaneous real time and timestamp.
ts1 = m_device.get_timestamp();
clock_gettime(CLOCK_REALTIME, &tp1);
ts2 = m_device.get_timestamp();
clock_gettime(CLOCK_REALTIME, &tp2);
ts3 = m_device.get_timestamp();
clock_gettime(CLOCK_REALTIME, &tp3);
ts4 = m_device.get_timestamp();
// Choose shortest interval out of three.
const uint64_t timestamp_mask = 0xffffffffffff;
uint64_t d1 = (ts2 - ts1) & timestamp_mask;
uint64_t d2 = (ts3 - ts2) & timestamp_mask;
uint64_t d3 = (ts4 - ts3) & timestamp_mask;
if (d3 < d1 && d3 < d2) {
ts1 = ts3;
tp1 = tp3;
} else if (d2 < d1) {
ts1 = ts2;
tp1 = tp2;
}
return str_format("%lld.%09ld %llu",
(long long)tp1.tv_sec, (long)tp1.tv_nsec, ts1);
}
/** Handle command AIN:CHANNELS:COUNT? */
std::string qry_channels_count(CommandEnvironment env)
{
@ -1424,6 +1457,7 @@ private:
command_table_no_args = {
{ "*idn?", &CommandHandler::qry_idn },
{ "timestamp?", &CommandHandler::qry_timestamp },
{ "realtime?", &CommandHandler::qry_realtime },
{ "ain:channels:count?", &CommandHandler::qry_channels_count },
{ "ain:channels:active?", &CommandHandler::qry_channels_active },
{ "ain:chN:range?", &CommandHandler::qry_channel_range },