From d85861d296c6a34693af0a2aadd4019f8e38cc80 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Wed, 22 Jul 2015 15:39:23 -0700 Subject: [PATCH 1/2] Move macros, add ENABLED/DISABLED, add includes --- Marlin/Configuration.h | 1 + Marlin/Marlin.h | 8 ------ Marlin/configurator/config/Configuration.h | 1 + .../Felix/Configuration.h | 1 + .../Felix/Configuration_DUAL.h | 1 + .../Hephestos/Configuration.h | 1 + .../K8200/Configuration.h | 1 + .../RepRapWorld/Megatronics/Configuration.h | 1 + .../RigidBot/Configuration.h | 1 + .../SCARA/Configuration.h | 1 + .../WITBOX/Configuration.h | 1 + .../adafruit/ST7565/Configuration.h | 1 + .../delta/biv2.5/Configuration.h | 1 + .../delta/generic/Configuration.h | 1 + .../delta/kossel_mini/Configuration.h | 1 + .../delta/kossel_pro/Configuration.h | 1 + .../makibox/Configuration.h | 1 + .../tvrrug/Round2/Configuration.h | 1 + Marlin/macros.h | 25 +++++++++++++++++++ Marlin/stepper_indirection.h | 2 ++ 20 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 Marlin/macros.h diff --git a/Marlin/Configuration.h b/Marlin/Configuration.h index 432055eb3e..400d226e00 100644 --- a/Marlin/Configuration.h +++ b/Marlin/Configuration.h @@ -2,6 +2,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/Marlin.h b/Marlin/Marlin.h index 7bdb12b7aa..ee05b46835 100644 --- a/Marlin/Marlin.h +++ b/Marlin/Marlin.h @@ -28,14 +28,6 @@ #include "Arduino.h" -#define BIT(b) (1<<(b)) -#define TEST(n,b) (((n)&BIT(b))!=0) -#define SET_BIT(n,b,value) (n) ^= ((-value)^(n)) & (BIT(b)) -#define RADIANS(d) ((d)*M_PI/180.0) -#define DEGREES(r) ((r)*180.0/M_PI) -#define NOLESS(v,n) do{ if (v < n) v = n; }while(0) -#define NOMORE(v,n) do{ if (v > n) v = n; }while(0) - typedef unsigned long millis_t; // Arduino < 1.0.0 does not define this, so we need to do it ourselves diff --git a/Marlin/configurator/config/Configuration.h b/Marlin/configurator/config/Configuration.h index 432055eb3e..400d226e00 100644 --- a/Marlin/configurator/config/Configuration.h +++ b/Marlin/configurator/config/Configuration.h @@ -2,6 +2,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/example_configurations/Felix/Configuration.h b/Marlin/example_configurations/Felix/Configuration.h index a3806de99c..8a5f2ee580 100644 --- a/Marlin/example_configurations/Felix/Configuration.h +++ b/Marlin/example_configurations/Felix/Configuration.h @@ -2,6 +2,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/example_configurations/Felix/Configuration_DUAL.h b/Marlin/example_configurations/Felix/Configuration_DUAL.h index 81db1aae3a..27c376f6d8 100644 --- a/Marlin/example_configurations/Felix/Configuration_DUAL.h +++ b/Marlin/example_configurations/Felix/Configuration_DUAL.h @@ -2,6 +2,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/example_configurations/Hephestos/Configuration.h b/Marlin/example_configurations/Hephestos/Configuration.h index 4b0192a50c..6b6fd731a0 100644 --- a/Marlin/example_configurations/Hephestos/Configuration.h +++ b/Marlin/example_configurations/Hephestos/Configuration.h @@ -2,6 +2,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/example_configurations/K8200/Configuration.h b/Marlin/example_configurations/K8200/Configuration.h index d7e7811629..b503844856 100644 --- a/Marlin/example_configurations/K8200/Configuration.h +++ b/Marlin/example_configurations/K8200/Configuration.h @@ -7,6 +7,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/example_configurations/RepRapWorld/Megatronics/Configuration.h b/Marlin/example_configurations/RepRapWorld/Megatronics/Configuration.h index 540019e451..aca519997b 100644 --- a/Marlin/example_configurations/RepRapWorld/Megatronics/Configuration.h +++ b/Marlin/example_configurations/RepRapWorld/Megatronics/Configuration.h @@ -2,6 +2,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/example_configurations/RigidBot/Configuration.h b/Marlin/example_configurations/RigidBot/Configuration.h index 7c2d7f25b7..6baf0c04f3 100644 --- a/Marlin/example_configurations/RigidBot/Configuration.h +++ b/Marlin/example_configurations/RigidBot/Configuration.h @@ -2,6 +2,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/example_configurations/SCARA/Configuration.h b/Marlin/example_configurations/SCARA/Configuration.h index a835a83b40..e0f79a9e4f 100644 --- a/Marlin/example_configurations/SCARA/Configuration.h +++ b/Marlin/example_configurations/SCARA/Configuration.h @@ -2,6 +2,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/example_configurations/WITBOX/Configuration.h b/Marlin/example_configurations/WITBOX/Configuration.h index 5fb542f6de..b4dfd588ca 100644 --- a/Marlin/example_configurations/WITBOX/Configuration.h +++ b/Marlin/example_configurations/WITBOX/Configuration.h @@ -2,6 +2,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/example_configurations/adafruit/ST7565/Configuration.h b/Marlin/example_configurations/adafruit/ST7565/Configuration.h index d9aef0431c..19f93e99ae 100644 --- a/Marlin/example_configurations/adafruit/ST7565/Configuration.h +++ b/Marlin/example_configurations/adafruit/ST7565/Configuration.h @@ -2,6 +2,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/example_configurations/delta/biv2.5/Configuration.h b/Marlin/example_configurations/delta/biv2.5/Configuration.h index 378a454fa9..a64641e3af 100644 --- a/Marlin/example_configurations/delta/biv2.5/Configuration.h +++ b/Marlin/example_configurations/delta/biv2.5/Configuration.h @@ -2,6 +2,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/example_configurations/delta/generic/Configuration.h b/Marlin/example_configurations/delta/generic/Configuration.h index 646e3118b5..c9560f15cd 100644 --- a/Marlin/example_configurations/delta/generic/Configuration.h +++ b/Marlin/example_configurations/delta/generic/Configuration.h @@ -2,6 +2,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/example_configurations/delta/kossel_mini/Configuration.h b/Marlin/example_configurations/delta/kossel_mini/Configuration.h index 129c1c1a24..643da2f717 100644 --- a/Marlin/example_configurations/delta/kossel_mini/Configuration.h +++ b/Marlin/example_configurations/delta/kossel_mini/Configuration.h @@ -2,6 +2,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/example_configurations/delta/kossel_pro/Configuration.h b/Marlin/example_configurations/delta/kossel_pro/Configuration.h index 0d065e903c..112a6e77ce 100644 --- a/Marlin/example_configurations/delta/kossel_pro/Configuration.h +++ b/Marlin/example_configurations/delta/kossel_pro/Configuration.h @@ -6,6 +6,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/example_configurations/makibox/Configuration.h b/Marlin/example_configurations/makibox/Configuration.h index ffb062a0a8..5e486c01ed 100644 --- a/Marlin/example_configurations/makibox/Configuration.h +++ b/Marlin/example_configurations/makibox/Configuration.h @@ -2,6 +2,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/example_configurations/tvrrug/Round2/Configuration.h b/Marlin/example_configurations/tvrrug/Round2/Configuration.h index 79bcb76d33..2278f79b2d 100644 --- a/Marlin/example_configurations/tvrrug/Round2/Configuration.h +++ b/Marlin/example_configurations/tvrrug/Round2/Configuration.h @@ -2,6 +2,7 @@ #define CONFIGURATION_H #include "boards.h" +#include "macros.h" //=========================================================================== //============================= Getting Started ============================= diff --git a/Marlin/macros.h b/Marlin/macros.h new file mode 100644 index 0000000000..f6acf7eec9 --- /dev/null +++ b/Marlin/macros.h @@ -0,0 +1,25 @@ +#ifndef MACROS_H +#define MACROS_H + +// Macros for bit masks +#define BIT(b) (1<<(b)) +#define TEST(n,b) (((n)&BIT(b))!=0) +#define SET_BIT(n,b,value) (n) ^= ((-value)^(n)) & (BIT(b)) + +// Macros for maths shortcuts +#define RADIANS(d) ((d)*M_PI/180.0) +#define DEGREES(r) ((r)*180.0/M_PI) + +// Macros to contrain values +#define NOLESS(v,n) do{ if (v < n) v = n; }while(0) +#define NOMORE(v,n) do{ if (v > n) v = n; }while(0) + +// Macros to support option testing +#define _CAT(a, ...) a ## __VA_ARGS__ +#define SWITCH_ENABLED_0 0 +#define SWITCH_ENABLED_1 1 +#define SWITCH_ENABLED_ 1 +#define ENABLED(b) _CAT(SWITCH_ENABLED_, b) +#define DISABLED(b) (!_CAT(SWITCH_ENABLED_, b)) + +#endif //__MACROS_H diff --git a/Marlin/stepper_indirection.h b/Marlin/stepper_indirection.h index 408fccbec4..04bb223e1c 100644 --- a/Marlin/stepper_indirection.h +++ b/Marlin/stepper_indirection.h @@ -22,6 +22,8 @@ #ifndef STEPPER_INDIRECTION_H #define STEPPER_INDIRECTION_H +#include "macros.h" + // X motor #define X_STEP_INIT SET_OUTPUT(X_STEP_PIN) #define X_STEP_WRITE(STATE) WRITE(X_STEP_PIN,STATE) From 278aa15301470c7cb5ec433afc4bc192dd3780be Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Wed, 22 Jul 2015 15:50:20 -0700 Subject: [PATCH 2/2] Add COUNT macro and apply --- Marlin/Marlin_main.cpp | 4 ++-- Marlin/Sd2PinMap.h | 4 +++- Marlin/configuration_store.cpp | 2 +- Marlin/digipot_mcp4451.cpp | 2 +- Marlin/macros.h | 2 ++ Marlin/stepper.cpp | 2 +- Marlin/thermistortables.h | 11 ++++++----- 7 files changed, 16 insertions(+), 11 deletions(-) diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index 3c7675b02d..5202ed2327 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -3047,7 +3047,7 @@ inline void gcode_M42() { if (code_seen('P') && pin_status >= 0 && pin_status <= 255) pin_number = code_value_short(); - for (int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins) / sizeof(*sensitive_pins)); i++) { + for (int8_t i = 0; i < COUNT(sensitive_pins); i++) { if (sensitive_pins[i] == pin_number) { pin_number = -1; break; @@ -4192,7 +4192,7 @@ inline void gcode_M226() { if (pin_state >= -1 && pin_state <= 1) { - for (int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins)/sizeof(*sensitive_pins)); i++) { + for (int8_t i = 0; i < COUNT(sensitive_pins); i++) { if (sensitive_pins[i] == pin_number) { pin_number = -1; break; diff --git a/Marlin/Sd2PinMap.h b/Marlin/Sd2PinMap.h index 97fea8bb0d..80f5185f5c 100644 --- a/Marlin/Sd2PinMap.h +++ b/Marlin/Sd2PinMap.h @@ -19,6 +19,8 @@ */ // Warning this file was generated by a program. #include "Marlin.h" +#include "macros.h" + #ifdef SDSUPPORT #ifndef Sd2PinMap_h @@ -385,7 +387,7 @@ static const pin_map_t digitalPinMap[] = { #error unknown chip #endif // defined(__AVR_ATmega1280__) //------------------------------------------------------------------------------ -static const uint8_t digitalPinCount = sizeof(digitalPinMap)/sizeof(pin_map_t); +static const uint8_t digitalPinCount = COUNT(digitalPinMap); uint8_t badPinNumber(void) __attribute__((error("Pin number is too large or not a constant"))); diff --git a/Marlin/configuration_store.cpp b/Marlin/configuration_store.cpp index 1bc5940618..559cda0130 100644 --- a/Marlin/configuration_store.cpp +++ b/Marlin/configuration_store.cpp @@ -475,7 +475,7 @@ void Config_ResetDefault() { max_feedrate[i] = tmp2[i]; max_acceleration_units_per_sq_second[i] = tmp3[i]; #ifdef SCARA - if (i < sizeof(axis_scaling) / sizeof(*axis_scaling)) + if (i < COUNT(axis_scaling)) axis_scaling[i] = 1; #endif } diff --git a/Marlin/digipot_mcp4451.cpp b/Marlin/digipot_mcp4451.cpp index 22d2700253..f9d6013d29 100644 --- a/Marlin/digipot_mcp4451.cpp +++ b/Marlin/digipot_mcp4451.cpp @@ -50,7 +50,7 @@ void digipot_i2c_init() { const float digipot_motor_current[] = DIGIPOT_I2C_MOTOR_CURRENTS; Wire.begin(); // setup initial currents as defined in Configuration_adv.h - for(int i = 0; i <= sizeof(digipot_motor_current) / sizeof(float); i++) { + for(int i = 0; i < COUNT(digipot_motor_current); i++) { digipot_i2c_set_current(i, digipot_motor_current[i]); } } diff --git a/Marlin/macros.h b/Marlin/macros.h index f6acf7eec9..09b6f1933f 100644 --- a/Marlin/macros.h +++ b/Marlin/macros.h @@ -22,4 +22,6 @@ #define ENABLED(b) _CAT(SWITCH_ENABLED_, b) #define DISABLED(b) (!_CAT(SWITCH_ENABLED_, b)) +#define COUNT(a) (sizeof(a)/sizeof(*a)) + #endif //__MACROS_H diff --git a/Marlin/stepper.cpp b/Marlin/stepper.cpp index 8d63420a21..14f0475762 100644 --- a/Marlin/stepper.cpp +++ b/Marlin/stepper.cpp @@ -1249,7 +1249,7 @@ void microstep_init() { pinMode(E0_MS1_PIN,OUTPUT); pinMode(E0_MS2_PIN,OUTPUT); const uint8_t microstep_modes[] = MICROSTEP_MODES; - for (uint16_t i = 0; i < sizeof(microstep_modes) / sizeof(microstep_modes[0]); i++) + for (uint16_t i = 0; i < COUNT(microstep_modes); i++) microstep_mode(i, microstep_modes[i]); #endif } diff --git a/Marlin/thermistortables.h b/Marlin/thermistortables.h index 61092f005e..713e633dbd 100644 --- a/Marlin/thermistortables.h +++ b/Marlin/thermistortables.h @@ -2,6 +2,7 @@ #define THERMISTORTABLES_H_ #include "Marlin.h" +#include "macros.h" #define OVERSAMPLENR 16 @@ -1123,7 +1124,7 @@ const short temptable_1047[][2] PROGMEM = { #ifdef THERMISTORHEATER_0 # define HEATER_0_TEMPTABLE TT_NAME(THERMISTORHEATER_0) -# define HEATER_0_TEMPTABLE_LEN (sizeof(HEATER_0_TEMPTABLE)/sizeof(*HEATER_0_TEMPTABLE)) +# define HEATER_0_TEMPTABLE_LEN COUNT(HEATER_0_TEMPTABLE) #else # ifdef HEATER_0_USES_THERMISTOR # error No heater 0 thermistor table specified @@ -1146,7 +1147,7 @@ const short temptable_1047[][2] PROGMEM = { #ifdef THERMISTORHEATER_1 # define HEATER_1_TEMPTABLE TT_NAME(THERMISTORHEATER_1) -# define HEATER_1_TEMPTABLE_LEN (sizeof(HEATER_1_TEMPTABLE)/sizeof(*HEATER_1_TEMPTABLE)) +# define HEATER_1_TEMPTABLE_LEN COUNT(HEATER_1_TEMPTABLE) #else # ifdef HEATER_1_USES_THERMISTOR # error No heater 1 thermistor table specified @@ -1169,7 +1170,7 @@ const short temptable_1047[][2] PROGMEM = { #ifdef THERMISTORHEATER_2 # define HEATER_2_TEMPTABLE TT_NAME(THERMISTORHEATER_2) -# define HEATER_2_TEMPTABLE_LEN (sizeof(HEATER_2_TEMPTABLE)/sizeof(*HEATER_2_TEMPTABLE)) +# define HEATER_2_TEMPTABLE_LEN COUNT(HEATER_2_TEMPTABLE) #else # ifdef HEATER_2_USES_THERMISTOR # error No heater 2 thermistor table specified @@ -1192,7 +1193,7 @@ const short temptable_1047[][2] PROGMEM = { #ifdef THERMISTORHEATER_3 # define HEATER_3_TEMPTABLE TT_NAME(THERMISTORHEATER_3) -# define HEATER_3_TEMPTABLE_LEN (sizeof(HEATER_3_TEMPTABLE)/sizeof(*HEATER_3_TEMPTABLE)) +# define HEATER_3_TEMPTABLE_LEN COUNT(HEATER_3_TEMPTABLE) #else # ifdef HEATER_3_USES_THERMISTOR # error No heater 3 thermistor table specified @@ -1215,7 +1216,7 @@ const short temptable_1047[][2] PROGMEM = { #ifdef THERMISTORBED # define BEDTEMPTABLE TT_NAME(THERMISTORBED) -# define BEDTEMPTABLE_LEN (sizeof(BEDTEMPTABLE)/sizeof(*BEDTEMPTABLE)) +# define BEDTEMPTABLE_LEN COUNT(BEDTEMPTABLE) #else # ifdef BED_USES_THERMISTOR # error No bed thermistor table specified