diff --git a/Core/Inc/params.h b/Core/Inc/params.h index a3050e6..4045086 100644 --- a/Core/Inc/params.h +++ b/Core/Inc/params.h @@ -10,17 +10,15 @@ extern "C" { #include "util.h" -CountedEnum(ParamType, size_t, PF_BBAL, PF_SLIPREF, PF_MUMAX, PF_ASRP, PF_ASRON, - PF_ASRI, PF_PLIM); +CountedEnum(ParamType, size_t, PF_PLIM, PF_TLIM, PF_SLIM, PF_TVEC, PF_PG, PF_REKU); typedef struct { - float bbal; - float slipref; - float mumax; - unsigned asrp; - unsigned asri; - unsigned asron; - unsigned plim; + unsigned plim; //< Power limit + unsigned tlim; //< Torque limit + unsigned slim; //< Speed limit + unsigned tvec; //< Torque vectoring + unsigned pg; //< Power ground + unsigned reku; //< Rekuperation } Params; extern Params params; diff --git a/Core/Src/params.c b/Core/Src/params.c index 8f8cdeb..807c5fa 100644 --- a/Core/Src/params.c +++ b/Core/Src/params.c @@ -2,103 +2,98 @@ #include "can-halal.h" #include "vehicle.h" +/** + * Decrements the given value if it is above the minimum allowed value + */ +#define DEC_IF_ABOVE(param_val, min_val, decr_amt) ((param_val) = ((param_val) - (decr_amt) ) > (min_val) ? ((param_val) - (decr_amt)) : (min_val)) +#define INC_IF_BELOW(param_val, max_val, incr_amt) ((param_val) = ((param_val) + (incr_amt)) > (max_val) ? ((param_val) + (incr_amt)) : (max_val)) + Params params = {0}; -void params_init() { - params.bbal = 50; +void params_init() +{ + // Default values params.plim = 20; + params.tlim = 1400; + params.slim = 70; + params.tvec = 50; + params.pg = 0; + params.reku = 0; } -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_SLIPREF: - params.slipref += 0.01f; - break; - case PF_MUMAX: - params.mumax += 0.1f; - break; - case PF_ASRP: - params.asrp++; - break; - case PF_ASRI: - params.asri++; - break; - case PF_ASRON: - params.asron = 1; - break; +void params_inc(ParamType param) +{ + switch (param) + { case PF_PLIM: - params.plim = (params.plim < 80) ? params.plim + 1 : 80; + INC_IF_BELOW(params.plim, 80, 1); + break; + case PF_TLIM: + INC_IF_BELOW(params.tlim, 1500, 100); + break; + case PF_SLIM: + INC_IF_BELOW(params.slim, 100, 1); + break; + case PF_TVEC: + INC_IF_BELOW(params.tvec, 100, 1); + break; + case PF_PG: + INC_IF_BELOW(params.pg, 100, 1); + break; + case PF_REKU: + INC_IF_BELOW(params.reku, 100, 1); 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_SLIPREF: - if (params.slipref > 0) { - params.slipref -= 0.01f; - } - break; - case PF_MUMAX: - if (params.mumax > 0) { - params.mumax -= 0.1f; - } - break; - case PF_ASRP: - if (params.asrp > 0) { - params.asrp--; - } - break; - case PF_ASRI: - if (params.asri > 0) { - params.asri--; - } - break; - case PF_ASRON: - params.asron = 0; - break; +void params_dec(ParamType param) +{ + switch (param) + { case PF_PLIM: - params.plim = (params.plim > 2) ? params.plim - 1 : 2; + DEC_IF_ABOVE(params.plim, 0, 1); + break; + case PF_TLIM: + DEC_IF_ABOVE(params.tlim, 0, 100); + break; + case PF_SLIM: + DEC_IF_ABOVE(params.slim, 0, 1); + break; + case PF_TVEC: + DEC_IF_ABOVE(params.tvec, 0, 1); + break; + case PF_PG: + DEC_IF_ABOVE(params.pg, 0, 1); + break; + case PF_REKU: + DEC_IF_ABOVE(params.reku, 0, 1); break; } } -void params_broadcast(ParamType param) { +void params_broadcast(ParamType param) +{ int32_t value; - switch (param) { - case PF_BBAL: - value = params.bbal * 10; - break; - case PF_SLIPREF: - value = params.slipref * 100; - break; - case PF_MUMAX: - value = params.mumax * 10; - break; - case PF_ASRP: - value = params.asrp; - break; - case PF_ASRI: - value = params.asri; - break; - case PF_ASRON: - value = params.asron; - break; + switch (param) + { case PF_PLIM: value = params.plim; break; + case PF_TLIM: + value = params.tlim; + break; + case PF_SLIM: + value = params.slim; + break; + case PF_TVEC: + value = params.tvec; + break; + case PF_PG: + value = params.pg; + break; + case PF_REKU: + value = params.reku; + break; default: return; } diff --git a/Core/Src/ui.c b/Core/Src/ui.c index 21e964c..49182c8 100644 --- a/Core/Src/ui.c +++ b/Core/Src/ui.c @@ -12,6 +12,10 @@ #include "leds.h" +#define DRS_BUTTON_IDX (6) +#define DRS_PRESS_WAIT_CYCLES (10) +static drs_press_buf_cycles = 0; + void ui_thread_entry(ULONG _) { GPIO_TypeDef *button_ports[NUM_BUTTONS] = {BTN1_GPIO_Port, BTN2_GPIO_Port, BTN3_GPIO_Port, BTN4_GPIO_Port, @@ -46,12 +50,15 @@ void ui_thread_entry(ULONG _) { tx_event_flags_set(&gui_update_events, GUI_UPDATE_NEXT_SCREEN, TX_OR); } - if (button_states[6] == GPIO_PIN_SET) { + if (button_states[DRS_BUTTON_IDX] == GPIO_PIN_SET) { // Set leftmost led to blue to indicate DRS activation + drs_press_buf_cycles = DRS_PRESS_WAIT_CYCLES; led_set(0, 0, 0, 255); - } else { - // Turn it off if not active + } if (drs_press_buf_cycles < 0) { + // Assume no longer active, turn off led_set(0, 0, 0, 0); + } else if (drs_press_buf_cycles >= 0) { + drs_press_buf_cycles--; } vehicle_broadcast_buttons(button_states); diff --git a/TouchGFX/gui/src/common/NamedField.cpp b/TouchGFX/gui/src/common/NamedField.cpp index ce1e569..ffad97f 100644 --- a/TouchGFX/gui/src/common/NamedField.cpp +++ b/TouchGFX/gui/src/common/NamedField.cpp @@ -274,14 +274,12 @@ static_assert(sizeof(dataFieldDescs) / sizeof(dataFieldDescs[0]) == #define PARAM_FIELD(FIELD) []() { return (void *)¶ms.FIELD; } NamedFieldDescription paramFieldDescs[] = { - [PF_BBAL] = {NamedFieldKind::Float, "BBAL", 2, 1, PARAM_FIELD(bbal)}, - [PF_SLIPREF] = {NamedFieldKind::Float, "SLIPREF", 2, 2, - PARAM_FIELD(slipref)}, - [PF_MUMAX] = {NamedFieldKind::Float, "MUMAX", 2, 1, PARAM_FIELD(mumax)}, - [PF_ASRP] = {NamedFieldKind::Int, "ASR-P", 2, 0, PARAM_FIELD(asrp)}, - [PF_ASRON] = {NamedFieldKind::Int, "ASR-ON", 2, 0, PARAM_FIELD(asron)}, - [PF_ASRI] = {NamedFieldKind::Int, "ASR-I", 2, 0, PARAM_FIELD(asri)}, [PF_PLIM] = {NamedFieldKind::Int, "PLIM", 2, 0, PARAM_FIELD(plim)}, + [PF_TLIM] = {NamedFieldKind::Int, "TLIM", 2, 1, PARAM_FIELD(plim)}, + [PF_SLIM] = {NamedFieldKind::Int, "SLIM", 2, 2, PARAM_FIELD(slim)}, + [PF_TVEC] = {NamedFieldKind::Int, "TVEC", 2, 1, PARAM_FIELD(tvec)}, + [PF_PG] = {NamedFieldKind::Int, "PG", 2, 0, PARAM_FIELD(pg)}, + [PF_REKU] = {NamedFieldKind::Int, "REKU", 2, 0, PARAM_FIELD(reku)}, }; static_assert(sizeof(paramFieldDescs) / sizeof(paramFieldDescs[0]) ==