From 278aa15301470c7cb5ec433afc4bc192dd3780be Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Wed, 22 Jul 2015 15:50:20 -0700 Subject: [PATCH] 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 3c7675b02..5202ed232 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 97fea8bb0..80f5185f5 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 1bc594061..559cda013 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 22d270025..f9d6013d2 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 f6acf7eec..09b6f1933 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 8d63420a2..14f047576 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 61092f005..713e633db 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