diff --git a/Core/Inc/params.h b/Core/Inc/params.h index 2fe6ad8..83e4b72 100644 --- a/Core/Inc/params.h +++ b/Core/Inc/params.h @@ -5,8 +5,14 @@ extern "C" { #endif +#include #include +#include "util.h" + +CountedEnum(ParamType, size_t, PF_BBAL, PF_TC1, PF_TC2, PF_TORQUEMAP, PF_TEST1, + PF_TEST2, PF_TEST3, PF_TEST4); + typedef struct { float bbal; unsigned tc1; @@ -17,6 +23,11 @@ typedef struct { extern Params params; +void params_init(); +void params_inc(ParamType param); +void params_dec(ParamType param); +void params_broadcast(ParamType param); + #ifdef __cplusplus } #endif diff --git a/Core/Inc/util.h b/Core/Inc/util.h new file mode 100644 index 0000000..c7e76b4 --- /dev/null +++ b/Core/Inc/util.h @@ -0,0 +1,11 @@ +#ifndef INC_UTIL_H +#define INC_UTIL_H + +// This is wacky preprocessor magic that allows us to count the number of +// members of an enum. Unfortunately, it doesn't work with enum classes, so we +// have to use C-style enums. +#define CountedEnum(NAME, TYPE, ...) \ + typedef enum { __VA_ARGS__ } NAME; \ + static const size_t NAME##_COUNT = sizeof((int[]){__VA_ARGS__}) / sizeof(int); + +#endif // INC_UTIL_H diff --git a/Core/Inc/vehicle.h b/Core/Inc/vehicle.h index 0f64344..a5be70e 100644 --- a/Core/Inc/vehicle.h +++ b/Core/Inc/vehicle.h @@ -1,6 +1,7 @@ #ifndef __INC_VEHICLE_H #define __INC_VEHICLE_H +#include "params.h" #include "stw_defines.h" #include "tx_port.h" @@ -106,6 +107,8 @@ void vehicle_thread_entry(ULONG hfdcan_addr); void vehicle_select_mission(Mission mission); +void vehicle_broadcast_param(ParamType param, int32_t value); + #ifdef __cplusplus } #endif diff --git a/Core/Src/main.c b/Core/Src/main.c index 0109b91..42b9a6a 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -27,6 +27,7 @@ #include "ft_logo_rainbow_rgb565.h" #include "hx8357d.h" #include "leds.h" +#include "params.h" #include "shorttimer.h" #include @@ -144,6 +145,7 @@ int main(void) { if (HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4) != HAL_OK) { Error_Handler(); } + params_init(); shorttimer_init(htim_us); led_init(&hspi3, &htim1); diff --git a/Core/Src/params.c b/Core/Src/params.c index 529ce56..b6bc0db 100644 --- a/Core/Src/params.c +++ b/Core/Src/params.c @@ -1,3 +1,118 @@ #include "params.h" +#include "can-halal.h" +#include "vehicle.h" Params params = {0}; + +void params_init() { params.bbal = 50; } + +void params_inc(ParamType param) { + switch (param) { + case PF_BBAL: + params.bbal += 0.5f; + if (params.bbal > 100.0f) { + params.bbal = 100.0f; + } + break; + case PF_TC1: + params.tc1++; + break; + case PF_TC2: + params.tc2++; + break; + case PF_TORQUEMAP: + params.torque_map++; + break; + case PF_TEST1: + params.test[0]++; + break; + case PF_TEST2: + params.test[1]++; + break; + case PF_TEST3: + params.test[2]++; + break; + case PF_TEST4: + params.test[3]++; + break; + } +} + +void params_dec(ParamType param) { + switch (param) { + case PF_BBAL: + params.bbal -= 0.5f; + if (params.bbal < 0.0f) { + params.bbal = 0.0f; + } + break; + case PF_TC1: + if (params.tc1 > 0) { + params.tc1--; + } + break; + case PF_TC2: + if (params.tc2 > 0) { + params.tc2--; + } + break; + case PF_TORQUEMAP: + if (params.torque_map > 0) { + params.torque_map--; + } + break; + case PF_TEST1: + if (params.test[0] > 0) { + params.test[0]--; + } + break; + case PF_TEST2: + if (params.test[1] > 0) { + params.test[1]--; + } + break; + case PF_TEST3: + if (params.test[2] > 0) { + params.test[2]--; + } + break; + case PF_TEST4: + if (params.test[3] > 0) { + params.test[3]--; + } + break; + } +} + +void params_broadcast(ParamType param) { + int32_t value; + switch (param) { + case PF_BBAL: + value = params.bbal * 10; + break; + case PF_TC1: + value = params.tc1; + break; + case PF_TC2: + value = params.tc2; + break; + case PF_TORQUEMAP: + value = params.torque_map; + break; + case PF_TEST1: + value = params.test[0]; + break; + case PF_TEST2: + value = params.test[1]; + break; + case PF_TEST3: + value = params.test[2]; + break; + case PF_TEST4: + value = params.test[3]; + break; + default: + return; + } + vehicle_broadcast_param(param, value); +} diff --git a/Core/Src/vehicle.c b/Core/Src/vehicle.c index 292cadb..eded09d 100644 --- a/Core/Src/vehicle.c +++ b/Core/Src/vehicle.c @@ -13,6 +13,7 @@ #define CAN_ID_AMS_STATUS 0xA #define CAN_ID_MISSION_SELECTED 0x400 +#define CAN_ID_STW_PARAM_SET 0x402 #define CAN_ID_AS_MISSION_FB 0x410 #define CAN_ID_STW_STATUS 0x412 #define CAN_ID_SHUNT_CURRENT 0x521 @@ -33,8 +34,6 @@ void vehicle_thread_entry(ULONG hfdcan_addr) { ftcan_add_filter(CAN_ID_SHUNT_VOLTAGE3, 0x7FF); while (1) { - uint8_t data[] = {0xFF, 0xEE}; - ftcan_transmit(0x456, data, 2); tx_thread_sleep(10); } } @@ -44,6 +43,14 @@ void vehicle_select_mission(Mission mission) { ftcan_transmit(CAN_ID_MISSION_SELECTED, &mission_int, 1); } +void vehicle_broadcast_param(ParamType param, int32_t value) { + uint8_t data[5]; + uint8_t *ptr = data; + ptr = ftcan_marshal_unsigned(ptr, param, 1); + ptr = ftcan_marshal_signed(ptr, value, 4); + ftcan_transmit(CAN_ID_STW_PARAM_SET, data, 5); +} + void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data) { switch (id) { case CAN_ID_AMS_STATUS: diff --git a/TouchGFX/gui/include/gui/common/NamedField.hpp b/TouchGFX/gui/include/gui/common/NamedField.hpp index c746e29..647fbab 100644 --- a/TouchGFX/gui/include/gui/common/NamedField.hpp +++ b/TouchGFX/gui/include/gui/common/NamedField.hpp @@ -6,13 +6,7 @@ #include #include -// 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 }; diff --git a/TouchGFX/gui/include/gui/containers/ConfigItem.hpp b/TouchGFX/gui/include/gui/containers/ConfigItem.hpp index 89c436c..4bcc82a 100644 --- a/TouchGFX/gui/include/gui/containers/ConfigItem.hpp +++ b/TouchGFX/gui/include/gui/containers/ConfigItem.hpp @@ -4,7 +4,9 @@ #include "gui/common/NamedField.hpp" #include -class ConfigItem : public ConfigItemBase, public NamedField { +#include "params.h" + +class ConfigItem : public ConfigItemBase, public NamedField { public: ConfigItem(); virtual ~ConfigItem() {} diff --git a/TouchGFX/gui/src/common/NamedField.cpp b/TouchGFX/gui/src/common/NamedField.cpp index 96fbca0..3c52f24 100644 --- a/TouchGFX/gui/src/common/NamedField.cpp +++ b/TouchGFX/gui/src/common/NamedField.cpp @@ -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 @@ -383,4 +412,4 @@ template void NamedField::updateValueBuffer() { } template class NamedField; -template class NamedField; +template class NamedField; diff --git a/TouchGFX/gui/src/vehicleconfig_screen/VehicleConfigView.cpp b/TouchGFX/gui/src/vehicleconfig_screen/VehicleConfigView.cpp index 24be303..52b8a39 100644 --- a/TouchGFX/gui/src/vehicleconfig_screen/VehicleConfigView.cpp +++ b/TouchGFX/gui/src/vehicleconfig_screen/VehicleConfigView.cpp @@ -1,6 +1,7 @@ #include "gui/common/NamedField.hpp" #include "gui/containers/ConfigItem.hpp" #include "main.h" +#include "params.h" #include #include @@ -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(itemIndex)); + item.setType(static_cast(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(selectedParam)); + params.itemChanged(selectedParam); } void VehicleConfigView::incParam() { - // TODO: How to handle this for different parameter types? + params_inc(static_cast(selectedParam)); + params.itemChanged(selectedParam); } void VehicleConfigView::confirmParam() { - // TODO: How to handle this for different parameter types? + params_broadcast(static_cast(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); }