/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/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 .
*
*/
#pragma once
#include
#include
#include "millis_t.h"
class __FlashStringHelper;
typedef const __FlashStringHelper *progmem_str;
//
// Enumerated axis indices
//
// - X_AXIS, Y_AXIS, and Z_AXIS should be used for axes in Cartesian space
// - A_AXIS, B_AXIS, and C_AXIS should be used for Steppers, corresponding to XYZ on Cartesians
// - X_HEAD, Y_HEAD, and Z_HEAD should be used for Steppers on Core kinematics
//
enum AxisEnum : uint8_t {
X_AXIS = 0, A_AXIS = 0,
Y_AXIS = 1, B_AXIS = 1,
Z_AXIS = 2, C_AXIS = 2,
E_AXIS = 3,
X_HEAD = 4, Y_HEAD = 5, Z_HEAD = 6,
E0_AXIS = 3,
E1_AXIS, E2_AXIS, E3_AXIS, E4_AXIS, E5_AXIS, E6_AXIS, E7_AXIS,
ALL_AXES = 0xFE, NO_AXIS = 0xFF
};
//
// Loop over XYZE axes
//
#define LOOP_S_LE_N(VAR, S, N) for (uint8_t VAR=(S); VAR<=(N); VAR++)
#define LOOP_S_L_N(VAR, S, N) for (uint8_t VAR=(S); VAR<(N); VAR++)
#define LOOP_LE_N(VAR, N) LOOP_S_LE_N(VAR, 0, N)
#define LOOP_L_N(VAR, N) LOOP_S_L_N(VAR, 0, N)
#define LOOP_XYZ(VAR) LOOP_S_LE_N(VAR, X_AXIS, Z_AXIS)
#define LOOP_XYZE(VAR) LOOP_S_LE_N(VAR, X_AXIS, E_AXIS)
#define LOOP_XYZE_N(VAR) LOOP_S_L_N(VAR, X_AXIS, XYZE_N)
#define LOOP_ABC(VAR) LOOP_S_LE_N(VAR, A_AXIS, C_AXIS)
#define LOOP_ABCE(VAR) LOOP_S_LE_N(VAR, A_AXIS, E_AXIS)
#define LOOP_ABCE_N(VAR) LOOP_S_L_N(VAR, A_AXIS, XYZE_N)
//
// Conditional type assignment magic. For example...
//
// typename IF<(MYOPT==12), int, float>::type myvar;
//
template
struct IF { typedef R type; };
template
struct IF { typedef L type; };
//
// feedRate_t is just a humble float
//
typedef float feedRate_t;
// Conversion macros
#define MMM_TO_MMS(MM_M) feedRate_t(float(MM_M) / 60.0f)
#define MMS_TO_MMM(MM_S) (float(MM_S) * 60.0f)
#define MMS_SCALED(V) ((V) * 0.01f * feedrate_percentage)
//
// Coordinates structures for XY, XYZ, XYZE...
//
// Helpers
#define _RECIP(N) ((N) ? 1.0f / float(N) : 0.0f)
#define _ABS(N) ((N) < 0 ? -(N) : (N))
#define _LS(N) (N = (T)(uint32_t(N) << v))
#define _RS(N) (N = (T)(uint32_t(N) >> v))
#define FI FORCE_INLINE
// Forward declarations
template struct XYval;
template struct XYZval;
template struct XYZEval;
typedef struct XYval xy_bool_t;
typedef struct XYZval xyz_bool_t;
typedef struct XYZEval xyze_bool_t;
typedef struct XYval xy_char_t;
typedef struct XYZval xyz_char_t;
typedef struct XYZEval xyze_char_t;
typedef struct XYval xy_uchar_t;
typedef struct XYZval xyz_uchar_t;
typedef struct XYZEval xyze_uchar_t;
typedef struct XYval xy_int8_t;
typedef struct XYZval xyz_int8_t;
typedef struct XYZEval xyze_int8_t;
typedef struct XYval xy_uint8_t;
typedef struct XYZval xyz_uint8_t;
typedef struct XYZEval xyze_uint8_t;
typedef struct XYval xy_int_t;
typedef struct XYZval xyz_int_t;
typedef struct XYZEval xyze_int_t;
typedef struct XYval xy_uint_t;
typedef struct XYZval xyz_uint_t;
typedef struct XYZEval xyze_uint_t;
typedef struct XYval xy_long_t;
typedef struct XYZval xyz_long_t;
typedef struct XYZEval xyze_long_t;
typedef struct XYval xy_ulong_t;
typedef struct XYZval xyz_ulong_t;
typedef struct XYZEval xyze_ulong_t;
typedef struct XYZval xyz_vlong_t;
typedef struct XYZEval xyze_vlong_t;
typedef struct XYval xy_float_t;
typedef struct XYZval xyz_float_t;
typedef struct XYZEval xyze_float_t;
typedef struct XYval xy_feedrate_t;
typedef struct XYZval xyz_feedrate_t;
typedef struct XYZEval xyze_feedrate_t;
typedef xy_uint8_t xy_byte_t;
typedef xyz_uint8_t xyz_byte_t;
typedef xyze_uint8_t xyze_byte_t;
typedef xyz_long_t abc_long_t;
typedef xyze_long_t abce_long_t;
typedef xyz_ulong_t abc_ulong_t;
typedef xyze_ulong_t abce_ulong_t;
typedef xy_float_t xy_pos_t;
typedef xyz_float_t xyz_pos_t;
typedef xyze_float_t xyze_pos_t;
typedef xy_float_t ab_float_t;
typedef xyz_float_t abc_float_t;
typedef xyze_float_t abce_float_t;
typedef ab_float_t ab_pos_t;
typedef abc_float_t abc_pos_t;
typedef abce_float_t abce_pos_t;
// External conversion methods
void toLogical(xy_pos_t &raw);
void toLogical(xyz_pos_t &raw);
void toLogical(xyze_pos_t &raw);
void toNative(xy_pos_t &raw);
void toNative(xyz_pos_t &raw);
void toNative(xyze_pos_t &raw);
//
// XY coordinates, counters, etc.
//
template
struct XYval {
union {
struct { T x, y; };
struct { T a, b; };
T pos[2];
};
FI void set(const T px) { x = px; }
FI void set(const T px, const T py) { x = px; y = py; }
FI void reset() { x = y = 0; }
FI T magnitude() const { return (T)sqrtf(x*x + y*y); }
FI operator T* () { return pos; }
FI operator bool() { return x || y; }
FI XYval copy() const { return *this; }
FI XYval ABS() const { return { T(_ABS(x)), T(_ABS(y)) }; }
FI XYval asInt() { return { int16_t(x), int16_t(y) }; }
FI XYval asInt() const { return { int16_t(x), int16_t(y) }; }
FI XYval asLong() { return { int32_t(x), int32_t(y) }; }
FI XYval asLong() const { return { int32_t(x), int32_t(y) }; }
FI XYval asFloat() { return { float(x), float(y) }; }
FI XYval asFloat() const { return { float(x), float(y) }; }
FI XYval reciprocal() const { return { _RECIP(x), _RECIP(y) }; }
FI XYval asLogical() const { XYval o = asFloat(); toLogical(o); return o; }
FI XYval asNative() const { XYval o = asFloat(); toNative(o); return o; }
FI operator XYZval() { return { x, y }; }
FI operator XYZval() const { return { x, y }; }
FI operator XYZEval() { return { x, y }; }
FI operator XYZEval() const { return { x, y }; }
FI T& operator[](const int i) { return pos[i]; }
FI const T& operator[](const int i) const { return pos[i]; }
FI XYval& operator= (const T v) { set(v, v ); return *this; }
FI XYval& operator= (const XYZval &rs) { set(rs.x, rs.y); return *this; }
FI XYval& operator= (const XYZEval &rs) { set(rs.x, rs.y); return *this; }
FI XYval operator+ (const XYval &rs) const { XYval ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
FI XYval operator+ (const XYval &rs) { XYval ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
FI XYval operator- (const XYval &rs) const { XYval ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
FI XYval operator- (const XYval &rs) { XYval ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
FI XYval operator* (const XYval &rs) const { XYval ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
FI XYval operator* (const XYval &rs) { XYval ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
FI XYval operator/ (const XYval &rs) const { XYval ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
FI XYval operator/ (const XYval &rs) { XYval ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
FI XYval operator+ (const XYZval &rs) const { XYval ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
FI XYval operator+ (const XYZval &rs) { XYval ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
FI XYval operator- (const XYZval &rs) const { XYval ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
FI XYval operator- (const XYZval &rs) { XYval ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
FI XYval operator* (const XYZval &rs) const { XYval ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
FI XYval operator* (const XYZval &rs) { XYval ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
FI XYval operator/ (const XYZval &rs) const { XYval ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
FI XYval operator/ (const XYZval &rs) { XYval ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
FI XYval operator+ (const XYZEval &rs) const { XYval ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
FI XYval operator+ (const XYZEval &rs) { XYval ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
FI XYval operator- (const XYZEval &rs) const { XYval ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
FI XYval operator- (const XYZEval &rs) { XYval ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
FI XYval operator* (const XYZEval &rs) const { XYval ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
FI XYval operator* (const XYZEval &rs) { XYval ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
FI XYval operator/ (const XYZEval &rs) const { XYval ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
FI XYval operator/ (const XYZEval &rs) { XYval ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
FI XYval operator* (const float &v) const { XYval ls = *this; ls.x *= v; ls.y *= v; return ls; }
FI XYval operator* (const float &v) { XYval ls = *this; ls.x *= v; ls.y *= v; return ls; }
FI XYval operator* (const int &v) const { XYval ls = *this; ls.x *= v; ls.y *= v; return ls; }
FI XYval operator* (const int &v) { XYval ls = *this; ls.x *= v; ls.y *= v; return ls; }
FI XYval operator/ (const float &v) const { XYval ls = *this; ls.x /= v; ls.y /= v; return ls; }
FI XYval operator/ (const float &v) { XYval ls = *this; ls.x /= v; ls.y /= v; return ls; }
FI XYval operator/ (const int &v) const { XYval ls = *this; ls.x /= v; ls.y /= v; return ls; }
FI XYval operator/ (const int &v) { XYval ls = *this; ls.x /= v; ls.y /= v; return ls; }
FI XYval operator>>(const int &v) const { XYval ls = *this; _RS(ls.x); _RS(ls.y); return ls; }
FI XYval operator>>(const int &v) { XYval ls = *this; _RS(ls.x); _RS(ls.y); return ls; }
FI XYval operator<<(const int &v) const { XYval ls = *this; _LS(ls.x); _LS(ls.y); return ls; }
FI XYval operator<<(const int &v) { XYval ls = *this; _LS(ls.x); _LS(ls.y); return ls; }
FI XYval& operator+=(const XYval &rs) { x += rs.x; y += rs.y; return *this; }
FI XYval& operator-=(const XYval &rs) { x -= rs.x; y -= rs.y; return *this; }
FI XYval& operator*=(const XYval &rs) { x *= rs.x; y *= rs.y; return *this; }
FI XYval& operator+=(const XYZval &rs) { x += rs.x; y += rs.y; return *this; }
FI XYval& operator-=(const XYZval &rs) { x -= rs.x; y -= rs.y; return *this; }
FI XYval& operator*=(const XYZval &rs) { x *= rs.x; y *= rs.y; return *this; }
FI XYval& operator+=(const XYZEval &rs) { x += rs.x; y += rs.y; return *this; }
FI XYval& operator-=(const XYZEval &rs) { x -= rs.x; y -= rs.y; return *this; }
FI XYval& operator*=(const XYZEval &rs) { x *= rs.x; y *= rs.y; return *this; }
FI XYval& operator*=(const float &v) { x *= v; y *= v; return *this; }
FI XYval& operator*=(const int &v) { x *= v; y *= v; return *this; }
FI XYval& operator>>=(const int &v) { _RS(x); _RS(y); return *this; }
FI XYval& operator<<=(const int &v) { _LS(x); _LS(y); return *this; }
FI bool operator==(const XYval &rs) { return x == rs.x && y == rs.y; }
FI bool operator==(const XYZval &rs) { return x == rs.x && y == rs.y; }
FI bool operator==(const XYZEval &rs) { return x == rs.x && y == rs.y; }
FI bool operator==(const XYval &rs) const { return x == rs.x && y == rs.y; }
FI bool operator==(const XYZval &rs) const { return x == rs.x && y == rs.y; }
FI bool operator==(const XYZEval &rs) const { return x == rs.x && y == rs.y; }
FI bool operator!=(const XYval &rs) { return !operator==(rs); }
FI bool operator!=(const XYZval &rs) { return !operator==(rs); }
FI bool operator!=(const XYZEval &rs) { return !operator==(rs); }
FI bool operator!=(const XYval &rs) const { return !operator==(rs); }
FI bool operator!=(const XYZval &rs) const { return !operator==(rs); }
FI bool operator!=(const XYZEval &rs) const { return !operator==(rs); }
FI XYval operator-() { XYval o = *this; o.x = -x; o.y = -y; return o; }
FI const XYval operator-() const { XYval o = *this; o.x = -x; o.y = -y; return o; }
};
//
// XYZ coordinates, counters, etc.
//
template
struct XYZval {
union {
struct { T x, y, z; };
struct { T a, b, c; };
T pos[3];
};
FI void set(const T px) { x = px; }
FI void set(const T px, const T py) { x = px; y = py; }
FI void set(const T px, const T py, const T pz) { x = px; y = py; z = pz; }
FI void set(const XYval pxy, const T pz) { x = pxy.x; y = pxy.y; z = pz; }
FI void reset() { x = y = z = 0; }
FI T magnitude() const { return (T)sqrtf(x*x + y*y + z*z); }
FI operator T* () { return pos; }
FI operator bool() { return z || x || y; }
FI XYZval copy() const { XYZval o = *this; return o; }
FI XYZval ABS() const { return { T(_ABS(x)), T(_ABS(y)), T(_ABS(z)) }; }
FI XYZval asInt() { return { int16_t(x), int16_t(y), int16_t(z) }; }
FI XYZval asInt() const { return { int16_t(x), int16_t(y), int16_t(z) }; }
FI XYZval asLong() { return { int32_t(x), int32_t(y), int32_t(z) }; }
FI XYZval asLong() const { return { int32_t(x), int32_t(y), int32_t(z) }; }
FI XYZval asFloat() { return { float(x), float(y), float(z) }; }
FI XYZval asFloat() const { return { float(x), float(y), float(z) }; }
FI XYZval reciprocal() const { return { _RECIP(x), _RECIP(y), _RECIP(z) }; }
FI XYZval asLogical() const { XYZval o = asFloat(); toLogical(o); return o; }
FI XYZval asNative() const { XYZval o = asFloat(); toNative(o); return o; }
FI operator XYval&() { return *(XYval*)this; }
FI operator const XYval&() const { return *(const XYval*)this; }
FI operator XYZEval() const { return { x, y, z }; }
FI T& operator[](const int i) { return pos[i]; }
FI const T& operator[](const int i) const { return pos[i]; }
FI XYZval& operator= (const T v) { set(v, v, v ); return *this; }
FI XYZval& operator= (const XYval &rs) { set(rs.x, rs.y ); return *this; }
FI XYZval& operator= (const XYZEval &rs) { set(rs.x, rs.y, rs.z); return *this; }
FI XYZval operator+ (const XYval &rs) const { XYZval ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
FI XYZval operator+ (const XYval &rs) { XYZval ls = *this; ls.x += rs.x; ls.y += rs.y; return ls; }
FI XYZval operator- (const XYval &rs) const { XYZval ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
FI XYZval operator- (const XYval &rs) { XYZval ls = *this; ls.x -= rs.x; ls.y -= rs.y; return ls; }
FI XYZval operator* (const XYval &rs) const { XYZval ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
FI XYZval operator* (const XYval &rs) { XYZval ls = *this; ls.x *= rs.x; ls.y *= rs.y; return ls; }
FI XYZval operator/ (const XYval &rs) const { XYZval ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
FI XYZval operator/ (const XYval &rs) { XYZval ls = *this; ls.x /= rs.x; ls.y /= rs.y; return ls; }
FI XYZval operator+ (const XYZval &rs) const { XYZval ls = *this; ls.x += rs.x; ls.y += rs.y; ls.z += rs.z; return ls; }
FI XYZval operator+ (const XYZval