Program Listing for File HsmEventDispatcherArduino.hpp

Return to documentation for file (include/hsmcpp/HsmEventDispatcherArduino.hpp)

// Copyright (C) 2022 Igor Krechetov
// Distributed under MIT license. See file LICENSE for details

#ifndef HSMCPP_HSMEVENTDISPATCHERARDUINO_HPP
#define HSMCPP_HSMEVENTDISPATCHERARDUINO_HPP

#include <map>
#include <vector>

#include "HsmEventDispatcherBase.hpp"

namespace hsmcpp {

class HsmEventDispatcherArduino : public HsmEventDispatcherBase {
private:
    struct RunningTimerInfo {
        unsigned long startedAt;    // monotonic time when timer was started (ms)
        unsigned long elapseAfter;  // monotonic time when timer should elapse next time (ms)
    };

public:
    // cppcheck-suppress misra-c2012-17.8 ; false positive. setting default parameter value is not parameter modification
    static std::shared_ptr<HsmEventDispatcherArduino> create(
        const size_t eventsCacheSize = DISPATCHER_DEFAULT_EVENTS_CACHESIZE);

    bool start() override;

    void emitEvent(const HandlerID_t handlerID) override;

    void dispatchEvents();

protected:
    explicit HsmEventDispatcherArduino(const size_t eventsCacheSize);

    virtual ~HsmEventDispatcherArduino();

    bool deleteSafe() override;

    void notifyDispatcherAboutEvent() override;

    void startTimerImpl(const TimerID_t timerID, const unsigned int intervalMs, const bool isSingleShot) override;
    void stopTimerImpl(const TimerID_t timerID) override;

    void handleTimers();

private:
    std::map<TimerID_t, RunningTimerInfo> mRunningTimers;
};

}  // namespace hsmcpp

#endif  // HSMCPP_HSMEVENTDISPATCHERARDUINO_HPP