|
| 1 | +// ====================================================================== |
| 2 | +// \title ComDelay.cpp |
| 3 | +// \author starchmd |
| 4 | +// \brief cpp file for ComDelay component implementation class |
| 5 | +// ====================================================================== |
| 6 | + |
| 7 | +#include "FprimeZephyrReference/Components/ComDelay/ComDelay.hpp" |
| 8 | +#include "FprimeZephyrReference/Components/ComDelay/FppConstantsAc.hpp" |
| 9 | + |
| 10 | +namespace Components { |
| 11 | + |
| 12 | +// ---------------------------------------------------------------------- |
| 13 | +// Component construction and destruction |
| 14 | +// ---------------------------------------------------------------------- |
| 15 | + |
| 16 | +ComDelay ::ComDelay(const char* const compName) |
| 17 | + : ComDelayComponentBase(compName), m_last_status_valid(false), m_last_status(Fw::Success::FAILURE) {} |
| 18 | + |
| 19 | +ComDelay ::~ComDelay() {} |
| 20 | + |
| 21 | +void ComDelay ::parameterUpdated(FwPrmIdType id) { |
| 22 | + switch (id) { |
| 23 | + case ComDelay::PARAMID_DIVIDER: { |
| 24 | + Fw::ParamValid is_valid; |
| 25 | + U8 new_divider = this->paramGet_DIVIDER(is_valid); |
| 26 | + if ((is_valid != Fw::ParamValid::INVALID) && (is_valid != Fw::ParamValid::UNINIT)) { |
| 27 | + this->log_ACTIVITY_HI_DividerSet(new_divider); |
| 28 | + } |
| 29 | + } break; |
| 30 | + default: |
| 31 | + FW_ASSERT(0); |
| 32 | + break; // Fallthrough from assert (static analysis) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// ---------------------------------------------------------------------- |
| 37 | +// Handler implementations for typed input ports |
| 38 | +// ---------------------------------------------------------------------- |
| 39 | + |
| 40 | +void ComDelay ::comStatusIn_handler(FwIndexType portNum, Fw::Success& condition) { |
| 41 | + this->m_last_status = condition; |
| 42 | + this->m_last_status_valid = true; |
| 43 | +} |
| 44 | + |
| 45 | +void ComDelay ::run_handler(FwIndexType portNum, U32 context) { |
| 46 | + // On the cycle after the tick count is reset, attempt to output any current com status |
| 47 | + if (this->m_tick_count == 0) { |
| 48 | + bool expected = true; |
| 49 | + // Receive the current "last status" validity flag and atomically exchange it with false. This effectively |
| 50 | + // "consumes" a valid status. When valid, the last status is sent out. |
| 51 | + bool valid = this->m_last_status_valid.compare_exchange_strong(expected, false); |
| 52 | + if (valid) { |
| 53 | + this->comStatusOut_out(0, this->m_last_status); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Unless there is corruption, the parameter should always be valid via its default value; however, in the interest |
| 58 | + // of failing-safe and continuing some sort of communication we default the current_divisor to the default value. |
| 59 | + Fw::ParamValid is_valid; |
| 60 | + U8 current_divisor = this->paramGet_DIVIDER(is_valid); |
| 61 | + |
| 62 | + // Increment and module the tick count by the divisor |
| 63 | + if ((is_valid == Fw::ParamValid::INVALID) || (is_valid == Fw::ParamValid::UNINIT)) { |
| 64 | + current_divisor = Components::DEFAULT_DIVIDER; |
| 65 | + } |
| 66 | + // Count this new tick, resetting whenever the current count is at or higher than the current divider. |
| 67 | + this->m_tick_count = (this->m_tick_count >= current_divisor) ? 0 : this->m_tick_count + 1; |
| 68 | +} |
| 69 | + |
| 70 | +} // namespace Components |
0 commit comments