1+ // ======================================================================
2+ // \title Led.cpp
3+ // \author ortega
4+ // \brief cpp file for Led component implementation class
5+ // ======================================================================
6+
7+ #include " FprimeZephyrReference/Components/Led/Led.hpp"
8+ #include " config/FpConfig.hpp"
9+
10+ namespace Components {
11+
12+ // ----------------------------------------------------------------------
13+ // Component construction and destruction
14+ // ----------------------------------------------------------------------
15+
16+ Led ::Led (const char * const compName) : LedComponentBase(compName) {}
17+
18+ Led ::~Led () {}
19+
20+ void Led ::parameterUpdated (FwPrmIdType id) {
21+ Fw::ParamValid isValid = Fw::ParamValid::INVALID;
22+ switch (id) {
23+ case PARAMID_BLINK_INTERVAL: {
24+ // Read back the parameter value
25+ const U32 interval = this ->paramGet_BLINK_INTERVAL (isValid);
26+ // NOTE: isValid is always VALID in parameterUpdated as it was just properly set
27+ FW_ASSERT (isValid == Fw::ParamValid::VALID, static_cast <FwAssertArgType>(isValid));
28+
29+ // Emit the blink interval set event
30+ this ->log_ACTIVITY_HI_BlinkIntervalSet (interval);
31+ break ;
32+ }
33+ default :
34+ FW_ASSERT (0 , static_cast <FwAssertArgType>(id));
35+ break ;
36+ }
37+ }
38+
39+ // ----------------------------------------------------------------------
40+ // Handler implementations for user-defined typed input ports
41+ // ----------------------------------------------------------------------
42+
43+ void Led ::run_handler (FwIndexType portNum, U32 context) {
44+ // Read back the parameter value
45+ Fw::ParamValid isValid = Fw::ParamValid::INVALID;
46+ U32 interval = this ->paramGet_BLINK_INTERVAL (isValid);
47+ FW_ASSERT ((isValid != Fw::ParamValid::INVALID) && (isValid != Fw::ParamValid::UNINIT),
48+ static_cast <FwAssertArgType>(isValid));
49+
50+ // Only perform actions when set to blinking
51+ if (this ->m_blinking && (interval != 0 )) {
52+ // If toggling state
53+ if (this ->m_toggleCounter == 0 ) {
54+ // Toggle state
55+ this ->m_state = (this ->m_state == Fw::On::ON) ? Fw::On::OFF : Fw::On::ON;
56+ this ->m_transitions ++;
57+ this ->tlmWrite_LedTransitions (this ->m_transitions );
58+
59+ // Port may not be connected, so check before sending output
60+ if (this ->isConnected_gpioSet_OutputPort (0 )) {
61+ this ->gpioSet_out (0 , (Fw::On::ON == this ->m_state ) ? Fw::Logic::HIGH : Fw::Logic::LOW);
62+ }
63+
64+ this ->log_ACTIVITY_LO_LedState (this ->m_state );
65+ }
66+
67+ this ->m_toggleCounter = (this ->m_toggleCounter + 1 ) % interval;
68+ }
69+ // We are not blinking
70+ else {
71+ if (this ->m_state == Fw::On::ON) {
72+ // Port may not be connected, so check before sending output
73+ if (this ->isConnected_gpioSet_OutputPort (0 )) {
74+ this ->gpioSet_out (0 , Fw::Logic::LOW);
75+ }
76+
77+ this ->m_state = Fw::On::OFF;
78+ this ->log_ACTIVITY_LO_LedState (this ->m_state );
79+ }
80+ }
81+ }
82+
83+ // ----------------------------------------------------------------------
84+ // Handler implementations for commands
85+ // ----------------------------------------------------------------------
86+
87+ void Led ::BLINKING_ON_OFF_cmdHandler (FwOpcodeType opCode, U32 cmdSeq, Fw::On onOff) {
88+ this ->m_toggleCounter = 0 ; // Reset count on any successful command
89+ this ->m_blinking = Fw::On::ON == onOff; // Update blinking state
90+
91+ this ->log_ACTIVITY_HI_SetBlinkingState (onOff);
92+
93+ this ->tlmWrite_BlinkingState (onOff);
94+
95+ // Provide command response
96+ this ->cmdResponse_out (opCode, cmdSeq, Fw::CmdResponse::OK);
97+ }
98+
99+ } // namespace Components
0 commit comments