From f1f94deda88f928f0b3ffcbcfbb3c4c68977070d Mon Sep 17 00:00:00 2001 From: nick black Date: Fri, 3 Apr 2020 05:35:23 -0400 Subject: [PATCH] keyplot: input loop #430 --- src/input/keyplot.cpp | 22 ++++++++++++++++++++++ src/lib/plot.c | 9 ++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/input/keyplot.cpp b/src/input/keyplot.cpp index 90ad94847..e21e0cf2e 100644 --- a/src/input/keyplot.cpp +++ b/src/input/keyplot.cpp @@ -1,6 +1,13 @@ #include #include +#define NANOSECS_IN_SEC 1000000000 + +static inline uint64_t +timespec_to_ns(const struct timespec* t){ + return t->tv_sec * NANOSECS_IN_SEC + t->tv_nsec; +} + int main(void){ if(setlocale(LC_ALL, "") == nullptr){ return EXIT_FAILURE; @@ -28,10 +35,25 @@ int main(void){ if(!nc.render()){ return EXIT_FAILURE; } + struct timespec start; + if(clock_gettime(CLOCK_MONOTONIC, &start)){ + return EXIT_FAILURE; + } while(errno = 0, (r = nc.getc(true, &ni)) != (char32_t)-1){ if(r == 0){ // interrupted by signal continue; } + struct timespec now; + if(clock_gettime(CLOCK_MONOTONIC, &now)){ + return EXIT_FAILURE; + } + const uint64_t sec = (timespec_to_ns(&now) - timespec_to_ns(&start)) / NANOSECS_IN_SEC; + if(ncplot_add_sample(plot, sec, 1)){ + return EXIT_FAILURE; + } + if(!nc.render()){ + return EXIT_FAILURE; + } } ncplot_destroy(plot); return EXIT_SUCCESS; diff --git a/src/lib/plot.c b/src/lib/plot.c index a5eaeb32f..6280fb3b7 100644 --- a/src/lib/plot.c +++ b/src/lib/plot.c @@ -52,7 +52,14 @@ ncplane* ncplot_plane(ncplot* n){ static inline bool invalid_y(ncplot* n, int64_t y){ - if(y > n->maxy || y < n->miny){ + if(n->detectdomain){ + if(y > n->maxy){ + n->maxy = y; + } + if(y < n->miny){ + n->miny = y; + } + }else if(y > n->maxy || y < n->miny){ return true; } return false;