eive-obsw/mission/tcs/Tmp1075Handler.cpp

147 lines
5.2 KiB
C++
Raw Normal View History

2022-01-17 15:58:27 +01:00
#include <OBSWConfig.h>
#include <fsfw/datapool/PoolReadGuard.h>
2023-03-26 16:42:00 +02:00
#include <mission/tcs/Tmp1075Definitions.h>
#include <mission/tcs/Tmp1075Handler.h>
2021-01-08 09:34:43 +01:00
2022-01-17 15:58:27 +01:00
Tmp1075Handler::Tmp1075Handler(object_id_t objectId, object_id_t comIF, CookieIF *comCookie)
: DeviceHandlerBase(objectId, comIF, comCookie), dataset(this) {
if (comCookie == NULL) {
sif::error << "Tmp1075Handler: Invalid com cookie" << std::endl;
}
2021-01-08 09:34:43 +01:00
}
2022-01-17 15:58:27 +01:00
Tmp1075Handler::~Tmp1075Handler() {}
2021-01-08 09:34:43 +01:00
2023-06-18 13:38:00 +02:00
void Tmp1075Handler::doStartUp() { setMode(MODE_ON); }
2021-01-08 09:34:43 +01:00
2023-01-23 11:31:15 +01:00
void Tmp1075Handler::doShutDown() {
communicationStep = CommunicationStep::START_ADC_CONVERSION;
2023-07-06 13:56:43 +02:00
PoolReadGuard pg(&dataset);
2023-07-05 16:31:41 +02:00
dataset.setValidity(false, true);
2023-01-23 11:31:15 +01:00
setMode(_MODE_POWER_DOWN);
}
2022-01-17 15:58:27 +01:00
ReturnValue_t Tmp1075Handler::buildNormalDeviceCommand(DeviceCommandId_t *id) {
if (communicationStep == CommunicationStep::START_ADC_CONVERSION) {
*id = TMP1075::START_ADC_CONVERSION;
communicationStep = CommunicationStep::GET_TEMPERATURE;
return buildCommandFromCommand(*id, NULL, 0);
} else {
*id = TMP1075::GET_TEMP;
communicationStep = CommunicationStep::START_ADC_CONVERSION;
return buildCommandFromCommand(*id, NULL, 0);
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-01-08 09:34:43 +01:00
}
2022-01-17 15:58:27 +01:00
ReturnValue_t Tmp1075Handler::buildTransitionDeviceCommand(DeviceCommandId_t *id) {
2023-01-23 11:31:15 +01:00
return NOTHING_TO_SEND;
2021-01-08 09:34:43 +01:00
}
2022-01-17 15:58:27 +01:00
ReturnValue_t Tmp1075Handler::buildCommandFromCommand(DeviceCommandId_t deviceCommand,
const uint8_t *commandData,
size_t commandDataLen) {
switch (deviceCommand) {
case (TMP1075::START_ADC_CONVERSION): {
std::memset(cmdBuffer, 0, sizeof(cmdBuffer));
prepareAdcConversionCommand();
rawPacket = cmdBuffer;
rawPacketLen = TMP1075::CFGR_CMD_SIZE;
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-01-17 15:58:27 +01:00
}
case (TMP1075::GET_TEMP): {
std::memset(cmdBuffer, 0, sizeof(cmdBuffer));
prepareGetTempCommand();
rawPacket = cmdBuffer;
rawPacketLen = TMP1075::POINTER_REG_SIZE;
rememberCommandId = TMP1075::GET_TEMP;
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2022-01-17 15:58:27 +01:00
}
default:
return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
}
2022-08-24 17:27:47 +02:00
return returnvalue::FAILED;
2021-01-08 09:34:43 +01:00
}
2022-01-17 15:58:27 +01:00
void Tmp1075Handler::fillCommandAndReplyMap() {
this->insertInCommandMap(TMP1075::START_ADC_CONVERSION);
this->insertInCommandAndReplyMap(TMP1075::GET_TEMP, 1, &dataset, TMP1075::GET_TEMP_REPLY_SIZE);
2021-01-08 09:34:43 +01:00
}
2022-01-17 15:58:27 +01:00
ReturnValue_t Tmp1075Handler::scanForReply(const uint8_t *start, size_t remainingSize,
DeviceCommandId_t *foundId, size_t *foundLen) {
switch (rememberCommandId) {
case (TMP1075::GET_TEMP):
*foundId = TMP1075::GET_TEMP;
*foundLen = TMP1075::GET_TEMP_REPLY_SIZE;
rememberCommandId = TMP1075::NONE;
break;
default:
return IGNORE_REPLY_DATA;
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-01-08 09:34:43 +01:00
}
2022-01-17 15:58:27 +01:00
ReturnValue_t Tmp1075Handler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) {
switch (id) {
case TMP1075::GET_TEMP: {
2023-06-14 18:51:06 +02:00
// Convert 12 bit MSB first raw temperature to 16 bit first.
int16_t tempValueRaw = static_cast<uint16_t>((packet[0] << 8) | packet[1]) >> 4;
// Sign extension to 16 bits: If the sign bit is set, fill up with ones on the left.
tempValueRaw = (packet[0] & 0x80) ? (tempValueRaw | 0xF000) : tempValueRaw;
2023-06-13 23:25:44 +02:00
// 0.0625 is the sensor sensitivity.
2022-01-17 15:58:27 +01:00
float tempValue = ((static_cast<float>(tempValueRaw)) * 0.0625);
2022-11-11 17:01:19 +01:00
#if OBSW_DEBUG_TMP1075 == 1
2022-01-17 15:58:27 +01:00
sif::info << "Tmp1075 with object id: 0x" << std::hex << getObjectId()
<< ": Temperature: " << tempValue << " °C" << std::endl;
2021-01-08 09:34:43 +01:00
#endif
2022-01-17 15:58:27 +01:00
ReturnValue_t result = dataset.read();
2022-08-24 17:27:47 +02:00
if (result == returnvalue::OK) {
2022-01-17 15:58:27 +01:00
dataset.temperatureCelcius = tempValue;
2022-05-31 16:44:57 +02:00
dataset.setValidity(true, true);
2022-01-17 15:58:27 +01:00
dataset.commit();
2022-06-17 08:31:36 +02:00
} else {
dataset.setValidity(false, true);
2022-05-31 16:44:57 +02:00
}
2022-01-17 15:58:27 +01:00
break;
}
default: {
return DeviceHandlerIF::UNKNOWN_DEVICE_REPLY;
}
}
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-01-08 09:34:43 +01:00
}
2022-01-17 15:58:27 +01:00
void Tmp1075Handler::setNormalDatapoolEntriesInvalid() {}
2021-01-08 09:34:43 +01:00
2022-01-17 15:58:27 +01:00
void Tmp1075Handler::prepareAdcConversionCommand() {
cmdBuffer[0] = TMP1075::CFGR_ADDR;
cmdBuffer[1] = TMP1075::ONE_SHOT_MODE >> 8;
cmdBuffer[2] = TMP1075::ONE_SHOT_MODE & 0xFF;
2021-01-08 09:34:43 +01:00
}
2022-01-17 15:58:27 +01:00
void Tmp1075Handler::prepareGetTempCommand() { cmdBuffer[0] = TMP1075::TEMP_REG_ADDR; }
2021-01-08 09:34:43 +01:00
2022-01-17 15:58:27 +01:00
uint32_t Tmp1075Handler::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) { return 500; }
2021-01-08 09:34:43 +01:00
2022-01-17 15:58:27 +01:00
ReturnValue_t Tmp1075Handler::initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
LocalDataPoolManager &poolManager) {
localDataPoolMap.emplace(TMP1075::TEMPERATURE_C_TMP1075, new PoolEntry<float>({0.0}));
2022-08-15 11:57:57 +02:00
poolManager.subscribeForRegularPeriodicPacket(
subdp::RegularHkPeriodicParams(dataset.getSid(), false, 30.0));
2022-08-24 17:27:47 +02:00
return returnvalue::OK;
2021-01-08 09:34:43 +01:00
}
2022-11-11 11:12:43 +01:00
2023-04-04 14:45:09 +02:00
ReturnValue_t Tmp1075Handler::setHealth(HealthState health) {
2023-04-04 14:52:22 +02:00
if (health != FAULTY and health != PERMANENT_FAULTY and health != HEALTHY and
2023-04-04 14:45:09 +02:00
health != EXTERNAL_CONTROL) {
return returnvalue::FAILED;
}
// Required because we do not have an assembly.
2023-07-06 17:23:55 +02:00
if (health == FAULTY or health == PERMANENT_FAULTY) {
setMode(_MODE_SHUT_DOWN);
}
return DeviceHandlerBase::setHealth(health);
2023-04-04 14:45:09 +02:00
}