Skip to content

Commit a60b4cb

Browse files
committed
use function type
1 parent d4eb924 commit a60b4cb

File tree

8 files changed

+186
-161
lines changed

8 files changed

+186
-161
lines changed

Marlin/src/core/types.h

Lines changed: 7 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,8 @@
2121
*/
2222
#pragma once
2323

24-
#include <math.h>
25-
#include <stddef.h>
26-
2724
#include "../inc/MarlinConfigPre.h"
2825

29-
//
30-
// Conditional type assignment magic. For example...
31-
//
32-
// typename IF<(MYOPT==12), int, float>::type myvar;
33-
//
34-
template <bool, class L, class R> struct IF { typedef R type; };
35-
template <class L, class R> struct IF<true, L, R> { typedef L type; };
36-
3726
#define ALL_AXIS_NAMES X, X2, Y, Y2, Z, Z2, Z3, Z4, I, J, K, U, V, W, E0, E1, E2, E3, E4, E5, E6, E7
3827

3928
#define NUM_AXIS_GANG(V...) GANG_N(NUM_AXES, V)
@@ -84,59 +73,6 @@ template <class L, class R> struct IF<true, L, R> { typedef L type; };
8473

8574
#define AXIS_COLLISION(L) (AXIS4_NAME == L || AXIS5_NAME == L || AXIS6_NAME == L || AXIS7_NAME == L || AXIS8_NAME == L || AXIS9_NAME == L)
8675

87-
// Helpers
88-
#define _RECIP(N) ((N) ? 1.0f / static_cast<float>(N) : 0.0f)
89-
#define _ABS(N) ((N) < 0 ? -(N) : (N))
90-
#define _LS(N) (N = (T)(uint32_t(N) << p))
91-
#define _RS(N) (N = (T)(uint32_t(N) >> p))
92-
#define FI FORCE_INLINE
93-
94-
// Define types based on largest bit width stored value required
95-
#define bits_t(W) typename IF<((W)> 16), uint32_t, typename IF<((W)> 8), uint16_t, uint8_t>::type>::type
96-
#define uvalue_t(V) typename IF<((V)>65535), uint32_t, typename IF<((V)>255), uint16_t, uint8_t>::type>::type
97-
#define value_t(V) typename IF<((V)>32767), int32_t, typename IF<((V)>127), int16_t, int8_t>::type>::type
98-
99-
// General Flags for some number of states
100-
template<size_t N>
101-
struct Flags {
102-
typedef value_t(N) flagbits_t;
103-
typedef struct { bool b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1; } N8;
104-
typedef struct { bool b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1, b8:1, b9:1, b10:1, b11:1, b12:1, b13:1, b14:1, b15:1; } N16;
105-
typedef struct { bool b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1, b8:1, b9:1, b10:1, b11:1, b12:1, b13:1, b14:1, b15:1,
106-
b16:1, b17:1, b18:1, b19:1, b20:1, b21:1, b22:1, b23:1, b24:1, b25:1, b26:1, b27:1, b28:1, b29:1, b30:1, b31:1; } N32;
107-
union {
108-
flagbits_t b;
109-
typename IF<(N>16), N32, typename IF<(N>8), N16, N8>::type>::type flag;
110-
};
111-
FI void reset() { b = 0; }
112-
FI void set(const int n, const bool onoff) { onoff ? set(n) : clear(n); }
113-
FI void set(const int n) { b |= (flagbits_t)_BV(n); }
114-
FI void clear(const int n) { b &= ~(flagbits_t)_BV(n); }
115-
FI bool test(const int n) const { return TEST(b, n); }
116-
FI bool operator[](const int n) { return test(n); }
117-
FI bool operator[](const int n) const { return test(n); }
118-
FI int size() const { return sizeof(b); }
119-
FI operator bool() const { return b; }
120-
};
121-
122-
// Specialization for a single bool flag
123-
template<>
124-
struct Flags<1> {
125-
bool b;
126-
FI void reset() { b = false; }
127-
FI void set(const int n, const bool onoff) { onoff ? set(n) : clear(n); }
128-
FI void set(const int) { b = true; }
129-
FI void clear(const int) { b = false; }
130-
FI bool test(const int) const { return b; }
131-
FI bool& operator[](const int) { return b; }
132-
FI bool operator[](const int) const { return b; }
133-
FI int size() const { return sizeof(b); }
134-
FI operator bool() const { return b; }
135-
};
136-
137-
typedef Flags<8> flags_8_t;
138-
typedef Flags<16> flags_16_t;
139-
14076
// Flags for some axis states, with per-axis aliases xyzijkuvwe
14177
typedef struct AxisFlags {
14278
union {
@@ -208,34 +144,6 @@ typedef bits_t(NUM_AXIS_ENUMS) axis_bits_t;
208144
#define LOOP_DISTINCT_AXES(VAR) LOOP_S_L_N(VAR, 0, DISTINCT_AXES)
209145
#define LOOP_DISTINCT_E(VAR) LOOP_L_N(VAR, DISTINCT_E)
210146

211-
//
212-
// feedRate_t is just a humble float
213-
//
214-
typedef float feedRate_t;
215-
216-
//
217-
// celsius_t is the native unit of temperature. Signed to handle a disconnected thermistor value (-14).
218-
// For more resolition (e.g., for a chocolate printer) this may later be changed to Celsius x 100
219-
//
220-
typedef uint16_t raw_adc_t;
221-
typedef int16_t celsius_t;
222-
typedef float celsius_float_t;
223-
224-
//
225-
// On AVR pointers are only 2 bytes so use 'const float &' for 'const float'
226-
//
227-
#ifdef __AVR__
228-
typedef const float & const_float_t;
229-
#else
230-
typedef const float const_float_t;
231-
#endif
232-
typedef const_float_t const_feedRate_t;
233-
typedef const_float_t const_celsius_float_t;
234-
235-
// Conversion macros
236-
#define MMM_TO_MMS(MM_M) feedRate_t(static_cast<float>(MM_M) / 60.0f)
237-
#define MMS_TO_MMM(MM_S) (static_cast<float>(MM_S) * 60.0f)
238-
239147
//
240148
// Coordinates structures for XY, XYZ, XYZE...
241149
//
@@ -321,6 +229,13 @@ void toNative(xy_pos_t &lpos);
321229
void toNative(xyz_pos_t &lpos);
322230
void toNative(xyze_pos_t &lpos);
323231

232+
// Helpers for axis types below
233+
#define _RECIP(N) ((N) ? 1.0f / static_cast<float>(N) : 0.0f)
234+
#define _ABS(N) ((N) < 0 ? -(N) : (N))
235+
#define _LS(N) (N = (T)(uint32_t(N) << p))
236+
#define _RS(N) (N = (T)(uint32_t(N) >> p))
237+
#define FI FORCE_INLINE
238+
324239
//
325240
// Paired XY coordinates, counters, flags, etc.
326241
//

Marlin/src/core/types_base.h

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
* Copyright (c) 2023 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4+
*
5+
* Based on Sprinter and grbl.
6+
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
#pragma once
23+
24+
#include <math.h>
25+
#include <stddef.h>
26+
#include <stdint.h>
27+
28+
typedef void (*voidFunc_t)();
29+
30+
//
31+
// Conditional type assignment magic. For example...
32+
//
33+
// typename IF<(MYOPT==12), int, float>::type myvar;
34+
//
35+
template <bool, class L, class R> struct IF { typedef R type; };
36+
template <class L, class R> struct IF<true, L, R> { typedef L type; };
37+
38+
// Define types based on largest bit width stored value required
39+
#define bits_t(W) typename IF<((W)> 16), uint32_t, typename IF<((W)> 8), uint16_t, uint8_t>::type>::type
40+
#define uvalue_t(V) typename IF<((V)>65535), uint32_t, typename IF<((V)>255), uint16_t, uint8_t>::type>::type
41+
#define value_t(V) typename IF<((V)>32767), int32_t, typename IF<((V)>127), int16_t, int8_t>::type>::type
42+
43+
// General Flags for some number of states
44+
template<size_t N>
45+
struct Flags {
46+
typedef value_t(N) flagbits_t;
47+
typedef struct { bool b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1; } N8;
48+
typedef struct { bool b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1, b8:1, b9:1, b10:1, b11:1, b12:1, b13:1, b14:1, b15:1; } N16;
49+
typedef struct { bool b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1, b8:1, b9:1, b10:1, b11:1, b12:1, b13:1, b14:1, b15:1,
50+
b16:1, b17:1, b18:1, b19:1, b20:1, b21:1, b22:1, b23:1, b24:1, b25:1, b26:1, b27:1, b28:1, b29:1, b30:1, b31:1; } N32;
51+
union {
52+
flagbits_t b;
53+
typename IF<(N>16), N32, typename IF<(N>8), N16, N8>::type>::type flag;
54+
};
55+
FI void reset() { b = 0; }
56+
FI void set(const int n, const bool onoff) { onoff ? set(n) : clear(n); }
57+
FI void set(const int n) { b |= (flagbits_t)_BV(n); }
58+
FI void clear(const int n) { b &= ~(flagbits_t)_BV(n); }
59+
FI bool test(const int n) const { return TEST(b, n); }
60+
FI bool operator[](const int n) { return test(n); }
61+
FI bool operator[](const int n) const { return test(n); }
62+
FI int size() const { return sizeof(b); }
63+
FI operator bool() const { return b; }
64+
};
65+
66+
// Specialization for a single bool flag
67+
template<>
68+
struct Flags<1> {
69+
bool b;
70+
FI void reset() { b = false; }
71+
FI void set(const int n, const bool onoff) { onoff ? set(n) : clear(n); }
72+
FI void set(const int) { b = true; }
73+
FI void clear(const int) { b = false; }
74+
FI bool test(const int) const { return b; }
75+
FI bool& operator[](const int) { return b; }
76+
FI bool operator[](const int) const { return b; }
77+
FI int size() const { return sizeof(b); }
78+
FI operator bool() const { return b; }
79+
};
80+
81+
typedef Flags<8> flags_8_t;
82+
typedef Flags<16> flags_16_t;
83+
84+
//
85+
// feedRate_t is just a humble float
86+
//
87+
typedef float feedRate_t;
88+
89+
//
90+
// celsius_t is the native unit of temperature. Signed to handle a disconnected thermistor value (-14).
91+
// For more resolition (e.g., for a chocolate printer) this may later be changed to Celsius x 100
92+
//
93+
typedef uint16_t raw_adc_t;
94+
typedef int16_t celsius_t;
95+
typedef float celsius_float_t;
96+
97+
//
98+
// On AVR pointers are only 2 bytes so use 'const float &' for 'const float'
99+
//
100+
#ifdef __AVR__
101+
typedef const float & const_float_t;
102+
#else
103+
typedef const float const_float_t;
104+
#endif
105+
typedef const_float_t const_feedRate_t;
106+
typedef const_float_t const_celsius_float_t;
107+
108+
// Conversion macros
109+
#define MMM_TO_MMS(MM_M) feedRate_t(static_cast<float>(MM_M) / 60.0f)
110+
#define MMS_TO_MMM(MM_S) (static_cast<float>(MM_S) * 60.0f)

Marlin/src/inc/MarlinConfigPre.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
//
2929
// Prefix header to acquire configurations
3030
//
31-
#include <stdint.h>
31+
#include "../core/types_base.h"
3232

3333
#ifndef __MARLIN_DEPS__
3434
#include "../HAL/platforms.h"

Marlin/src/lcd/tft/touch.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ void Touch::init() {
6363
enable();
6464
}
6565

66-
void Touch::add_control(TouchControlType type, uint16_t x, uint16_t y, uint16_t width, uint16_t height, intptr_t data) {
66+
void Touch::add_control(TouchControlType type, uint16_t x, uint16_t y, uint16_t width, uint16_t height, voidFunc_t func) {
6767
if (controls_count == MAX_CONTROLS) return;
6868

6969
controls[controls_count].type = type;
7070
controls[controls_count].x = x;
7171
controls[controls_count].y = y;
7272
controls[controls_count].width = width;
7373
controls[controls_count].height = height;
74-
controls[controls_count].data = data;
74+
controls[controls_count].func = func;
7575
controls_count++;
7676
}
7777

@@ -244,11 +244,11 @@ void Touch::touch(touch_control_t *control) {
244244
#endif
245245

246246
case MOVE_AXIS:
247-
ui.goto_screen((screenFunc_t)ui.move_axis_screen);
247+
ui.goto_screen(ui.move_axis_screen);
248248
break;
249249

250250
// TODO: TOUCH could receive data to pass to the callback
251-
case BUTTON: ((screenFunc_t)control->data)(); break;
251+
case BUTTON: (*control->func)(); break;
252252

253253
default: break;
254254
}
@@ -320,13 +320,13 @@ bool MarlinUI::touch_pressed() {
320320
return touch.is_clicked();
321321
}
322322

323-
void add_control(uint16_t x, uint16_t y, TouchControlType control_type, intptr_t data, MarlinImage image, bool is_enabled, uint16_t color_enabled, uint16_t color_disabled) {
323+
void add_control(uint16_t x, uint16_t y, TouchControlType control_type, voidFunc_t func, MarlinImage image, bool is_enabled, uint16_t color_enabled, uint16_t color_disabled) {
324324
uint16_t width = Images[image].width;
325325
uint16_t height = Images[image].height;
326326
tft.canvas(x, y, width, height);
327327
tft.add_image(0, 0, image, is_enabled ? color_enabled : color_disabled);
328328
if (is_enabled)
329-
touch.add_control(control_type, x, y, width, height, data);
329+
touch.add_control(control_type, x, y, width, height, func);
330330
}
331331

332332
#endif // TOUCH_SCREEN

Marlin/src/lcd/tft/touch.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@ enum TouchControlType : uint16_t {
7171

7272
typedef void (*screenFunc_t)();
7373

74-
void add_control(uint16_t x, uint16_t y, TouchControlType control_type, intptr_t data, MarlinImage image, bool is_enabled = true, uint16_t color_enabled = COLOR_CONTROL_ENABLED, uint16_t color_disabled = COLOR_CONTROL_DISABLED);
74+
void add_control(uint16_t x, uint16_t y, TouchControlType control_type, voidFunc_t func, MarlinImage image, bool is_enabled = true, uint16_t color_enabled = COLOR_CONTROL_ENABLED, uint16_t color_disabled = COLOR_CONTROL_DISABLED);
7575
inline void add_control(uint16_t x, uint16_t y, TouchControlType control_type, MarlinImage image, bool is_enabled = true, uint16_t color_enabled = COLOR_CONTROL_ENABLED, uint16_t color_disabled = COLOR_CONTROL_DISABLED) { add_control(x, y, control_type, 0, image, is_enabled, color_enabled, color_disabled); }
76-
inline void add_control(uint16_t x, uint16_t y, screenFunc_t screen, MarlinImage image, bool is_enabled = true, uint16_t color_enabled = COLOR_CONTROL_ENABLED, uint16_t color_disabled = COLOR_CONTROL_DISABLED) { add_control(x, y, MENU_SCREEN, (intptr_t)screen, image, is_enabled, color_enabled, color_disabled); }
76+
inline void add_control(uint16_t x, uint16_t y, screenFunc_t screen, MarlinImage image, bool is_enabled = true, uint16_t color_enabled = COLOR_CONTROL_ENABLED, uint16_t color_disabled = COLOR_CONTROL_DISABLED) { add_control(x, y, MENU_SCREEN, screen, image, is_enabled, color_enabled, color_disabled); }
7777

7878
typedef struct __attribute__((__packed__)) {
7979
TouchControlType type;
8080
uint16_t x;
8181
uint16_t y;
8282
uint16_t width;
8383
uint16_t height;
84-
intptr_t data;
84+
voidFunc_t func;
8585
} touch_control_t;
8686

8787
#define MAX_CONTROLS 16
@@ -131,7 +131,7 @@ class Touch {
131131
static void sleepTimeout();
132132
static void wakeUp();
133133
#endif
134-
static void add_control(TouchControlType type, uint16_t x, uint16_t y, uint16_t width, uint16_t height, intptr_t data = 0);
134+
static void add_control(TouchControlType type, uint16_t x, uint16_t y, uint16_t width, uint16_t height, voidFunc_t func=nullptr);
135135
};
136136

137137
extern Touch touch;

0 commit comments

Comments
 (0)