#include <why-irq.h>
#include <why-spinlock.h>
#include <why-time.h>
#include <why-sensor.h>
#include <print>
#include <list>

using sensor_data = std::pair<uint64_t/*timestamp*/, uint64_t/*value*/>;
std::list<sensor_data> the_data;
Why::Spinlock the_lock;                                // <-- instantiate

int main()
{
    Why::init();

    Why::RandomSensor sensor(0, 100);
    auto isr = [&sensor](int irqnum){
        the_lock.lock();                               // <-- use
        the_data.emplace_back(
            Why::now_monotonic(), sensor.get_value());
        the_lock.unlock();                             // <-- use
    };

    Why::IRQ::connect(2, isr);

    while (true) {
        if (the_data.size()) {
            the_lock.lock();                           // <-- use
            auto [timestamp, value] = the_data.front();
            the_data.pop_front();
            the_lock.unlock();                         // <-- use
            std::println("timestamp={}, value={}", timestamp, value);
        }
        else
            /*power management?*/;
    }
    return 0;
}
