fsfw/power/PowerSwitcher.cpp

136 lines
3.1 KiB
C++
Raw Normal View History

2020-08-13 20:53:35 +02:00
#include "PowerSwitcher.h"
2020-12-03 18:29:28 +01:00
2021-06-05 19:52:38 +02:00
#include "../objectmanager/ObjectManager.h"
#include "../serviceinterface/ServiceInterface.h"
PowerSwitcher::PowerSwitcher(uint8_t setSwitch1, uint8_t setSwitch2,
2020-12-03 18:29:28 +01:00
PowerSwitcher::State_t setStartState):
state(setStartState), firstSwitch(setSwitch1),
secondSwitch(setSwitch2) {
}
ReturnValue_t PowerSwitcher::initialize(object_id_t powerSwitchId) {
2021-06-05 19:52:38 +02:00
power = ObjectManager::instance()->get<PowerSwitchIF>(powerSwitchId);
2020-12-03 18:29:28 +01:00
if (power == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t PowerSwitcher::getStateOfSwitches() {
SwitchReturn_t result = howManySwitches();
2020-12-03 18:29:28 +01:00
switch (result) {
case ONE_SWITCH:
return power->getSwitchState(firstSwitch);
2020-12-03 18:29:28 +01:00
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;
2020-12-03 18:29:28 +01:00
}
else if ((firstSwitchState == PowerSwitchIF::SWITCH_OFF)
&& (secondSwitchState == PowerSwitchIF::SWITCH_OFF)) {
return PowerSwitchIF::SWITCH_OFF;
2020-12-03 18:29:28 +01:00
}
else {
return HasReturnvaluesIF::RETURN_FAILED;
}
2020-12-03 18:29:28 +01:00
}
default:
return HasReturnvaluesIF::RETURN_FAILED;
}
}
void PowerSwitcher::commandSwitches(ReturnValue_t onOff) {
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;
}
void PowerSwitcher::turnOn() {
commandSwitches(PowerSwitchIF::SWITCH_ON);
state = WAIT_ON;
}
void PowerSwitcher::turnOff() {
commandSwitches(PowerSwitchIF::SWITCH_OFF);
state = WAIT_OFF;
}
PowerSwitcher::SwitchReturn_t PowerSwitcher::howManySwitches() {
if (secondSwitch == NO_SWITCH) {
return ONE_SWITCH;
} else {
return TWO_SWITCHES;
}
}
void PowerSwitcher::doStateMachine() {
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() {
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;
}
PowerSwitcher::State_t PowerSwitcher::getState() {
return state;
}
uint32_t PowerSwitcher::getSwitchDelay() {
return power->getSwitchDelayMs();
}
uint8_t PowerSwitcher::getFirstSwitch() const {
return firstSwitch;
}
uint8_t PowerSwitcher::getSecondSwitch() const {
return secondSwitch;
}