Polling: Event Driven Programming#

To Be Done#

jjj doit

see tests/test-eventloop.cpp

#include <why-irq.h>
#include <why-messagequeue.h>
#include <why-eventloop.h>
#include <why-time.h>
#include <why-sensor.h>
#include <print>
#include <list>
#include <cstdlib>

using sensor_data = std::pair<uint64_t/*timestamp*/, uint64_t/*value*/>;
auto the_queue = Why::MessageQueue<sensor_data>::create();

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

    Why::RandomSensor sensor(0, 100);
    auto isr = [&sensor](int irqnum){
        auto written = the_queue->put_noblock({Why::now_monotonic(), sensor.get_value()});
        if (! written) {
            if (written.error().sys_errno() == EAGAIN)
                std::println(stderr, "OIDA! ZAH AUN!!");
            else {
                std::println(stderr, "OS Error: {}", written.error().msg());
                exit(1);
            }
        }
    };

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

    Why::Eventloop loop;
    loop.watch(*the_queue, [](){
        auto sample = the_queue->get();
        auto [timestamp, value] = *sample;
        std::println("timestamp={}, value={}", timestamp, value);
    });

    while (true)
        loop.one();
    return 0;
}