Skip to content

Commit

Permalink
add push() to ChannelHistory structs
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Mar 6, 2024
1 parent 795d0fb commit 7ac0537
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
34 changes: 18 additions & 16 deletions examples_linux/ncurses/scanner_curses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
15 changes: 10 additions & 5 deletions examples_linux/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7ac0537

Please sign in to comment.