Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Width/Magnitude-based types (MarlinFirmware#25458)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored and EvilGremlin committed Apr 8, 2023
1 parent 953b231 commit 3144041
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 110 deletions.
7 changes: 2 additions & 5 deletions Marlin/src/HAL/AVR/MarlinSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <WString.h>

#include "../../inc/MarlinConfigPre.h"
#include "../../core/types.h"
#include "../../core/serial_hook.h"

#ifndef SERIAL_PORT
Expand Down Expand Up @@ -138,10 +139,6 @@

#define BYTE 0

// Templated type selector
template<bool b, typename T, typename F> struct TypeSelector { typedef T type;} ;
template<typename T, typename F> struct TypeSelector<false, T, F> { typedef F type; };

template<typename Cfg>
class MarlinSerial {
protected:
Expand All @@ -164,7 +161,7 @@
static constexpr B_U2Xx<Cfg::PORT> B_U2X = 0;

// Base size of type on buffer size
typedef typename TypeSelector<(Cfg::RX_SIZE>256), uint16_t, uint8_t>::type ring_buffer_pos_t;
typedef uvalue_t(Cfg::RX_SIZE - 1) ring_buffer_pos_t;

struct ring_buffer_r {
volatile ring_buffer_pos_t head, tail;
Expand Down
7 changes: 2 additions & 5 deletions Marlin/src/HAL/DUE/MarlinSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <WString.h>

#include "../../inc/MarlinConfigPre.h"
#include "../../core/types.h"
#include "../../core/serial_hook.h"

// Define constants and variables for buffering incoming serial data. We're
Expand All @@ -52,10 +53,6 @@
// #error "TX_BUFFER_SIZE must be 0, a power of 2 greater than 1, and no greater than 256."
//#endif

// Templated type selector
template<bool b, typename T, typename F> struct TypeSelector { typedef T type;} ;
template<typename T, typename F> struct TypeSelector<false, T, F> { typedef F type; };

// Templated structure wrapper
template<typename S, unsigned int addr> struct StructWrapper {
constexpr StructWrapper(int) {}
Expand All @@ -76,7 +73,7 @@ class MarlinSerial {
static constexpr int HWUART_IRQ_ID = IRQ_IDS[Cfg::PORT];

// Base size of type on buffer size
typedef typename TypeSelector<(Cfg::RX_SIZE>256), uint16_t, uint8_t>::type ring_buffer_pos_t;
typedef uvalue_t(Cfg::RX_SIZE - 1) ring_buffer_pos_t;

struct ring_buffer_r {
volatile ring_buffer_pos_t head, tail;
Expand Down
25 changes: 15 additions & 10 deletions Marlin/src/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@
//
// 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; };
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

Expand Down Expand Up @@ -86,20 +84,27 @@ 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)

// 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 typename IF<(N>8), uint16_t, uint8_t>::type bits_t;
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 {
bits_t b;
typename IF<(N>8), N16, N8>::type flag;
flagbits_t b;
typename IF<(N>16), N32, typename IF<(N>8), N16, N8>::type>::type flag;
};
void reset() { b = 0; }
void set(const int n, const bool onoff) { onoff ? set(n) : clear(n); }
void set(const int n) { b |= (bits_t)_BV(n); }
void clear(const int n) { b &= ~(bits_t)_BV(n); }
void set(const int n) { b |= (flagbits_t)_BV(n); }
void clear(const int n) { b &= ~(flagbits_t)_BV(n); }
bool test(const int n) const { return TEST(b, n); }
bool operator[](const int n) { return test(n); }
bool operator[](const int n) const { return test(n); }
Expand Down Expand Up @@ -182,7 +187,7 @@ enum AxisEnum : uint8_t {
, ALL_AXES_ENUM = 0xFE, NO_AXIS_ENUM = 0xFF
};

typedef IF<(NUM_AXIS_ENUMS > 8), uint16_t, uint8_t>::type axis_bits_t;
typedef bits_t(NUM_AXIS_ENUMS) axis_bits_t;

//
// Loop over axes
Expand Down
12 changes: 6 additions & 6 deletions Marlin/src/core/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ void safe_delay(millis_t ms); // Delay ensuring that temperatures are
// 16x16 bit arrays
template <int W, int H>
struct FlagBits {
typename IF<(W>8), uint16_t, uint8_t>::type bits[H];
void fill() { memset(bits, 0xFF, sizeof(bits)); }
void reset() { memset(bits, 0x00, sizeof(bits)); }
void unmark(const uint8_t x, const uint8_t y) { CBI(bits[y], x); }
void mark(const uint8_t x, const uint8_t y) { SBI(bits[y], x); }
bool marked(const uint8_t x, const uint8_t y) { return TEST(bits[y], x); }
bits_t(W) flags[H];
void fill() { memset(flags, 0xFF, sizeof(flags)); }
void reset() { memset(flags, 0x00, sizeof(flags)); }
void unmark(const uint8_t x, const uint8_t y) { CBI(flags[y], x); }
void mark(const uint8_t x, const uint8_t y) { SBI(flags[y], x); }
bool marked(const uint8_t x, const uint8_t y) { return TEST(flags[y], x); }
inline void unmark(const xy_int8_t &xy) { unmark(xy.x, xy.y); }
inline void mark(const xy_int8_t &xy) { mark(xy.x, xy.y); }
inline bool marked(const xy_int8_t &xy) { return marked(xy.x, xy.y); }
Expand Down
7 changes: 2 additions & 5 deletions Marlin/src/feature/direct_stepping.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ namespace DirectStepping {
static void set_page_state(const page_idx_t page_idx, const PageState page_state);
};

template<bool b, typename T, typename F> struct TypeSelector { typedef T type;} ;
template<typename T, typename F> struct TypeSelector<false, T, F> { typedef F type; };

template <int num_pages, int num_axes, int bits_segment, bool dir, int segments>
struct config_t {
static constexpr char CONTROL_CHAR = '!';
Expand All @@ -98,8 +95,8 @@ namespace DirectStepping {
static constexpr int TOTAL_STEPS = SEGMENT_STEPS * SEGMENTS;
static constexpr int PAGE_SIZE = (AXIS_COUNT * BITS_SEGMENT * SEGMENTS) / 8;

typedef typename TypeSelector<(PAGE_SIZE>256), uint16_t, uint8_t>::type write_byte_idx_t;
typedef typename TypeSelector<(PAGE_COUNT>256), uint16_t, uint8_t>::type page_idx_t;
typedef uvalue_t(PAGE_SIZE - 1) write_byte_idx_t;
typedef uvalue_t(PAGE_COUNT - 1) page_idx_t;
};

template <uint8_t num_pages>
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/leds/neopixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
// Types
// ------------------------

typedef IF<(TERN0(NEOPIXEL_LED, NEOPIXEL_PIXELS > 127)), int16_t, int8_t>::type pixel_index_t;
typedef value_t(TERN0(NEOPIXEL_LED, NEOPIXEL_PIXELS)) pixel_index_t;

// ------------------------
// Classes
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/max7219.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ void Max7219::register_setup() {
constexpr millis_t pattern_delay = 4;

int8_t spiralx, spiraly, spiral_dir;
IF<(MAX7219_LEDS > 255), uint16_t, uint8_t>::type spiral_count;
uvalue_t(MAX7219_LEDS) spiral_count;

void Max7219::test_pattern() {
constexpr int8_t way[][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/spindle_laser_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
#endif
#endif

typedef IF<(SPEED_POWER_MAX > 255), uint16_t, uint8_t>::type cutter_cpower_t;
typedef uvalue_t(SPEED_POWER_MAX) cutter_cpower_t;

#if CUTTER_UNIT_IS(RPM) && SPEED_POWER_MAX > 255
typedef uint16_t cutter_power_t;
Expand Down
5 changes: 3 additions & 2 deletions Marlin/src/lcd/dogm/status_screen_DOGM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@
DRAWBIT_HOTEND,
DRAWBIT_BED = HOTENDS,
DRAWBIT_CHAMBER,
DRAWBIT_CUTTER
DRAWBIT_CUTTER,
DRAWBIT_COUNT
};
IF<(DRAWBIT_CUTTER > 7), uint16_t, uint8_t>::type draw_bits;
bits_t(DRAWBIT_COUNT) draw_bits;
#endif

#if ANIM_HOTEND
Expand Down
Loading

0 comments on commit 3144041

Please sign in to comment.