fsfw/src/fsfw/power/PowerSwitcher.cpp

135 lines
3.6 KiB
C++
Raw Normal View History

2021-07-13 20:22:54 +02:00
#include "fsfw/power/PowerSwitcher.h"
2020-12-03 18:29:28 +01:00
2022-03-28 12:57:11 +02:00
#include "definitions.h"
2021-07-13 20:22:54 +02:00
#include "fsfw/objectmanager/ObjectManager.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
2022-03-28 12:48:15 +02:00
PowerSwitcher::PowerSwitcher(PowerSwitchIF* switcher, power::Switch_t setSwitch1,
power::Switch_t setSwitch2, PowerSwitcher::State_t setStartState)
: power(switcher), state(setStartState), firstSwitch(setSwitch1), secondSwitch(setSwitch2) {}
ReturnValue_t PowerSwitcher::getStateOfSwitches() {
2022-02-02 10:29:30 +01:00
SwitchReturn_t result = howManySwitches();
switch (result) {
case ONE_SWITCH:
return power->getSwitchState(firstSwitch);
case TWO_SWITCHES: {
ReturnValue_t firstSwitchState = power->getSwitchState(firstSwitch);
ReturnValue_t secondSwitchState = power->getSwitchState(firstSwitch);
if ((firstSwitchState == PowerSwitchIF::SWITCH_ON) &&
(secondSwitchState == PowerSwitchIF::SWITCH_ON)) {
return PowerSwitchIF::SWITCH_ON;
} else if ((firstSwitchState == PowerSwitchIF::SWITCH_OFF) &&
(secondSwitchState == PowerSwitchIF::SWITCH_OFF)) {
return PowerSwitchIF::SWITCH_OFF;
} else {
return HasReturnvaluesIF::RETURN_FAILED;
}
}
default:
return HasReturnvaluesIF::RETURN_FAILED;
}
}
void PowerSwitcher::commandSwitches(ReturnValue_t onOff) {
2022-02-02 10:29:30 +01:00
SwitchReturn_t result = howManySwitches();
switch (result) {
case TWO_SWITCHES:
power->sendSwitchCommand(secondSwitch, onOff);
/* NO BREAK falls through*/
case ONE_SWITCH:
power->sendSwitchCommand(firstSwitch, onOff);
break;
}
return;
}
2022-03-28 12:48:15 +02:00
void PowerSwitcher::turnOn(bool checkCurrentState) {
2022-03-28 12:57:11 +02:00
if (checkCurrentState) {
if (getStateOfSwitches() == PowerSwitchIF::SWITCH_ON) {
2022-03-28 12:48:15 +02:00
state = SWITCH_IS_ON;
return;
}
}
2022-02-02 10:29:30 +01:00
commandSwitches(PowerSwitchIF::SWITCH_ON);
state = WAIT_ON;
}
2022-03-28 12:48:15 +02:00
void PowerSwitcher::turnOff(bool checkCurrentState) {
2022-03-28 12:57:11 +02:00
if (checkCurrentState) {
if (getStateOfSwitches() == PowerSwitchIF::SWITCH_OFF) {
2022-03-28 12:48:15 +02:00
state = SWITCH_IS_OFF;
return;
}
}
2022-02-02 10:29:30 +01:00
commandSwitches(PowerSwitchIF::SWITCH_OFF);
state = WAIT_OFF;
}
2022-03-28 12:48:15 +02:00
bool PowerSwitcher::active() {
2022-03-28 12:57:11 +02:00
if (state == WAIT_OFF or state == WAIT_ON) {
2022-03-28 12:48:15 +02:00
return true;
}
return false;
}
PowerSwitcher::SwitchReturn_t PowerSwitcher::howManySwitches() {
2022-03-28 12:48:15 +02:00
if (secondSwitch == power::NO_SWITCH) {
2022-02-02 10:29:30 +01:00
return ONE_SWITCH;
} else {
return TWO_SWITCHES;
}
}
void PowerSwitcher::doStateMachine() {
2022-02-02 10:29:30 +01:00
switch (state) {
case SWITCH_IS_OFF:
case SWITCH_IS_ON:
// Do nothing.
break;
case WAIT_OFF:
if (getStateOfSwitches() == PowerSwitchIF::SWITCH_OFF) {
state = SWITCH_IS_OFF;
}
break;
case WAIT_ON:
if (getStateOfSwitches() == PowerSwitchIF::SWITCH_ON) {
state = SWITCH_IS_ON;
}
break;
default:
// Should never happen.
break;
}
}
ReturnValue_t PowerSwitcher::checkSwitchState() {
2022-02-02 10:29:30 +01:00
switch (state) {
case WAIT_OFF:
case WAIT_ON:
return IN_POWER_TRANSITION;
case SWITCH_IS_OFF:
if (getStateOfSwitches() == PowerSwitchIF::SWITCH_OFF) {
return RETURN_OK;
} else {
return SWITCH_STATE_MISMATCH;
}
case SWITCH_IS_ON:
if (getStateOfSwitches() == PowerSwitchIF::SWITCH_ON) {
return RETURN_OK;
} else {
return SWITCH_STATE_MISMATCH;
}
}
return RETURN_FAILED;
}
2022-02-02 10:29:30 +01:00
PowerSwitcher::State_t PowerSwitcher::getState() { return state; }
2022-02-02 10:29:30 +01:00
uint32_t PowerSwitcher::getSwitchDelay() { return power->getSwitchDelayMs(); }
2022-02-02 10:29:30 +01:00
uint8_t PowerSwitcher::getFirstSwitch() const { return firstSwitch; }
2022-02-02 10:29:30 +01:00
uint8_t PowerSwitcher::getSecondSwitch() const { return secondSwitch; }