.. include:: Datalogger: A Semi-Realistic Project (Events Version) ===================================================== .. topic:: See also * :doc:`../polling/index` * :doc:`../timer/index` .. topic:: Trainer's note * Continue from :ref:`why-datalogger-snapshot-button` * Copy over :download:`datalogger-snapshot-button.cpp <../datalogger/code/datalogger-snapshot-button.cpp>` Polling-Thread To Consume Both Message Queues --------------------------------------------- * Two message queues: sensor data, snapshot requests * Collapse both threads into one * Rip out the two thread while-loop bodies * Create respective lambdas/callback for event notification .. important:: No need to protect CSV writing against snapshot rotation |longrightarrow| remove lock .. literalinclude:: code/datalogger-polling-queues.cpp :caption: :download:`code/datalogger-polling-queues.cpp` :language: c++ Watchdog Timer Into Polling-Thread (Semaphore Version) ------------------------------------------------------ * Currently, we do a (blocking) sleep in main loop * Plan: * Start a :doc:`timer <../timer/index>` * In the timer callback function - *in ISR context* -, give a semaphore * In event loop, watch semaphore for takeability * Feed watchdog in event callback .. literalinclude:: code/datalogger-polling-timer-sem.cpp :caption: :download:`code/datalogger-polling-timer-sem.cpp` :language: c++ Watchdog Timer Into Polling-Thread (Poll-Signal Version) -------------------------------------------------------- .. topic:: Documentation * :doc:`../why-os/pollsignal` * Semaphore is heavyweight: can be waited for which is extra logic * Poll signal is rather lightweight * Specific to event driven applications .. literalinclude:: code/datalogger-polling-pollsignal.cpp :caption: :download:`code/datalogger-polling-pollsignal.cpp` :language: c++ And Single-Threaded Programs? ----------------------------- * Main thread is useless now * All logic in the poller thread * Why not eliminate that thread altogether? .. literalinclude:: code/datalogger-polling-singlethreaded.cpp :caption: :download:`code/datalogger-polling-singlethreaded.cpp` :language: c++