#include "pollingSeqTables.h"

#include <fsfw/devicehandlers/DeviceHandlerIF.h>
#include <fsfw/objectmanager/ObjectManagerIF.h>
#include <fsfw/serviceinterface/ServiceInterfaceStream.h>
#include <fsfw/tasks/FixedTimeslotTaskIF.h>
#include <mission/acs/imtqHelpers.h>
#include <mission/tcs/Max31865Definitions.h>

#include "OBSWConfig.h"
#include "eive/definitions.h"
#include "eive/objects.h"
#include "linux/payload/FreshSupvHandler.h"

#ifndef RPI_TEST_ADIS16507
#define RPI_TEST_ADIS16507 0
#endif

#ifndef RPI_TEST_GPS_HANDLER
#define RPI_TEST_GPS_HANDLER 0
#endif

ReturnValue_t pst::pstSyrlinks(FixedTimeslotTaskIF *thisSequence) {
  uint32_t length = thisSequence->getPeriodMs();

  thisSequence->addSlot(objects::SYRLINKS_HANDLER, length * 0, DeviceHandlerIF::PERFORM_OPERATION);
  thisSequence->addSlot(objects::SYRLINKS_HANDLER, length * 0, DeviceHandlerIF::SEND_WRITE);
  thisSequence->addSlot(objects::SYRLINKS_HANDLER, length * 0, DeviceHandlerIF::GET_WRITE);
  thisSequence->addSlot(objects::SYRLINKS_HANDLER, length * 0.25, DeviceHandlerIF::SEND_READ);
  thisSequence->addSlot(objects::SYRLINKS_HANDLER, length * 0.25, DeviceHandlerIF::GET_READ);

  thisSequence->addSlot(objects::SYRLINKS_HANDLER, length * 0.4, DeviceHandlerIF::SEND_WRITE);
  thisSequence->addSlot(objects::SYRLINKS_HANDLER, length * 0.4, DeviceHandlerIF::GET_WRITE);
  thisSequence->addSlot(objects::SYRLINKS_HANDLER, length * 0.75, DeviceHandlerIF::SEND_READ);
  thisSequence->addSlot(objects::SYRLINKS_HANDLER, length * 0.75, DeviceHandlerIF::GET_READ);

  static_cast<void>(length);

  return thisSequence->checkSequence();
}

// I don't think this needs to be in a PST because linux takes care of bus serialization, but
// keep it like this for now, it works
ReturnValue_t pst::pstI2c(TmpSchedConfig schedConf, FixedTimeslotTaskIF *thisSequence) {
  // Length of a communication cycle
  uint32_t length = thisSequence->getPeriodMs();
  static_cast<void>(length);

  if (schedConf.scheduleTmpDev0) {
    thisSequence->addSlot(objects::TMP1075_HANDLER_TCS_0, length * 0,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::TMP1075_HANDLER_TCS_0, length * 0, DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::TMP1075_HANDLER_TCS_0, length * 0.1, DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::TMP1075_HANDLER_TCS_0, length * 0.1, DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::TMP1075_HANDLER_TCS_0, length * 0.1, DeviceHandlerIF::GET_READ);
  }

  if (schedConf.scheduleTmpDev1) {
    thisSequence->addSlot(objects::TMP1075_HANDLER_TCS_1, length * 0.2,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::TMP1075_HANDLER_TCS_1, length * 0.2,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::TMP1075_HANDLER_TCS_1, length * 0.2, DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::TMP1075_HANDLER_TCS_1, length * 0.3, DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::TMP1075_HANDLER_TCS_1, length * 0.3, DeviceHandlerIF::GET_READ);
  }

  if (schedConf.schedulePlPcduDev0) {
    thisSequence->addSlot(objects::TMP1075_HANDLER_PLPCDU_0, length * 0.4,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::TMP1075_HANDLER_PLPCDU_0, length * 0.4,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::TMP1075_HANDLER_PLPCDU_0, length * 0.4,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::TMP1075_HANDLER_PLPCDU_0, length * 0.5,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::TMP1075_HANDLER_PLPCDU_0, length * 0.5,
                          DeviceHandlerIF::GET_READ);
  }
  if (schedConf.schedulePlPcduDev1) {
    thisSequence->addSlot(objects::TMP1075_HANDLER_PLPCDU_1, length * 0.6,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::TMP1075_HANDLER_PLPCDU_1, length * 0.6,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::TMP1075_HANDLER_PLPCDU_1, length * 0.6,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::TMP1075_HANDLER_PLPCDU_1, length * 0.7,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::TMP1075_HANDLER_PLPCDU_1, length * 0.7,
                          DeviceHandlerIF::GET_READ);
  }

  if (schedConf.scheduleIfBoardDev) {
    thisSequence->addSlot(objects::TMP1075_HANDLER_IF_BOARD, length * 0.8,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::TMP1075_HANDLER_IF_BOARD, length * 0.8,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::TMP1075_HANDLER_IF_BOARD, length * 0.8,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::TMP1075_HANDLER_IF_BOARD, length * 0.9,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::TMP1075_HANDLER_IF_BOARD, length * 0.9,
                          DeviceHandlerIF::GET_READ);
  }
  static_cast<void>(length);
  return thisSequence->checkSequence();
}

ReturnValue_t pst::pstGompaceCan(FixedTimeslotTaskIF *thisSequence) {
  uint32_t length = thisSequence->getPeriodMs();
  // PCDU handlers receives two messages and both must be handled
  thisSequence->addSlot(objects::PCDU_HANDLER, length * 0, DeviceHandlerIF::PERFORM_OPERATION);

  thisSequence->addSlot(objects::P60DOCK_HANDLER, length * 0, DeviceHandlerIF::PERFORM_OPERATION);
  thisSequence->addSlot(objects::PDU1_HANDLER, length * 0, DeviceHandlerIF::PERFORM_OPERATION);
  thisSequence->addSlot(objects::PDU2_HANDLER, length * 0, DeviceHandlerIF::PERFORM_OPERATION);
  thisSequence->addSlot(objects::ACU_HANDLER, length * 0, DeviceHandlerIF::PERFORM_OPERATION);

  thisSequence->addSlot(objects::P60DOCK_HANDLER, length * 0, DeviceHandlerIF::SEND_WRITE);
  thisSequence->addSlot(objects::PDU1_HANDLER, length * 0, DeviceHandlerIF::SEND_WRITE);
  thisSequence->addSlot(objects::PDU2_HANDLER, length * 0, DeviceHandlerIF::SEND_WRITE);
  thisSequence->addSlot(objects::ACU_HANDLER, length * 0, DeviceHandlerIF::SEND_WRITE);

  thisSequence->addSlot(objects::P60DOCK_HANDLER, length * 0, DeviceHandlerIF::GET_WRITE);
  thisSequence->addSlot(objects::PDU1_HANDLER, length * 0, DeviceHandlerIF::GET_WRITE);
  thisSequence->addSlot(objects::PDU2_HANDLER, length * 0, DeviceHandlerIF::GET_WRITE);
  thisSequence->addSlot(objects::ACU_HANDLER, length * 0, DeviceHandlerIF::GET_WRITE);

  thisSequence->addSlot(objects::P60DOCK_HANDLER, length * 0.5, DeviceHandlerIF::SEND_READ);
  thisSequence->addSlot(objects::PDU1_HANDLER, length * 0.5, DeviceHandlerIF::SEND_READ);
  thisSequence->addSlot(objects::PDU2_HANDLER, length * 0.5, DeviceHandlerIF::SEND_READ);
  thisSequence->addSlot(objects::ACU_HANDLER, length * 0.5, DeviceHandlerIF::SEND_READ);

  thisSequence->addSlot(objects::P60DOCK_HANDLER, length * 0.5, DeviceHandlerIF::GET_READ);
  thisSequence->addSlot(objects::PDU1_HANDLER, length * 0.5, DeviceHandlerIF::GET_READ);
  thisSequence->addSlot(objects::PDU2_HANDLER, length * 0.5, DeviceHandlerIF::GET_READ);
  thisSequence->addSlot(objects::ACU_HANDLER, length * 0.5, DeviceHandlerIF::GET_READ);

  if (thisSequence->checkSequence() != returnvalue::OK) {
    sif::error << "GomSpace PST initialization failed" << std::endl;
    return returnvalue::FAILED;
  }
  static_cast<void>(length);
  return returnvalue::OK;
}

ReturnValue_t pst::pstTest(FixedTimeslotTaskIF *thisSequence) {
  /* Length of a communication cycle */
  uint32_t length = thisSequence->getPeriodMs();
  bool notEmpty = false;

#if RPI_TEST_ADIS16507 == 1
  notEmpty = true;
  thisSequence->addSlot(objects::GYRO_0_ADIS_HANDLER, length * 0,
                        DeviceHandlerIF::PERFORM_OPERATION);
  thisSequence->addSlot(objects::GYRO_0_ADIS_HANDLER, length * 0.2, DeviceHandlerIF::SEND_WRITE);
  thisSequence->addSlot(objects::GYRO_0_ADIS_HANDLER, length * 0.4, DeviceHandlerIF::GET_WRITE);
  thisSequence->addSlot(objects::GYRO_0_ADIS_HANDLER, length * 0.6, DeviceHandlerIF::SEND_READ);
  thisSequence->addSlot(objects::GYRO_0_ADIS_HANDLER, length * 0.8, DeviceHandlerIF::GET_READ);
#endif

#if RPI_TEST_GPS_HANDLER == 1
  notEmpty = true;
  thisSequence->addSlot(objects::GPS0_HANDLER, length * 0, DeviceHandlerIF::PERFORM_OPERATION);

  thisSequence->addSlot(objects::GPS0_HANDLER, length * 0, DeviceHandlerIF::SEND_READ);
  thisSequence->addSlot(objects::GPS0_HANDLER, length * 0, DeviceHandlerIF::GET_READ);

  thisSequence->addSlot(objects::GPS0_HANDLER, length * 0.5, DeviceHandlerIF::SEND_READ);
  thisSequence->addSlot(objects::GPS0_HANDLER, length * 0.5, DeviceHandlerIF::GET_READ);
#endif
  static_cast<void>(length);
  if (not notEmpty) {
    return returnvalue::FAILED;
  }
  if (thisSequence->checkSequence() != returnvalue::OK) {
    sif::error << "Test PST initialization failed" << std::endl;
    return returnvalue::FAILED;
  }
  return returnvalue::OK;
}

ReturnValue_t pst::pstTcsAndAcs(FixedTimeslotTaskIF *thisSequence, AcsPstCfg cfg) {
  /* Length of a communication cycle */
  uint32_t length = thisSequence->getPeriodMs();

  // SUS: 16 ms
  if (cfg.scheduleSus) {
    /* Write setup */
    thisSequence->addSlot(objects::SUS_0_N_LOC_XFYFZM_PT_XF, length * 0,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::SUS_0_N_LOC_XFYFZM_PT_XF, length * 0,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::SUS_0_N_LOC_XFYFZM_PT_XF, length * 0,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::SUS_1_N_LOC_XBYFZM_PT_XB, length * 0,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::SUS_1_N_LOC_XBYFZM_PT_XB, length * 0,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::SUS_1_N_LOC_XBYFZM_PT_XB, length * 0,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::SUS_2_N_LOC_XFYBZB_PT_YB, length * 0,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::SUS_2_N_LOC_XFYBZB_PT_YB, length * 0,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::SUS_2_N_LOC_XFYBZB_PT_YB, length * 0,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::SUS_3_N_LOC_XFYBZF_PT_YF, length * 0,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::SUS_3_N_LOC_XFYBZF_PT_YF, length * 0,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::SUS_3_N_LOC_XFYBZF_PT_YF, length * 0,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::SUS_4_N_LOC_XMYFZF_PT_ZF, length * 0,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::SUS_4_N_LOC_XMYFZF_PT_ZF, length * 0,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::SUS_4_N_LOC_XMYFZF_PT_ZF, length * 0,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::SUS_5_N_LOC_XFYMZB_PT_ZB, length * 0,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::SUS_5_N_LOC_XFYMZB_PT_ZB, length * 0,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::SUS_5_N_LOC_XFYMZB_PT_ZB, length * 0,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::SUS_6_R_LOC_XFYBZM_PT_XF, length * 0,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::SUS_6_R_LOC_XFYBZM_PT_XF, length * 0,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::SUS_6_R_LOC_XFYBZM_PT_XF, length * 0,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::SUS_7_R_LOC_XBYBZM_PT_XB, length * 0,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::SUS_7_R_LOC_XBYBZM_PT_XB, length * 0,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::SUS_7_R_LOC_XBYBZM_PT_XB, length * 0,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::SUS_8_R_LOC_XBYBZB_PT_YB, length * 0,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::SUS_8_R_LOC_XBYBZB_PT_YB, length * 0,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::SUS_8_R_LOC_XBYBZB_PT_YB, length * 0,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::SUS_9_R_LOC_XBYBZB_PT_YF, length * 0,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::SUS_9_R_LOC_XBYBZB_PT_YF, length * 0,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::SUS_9_R_LOC_XBYBZB_PT_YF, length * 0,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::SUS_10_N_LOC_XMYBZF_PT_ZF, length * 0,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::SUS_10_N_LOC_XMYBZF_PT_ZF, length * 0,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::SUS_10_N_LOC_XMYBZF_PT_ZF, length * 0,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::SUS_11_R_LOC_XBYMZB_PT_ZB, length * 0,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::SUS_11_R_LOC_XBYMZB_PT_ZB, length * 0,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::SUS_11_R_LOC_XBYMZB_PT_ZB, length * 0,
                          DeviceHandlerIF::GET_WRITE);

    thisSequence->addSlot(objects::SUS_0_N_LOC_XFYFZM_PT_XF,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::SUS_0_N_LOC_XFYFZM_PT_XF,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::GET_READ);
    thisSequence->addSlot(objects::SUS_1_N_LOC_XBYFZM_PT_XB,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::SUS_1_N_LOC_XBYFZM_PT_XB,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::GET_READ);
    thisSequence->addSlot(objects::SUS_2_N_LOC_XFYBZB_PT_YB,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::SUS_2_N_LOC_XFYBZB_PT_YB,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::GET_READ);
    thisSequence->addSlot(objects::SUS_3_N_LOC_XFYBZF_PT_YF,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::SUS_3_N_LOC_XFYBZF_PT_YF,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::GET_READ);
    thisSequence->addSlot(objects::SUS_4_N_LOC_XMYFZF_PT_ZF,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::SUS_4_N_LOC_XMYFZF_PT_ZF,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::GET_READ);
    thisSequence->addSlot(objects::SUS_5_N_LOC_XFYMZB_PT_ZB,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::SUS_5_N_LOC_XFYMZB_PT_ZB,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::GET_READ);
    thisSequence->addSlot(objects::SUS_6_R_LOC_XFYBZM_PT_XF,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::SUS_6_R_LOC_XFYBZM_PT_XF,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::GET_READ);
    thisSequence->addSlot(objects::SUS_7_R_LOC_XBYBZM_PT_XB,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::SUS_7_R_LOC_XBYBZM_PT_XB,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::GET_READ);
    thisSequence->addSlot(objects::SUS_8_R_LOC_XBYBZB_PT_YB,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::SUS_8_R_LOC_XBYBZB_PT_YB,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::GET_READ);
    thisSequence->addSlot(objects::SUS_9_R_LOC_XBYBZB_PT_YF,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::SUS_9_R_LOC_XBYBZB_PT_YF,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::GET_READ);
    thisSequence->addSlot(objects::SUS_10_N_LOC_XMYBZF_PT_ZF,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::SUS_10_N_LOC_XMYBZF_PT_ZF,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::GET_READ);
    thisSequence->addSlot(objects::SUS_11_R_LOC_XBYMZB_PT_ZB,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::SUS_11_R_LOC_XBYMZB_PT_ZB,
                          length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          DeviceHandlerIF::GET_READ);
  }

  if (cfg.scheduleStr) {
    // 2 COM cycles for full PST for STR. The STR requests 2 packets types in NORMAL mode, this
    // ensures we always get an updated STR dataset for a regular normal mode cycle.
    thisSequence->addSlot(objects::STAR_TRACKER, length * 0, DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::STAR_TRACKER, length * 0, DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::STAR_TRACKER, length * 0, DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::STAR_TRACKER, length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::STAR_TRACKER, length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                          DeviceHandlerIF::GET_READ);
    thisSequence->addSlot(objects::STAR_TRACKER, length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::STAR_TRACKER, length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::STAR_TRACKER, length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::STAR_TRACKER, length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                          DeviceHandlerIF::GET_READ);
  }

  bool enableAside = true;
  bool enableBside = true;
  if (cfg.scheduleAcsBoard) {
    if (enableAside) {
      // A side
      thisSequence->addSlot(objects::MGM_0_LIS3_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::PERFORM_OPERATION);
      thisSequence->addSlot(objects::MGM_0_LIS3_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::SEND_WRITE);
      thisSequence->addSlot(objects::MGM_0_LIS3_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::GET_WRITE);
      thisSequence->addSlot(objects::MGM_0_LIS3_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                            DeviceHandlerIF::SEND_READ);
      thisSequence->addSlot(objects::MGM_0_LIS3_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                            DeviceHandlerIF::GET_READ);

      thisSequence->addSlot(objects::MGM_1_RM3100_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::PERFORM_OPERATION);
      thisSequence->addSlot(objects::MGM_1_RM3100_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::SEND_WRITE);
      thisSequence->addSlot(objects::MGM_1_RM3100_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::GET_WRITE);
      thisSequence->addSlot(objects::MGM_1_RM3100_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                            DeviceHandlerIF::SEND_READ);
      thisSequence->addSlot(objects::MGM_1_RM3100_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                            DeviceHandlerIF::GET_READ);
    }
    if (enableBside) {
      // B side
      thisSequence->addSlot(objects::MGM_2_LIS3_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::PERFORM_OPERATION);
      thisSequence->addSlot(objects::MGM_2_LIS3_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::SEND_WRITE);
      thisSequence->addSlot(objects::MGM_2_LIS3_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::GET_WRITE);
      thisSequence->addSlot(objects::MGM_2_LIS3_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                            DeviceHandlerIF::SEND_READ);
      thisSequence->addSlot(objects::MGM_2_LIS3_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                            DeviceHandlerIF::GET_READ);

      thisSequence->addSlot(objects::MGM_3_RM3100_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::PERFORM_OPERATION);
      thisSequence->addSlot(objects::MGM_3_RM3100_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::SEND_WRITE);
      thisSequence->addSlot(objects::MGM_3_RM3100_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::GET_WRITE);
      thisSequence->addSlot(objects::MGM_3_RM3100_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                            DeviceHandlerIF::SEND_READ);
      thisSequence->addSlot(objects::MGM_3_RM3100_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                            DeviceHandlerIF::GET_READ);
    }
    if (enableAside) {
      thisSequence->addSlot(objects::GYRO_0_ADIS_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::PERFORM_OPERATION);
      thisSequence->addSlot(objects::GYRO_0_ADIS_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::SEND_WRITE);
      thisSequence->addSlot(objects::GYRO_0_ADIS_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::GET_WRITE);
      thisSequence->addSlot(objects::GYRO_0_ADIS_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                            DeviceHandlerIF::SEND_READ);
      thisSequence->addSlot(objects::GYRO_0_ADIS_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                            DeviceHandlerIF::GET_READ);

      thisSequence->addSlot(objects::GYRO_1_L3G_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::PERFORM_OPERATION);
      thisSequence->addSlot(objects::GYRO_1_L3G_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::SEND_WRITE);
      thisSequence->addSlot(objects::GYRO_1_L3G_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::GET_WRITE);
      thisSequence->addSlot(objects::GYRO_1_L3G_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                            DeviceHandlerIF::SEND_READ);
      thisSequence->addSlot(objects::GYRO_1_L3G_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                            DeviceHandlerIF::GET_READ);
    }
    if (enableBside) {
      thisSequence->addSlot(objects::GYRO_2_ADIS_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::PERFORM_OPERATION);
      thisSequence->addSlot(objects::GYRO_2_ADIS_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::SEND_WRITE);
      thisSequence->addSlot(objects::GYRO_2_ADIS_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::GET_WRITE);
      thisSequence->addSlot(objects::GYRO_2_ADIS_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                            DeviceHandlerIF::SEND_READ);
      thisSequence->addSlot(objects::GYRO_2_ADIS_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                            DeviceHandlerIF::GET_READ);

      thisSequence->addSlot(objects::GYRO_3_L3G_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::PERFORM_OPERATION);
      thisSequence->addSlot(objects::GYRO_3_L3G_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::SEND_WRITE);
      thisSequence->addSlot(objects::GYRO_3_L3G_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_2_PERIOD,
                            DeviceHandlerIF::GET_WRITE);
      thisSequence->addSlot(objects::GYRO_3_L3G_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                            DeviceHandlerIF::SEND_READ);
      thisSequence->addSlot(objects::GYRO_3_L3G_HANDLER,
                            length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                            DeviceHandlerIF::GET_READ);
    }
  }

  if (cfg.scheduleImtq) {
    // This is the MTM measurement cycle
    thisSequence->addSlot(objects::IMTQ_HANDLER, length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          imtq::ComStep::DHB_OP);
    thisSequence->addSlot(objects::IMTQ_HANDLER, length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          imtq::ComStep::START_MEASURE_SEND);
    thisSequence->addSlot(objects::IMTQ_HANDLER, length * config::spiSched::SCHED_BLOCK_1_PERIOD,
                          imtq::ComStep::START_MEASURE_GET);
    thisSequence->addSlot(objects::IMTQ_HANDLER, length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                          imtq::ComStep::READ_MEASURE_SEND);
    thisSequence->addSlot(objects::IMTQ_HANDLER, length * config::spiSched::SCHED_BLOCK_3_PERIOD,
                          imtq::ComStep::READ_MEASURE_GET);
  }

  thisSequence->addSlot(objects::ACS_CONTROLLER, length * config::spiSched::SCHED_BLOCK_4_PERIOD,
                        0);

  if (cfg.scheduleImtq) {
    // This is the torquing cycle.
    thisSequence->addSlot(objects::IMTQ_HANDLER, length * config::spiSched::SCHED_BLOCK_5_PERIOD,
                          imtq::ComStep::START_ACTUATE_SEND);
    thisSequence->addSlot(objects::IMTQ_HANDLER, length * config::spiSched::SCHED_BLOCK_5_PERIOD,
                          imtq::ComStep::START_ACTUATE_GET);
    thisSequence->addSlot(objects::IMTQ_HANDLER, length * config::spiSched::SCHED_BLOCK_6_PERIOD,
                          imtq::ComStep::READ_ACTUATE_SEND);
    thisSequence->addSlot(objects::IMTQ_HANDLER, length * config::spiSched::SCHED_BLOCK_6_PERIOD,
                          imtq::ComStep::READ_ACTUATE_GET);
  }

  if (cfg.scheduleRws) {
    // this is the torquing cycle
    thisSequence->addSlot(objects::RW1, length * config::spiSched::SCHED_BLOCK_5_PERIOD,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::RW2, length * config::spiSched::SCHED_BLOCK_5_PERIOD,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::RW3, length * config::spiSched::SCHED_BLOCK_5_PERIOD,
                          DeviceHandlerIF::PERFORM_OPERATION);
    thisSequence->addSlot(objects::RW4, length * config::spiSched::SCHED_BLOCK_5_PERIOD,
                          DeviceHandlerIF::PERFORM_OPERATION);

    thisSequence->addSlot(objects::RW1, length * config::spiSched::SCHED_BLOCK_5_PERIOD,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::RW2, length * config::spiSched::SCHED_BLOCK_5_PERIOD,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::RW3, length * config::spiSched::SCHED_BLOCK_5_PERIOD,
                          DeviceHandlerIF::SEND_WRITE);
    thisSequence->addSlot(objects::RW4, length * config::spiSched::SCHED_BLOCK_5_PERIOD,
                          DeviceHandlerIF::SEND_WRITE);

    thisSequence->addSlot(objects::RW1, length * config::spiSched::SCHED_BLOCK_5_PERIOD,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::RW2, length * config::spiSched::SCHED_BLOCK_5_PERIOD,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::RW3, length * config::spiSched::SCHED_BLOCK_5_PERIOD,
                          DeviceHandlerIF::GET_WRITE);
    thisSequence->addSlot(objects::RW4, length * config::spiSched::SCHED_BLOCK_5_PERIOD,
                          DeviceHandlerIF::GET_WRITE);

    thisSequence->addSlot(objects::RW1, length * config::spiSched::SCHED_BLOCK_7_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::RW2, length * config::spiSched::SCHED_BLOCK_7_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::RW3, length * config::spiSched::SCHED_BLOCK_7_PERIOD,
                          DeviceHandlerIF::SEND_READ);
    thisSequence->addSlot(objects::RW4, length * config::spiSched::SCHED_BLOCK_7_PERIOD,
                          DeviceHandlerIF::SEND_READ);

    thisSequence->addSlot(objects::RW1, length * config::spiSched::SCHED_BLOCK_7_PERIOD,
                          DeviceHandlerIF::GET_READ);
    thisSequence->addSlot(objects::RW2, length * config::spiSched::SCHED_BLOCK_7_PERIOD,
                          DeviceHandlerIF::GET_READ);
    thisSequence->addSlot(objects::RW3, length * config::spiSched::SCHED_BLOCK_7_PERIOD,
                          DeviceHandlerIF::GET_READ);
    thisSequence->addSlot(objects::RW4, length * config::spiSched::SCHED_BLOCK_7_PERIOD,
                          DeviceHandlerIF::GET_READ);
  }

  thisSequence->addSlot(objects::SPI_RTD_COM_IF, length * config::spiSched::SCHED_BLOCK_RTD_PERIOD,
                        0);

  thisSequence->addSlot(objects::PLPCDU_HANDLER, length * config::spiSched::SCHED_BLOCK_8_PERIOD,
                        DeviceHandlerIF::PERFORM_OPERATION);
  thisSequence->addSlot(objects::PLPCDU_HANDLER, length * config::spiSched::SCHED_BLOCK_8_PERIOD,
                        DeviceHandlerIF::SEND_WRITE);
  thisSequence->addSlot(objects::PLPCDU_HANDLER, length * config::spiSched::SCHED_BLOCK_8_PERIOD,
                        DeviceHandlerIF::GET_WRITE);
  thisSequence->addSlot(objects::PLPCDU_HANDLER, length * config::spiSched::SCHED_BLOCK_8_PERIOD,
                        DeviceHandlerIF::SEND_READ);
  thisSequence->addSlot(objects::PLPCDU_HANDLER, length * config::spiSched::SCHED_BLOCK_8_PERIOD,
                        DeviceHandlerIF::GET_READ);
  thisSequence->addSlot(objects::BPX_BATT_HANDLER, length * config::spiSched::SCHED_BLOCK_8_PERIOD,
                        DeviceHandlerIF::PERFORM_OPERATION);
  thisSequence->addSlot(objects::BPX_BATT_HANDLER, length * config::spiSched::SCHED_BLOCK_8_PERIOD,
                        DeviceHandlerIF::SEND_WRITE);
  thisSequence->addSlot(objects::BPX_BATT_HANDLER, length * config::spiSched::SCHED_BLOCK_8_PERIOD,
                        DeviceHandlerIF::GET_WRITE);
  thisSequence->addSlot(objects::BPX_BATT_HANDLER, length * config::spiSched::SCHED_BLOCK_9_PERIOD,
                        DeviceHandlerIF::SEND_READ);
  thisSequence->addSlot(objects::BPX_BATT_HANDLER, length * config::spiSched::SCHED_BLOCK_9_PERIOD,
                        DeviceHandlerIF::GET_READ);

  /* Radiation sensor */
  thisSequence->addSlot(objects::RAD_SENSOR, length * config::spiSched::SCHED_BLOCK_9_PERIOD,
                        DeviceHandlerIF::PERFORM_OPERATION);
  thisSequence->addSlot(objects::RAD_SENSOR, length * config::spiSched::SCHED_BLOCK_9_PERIOD,
                        DeviceHandlerIF::SEND_WRITE);
  thisSequence->addSlot(objects::RAD_SENSOR, length * config::spiSched::SCHED_BLOCK_9_PERIOD,
                        DeviceHandlerIF::GET_WRITE);
  thisSequence->addSlot(objects::RAD_SENSOR, length * config::spiSched::SCHED_BLOCK_9_PERIOD,
                        DeviceHandlerIF::SEND_READ);
  thisSequence->addSlot(objects::RAD_SENSOR, length * config::spiSched::SCHED_BLOCK_9_PERIOD,
                        DeviceHandlerIF::GET_READ);

  thisSequence->addSlot(objects::POWER_CONTROLLER, length * config::spiSched::SCHED_BLOCK_10_PERIOD,
                        0);
  return returnvalue::OK;
}
ReturnValue_t pst::pstPayload(FixedTimeslotTaskIF *thisSequence) {
  uint32_t length = thisSequence->getPeriodMs();

  thisSequence->addSlot(objects::CAM_SWITCHER, length * 0, DeviceHandlerIF::PERFORM_OPERATION);
  thisSequence->addSlot(objects::PLOC_MPSOC_HANDLER, length * 0,
                        DeviceHandlerIF::PERFORM_OPERATION);
  thisSequence->addSlot(objects::PLOC_MPSOC_HANDLER, length * 0, DeviceHandlerIF::SEND_WRITE);
  thisSequence->addSlot(objects::PLOC_MPSOC_HANDLER, length * 0, DeviceHandlerIF::GET_WRITE);
  thisSequence->addSlot(objects::PLOC_MPSOC_HANDLER, length * 0, DeviceHandlerIF::SEND_READ);
  thisSequence->addSlot(objects::PLOC_MPSOC_HANDLER, length * 0, DeviceHandlerIF::GET_READ);

  thisSequence->addSlot(objects::PLOC_SUPERVISOR_HANDLER, length * 0,
                        FreshSupvHandler::OpCode::DEFAULT_OPERATION);
  // Two COM TM steps, which might cover telemetry which takes a bit longer to be sent.
  thisSequence->addSlot(objects::PLOC_SUPERVISOR_HANDLER, length * 0.1,
                        FreshSupvHandler::OpCode::PARSE_TM);
  thisSequence->addSlot(objects::PLOC_SUPERVISOR_HANDLER, length * 0.2,
                        FreshSupvHandler::OpCode::PARSE_TM);

  thisSequence->addSlot(objects::SCEX, length * 0.6, DeviceHandlerIF::PERFORM_OPERATION);
  thisSequence->addSlot(objects::SCEX, length * 0.6, DeviceHandlerIF::SEND_WRITE);
  thisSequence->addSlot(objects::SCEX, length * 0.6, DeviceHandlerIF::GET_WRITE);
  thisSequence->addSlot(objects::SCEX, length * 0.6, DeviceHandlerIF::SEND_READ);
  thisSequence->addSlot(objects::SCEX, length * 0.6, DeviceHandlerIF::GET_READ);
  thisSequence->addSlot(objects::SCEX, length * 0.8, DeviceHandlerIF::SEND_READ);
  thisSequence->addSlot(objects::SCEX, length * 0.8, DeviceHandlerIF::GET_READ);
  return thisSequence->checkSequence();
}