Skip to content

Commit ce38260

Browse files
committed
🩹 Improve edit menu rounding
1 parent 3ad9c0b commit ce38260

File tree

2 files changed

+38
-37
lines changed

2 files changed

+38
-37
lines changed

Marlin/src/lcd/menu/menu_item.h

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ template<typename NAME>
7979
class TMenuEditItem : MenuEditItemBase {
8080
private:
8181
typedef typename NAME::type_t type_t;
82-
static float scale(const_float_t value) { return NAME::scale(value); }
83-
static float unscale(const_float_t value) { return NAME::unscale(value); }
84-
static const char* to_string(const int32_t value) { return NAME::strfunc(unscale(value)); }
85-
static void load(void *ptr, const int32_t value) { *((type_t*)ptr) = unscale(value); }
82+
static int32_t scaleToEncoder(const type_t &value) { return NAME::scaleToEncoder(value); }
83+
static type_t unscaleEncoder(const int32_t value) { return NAME::unscaleEncoder(value); }
84+
static const char* to_string(const int32_t value) { return NAME::strfunc(unscaleEncoder(value)); }
85+
static void load(void *ptr, const int32_t value) { *((type_t*)ptr) = unscaleEncoder(value); }
8686
public:
8787
FORCE_INLINE static void draw(const bool sel, const uint8_t row, FSTR_P const fstr, type_t * const data, ...) {
8888
MenuEditItemBase::draw(sel, row, fstr, NAME::strfunc(*(data)));
@@ -101,9 +101,9 @@ class TMenuEditItem : MenuEditItemBase {
101101
const bool live=false // Callback during editing
102102
) {
103103
// Make sure minv and maxv fit within int32_t
104-
const int32_t minv = _MAX(scale(minValue), INT32_MIN),
105-
maxv = _MIN(scale(maxValue), INT32_MAX);
106-
goto_edit_screen(fstr, ptr, minv, maxv - minv, scale(*ptr) - minv,
104+
const int32_t minv = _MAX(scaleToEncoder(minValue), INT32_MIN),
105+
maxv = _MIN(scaleToEncoder(maxValue), INT32_MAX);
106+
goto_edit_screen(fstr, ptr, minv, maxv - minv, scaleToEncoder(*ptr) - minv,
107107
edit_screen, callback, live);
108108
}
109109
};
@@ -119,9 +119,9 @@ class TMenuEditItem : MenuEditItemBase {
119119
*
120120
* struct MenuEditItemInfo_percent {
121121
* typedef uint8_t type_t;
122-
* static float scale(const_float_t value) { return value * (100.f/255.f) +0.5f; }
123-
* static float unscale(const_float_t value) { return value / (100.f/255.f) +0.5f; }
124-
* static const char* strfunc(const_float_t value) { return ui8tostr4pctrj(_DOFIX(uint8_t,value)); }
122+
* static int32_t scaleToEncoder(const type_t &value) { return value * (100.f/255.f) +0.5f; }
123+
* static type_t unscaleEncoder(const int32_t value) { return type_t(value) / (100.f/255.f) +0.5f; }
124+
* static const char* strfunc(const type_t &value) { return ui8tostr4pctrj(_DOFIX(uint8_t,value)); }
125125
* };
126126
* typedef TMenuEditItem<MenuEditItemInfo_percent> MenuItem_percent
127127
*/
@@ -130,36 +130,37 @@ class TMenuEditItem : MenuEditItemBase {
130130
#define DEFINE_MENU_EDIT_ITEM_TYPE(NAME, TYPE, STRFUNC, SCALE, ETC...) \
131131
struct MenuEditItemInfo_##NAME { \
132132
typedef TYPE type_t; \
133-
static float scale(const_float_t value) { return value * (SCALE) ETC; } \
134-
static float unscale(const_float_t value) { return value / (SCALE) ETC; } \
135-
static const char* strfunc(const_float_t value) { return STRFUNC(_DOFIX(TYPE,value)); } \
133+
/* scale the given value to the encoder */ \
134+
static int32_t scaleToEncoder(const type_t &value) { return value * (SCALE) ETC; } \
135+
static type_t unscaleEncoder(const int32_t value) { return type_t(value) / (SCALE) ETC; } \
136+
static const char* strfunc(const type_t &value) { return STRFUNC(_DOFIX(TYPE,value)); } \
136137
}; \
137138
typedef TMenuEditItem<MenuEditItemInfo_##NAME> MenuItem_##NAME
138139

139140
// NAME TYPE STRFUNC SCALE ROUND
140-
DEFINE_MENU_EDIT_ITEM_TYPE(percent ,uint8_t ,ui8tostr4pctrj , 100.f/255.f, +0.5f); // 100% right-justified
141-
DEFINE_MENU_EDIT_ITEM_TYPE(percent_3 ,uint8_t ,pcttostrpctrj , 1 ); // 100% right-justified
142-
DEFINE_MENU_EDIT_ITEM_TYPE(int3 ,int16_t ,i16tostr3rj , 1 ); // 123, -12 right-justified
143-
DEFINE_MENU_EDIT_ITEM_TYPE(int4 ,int16_t ,i16tostr4signrj , 1 ); // 1234, -123 right-justified
144-
DEFINE_MENU_EDIT_ITEM_TYPE(int8 ,int8_t ,i8tostr3rj , 1 ); // 123, -12 right-justified
145-
DEFINE_MENU_EDIT_ITEM_TYPE(uint8 ,uint8_t ,ui8tostr3rj , 1 ); // 123 right-justified
146-
DEFINE_MENU_EDIT_ITEM_TYPE(uint16_3 ,uint16_t ,ui16tostr3rj , 1 ); // 123 right-justified
147-
DEFINE_MENU_EDIT_ITEM_TYPE(uint16_4 ,uint16_t ,ui16tostr4rj , 0.1f ); // 1234 right-justified
148-
DEFINE_MENU_EDIT_ITEM_TYPE(uint16_5 ,uint16_t ,ui16tostr5rj , 0.01f ); // 12345 right-justified
149-
DEFINE_MENU_EDIT_ITEM_TYPE(float3 ,float ,ftostr3 , 1 ); // 123 right-justified
150-
DEFINE_MENU_EDIT_ITEM_TYPE(float42_52 ,float ,ftostr42_52 , 100 ); // _2.34, 12.34, -2.34 or 123.45, -23.45
151-
DEFINE_MENU_EDIT_ITEM_TYPE(float43 ,float ,ftostr43sign ,1000 ); // -1.234, _1.234, +1.234
152-
DEFINE_MENU_EDIT_ITEM_TYPE(float4 ,float ,ftostr4sign , 1 ); // 1234 right-justified
153-
DEFINE_MENU_EDIT_ITEM_TYPE(float5 ,float ,ftostr5rj , 1 ); // 12345 right-justified
154-
DEFINE_MENU_EDIT_ITEM_TYPE(float5_25 ,float ,ftostr5rj , 0.04f ); // 12345 right-justified (25 increment)
155-
DEFINE_MENU_EDIT_ITEM_TYPE(float61 ,float ,ftostr61rj , 10 ); // 12345.6 right-justified
156-
DEFINE_MENU_EDIT_ITEM_TYPE(float72 ,float ,ftostr72rj , 100 ); // 12345.67 right-justified
157-
DEFINE_MENU_EDIT_ITEM_TYPE(float31sign ,float ,ftostr31sign , 10 ); // +12.3
158-
DEFINE_MENU_EDIT_ITEM_TYPE(float41sign ,float ,ftostr41sign , 10 ); // +123.4
159-
DEFINE_MENU_EDIT_ITEM_TYPE(float51sign ,float ,ftostr51sign , 10 ); // +1234.5
160-
DEFINE_MENU_EDIT_ITEM_TYPE(float52sign ,float ,ftostr52sign , 100 ); // +123.45
161-
DEFINE_MENU_EDIT_ITEM_TYPE(long5 ,uint32_t ,ftostr5rj , 0.01f ); // 12345 right-justified
162-
DEFINE_MENU_EDIT_ITEM_TYPE(long5_25 ,uint32_t ,ftostr5rj , 0.04f ); // 12345 right-justified (25 increment)
141+
DEFINE_MENU_EDIT_ITEM_TYPE(percent ,uint8_t ,ui8tostr4pctrj , 100.f/255.f, + 0.5f ); // 100% right-justified
142+
DEFINE_MENU_EDIT_ITEM_TYPE(percent_3 ,uint8_t ,pcttostrpctrj , 1 ); // 100% right-justified
143+
DEFINE_MENU_EDIT_ITEM_TYPE(int3 ,int16_t ,i16tostr3rj , 1 ); // 123, -12 right-justified
144+
DEFINE_MENU_EDIT_ITEM_TYPE(int4 ,int16_t ,i16tostr4signrj , 1 ); // 1234, -123 right-justified
145+
DEFINE_MENU_EDIT_ITEM_TYPE(int8 ,int8_t ,i8tostr3rj , 1 ); // 123, -12 right-justified
146+
DEFINE_MENU_EDIT_ITEM_TYPE(uint8 ,uint8_t ,ui8tostr3rj , 1 ); // 123 right-justified
147+
DEFINE_MENU_EDIT_ITEM_TYPE(uint16_3 ,uint16_t ,ui16tostr3rj , 1 ); // 123 right-justified
148+
DEFINE_MENU_EDIT_ITEM_TYPE(uint16_4 ,uint16_t ,ui16tostr4rj , 0.1f ); // 1234 right-justified
149+
DEFINE_MENU_EDIT_ITEM_TYPE(uint16_5 ,uint16_t ,ui16tostr5rj , 0.01f ); // 12345 right-justified
150+
DEFINE_MENU_EDIT_ITEM_TYPE(float3 ,float ,ftostr3rj , 1 ); // 123 right-justified
151+
DEFINE_MENU_EDIT_ITEM_TYPE(float42_52 ,float ,ftostr42_52 , 100 , + 0.001f ); // _2.34, 12.34, -2.34 or 123.45, -23.45
152+
DEFINE_MENU_EDIT_ITEM_TYPE(float43 ,float ,ftostr43sign ,1000 , + 0.0001f); // -1.234, _1.234, +1.234
153+
DEFINE_MENU_EDIT_ITEM_TYPE(float4 ,float ,ftostr4sign , 1 ); // 1234 right-justified
154+
DEFINE_MENU_EDIT_ITEM_TYPE(float5 ,float ,ftostr5rj , 1 ); // 12345 right-justified
155+
DEFINE_MENU_EDIT_ITEM_TYPE(float5_25 ,float ,ftostr5rj , 0.04f ); // 12345 right-justified (25 increment)
156+
DEFINE_MENU_EDIT_ITEM_TYPE(float61 ,float ,ftostr61rj , 10 , + 0.01f ); // 12345.6 right-justified
157+
DEFINE_MENU_EDIT_ITEM_TYPE(float72 ,float ,ftostr72rj , 100 , + 0.001f ); // 12345.67 right-justified
158+
DEFINE_MENU_EDIT_ITEM_TYPE(float31sign ,float ,ftostr31sign , 10 , + 0.01f ); // +12.3
159+
DEFINE_MENU_EDIT_ITEM_TYPE(float41sign ,float ,ftostr41sign , 10 , + 0.01f ); // +123.4
160+
DEFINE_MENU_EDIT_ITEM_TYPE(float51sign ,float ,ftostr51sign , 10 , + 0.01f ); // +1234.5
161+
DEFINE_MENU_EDIT_ITEM_TYPE(float52sign ,float ,ftostr52sign , 100 , + 0.001f ); // +123.45
162+
DEFINE_MENU_EDIT_ITEM_TYPE(long5 ,uint32_t ,ftostr5rj , 0.01f ); // 12345 right-justified
163+
DEFINE_MENU_EDIT_ITEM_TYPE(long5_25 ,uint32_t ,ftostr5rj , 0.04f ); // 12345 right-justified (25 increment)
163164

164165
#if HAS_BED_PROBE
165166
#if Z_PROBE_OFFSET_RANGE_MIN >= -9 && Z_PROBE_OFFSET_RANGE_MAX <= 9

Marlin/src/libs/numtostr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ const char* ftostr61rj(const_float_t x);
120120
const char* ftostr72rj(const_float_t x);
121121

122122
// Convert float to rj string with 123 or -12 format
123-
FORCE_INLINE const char* ftostr3(const_float_t x) { return i16tostr3rj(int16_t(x + (x < 0 ? -0.5f : 0.5f))); }
123+
FORCE_INLINE const char* ftostr3rj(const_float_t x) { return i16tostr3rj(int16_t(x + (x < 0 ? -0.5f : 0.5f))); }
124124

125125
#if ENABLED(LCD_DECIMAL_SMALL_XY)
126126
// Convert float to rj string with 1234, _123, 12.3, _1.2, -123, _-12, or -1.2 format

0 commit comments

Comments
 (0)