2018-07-16 00:13:52 +00:00
|
|
|
//menu.cpp
|
|
|
|
|
|
|
|
#include "menu.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
#include "lcd.h"
|
2018-07-16 21:23:15 +00:00
|
|
|
#include "Configuration.h"
|
|
|
|
#include "Marlin.h"
|
2018-08-01 20:37:10 +00:00
|
|
|
#include "ultralcd.h"
|
2018-07-16 00:13:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
extern int32_t lcd_encoder;
|
|
|
|
|
|
|
|
|
|
|
|
menu_record_t menu_stack[MENU_DEPTH_MAX];
|
|
|
|
|
|
|
|
uint8_t menu_data[MENU_DATA_SIZE];
|
|
|
|
|
|
|
|
uint8_t menu_depth = 0;
|
|
|
|
|
|
|
|
uint8_t menu_line = 0;
|
|
|
|
uint8_t menu_item = 0;
|
|
|
|
uint8_t menu_row = 0;
|
|
|
|
uint8_t menu_top = 0;
|
|
|
|
|
|
|
|
uint8_t menu_clicked = 0;
|
|
|
|
|
2018-08-19 11:59:33 +00:00
|
|
|
uint8_t menu_entering = 0;
|
|
|
|
uint8_t menu_leaving = 0;
|
|
|
|
|
2018-07-16 00:13:52 +00:00
|
|
|
menu_func_t menu_menu = 0;
|
|
|
|
|
|
|
|
|
|
|
|
void menu_goto(menu_func_t menu, const uint32_t encoder, const bool feedback, bool reset_menu_state)
|
|
|
|
{
|
|
|
|
asm("cli");
|
|
|
|
if (menu_menu != menu)
|
|
|
|
{
|
|
|
|
menu_menu = menu;
|
|
|
|
lcd_encoder = encoder;
|
|
|
|
asm("sei");
|
|
|
|
if (reset_menu_state)
|
|
|
|
{
|
|
|
|
// Resets the global shared C union.
|
|
|
|
// This ensures, that the menu entered will find out, that it shall initialize itself.
|
|
|
|
memset(&menu_data, 0, sizeof(menu_data));
|
|
|
|
}
|
|
|
|
if (feedback) lcd_quick_feedback();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
asm("sei");
|
|
|
|
}
|
|
|
|
|
|
|
|
void menu_start(void)
|
|
|
|
{
|
|
|
|
if (lcd_encoder > 0x8000) lcd_encoder = 0;
|
|
|
|
if (lcd_encoder < 0) lcd_encoder = 0;
|
|
|
|
if (lcd_encoder < menu_top)
|
|
|
|
menu_top = lcd_encoder;
|
|
|
|
menu_line = menu_top;
|
|
|
|
menu_clicked = LCD_CLICKED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void menu_end(void)
|
|
|
|
{
|
|
|
|
if (lcd_encoder >= menu_item)
|
|
|
|
lcd_encoder = menu_item - 1;
|
|
|
|
if (((uint8_t)lcd_encoder) >= menu_top + LCD_HEIGHT)
|
|
|
|
{
|
|
|
|
menu_top = lcd_encoder - LCD_HEIGHT + 1;
|
|
|
|
lcd_draw_update = 1;
|
|
|
|
menu_line = menu_top - 1;
|
|
|
|
menu_row = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void menu_back(void)
|
|
|
|
{
|
2018-08-19 11:59:33 +00:00
|
|
|
if (menu_depth > 0)
|
|
|
|
{
|
2018-07-17 16:55:03 +00:00
|
|
|
menu_depth--;
|
|
|
|
menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, true, true);
|
|
|
|
}
|
2018-07-16 00:13:52 +00:00
|
|
|
}
|
|
|
|
|
2018-08-19 11:59:33 +00:00
|
|
|
void menu_back_no_reset(void)
|
|
|
|
{
|
|
|
|
if (menu_depth > 0)
|
|
|
|
{
|
|
|
|
menu_depth--;
|
|
|
|
menu_goto(menu_stack[menu_depth].menu, menu_stack[menu_depth].position, true, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 00:13:52 +00:00
|
|
|
void menu_back_if_clicked(void)
|
|
|
|
{
|
|
|
|
if (lcd_clicked())
|
|
|
|
menu_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void menu_back_if_clicked_fb(void)
|
|
|
|
{
|
|
|
|
if (lcd_clicked())
|
|
|
|
{
|
|
|
|
lcd_quick_feedback();
|
|
|
|
menu_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void menu_submenu(menu_func_t submenu)
|
|
|
|
{
|
|
|
|
if (menu_depth <= MENU_DEPTH_MAX)
|
|
|
|
{
|
|
|
|
menu_stack[menu_depth].menu = menu_menu;
|
|
|
|
menu_stack[menu_depth++].position = lcd_encoder;
|
|
|
|
menu_goto(submenu, 0, true, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-19 11:59:33 +00:00
|
|
|
void menu_submenu_no_reset(menu_func_t submenu)
|
|
|
|
{
|
|
|
|
if (menu_depth <= MENU_DEPTH_MAX)
|
|
|
|
{
|
|
|
|
menu_stack[menu_depth].menu = menu_menu;
|
|
|
|
menu_stack[menu_depth++].position = lcd_encoder;
|
|
|
|
menu_goto(submenu, 0, true, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 00:13:52 +00:00
|
|
|
uint8_t menu_item_ret(void)
|
|
|
|
{
|
2018-07-16 02:13:26 +00:00
|
|
|
lcd_beeper_quick_feedback();
|
2018-07-16 00:13:52 +00:00
|
|
|
lcd_draw_update = 2;
|
|
|
|
lcd_button_pressed = false;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-08-09 15:43:43 +00:00
|
|
|
int menu_draw_item_printf_P(char type_char, const char* format, ...)
|
2018-07-16 00:13:52 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
int ret = 0;
|
|
|
|
lcd_set_cursor(0, menu_row);
|
|
|
|
if (lcd_encoder == menu_item)
|
2018-07-16 17:29:27 +00:00
|
|
|
lcd_print('>');
|
2018-07-16 00:13:52 +00:00
|
|
|
else
|
2018-07-16 17:29:27 +00:00
|
|
|
lcd_print(' ');
|
2018-07-16 00:13:52 +00:00
|
|
|
int cnt = vfprintf_P(lcdout, format, args);
|
|
|
|
for (int i = cnt; i < 18; i++)
|
2018-07-16 17:29:27 +00:00
|
|
|
lcd_print(' ');
|
|
|
|
lcd_print(type_char);
|
2018-07-16 00:13:52 +00:00
|
|
|
va_end(args);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
*/
|
2018-08-09 15:43:43 +00:00
|
|
|
|
2018-07-16 00:13:52 +00:00
|
|
|
int menu_draw_item_puts_P(char type_char, const char* str)
|
|
|
|
{
|
|
|
|
lcd_set_cursor(0, menu_row);
|
|
|
|
int cnt = lcd_printf_P(PSTR("%c%-18S%c"), (lcd_encoder == menu_item)?'>':' ', str, type_char);
|
|
|
|
return cnt;
|
|
|
|
}
|
2018-08-09 15:43:43 +00:00
|
|
|
|
2018-07-16 00:13:52 +00:00
|
|
|
/*
|
|
|
|
int menu_draw_item_puts_P_int16(char type_char, const char* str, int16_t val, )
|
|
|
|
{
|
|
|
|
lcd_set_cursor(0, menu_row);
|
|
|
|
int cnt = lcd_printf_P(PSTR("%c%-18S%c"), (lcd_encoder == menu_item)?'>':' ', str, type_char);
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
*/
|
2018-08-09 15:43:43 +00:00
|
|
|
|
2018-07-16 00:13:52 +00:00
|
|
|
void menu_item_dummy(void)
|
|
|
|
{
|
|
|
|
menu_item++;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t menu_item_text_P(const char* str)
|
|
|
|
{
|
|
|
|
if (menu_item == menu_line)
|
|
|
|
{
|
|
|
|
if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
|
|
|
|
if (menu_clicked && (lcd_encoder == menu_item))
|
|
|
|
return menu_item_ret();
|
|
|
|
}
|
|
|
|
menu_item++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t menu_item_submenu_P(const char* str, menu_func_t submenu)
|
|
|
|
{
|
|
|
|
if (menu_item == menu_line)
|
|
|
|
{
|
|
|
|
if (lcd_draw_update) menu_draw_item_puts_P(LCD_STR_ARROW_RIGHT[0], str);
|
|
|
|
if (menu_clicked && (lcd_encoder == menu_item))
|
|
|
|
{
|
|
|
|
menu_submenu(submenu);
|
|
|
|
return menu_item_ret();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
menu_item++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t menu_item_back_P(const char* str)
|
|
|
|
{
|
|
|
|
if (menu_item == menu_line)
|
|
|
|
{
|
|
|
|
if (lcd_draw_update) menu_draw_item_puts_P(LCD_STR_UPLEVEL[0], str);
|
|
|
|
if (menu_clicked && (lcd_encoder == menu_item))
|
|
|
|
{
|
|
|
|
menu_back();
|
|
|
|
return menu_item_ret();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
menu_item++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t menu_item_function_P(const char* str, menu_func_t func)
|
|
|
|
{
|
|
|
|
if (menu_item == menu_line)
|
|
|
|
{
|
|
|
|
if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
|
|
|
|
if (menu_clicked && (lcd_encoder == menu_item))
|
|
|
|
{
|
|
|
|
menu_clicked = false;
|
2018-07-16 02:24:27 +00:00
|
|
|
lcd_update_enabled = 0;
|
2018-07-16 00:13:52 +00:00
|
|
|
if (func) func();
|
2018-07-16 02:24:27 +00:00
|
|
|
lcd_update_enabled = 1;
|
2018-07-16 00:13:52 +00:00
|
|
|
return menu_item_ret();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
menu_item++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t menu_item_gcode_P(const char* str, const char* str_gcode)
|
|
|
|
{
|
|
|
|
if (menu_item == menu_line)
|
|
|
|
{
|
|
|
|
if (lcd_draw_update) menu_draw_item_puts_P(' ', str);
|
|
|
|
if (menu_clicked && (lcd_encoder == menu_item))
|
|
|
|
{
|
|
|
|
if (str_gcode) enquecommand_P(str_gcode);
|
|
|
|
return menu_item_ret();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
menu_item++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-30 17:24:04 +00:00
|
|
|
|
2018-07-19 13:21:46 +00:00
|
|
|
const char menu_20x_space[] PROGMEM = " ";
|
2018-07-19 14:57:31 +00:00
|
|
|
|
2018-07-19 13:21:46 +00:00
|
|
|
const char menu_fmt_int3[] PROGMEM = "%c%.15S:%s%3d";
|
2018-07-19 14:57:31 +00:00
|
|
|
|
2018-07-27 18:28:44 +00:00
|
|
|
const char menu_fmt_float31[] PROGMEM = "%c%.12S:%s%+06.1f";
|
2018-07-19 14:57:31 +00:00
|
|
|
|
2018-07-30 17:24:04 +00:00
|
|
|
const char menu_fmt_float13[] PROGMEM = "%c%.12S:%s%+06.3f";
|
|
|
|
|
|
|
|
|
2018-07-19 13:21:46 +00:00
|
|
|
void menu_draw_int3(char chr, const char* str, int16_t val)
|
|
|
|
{
|
|
|
|
int text_len = strlen_P(str);
|
|
|
|
if (text_len > 15) text_len = 15;
|
|
|
|
char spaces[21];
|
|
|
|
strcpy_P(spaces, menu_20x_space);
|
|
|
|
spaces[15 - text_len] = 0;
|
|
|
|
lcd_printf_P(menu_fmt_int3, chr, str, spaces, val);
|
|
|
|
}
|
|
|
|
|
2018-07-27 18:28:44 +00:00
|
|
|
//draw up to 12 chars of text, ':' and float number in format +123.0
|
2018-07-19 14:57:31 +00:00
|
|
|
void menu_draw_float31(char chr, const char* str, float val)
|
|
|
|
{
|
|
|
|
int text_len = strlen_P(str);
|
|
|
|
if (text_len > 12) text_len = 12;
|
|
|
|
char spaces[21];
|
|
|
|
strcpy_P(spaces, menu_20x_space);
|
|
|
|
spaces[12 - text_len] = 0;
|
2018-07-19 15:42:59 +00:00
|
|
|
lcd_printf_P(menu_fmt_float31, chr, str, spaces, val);
|
2018-07-19 14:57:31 +00:00
|
|
|
}
|
2018-07-16 00:13:52 +00:00
|
|
|
|
2018-07-30 17:24:04 +00:00
|
|
|
//draw up to 12 chars of text, ':' and float number in format +1.234
|
|
|
|
void menu_draw_float13(char chr, const char* str, float val)
|
|
|
|
{
|
|
|
|
int text_len = strlen_P(str);
|
|
|
|
if (text_len > 12) text_len = 12;
|
|
|
|
char spaces[21];
|
|
|
|
strcpy_P(spaces, menu_20x_space);
|
|
|
|
spaces[12 - text_len] = 0;
|
|
|
|
lcd_printf_P(menu_fmt_float13, chr, str, spaces, val);
|
|
|
|
}
|
|
|
|
|
2018-08-10 00:59:49 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
//Variables used when editing values.
|
|
|
|
const char* editLabel;
|
|
|
|
void* editValue;
|
|
|
|
int32_t minEditValue;
|
|
|
|
int32_t maxEditValue;
|
|
|
|
} menu_data_edit_t;
|
|
|
|
|
2018-07-16 00:13:52 +00:00
|
|
|
void _menu_edit_int3(void)
|
|
|
|
{
|
2018-08-10 00:59:49 +00:00
|
|
|
menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
|
2018-07-16 00:13:52 +00:00
|
|
|
if (lcd_draw_update)
|
|
|
|
{
|
2018-08-10 00:59:49 +00:00
|
|
|
if (lcd_encoder < _md->minEditValue) lcd_encoder = _md->minEditValue;
|
|
|
|
if (lcd_encoder > _md->maxEditValue) lcd_encoder = _md->maxEditValue;
|
2018-07-16 00:13:52 +00:00
|
|
|
lcd_set_cursor(0, 1);
|
2018-08-10 00:59:49 +00:00
|
|
|
menu_draw_int3(' ', _md->editLabel, (int)lcd_encoder);
|
2018-07-16 00:13:52 +00:00
|
|
|
}
|
|
|
|
if (LCD_CLICKED)
|
|
|
|
{
|
2018-08-10 00:59:49 +00:00
|
|
|
*((int*)(_md->editValue)) = (int)lcd_encoder;
|
2018-08-19 11:59:33 +00:00
|
|
|
menu_back_no_reset();
|
2018-07-16 00:13:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t menu_item_edit_int3(const char* str, int16_t* pval, int16_t min_val, int16_t max_val)
|
|
|
|
{
|
2018-08-10 00:59:49 +00:00
|
|
|
menu_data_edit_t* _md = (menu_data_edit_t*)&(menu_data[0]);
|
2018-07-16 00:13:52 +00:00
|
|
|
if (menu_item == menu_line)
|
|
|
|
{
|
|
|
|
if (lcd_draw_update)
|
|
|
|
{
|
|
|
|
lcd_set_cursor(0, menu_row);
|
2018-07-19 13:21:46 +00:00
|
|
|
menu_draw_int3((lcd_encoder == menu_item)?'>':' ', str, *pval);
|
2018-07-16 00:13:52 +00:00
|
|
|
}
|
|
|
|
if (menu_clicked && (lcd_encoder == menu_item))
|
|
|
|
{
|
2018-08-19 11:59:33 +00:00
|
|
|
menu_submenu_no_reset(_menu_edit_int3);
|
2018-08-10 00:59:49 +00:00
|
|
|
_md->editLabel = str;
|
|
|
|
_md->editValue = pval;
|
|
|
|
_md->minEditValue = min_val;
|
|
|
|
_md->maxEditValue = max_val;
|
2018-07-16 00:13:52 +00:00
|
|
|
lcd_encoder = *pval;
|
|
|
|
return menu_item_ret();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
menu_item++;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-07-19 14:57:31 +00:00
|
|
|
|
2018-07-16 00:13:52 +00:00
|
|
|
#undef _menu_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|