Skip to content

Commit

Permalink
Optimize menu editing by reducing template duplication
Browse files Browse the repository at this point in the history
Avoid instantiating multiple copies of menu_item_edit_P/_menu_edit_P:
perform type switching at runtime.
  • Loading branch information
wavexx committed Aug 1, 2023
1 parent 9220224 commit 719db8f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
25 changes: 13 additions & 12 deletions Firmware/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,7 @@ void menu_draw_float13(const char* str, float val)
lcd_printf_P(menu_fmt_float13, ' ', str, val);
}

template <typename T>
static void _menu_edit_P(void)
static void _menu_edit_P()
{
menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
if (lcd_draw_update)
Expand All @@ -487,28 +486,33 @@ static void _menu_edit_P(void)
}
if (lcd_clicked())
{
*((T)(_md->editValue)) = _md->currentValue;
if (_md->editValueBits == 8)
*((uint8_t*)(_md->editValuePtr)) = _md->currentValue;
else
*((int16_t*)(_md->editValuePtr)) = _md->currentValue;
menu_back_no_reset();
}
}

template <typename T>
void menu_item_edit_P(const char* str, T pval, int16_t min_val, int16_t max_val, int16_t jmp_val)
void menu_item_edit_P(const char* str, void* pval, uint8_t pbits, int16_t min_val, int16_t max_val, int16_t jmp_val)
{
menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
if (menu_item == menu_line)
{
int16_t cur_val = (pbits == 8 ? *((uint8_t*)pval) : *((int16_t*)pval));

if (lcd_draw_update)
{
lcd_set_cursor(0, menu_row);
menu_draw_P(menu_selection_mark(), str, *pval);
menu_draw_P(menu_selection_mark(), str, cur_val);
}
if (menu_clicked && (lcd_encoder == menu_item))
{
menu_submenu_no_reset(_menu_edit_P<T>);
menu_submenu_no_reset(_menu_edit_P);
_md->editLabel = str;
_md->editValue = pval;
_md->currentValue = *pval;
_md->editValuePtr = pval;
_md->editValueBits = pbits;
_md->currentValue = cur_val;
_md->minEditValue = min_val;
_md->maxEditValue = max_val;
_md->minJumpValue = jmp_val;
Expand All @@ -519,9 +523,6 @@ void menu_item_edit_P(const char* str, T pval, int16_t min_val, int16_t max_val,
menu_item++;
}

template void menu_item_edit_P<int16_t*>(const char* str, int16_t *pval, int16_t min_val, int16_t max_val, int16_t jmp_val);
template void menu_item_edit_P<uint8_t*>(const char* str, uint8_t *pval, int16_t min_val, int16_t max_val, int16_t jmp_val);

static uint8_t progressbar_block_count = 0;
static uint16_t progressbar_total = 0;
void menu_progressbar_init(uint16_t total, const char* title)
Expand Down
11 changes: 5 additions & 6 deletions Firmware/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ typedef struct
{
//Variables used when editing values.
const char* editLabel;
void* editValue;
uint8_t editValueBits; // 8 or 16
void* editValuePtr;
int16_t currentValue;
int16_t minEditValue;
int16_t maxEditValue;
Expand Down Expand Up @@ -144,11 +145,9 @@ struct SheetFormatBuffer

extern void menu_format_sheet_E(const Sheet &sheet_E, SheetFormatBuffer &buffer);


#define MENU_ITEM_EDIT_int3_P(str, pval, minval, maxval) do { menu_item_edit_P(str, pval, minval, maxval, 0); } while (0)
#define MENU_ITEM_EDIT_int3_jmp_P(str, pval, minval, maxval, jmpval) do { menu_item_edit_P(str, pval, minval, maxval, jmpval); } while (0)
template <typename T>
extern void menu_item_edit_P(const char* str, T pval, int16_t min_val, int16_t max_val, int16_t jmp_val);
#define MENU_ITEM_EDIT_int3_P(str, pval, minval, maxval) do { menu_item_edit_P(str, pval, sizeof(*pval)*8, minval, maxval, 0); } while (0)
#define MENU_ITEM_EDIT_int3_jmp_P(str, pval, minval, maxval, jmpval) do { menu_item_edit_P(str, pval, sizeof(*pval)*8, minval, maxval, jmpval); } while (0)
extern void menu_item_edit_P(const char* str, void* pval, uint8_t pbits, int16_t min_val, int16_t max_val, int16_t jmp_val);

extern void menu_progressbar_init(uint16_t total, const char* title);
extern void menu_progressbar_update(uint16_t newVal);
Expand Down

0 comments on commit 719db8f

Please sign in to comment.