Skip to content

Commit

Permalink
use function type
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Apr 20, 2023
1 parent d4eb924 commit a60b4cb
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 161 deletions.
99 changes: 7 additions & 92 deletions Marlin/src/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,8 @@
*/
#pragma once

#include <math.h>
#include <stddef.h>

#include "../inc/MarlinConfigPre.h"

//
// Conditional type assignment magic. For example...
//
// typename IF<(MYOPT==12), int, float>::type myvar;
//
template <bool, class L, class R> struct IF { typedef R type; };
template <class L, class R> struct IF<true, L, R> { typedef L type; };

#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

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

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

// Helpers
#define _RECIP(N) ((N) ? 1.0f / static_cast<float>(N) : 0.0f)
#define _ABS(N) ((N) < 0 ? -(N) : (N))
#define _LS(N) (N = (T)(uint32_t(N) << p))
#define _RS(N) (N = (T)(uint32_t(N) >> p))
#define FI FORCE_INLINE

// Define types based on largest bit width stored value required
#define bits_t(W) typename IF<((W)> 16), uint32_t, typename IF<((W)> 8), uint16_t, uint8_t>::type>::type
#define uvalue_t(V) typename IF<((V)>65535), uint32_t, typename IF<((V)>255), uint16_t, uint8_t>::type>::type
#define value_t(V) typename IF<((V)>32767), int32_t, typename IF<((V)>127), int16_t, int8_t>::type>::type

// General Flags for some number of states
template<size_t N>
struct Flags {
typedef value_t(N) flagbits_t;
typedef struct { bool b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1; } N8;
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;
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,
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;
union {
flagbits_t b;
typename IF<(N>16), N32, typename IF<(N>8), N16, N8>::type>::type flag;
};
FI void reset() { b = 0; }
FI void set(const int n, const bool onoff) { onoff ? set(n) : clear(n); }
FI void set(const int n) { b |= (flagbits_t)_BV(n); }
FI void clear(const int n) { b &= ~(flagbits_t)_BV(n); }
FI bool test(const int n) const { return TEST(b, n); }
FI bool operator[](const int n) { return test(n); }
FI bool operator[](const int n) const { return test(n); }
FI int size() const { return sizeof(b); }
FI operator bool() const { return b; }
};

// Specialization for a single bool flag
template<>
struct Flags<1> {
bool b;
FI void reset() { b = false; }
FI void set(const int n, const bool onoff) { onoff ? set(n) : clear(n); }
FI void set(const int) { b = true; }
FI void clear(const int) { b = false; }
FI bool test(const int) const { return b; }
FI bool& operator[](const int) { return b; }
FI bool operator[](const int) const { return b; }
FI int size() const { return sizeof(b); }
FI operator bool() const { return b; }
};

typedef Flags<8> flags_8_t;
typedef Flags<16> flags_16_t;

// Flags for some axis states, with per-axis aliases xyzijkuvwe
typedef struct AxisFlags {
union {
Expand Down Expand Up @@ -208,34 +144,6 @@ typedef bits_t(NUM_AXIS_ENUMS) axis_bits_t;
#define LOOP_DISTINCT_AXES(VAR) LOOP_S_L_N(VAR, 0, DISTINCT_AXES)
#define LOOP_DISTINCT_E(VAR) LOOP_L_N(VAR, DISTINCT_E)

//
// feedRate_t is just a humble float
//
typedef float feedRate_t;

//
// celsius_t is the native unit of temperature. Signed to handle a disconnected thermistor value (-14).
// For more resolition (e.g., for a chocolate printer) this may later be changed to Celsius x 100
//
typedef uint16_t raw_adc_t;
typedef int16_t celsius_t;
typedef float celsius_float_t;

//
// On AVR pointers are only 2 bytes so use 'const float &' for 'const float'
//
#ifdef __AVR__
typedef const float & const_float_t;
#else
typedef const float const_float_t;
#endif
typedef const_float_t const_feedRate_t;
typedef const_float_t const_celsius_float_t;

// Conversion macros
#define MMM_TO_MMS(MM_M) feedRate_t(static_cast<float>(MM_M) / 60.0f)
#define MMS_TO_MMM(MM_S) (static_cast<float>(MM_S) * 60.0f)

//
// Coordinates structures for XY, XYZ, XYZE...
//
Expand Down Expand Up @@ -321,6 +229,13 @@ void toNative(xy_pos_t &lpos);
void toNative(xyz_pos_t &lpos);
void toNative(xyze_pos_t &lpos);

// Helpers for axis types below
#define _RECIP(N) ((N) ? 1.0f / static_cast<float>(N) : 0.0f)
#define _ABS(N) ((N) < 0 ? -(N) : (N))
#define _LS(N) (N = (T)(uint32_t(N) << p))
#define _RS(N) (N = (T)(uint32_t(N) >> p))
#define FI FORCE_INLINE

//
// Paired XY coordinates, counters, flags, etc.
//
Expand Down
110 changes: 110 additions & 0 deletions Marlin/src/core/types_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2023 MarlinFirmware [https:/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#pragma once

#include <math.h>
#include <stddef.h>
#include <stdint.h>

typedef void (*voidFunc_t)();

//
// Conditional type assignment magic. For example...
//
// typename IF<(MYOPT==12), int, float>::type myvar;
//
template <bool, class L, class R> struct IF { typedef R type; };
template <class L, class R> struct IF<true, L, R> { typedef L type; };

// Define types based on largest bit width stored value required
#define bits_t(W) typename IF<((W)> 16), uint32_t, typename IF<((W)> 8), uint16_t, uint8_t>::type>::type
#define uvalue_t(V) typename IF<((V)>65535), uint32_t, typename IF<((V)>255), uint16_t, uint8_t>::type>::type
#define value_t(V) typename IF<((V)>32767), int32_t, typename IF<((V)>127), int16_t, int8_t>::type>::type

// General Flags for some number of states
template<size_t N>
struct Flags {
typedef value_t(N) flagbits_t;
typedef struct { bool b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1; } N8;
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;
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,
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;
union {
flagbits_t b;
typename IF<(N>16), N32, typename IF<(N>8), N16, N8>::type>::type flag;
};
FI void reset() { b = 0; }
FI void set(const int n, const bool onoff) { onoff ? set(n) : clear(n); }
FI void set(const int n) { b |= (flagbits_t)_BV(n); }
FI void clear(const int n) { b &= ~(flagbits_t)_BV(n); }
FI bool test(const int n) const { return TEST(b, n); }
FI bool operator[](const int n) { return test(n); }
FI bool operator[](const int n) const { return test(n); }
FI int size() const { return sizeof(b); }
FI operator bool() const { return b; }
};

// Specialization for a single bool flag
template<>
struct Flags<1> {
bool b;
FI void reset() { b = false; }
FI void set(const int n, const bool onoff) { onoff ? set(n) : clear(n); }
FI void set(const int) { b = true; }
FI void clear(const int) { b = false; }
FI bool test(const int) const { return b; }
FI bool& operator[](const int) { return b; }
FI bool operator[](const int) const { return b; }
FI int size() const { return sizeof(b); }
FI operator bool() const { return b; }
};

typedef Flags<8> flags_8_t;
typedef Flags<16> flags_16_t;

//
// feedRate_t is just a humble float
//
typedef float feedRate_t;

//
// celsius_t is the native unit of temperature. Signed to handle a disconnected thermistor value (-14).
// For more resolition (e.g., for a chocolate printer) this may later be changed to Celsius x 100
//
typedef uint16_t raw_adc_t;
typedef int16_t celsius_t;
typedef float celsius_float_t;

//
// On AVR pointers are only 2 bytes so use 'const float &' for 'const float'
//
#ifdef __AVR__
typedef const float & const_float_t;
#else
typedef const float const_float_t;
#endif
typedef const_float_t const_feedRate_t;
typedef const_float_t const_celsius_float_t;

// Conversion macros
#define MMM_TO_MMS(MM_M) feedRate_t(static_cast<float>(MM_M) / 60.0f)
#define MMS_TO_MMM(MM_S) (static_cast<float>(MM_S) * 60.0f)
2 changes: 1 addition & 1 deletion Marlin/src/inc/MarlinConfigPre.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
//
// Prefix header to acquire configurations
//
#include <stdint.h>
#include "../core/types_base.h"

#ifndef __MARLIN_DEPS__
#include "../HAL/platforms.h"
Expand Down
12 changes: 6 additions & 6 deletions Marlin/src/lcd/tft/touch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ void Touch::init() {
enable();
}

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

controls[controls_count].type = type;
controls[controls_count].x = x;
controls[controls_count].y = y;
controls[controls_count].width = width;
controls[controls_count].height = height;
controls[controls_count].data = data;
controls[controls_count].func = func;
controls_count++;
}

Expand Down Expand Up @@ -244,11 +244,11 @@ void Touch::touch(touch_control_t *control) {
#endif

case MOVE_AXIS:
ui.goto_screen((screenFunc_t)ui.move_axis_screen);
ui.goto_screen(ui.move_axis_screen);
break;

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

default: break;
}
Expand Down Expand Up @@ -320,13 +320,13 @@ bool MarlinUI::touch_pressed() {
return touch.is_clicked();
}

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) {
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) {
uint16_t width = Images[image].width;
uint16_t height = Images[image].height;
tft.canvas(x, y, width, height);
tft.add_image(0, 0, image, is_enabled ? color_enabled : color_disabled);
if (is_enabled)
touch.add_control(control_type, x, y, width, height, data);
touch.add_control(control_type, x, y, width, height, func);
}

#endif // TOUCH_SCREEN
8 changes: 4 additions & 4 deletions Marlin/src/lcd/tft/touch.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ enum TouchControlType : uint16_t {

typedef void (*screenFunc_t)();

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);
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);
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); }
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); }
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); }

typedef struct __attribute__((__packed__)) {
TouchControlType type;
uint16_t x;
uint16_t y;
uint16_t width;
uint16_t height;
intptr_t data;
voidFunc_t func;
} touch_control_t;

#define MAX_CONTROLS 16
Expand Down Expand Up @@ -131,7 +131,7 @@ class Touch {
static void sleepTimeout();
static void wakeUp();
#endif
static void add_control(TouchControlType type, uint16_t x, uint16_t y, uint16_t width, uint16_t height, intptr_t data = 0);
static void add_control(TouchControlType type, uint16_t x, uint16_t y, uint16_t width, uint16_t height, voidFunc_t func=nullptr);
};

extern Touch touch;
Loading

0 comments on commit a60b4cb

Please sign in to comment.