Skip to content

Commit

Permalink
Adding blinker component
Browse files Browse the repository at this point in the history
  • Loading branch information
nateinaction committed Nov 7, 2024
1 parent 65c2516 commit b5bdf00
Show file tree
Hide file tree
Showing 14 changed files with 640 additions and 3 deletions.
6 changes: 6 additions & 0 deletions BroncoDeployment/Top/BroncoDeploymentPackets.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
<channel name="hubComDriver.Status"/>
</packet>

<packet name="LedBlinkerChannels" id="9" level="3">
<channel name="ledBlinker.LedBlinks"/>
<channel name="ledBlinker.BlinkingState"/>
<channel name="ledBlinker.BlinkingColor"/>
</packet>

<!-- Ignored packets -->

<ignore>
Expand Down
9 changes: 7 additions & 2 deletions BroncoDeployment/Top/instances.fpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ module BroncoDeployment {
queue size Default.QUEUE_SIZE \
stack size Default.STACK_SIZE \
priority 97

instance ledBlinker: Components.LedBlinker base id 0x0E00 \
queue size Default.QUEUE_SIZE \
stack size Default.STACK_SIZE \
priority 95

# ----------------------------------------------------------------------
# Queued component instances
Expand Down Expand Up @@ -60,6 +65,8 @@ module BroncoDeployment {

instance rateDriver: Arduino.HardwareRateDriver base id 0x4A00

instance neoPixelDriver: Drv.NeoPixelDriver base id 0x4C00

# Hub Connections

instance hub: Svc.GenericHub base id 0x5000
Expand All @@ -70,8 +77,6 @@ module BroncoDeployment {

instance hubComDriver: Radio.RFM69 base id 0x5300



# Custom Connections

instance broncoOreMessageHandler: Components.BroncoOreMessageHandler base id 0x6000
Expand Down
11 changes: 11 additions & 0 deletions BroncoDeployment/Top/topology.fpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ module BroncoDeployment {

#custom instances
instance broncoOreMessageHandler
instance ledBlinker
instance neoPixelDriver

# ----------------------------------------------------------------------
# Pattern graph specifiers
Expand Down Expand Up @@ -126,6 +128,15 @@ module BroncoDeployment {
hubDeframer.bufferOut -> hub.dataIn
hub.dataInDeallocate -> bufferManager.bufferSendIn
}

# Named connection group
connections LedConnections {
# Rate Group 1 (1Hz cycle) ouput is connected to led's run input
rateGroup1.RateGroupMemberOut[3] -> ledBlinker.run
# led's neopixel output is connected to neoPixelDriver's neoPixelOnOff input
ledBlinker.neoPixelSet -> neoPixelDriver.neoPixelSet
}
}
}
2 changes: 1 addition & 1 deletion Components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
add_fprime_subdirectory("${CMAKE_CURRENT_LIST_DIR}/BroncoOreMessageHandler/")

add_fprime_subdirectory("${CMAKE_CURRENT_LIST_DIR}/Radio/")

add_fprime_subdirectory("${CMAKE_CURRENT_LIST_DIR}/LedBlinker/")
add_fprime_subdirectory("${CMAKE_CURRENT_LIST_DIR}/Drv/")
29 changes: 29 additions & 0 deletions Components/LedBlinker/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
####
# F prime CMakeLists.txt:
#
# SOURCE_FILES: combined list of source and autocoding files
# MOD_DEPS: (optional) module dependencies
# UT_SOURCE_FILES: list of source files for unit tests
#
####
set(SOURCE_FILES
"${CMAKE_CURRENT_LIST_DIR}/LedBlinker.fpp"
"${CMAKE_CURRENT_LIST_DIR}/LedBlinker.cpp"
)

# Uncomment and add any modules that this component depends on, else
# they might not be available when cmake tries to build this component.

# set(MOD_DEPS
# Adafruit_NeoPixel
# )

register_fprime_module()

set(UT_SOURCE_FILES
"${CMAKE_CURRENT_LIST_DIR}/LedBlinker.fpp"
"${CMAKE_CURRENT_LIST_DIR}/test/ut/LedBlinkerTestMain.cpp"
"${CMAKE_CURRENT_LIST_DIR}/test/ut/LedBlinkerTester.cpp"
)
set(UT_AUTO_HELPERS ON) # Additional Unit-Test autocoding
register_fprime_ut()
143 changes: 143 additions & 0 deletions Components/LedBlinker/LedBlinker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// ======================================================================
// \title LedBlinker.cpp
// \author nateinaction
// \brief cpp file for LedBlinker component implementation class
// ======================================================================

#include "Components/LedBlinker/LedBlinker.hpp"
#include "FpConfig.hpp"

namespace Components {

// ----------------------------------------------------------------------
// Component construction and destruction
// ----------------------------------------------------------------------
LedBlinker ::LedBlinker(const char* const compName) : LedBlinkerComponentBase(compName) {}

LedBlinker ::~LedBlinker() {}

// ----------------------------------------------------------------------
// Handler implementations for user-defined typed input ports
// ----------------------------------------------------------------------

void LedBlinker ::
run_handler(
const NATIVE_INT_TYPE portNum,
NATIVE_UINT_TYPE context)
{
// Only perform actions when set to blinking
if (this->blinkingState) {
// Blink the LED
blink();
} else {
// Turn off the LED
this->neoPixelSet_out(0, Drv::NeoPixelColor(0, 0, 0));
}

// Report the number of blinks via a telemetry channel
this->tlmWrite_LedBlinks(this->blinkCount);
}

void LedBlinker ::blink() {
// Get interval from parameter or use default
U32 interval = this->intervalParamOrDefault();

// Get color from parameter or use default
Drv::NeoPixelColor color = this->colorParamOrDefault();

// Set the LED color or turn it off based on cycle count
this->neoPixelSet_out(0, (this->cycleCount < (interval / 2)) ? color : Drv::NeoPixelColor(0, 0, 0));

// Increment blink count at the start of a cycle
this->blinkCount += (this->cycleCount == 0);

// Increment cycle count
this->cycleCount = (this->cycleCount + 1) % interval;
}

Drv::NeoPixelColor LedBlinker ::colorParamOrDefault() {
// Read back the parameter value
Fw::ParamValid isColorValid;
Drv::NeoPixelColor color = this->paramGet_BLINK_COLOR(isColorValid);

// Force color to be red when invalid or not set
return ((Fw::ParamValid::INVALID == isColorValid) || (Fw::ParamValid::UNINIT == isColorValid)) ? Drv::NeoPixelColor(50, 0, 0) : color;
}

U32 LedBlinker ::intervalParamOrDefault() {
// Read back the parameter value
Fw::ParamValid isIntervalValid;
U32 interval = this->paramGet_BLINK_INTERVAL(isIntervalValid);

// Force interval to be 10 when invalid or not set
return ((Fw::ParamValid::INVALID == isIntervalValid) || (Fw::ParamValid::UNINIT == isIntervalValid)) ? 10 : interval;
}

// ----------------------------------------------------------------------
// Handler implementations for commands
// ----------------------------------------------------------------------

void LedBlinker ::BLINKING_ON_OFF_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, Fw::On blinking_state) {
// Create a variable to represent the command response
auto cmdResp = Fw::CmdResponse::OK;

// Set blinking state
this->blinkingState = blinking_state;

// Reset cycle count
this->cycleCount = 0;

// Reports the state as an event
this->log_ACTIVITY_HI_SetBlinkingState(blinking_state);

// Report the blinking state via a telemetry channel.
this->tlmWrite_BlinkingState(blinking_state);

// Provide command response
this->cmdResponse_out(opCode, cmdSeq, cmdResp);
}

void LedBlinker ::parameterUpdated(FwPrmIdType id) {
switch (id)
{
case PARAMID_BLINK_COLOR:
// Validate and set the color parameter on update
this->parameterValidateColor();
break;
case PARAMID_BLINK_INTERVAL:
// Validate and set the interval parameter on update
this->parameterValidateInterval();
break;
default:
FW_ASSERT(1, id); // Should never reach, invalid parameter ID
break;
}
}

void LedBlinker ::parameterValidateColor() {
// Read back the parameter value
Fw::ParamValid isValid;
Drv::NeoPixelColor color = this->paramGet_BLINK_COLOR(isValid);

// Fail if the color is invalid
FW_ASSERT(isValid == Fw::ParamValid::VALID, isValid);

// Log the color change
this->log_ACTIVITY_HI_BlinkColorSet(color);

// Report the color via a telemetry channel
this->tlmWrite_BlinkingColor(color);
}

void LedBlinker ::parameterValidateInterval() {
// Read back the parameter value
Fw::ParamValid isValid;
U32 interval = this->paramGet_BLINK_INTERVAL(isValid);

// Fail if the interval is invalid
FW_ASSERT(isValid == Fw::ParamValid::VALID, isValid);

// Log the interval change
this->log_ACTIVITY_HI_BlinkIntervalSet(interval);
}
} // namespace Components
76 changes: 76 additions & 0 deletions Components/LedBlinker/LedBlinker.fpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module Components {
@ Component to blink an LED driven by a rate group
active component LedBlinker {
@ Command to turn on or off the blinking LED
async command BLINKING_ON_OFF(
blinking_state: Fw.On @< Turn the LED blinking on or off.
)

@ Reports the state we set to blinking.
event SetBlinkingState(state: Fw.On) \
severity activity high \
format "Set blinking state to {}."

@ Reports the color that has been set
event BlinkColorSet(color: Drv.NeoPixelColor) \
severity activity high \
format "LED blink color set to {}"

@ Reports the interval that has been set
event BlinkIntervalSet(interval: U32) \
severity activity high \
format "LED blink interval set to {}"

@ Telemetry channel to report blinking state.
telemetry BlinkingState: Fw.On

@ Telemetry channel to report blinking state.
telemetry BlinkingColor: Drv.NeoPixelColor

@ Telemetry channel to report the LED state.
telemetry LedBlinks: U64

@ Blinking interval in rate group ticks
param BLINK_INTERVAL: U32

@ Blinking interval in rate group ticks
param BLINK_COLOR: Drv.NeoPixelColor

@ Port receiving calls from the rate group
sync input port run: Svc.Sched

@ Port sending calls to the GPIO driver
output port neoPixelSet: Drv.NeoPixelSet

###############################################################################
# Standard AC Ports: Required for Channels, Events, Commands, and Parameters #
###############################################################################
@ Port for requesting the current time
time get port timeCaller

@ Port for sending command registrations
command reg port cmdRegOut

@ Port for receiving commands
command recv port cmdIn

@ Port for sending command responses
command resp port cmdResponseOut

@ Port for sending textual representation of events
text event port logTextOut

@ Port for sending events to downlink
event port logOut

@ Port for sending telemetry channels to downlink
telemetry port tlmOut

@ Port to return the value of a parameter
param get port prmGetOut

@Port to set the value of a parameter
param set port prmSetOut

}
}
Loading

0 comments on commit b5bdf00

Please sign in to comment.