From 3fa016d0c6a2d10381f00e2f866a3278c472c362 Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Wed, 6 Mar 2024 09:08:04 -0800 Subject: [PATCH] add push() to ChannelHistory structs --- examples_linux/ncurses/scanner_curses.cpp | 34 ++++++++++++----------- examples_linux/scanner.py | 15 ++++++---- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/examples_linux/ncurses/scanner_curses.cpp b/examples_linux/ncurses/scanner_curses.cpp index 54fc9cd9..809b60eb 100644 --- a/examples_linux/ncurses/scanner_curses.cpp +++ b/examples_linux/ncurses/scanner_curses.cpp @@ -46,6 +46,23 @@ struct ChannelHistory { unsigned int total = 0; // the summary of signal counts for the channel bool history[CACHE_MAX]; // a cache of history for the channel + + /** + * Push new scan result for a channel into the history. + * This function also increments the total signal count accordingly. + * @returns The count of cached signals found (including pushed result) + */ + uint8_t push(bool value) + { + uint8_t sum = value; + total += value; + for (uint8_t i = 0; i < CACHE_MAX - 1; ++i) { + history[i] = history[i + 1]; + sum += history[i]; + } + history[CACHE_MAX - 1] = value; + return sum; + } }; ChannelHistory stored[MAX_CHANNELS]; @@ -149,7 +166,7 @@ int main(int argc, char** argv) bpsUnit); bool foundSignal = scanChannel(channel); - uint8_t cachedCount = historyPush(channel, foundSignal); + uint8_t cachedCount = stored[channel].push(foundSignal); // output the summary/snapshot for this channel if (stored[channel].total) { @@ -313,19 +330,4 @@ void initBars() } } -/** - * Push new scan result for a channel into the history. - * @returns The count of historic signals found (including pushed result) - */ -uint8_t historyPush(uint8_t channel, bool value) -{ - uint8_t sum = 0; - for (uint8_t i = 0; i < CACHE_MAX - 1; ++i) { - stored[channel].history[i] = stored[channel].history[i + 1]; - sum += stored[channel].history[i]; - } - stored[channel].history[CACHE_MAX - 1] = value; - return sum + value; -} - // vim:ai:cin:sts=2 sw=2 ft=cpp diff --git a/examples_linux/scanner.py b/examples_linux/scanner.py index 2399094a..dc8edfff 100644 --- a/examples_linux/scanner.py +++ b/examples_linux/scanner.py @@ -43,6 +43,14 @@ def __init__(self) -> None: #: for the total signal counts self.total: int = 0 + def push(self, value: bool) -> int: + """Push a scan result's value into history while returning the sum of cached + signals found. This function also increments the total signal count accordingly. + """ + self.history = self.history[1:] + [value] + self.total += value + return self.history.count(True) + #: An array of histories for each channel stored = [ChannelHistory() for _ in range(TOTAL_CHANNELS)] @@ -190,12 +198,9 @@ def main(): while time.monotonic() < end: std_scr.addstr(2, 0, timer_prompt.format(int(end - time.monotonic()))) val = scan_channel(channel) - stored[channel].history = stored[channel].history[1:] + [val] - stored[channel].total += val + cache_sum = stored[channel].push(val) if stored[channel].total: - bars[channel].update( - stored[channel].history.count(True), stored[channel].total - ) + bars[channel].update(cache_sum, stored[channel].total) std_scr.refresh() if channel + 1 == TOTAL_CHANNELS: channel = 0