From 4d8854d1e6d4addbc040f3ec0d441a403fa33665 Mon Sep 17 00:00:00 2001 From: heck Date: Tue, 5 Nov 2024 15:44:08 +0100 Subject: [PATCH] main_adc.cc - use Cache and PeriodicTaskCT --- examples/main_adc.cc | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/examples/main_adc.cc b/examples/main_adc.cc index f394252..22f36db 100644 --- a/examples/main_adc.cc +++ b/examples/main_adc.cc @@ -1,12 +1,18 @@ #include "main_adc.hh" #include "utils.hh" -#include namespace Heck { namespace { // anonymous namespace for internal linkage ld::DaisySeed seed{}; - Observer pot1{}; - SWTimer scan_pots{}; + + Cache pot1{ []() { return seed.adc.Get(0); }, + [](Cache& cache) { seed.PrintLine("POT_1: %d", cache.read()); } }; + + void scan_pots(u32) { + pot1.update_and_notify_change(); + } + + PeriodicTaskCT task_scan_pots{}; void init() { @@ -20,21 +26,14 @@ namespace Heck { seed.adc.Init(adc_cfg, 1); seed.adc.Start(); } - - scan_pots.set_period(10); - scan_pots.set_callback([](u32) { - pot1.on_change_fuzzy(seed.adc.Get(0), 20, [](int val) { - seed.PrintLine("POT_1: %d", val); - }); - }); } void mainloop() { - u32&& time_boot_ms{}; + u32 uptime{}; while (true) { - time_boot_ms = ld::System::GetNow(); - scan_pots.is_it_already_time_again(time_boot_ms); + uptime = ld::System::GetNow(); + task_scan_pots.run_pending(uptime); } } } // namespace