Return to Home Page

Heartbeat Request Observer

This is a PiToPico that implements a handshake protocol from the Pi to the Pico to see if the Pico is still alive. I suppose in theory the Pi could do a handshake with the Pico on some interval, and then (by some technique not implemented), try to reboot the pico if there’s no response. However, that would have the side-effect of rebooting the Pi, because the power to the Meanwell powersupply would be cycled.

Really, this observer is just here to end-to-end test the Observer and Writer logic on the Pico – the quickest possible round-trip through the DmaManager that can be triggered repeatedly from the Pico Comms Debug Window.

Code

The source code for this observer is pretty straightforward.

void HeatbeatRequestObserver::onNewPiToPicoPacket(messages::PiToPicoMessage message) {
    if (message.messageType == messages::PiToPicoMessage::MessageType::HeartbeatRequest) {
        logger->d(getTag(), "Got a heartbeat request from Pi.");
        heartbeatResponseWriter->sendHeartbeatResponse();
    }
}

Response Writer

The corresponding response writer is simple.

//
// Created by stefan on 4/5/23.
//

#include "HeartbeatResponseWriter.h"

namespace pico::ibus::output::writer {
                HeartbeatResponseWriter::HeartbeatResponseWriter(
                        std::shared_ptr<logger::BaseLogger> logger,
                        std::shared_ptr<dma::IDmaManager> dmaManager) {
                    this->logger = logger;
                    this->dmaManager = dmaManager;
                    if (this->dmaManager == nullptr) {
                        logger->wtf("HeartbeatResponseWriter", "dmaManager is null");
                    }
                }

                std::shared_ptr<dma::IDmaManager> HeartbeatResponseWriter::getDmaManager() {
                    return dmaManager;
                }

                void HeartbeatResponseWriter::sendHeartbeatResponse() {
                    logger->d(getTag(), "Sending heartbeat response");
                    schedulePicoToPiMessageForWrite(messages::PicoToPiMessage {
                        .messageType = messages::PicoToPiMessage::MessageType::HeartbeatResponse
                    });
                }
            } // writer
Return to Top