Modify & transmit params via CAN
This commit is contained in:
		@ -6,13 +6,7 @@
 | 
			
		||||
#include <stddef.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
// We want to automatically count the number of data field types for the
 | 
			
		||||
// description array. This is wacky preprocessor magic that allows us to do just
 | 
			
		||||
// that. Unfortunately, it doesn't work with enum classes, so we have to use
 | 
			
		||||
// C-style enums.
 | 
			
		||||
#define CountedEnum(NAME, TYPE, ...)                                           \
 | 
			
		||||
  enum NAME : TYPE { __VA_ARGS__ };                                            \
 | 
			
		||||
  constexpr size_t NAME##_COUNT = sizeof((int[]){__VA_ARGS__}) / sizeof(int);
 | 
			
		||||
#include "util.h"
 | 
			
		||||
 | 
			
		||||
CountedEnum(DataFieldType, size_t, DF_TSState, DF_ASState, DF_ActiveMission,
 | 
			
		||||
            DF_R2DProgress, DF_INVLReady, DF_INVRReady, DF_SDC, DF_ERR,
 | 
			
		||||
@ -20,8 +14,6 @@ CountedEnum(DataFieldType, size_t, DF_TSState, DF_ASState, DF_ActiveMission,
 | 
			
		||||
            DF_TireTempRL, DF_TireTempRR, DF_MinCellVolt, DF_MaxCellTemp,
 | 
			
		||||
            DF_TSSoC, DF_LVSoC, DF_TSCurrent, DF_TSVoltageBat, DF_TSVoltageVeh,
 | 
			
		||||
            DF_Speed, DF_BBal);
 | 
			
		||||
CountedEnum(ParamFieldType, size_t, PF_BBAL, PF_TC1, PF_TC2, PF_TORQUEMAP,
 | 
			
		||||
            PF_TEST1, PF_TEST2, PF_TEST3, PF_TEST4);
 | 
			
		||||
 | 
			
		||||
enum class NamedFieldKind { Float, Bool, Text, Int };
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -4,7 +4,9 @@
 | 
			
		||||
#include "gui/common/NamedField.hpp"
 | 
			
		||||
#include <gui_generated/containers/ConfigItemBase.hpp>
 | 
			
		||||
 | 
			
		||||
class ConfigItem : public ConfigItemBase, public NamedField<ParamFieldType> {
 | 
			
		||||
#include "params.h"
 | 
			
		||||
 | 
			
		||||
class ConfigItem : public ConfigItemBase, public NamedField<ParamType> {
 | 
			
		||||
public:
 | 
			
		||||
  ConfigItem();
 | 
			
		||||
  virtual ~ConfigItem() {}
 | 
			
		||||
 | 
			
		||||
@ -278,6 +278,35 @@ static_assert(sizeof(dataFieldDescs) / sizeof(dataFieldDescs[0]) ==
 | 
			
		||||
 | 
			
		||||
#define PARAM_FIELD(FIELD) []() { return (void *)¶ms.FIELD; }
 | 
			
		||||
 | 
			
		||||
void inc_bbal(void *valPtr) {
 | 
			
		||||
  // float *val = (float *)valPtr;
 | 
			
		||||
  // *val += 0.1f;
 | 
			
		||||
  // if (*val > 100.0f) {
 | 
			
		||||
  //   *val = 100.0f;
 | 
			
		||||
  // }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void dec_bbal(void *valPtr) {
 | 
			
		||||
  // float *val = (float *)valPtr;
 | 
			
		||||
  // *val -= 0.1f;
 | 
			
		||||
  // if (*val < 0.0f) {
 | 
			
		||||
  //   *val = 0.0f;
 | 
			
		||||
  // }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void inc_int(void *valPtr) {
 | 
			
		||||
  // int *val = (int *)valPtr;
 | 
			
		||||
  // *val += 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void dec_int(void *valPtr) {
 | 
			
		||||
  // int *val = (int *)valPtr;
 | 
			
		||||
  // *val -= 1;
 | 
			
		||||
  // if (*val < 0) {
 | 
			
		||||
  //   *val = 0;
 | 
			
		||||
  // }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
NamedFieldDescription paramFieldDescs[] = {
 | 
			
		||||
    [PF_BBAL] = {NamedFieldKind::Float, "BBAL", 2, 1, PARAM_FIELD(bbal)},
 | 
			
		||||
    [PF_TC1] = {NamedFieldKind::Int, "TC1", 2, 0, PARAM_FIELD(tc1)},
 | 
			
		||||
@ -291,7 +320,7 @@ NamedFieldDescription paramFieldDescs[] = {
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static_assert(sizeof(paramFieldDescs) / sizeof(paramFieldDescs[0]) ==
 | 
			
		||||
                  ParamFieldType_COUNT,
 | 
			
		||||
                  ParamType_COUNT,
 | 
			
		||||
              "Incorrect number of param field descriptions");
 | 
			
		||||
 | 
			
		||||
template <class T>
 | 
			
		||||
@ -383,4 +412,4 @@ template <class T> void NamedField<T>::updateValueBuffer() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template class NamedField<DataFieldType>;
 | 
			
		||||
template class NamedField<ParamFieldType>;
 | 
			
		||||
template class NamedField<ParamType>;
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,7 @@
 | 
			
		||||
#include "gui/common/NamedField.hpp"
 | 
			
		||||
#include "gui/containers/ConfigItem.hpp"
 | 
			
		||||
#include "main.h"
 | 
			
		||||
#include "params.h"
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <gui/vehicleconfig_screen/VehicleConfigView.hpp>
 | 
			
		||||
 | 
			
		||||
@ -8,7 +9,7 @@ VehicleConfigView::VehicleConfigView() : selectedParam{0} {}
 | 
			
		||||
 | 
			
		||||
void VehicleConfigView::setupScreen() {
 | 
			
		||||
  VehicleConfigViewBase::setupScreen();
 | 
			
		||||
  params.setNumberOfItems(ParamFieldType_COUNT);
 | 
			
		||||
  params.setNumberOfItems(ParamType_COUNT);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void VehicleConfigView::tearDownScreen() {
 | 
			
		||||
@ -16,31 +17,33 @@ void VehicleConfigView::tearDownScreen() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void VehicleConfigView::paramsUpdateItem(ConfigItem &item, int16_t itemIndex) {
 | 
			
		||||
  item.setType(static_cast<ParamFieldType>(itemIndex));
 | 
			
		||||
  item.setType(static_cast<ParamType>(itemIndex));
 | 
			
		||||
  item.setSelected(itemIndex == selectedParam);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void VehicleConfigView::selectPrevParam() {
 | 
			
		||||
  if (selectedParam == 0) {
 | 
			
		||||
    updateSelectedParam(ParamFieldType_COUNT - 1);
 | 
			
		||||
    updateSelectedParam(ParamType_COUNT - 1);
 | 
			
		||||
  } else {
 | 
			
		||||
    updateSelectedParam(selectedParam - 1);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void VehicleConfigView::selectNextParam() {
 | 
			
		||||
  updateSelectedParam((selectedParam + 1) % ParamFieldType_COUNT);
 | 
			
		||||
  updateSelectedParam((selectedParam + 1) % ParamType_COUNT);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void VehicleConfigView::decParam() {
 | 
			
		||||
  // TODO: How to handle this for different parameter types?
 | 
			
		||||
  params_dec(static_cast<ParamType>(selectedParam));
 | 
			
		||||
  params.itemChanged(selectedParam);
 | 
			
		||||
}
 | 
			
		||||
void VehicleConfigView::incParam() {
 | 
			
		||||
  // TODO: How to handle this for different parameter types?
 | 
			
		||||
  params_inc(static_cast<ParamType>(selectedParam));
 | 
			
		||||
  params.itemChanged(selectedParam);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void VehicleConfigView::confirmParam() {
 | 
			
		||||
  // TODO: How to handle this for different parameter types?
 | 
			
		||||
  params_broadcast(static_cast<ParamType>(selectedParam));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void VehicleConfigView::updateSelectedParam(int select) {
 | 
			
		||||
@ -81,7 +84,7 @@ void VehicleConfigView::updateSelectedParam(int select) {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  int16_t firstWanted;
 | 
			
		||||
  if (selectedParam == 0 && previousSelected == ParamFieldType_COUNT - 1) {
 | 
			
		||||
  if (selectedParam == 0 && previousSelected == ParamType_COUNT - 1) {
 | 
			
		||||
    firstWanted = 0;
 | 
			
		||||
  } else if (selectedParam > previousSelected) {
 | 
			
		||||
    firstWanted = selectedParam - numVisible + 1;
 | 
			
		||||
@ -90,8 +93,8 @@ void VehicleConfigView::updateSelectedParam(int select) {
 | 
			
		||||
  }
 | 
			
		||||
  if (firstWanted < 0) {
 | 
			
		||||
    firstWanted = 0;
 | 
			
		||||
  } else if (firstWanted > ParamFieldType_COUNT - 1) {
 | 
			
		||||
    firstWanted = ParamFieldType_COUNT - 1;
 | 
			
		||||
  } else if (firstWanted > ParamType_COUNT - 1) {
 | 
			
		||||
    firstWanted = ParamType_COUNT - 1;
 | 
			
		||||
  }
 | 
			
		||||
  params.animateToItem(firstWanted, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user