Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply loop shorthand macros #17159

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Marlin/src/HAL/AVR/HAL_SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void spiBegin() {
// output pin high - like sending 0xFF
WRITE(MOSI_PIN, HIGH);

for (uint8_t i = 0; i < 8; i++) {
LOOP_L_N(i, 8) {
WRITE(SCK_PIN, HIGH);

nop; // adjust so SCK is nice
Expand All @@ -224,7 +224,7 @@ void spiBegin() {
void spiSend(uint8_t data) {
// no interrupts during byte send - about 8µs
cli();
for (uint8_t i = 0; i < 8; i++) {
LOOP_L_N(i, 8) {
WRITE(SCK_PIN, LOW);
WRITE(MOSI_PIN, data & 0x80);
data <<= 1;
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/AVR/MarlinSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@

// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i = 0; i < digits; ++i) rounding *= 0.1;
LOOP_L_N(i, digits) rounding *= 0.1;
number += rounding;

// Extract the integer part of the number and print it
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/AVR/fast_pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void set_pwm_frequency(const pin_t pin, int f_desired) {
uint16_t prescaler[] = { 0, 1, 8, /*TIMER2 ONLY*/32, 64, /*TIMER2 ONLY*/128, 256, 1024 };

// loop over prescaler values
for (uint8_t i = 1; i < 8; i++) {
LOOP_S_L_N(i, 1, 8) {
uint16_t res_temp_fast = 255, res_temp_phase_correct = 255;
if (timer.n == 2) {
// No resolution calculation for TIMER2 unless enabled USE_OCR2A_AS_TOP
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/HAL/AVR/pinsDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@

void PRINT_ARRAY_NAME(uint8_t x) {
char *name_mem_pointer = (char*)pgm_read_ptr(&pin_array[x].name);
for (uint8_t y = 0; y < MAX_NAME_LENGTH; y++) {
LOOP_L_N(y, MAX_NAME_LENGTH) {
char temp_char = pgm_read_byte(name_mem_pointer + y);
if (temp_char != 0)
SERIAL_CHAR(temp_char);
else {
for (uint8_t i = 0; i < MAX_NAME_LENGTH - y; i++) SERIAL_CHAR(' ');
LOOP_L_N(i, MAX_NAME_LENGTH - y) SERIAL_CHAR(' ');
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/HAL/AVR/u8g_com_HAL_AVR_sw_spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void u8g_spiSend_sw_AVR_mode_0(uint8_t val) {
volatile uint8_t *outData = u8g_outData,
*outClock = u8g_outClock;
U8G_ATOMIC_START();
for (uint8_t i = 0; i < 8; i++) {
LOOP_L_N(i, 8) {
if (val & 0x80)
*outData |= bitData;
else
Expand All @@ -108,7 +108,7 @@ void u8g_spiSend_sw_AVR_mode_3(uint8_t val) {
volatile uint8_t *outData = u8g_outData,
*outClock = u8g_outClock;
U8G_ATOMIC_START();
for (uint8_t i = 0; i < 8; i++) {
LOOP_L_N(i, 8) {
*outClock &= bitNotClock;
if (val & 0x80)
*outData |= bitData;
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/DUE/MarlinSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ void MarlinSerial<Cfg>::printFloat(double number, uint8_t digits) {

// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i = 0; i < digits; ++i) rounding *= 0.1;
LOOP_L_N(i, digits) rounding *= 0.1;
number += rounding;

// Extract the integer part of the number and print it
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/DUE/MarlinSerialUSB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ void MarlinSerialUSB::printFloat(double number, uint8_t digits) {

// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i = 0; i < digits; ++i)
LOOP_L_N(i, digits)
rounding *= 0.1;

number += rounding;
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/HAL/DUE/dogm/u8g_com_HAL_DUE_sw_spi_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Pio *SCK_pPio, *MOSI_pPio;
uint32_t SCK_dwMask, MOSI_dwMask;

void u8g_spiSend_sw_DUE_mode_0(uint8_t val) { // 3MHz
for (uint8_t i = 0; i < 8; i++) {
LOOP_L_N(i, 8) {
if (val & 0x80)
MOSI_pPio->PIO_SODR = MOSI_dwMask;
else
Expand All @@ -94,7 +94,7 @@ void u8g_spiSend_sw_DUE_mode_0(uint8_t val) { // 3MHz
}

void u8g_spiSend_sw_DUE_mode_3(uint8_t val) { // 3.5MHz
for (uint8_t i = 0; i < 8; i++) {
LOOP_L_N(i, 8) {
SCK_pPio->PIO_CODR = SCK_dwMask;
DELAY_NS(50);
if (val & 0x80)
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/DUE/fastio/G2_PWM.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extern PWM_map ISR_table[NUM_PWMS];
extern uint32_t motor_current_setting[3];

#define IR_BIT(p) (WITHIN(p, 0, 3) ? (p) : (p) + 4)
#define COPY_ACTIVE_TABLE() do{ for (uint8_t i = 0; i < 6 ; i++) work_table[i] = active_table[i]; }while(0)
#define COPY_ACTIVE_TABLE() do{ LOOP_L_N(i, 6) work_table[i] = active_table[i]; }while(0)

#define PWM_MR0 19999 // base repetition rate minus one count - 20mS
#define PWM_PR 24 // prescaler value - prescaler divide by 24 + 1 - 1 MHz output
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/LPC1768/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void HAL_init() {
#endif

// Flash status LED 3 times to indicate Marlin has started booting
for (uint8_t i = 0; i < 6; ++i) {
LOOP_L_N(i, 6) {
TOGGLE(LED_PIN);
delay(100);
}
Expand Down
16 changes: 8 additions & 8 deletions Marlin/src/HAL/LPC1768/u8g/u8g_com_HAL_LPC1768_sw_spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

uint8_t swSpiTransfer_mode_0(uint8_t b, const uint8_t spi_speed, const pin_t sck_pin, const pin_t miso_pin, const pin_t mosi_pin ) {

for (uint8_t i = 0; i < 8; i++) {
LOOP_L_N(i, 8) {
if (spi_speed == 0) {
LPC176x::gpio_set(mosi_pin, !!(b & 0x80));
LPC176x::gpio_set(sck_pin, HIGH);
Expand All @@ -83,16 +83,16 @@ uint8_t swSpiTransfer_mode_0(uint8_t b, const uint8_t spi_speed, const pin_t sck
}
else {
const uint8_t state = (b & 0x80) ? HIGH : LOW;
for (uint8_t j = 0; j < spi_speed; j++)
LOOP_L_N(j, spi_speed)
LPC176x::gpio_set(mosi_pin, state);

for (uint8_t j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1); j++)
LOOP_L_N(j, spi_speed + (miso_pin >= 0 ? 0 : 1))
LPC176x::gpio_set(sck_pin, HIGH);

b <<= 1;
if (miso_pin >= 0 && LPC176x::gpio_get(miso_pin)) b |= 1;

for (uint8_t j = 0; j < spi_speed; j++)
LOOP_L_N(j, spi_speed)
LPC176x::gpio_set(sck_pin, LOW);
}
}
Expand All @@ -102,7 +102,7 @@ uint8_t swSpiTransfer_mode_0(uint8_t b, const uint8_t spi_speed, const pin_t sck

uint8_t swSpiTransfer_mode_3(uint8_t b, const uint8_t spi_speed, const pin_t sck_pin, const pin_t miso_pin, const pin_t mosi_pin ) {

for (uint8_t i = 0; i < 8; i++) {
LOOP_L_N(i, 8) {
const uint8_t state = (b & 0x80) ? HIGH : LOW;
if (spi_speed == 0) {
LPC176x::gpio_set(sck_pin, LOW);
Expand All @@ -111,13 +111,13 @@ uint8_t swSpiTransfer_mode_3(uint8_t b, const uint8_t spi_speed, const pin_t sck
LPC176x::gpio_set(sck_pin, HIGH);
}
else {
for (uint8_t j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1); j++)
LOOP_L_N(j, spi_speed + (miso_pin >= 0 ? 0 : 1))
LPC176x::gpio_set(sck_pin, LOW);

for (uint8_t j = 0; j < spi_speed; j++)
LOOP_L_N(j, spi_speed)
LPC176x::gpio_set(mosi_pin, state);

for (uint8_t j = 0; j < spi_speed; j++)
LOOP_L_N(j, spi_speed)
LPC176x::gpio_set(sck_pin, HIGH);
}
b <<= 1;
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/HAL/SAMD51/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,10 @@ void HAL_adc_init() {
#if ADC_IS_REQUIRED
memset(HAL_adc_results, 0xFF, sizeof(HAL_adc_results)); // Fill result with invalid values

for (uint8_t pi = 0; pi < COUNT(adc_pins); ++pi)
LOOP_L_N(pi, COUNT(adc_pins))
pinPeripheral(adc_pins[pi], PIO_ANALOG);

for (uint8_t ai = FIRST_ADC; ai <= LAST_ADC; ++ai) {
LOOP_S_LE_N(ai, FIRST_ADC, LAST_ADC) {
Adc* adc = ((Adc*[])ADC_INSTS)[ai];

// ADC clock setup
Expand Down Expand Up @@ -513,7 +513,7 @@ void HAL_adc_init() {

void HAL_adc_start_conversion(const uint8_t adc_pin) {
#if ADC_IS_REQUIRED
for (uint8_t pi = 0; pi < COUNT(adc_pins); ++pi) {
LOOP_L_N(pi, COUNT(adc_pins)) {
if (adc_pin == adc_pins[pi]) {
HAL_adc_result = HAL_adc_results[pi];
return;
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/STM32/fastio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
GPIO_TypeDef* FastIOPortMap[LastPort + 1];

void FastIO_init() {
for (uint8_t i = 0; i < NUM_DIGITAL_PINS; i++)
LOOP_L_N(i, NUM_DIGITAL_PINS)
FastIOPortMap[STM_PORT(digitalPin[i])] = get_GPIO_Port(STM_PORT(digitalPin[i]));
}

Expand Down
16 changes: 8 additions & 8 deletions Marlin/src/HAL/STM32F1/dogm/u8g_com_stm32duino_swspi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
static uint8_t SPI_speed = SPI_SPEED;

static inline uint8_t swSpiTransfer_mode_0(uint8_t b, const uint8_t spi_speed, const pin_t miso_pin=-1) {
for (uint8_t i = 0; i < 8; i++) {
LOOP_L_N(i, 8) {
if (spi_speed == 0) {
WRITE(DOGLCD_MOSI, !!(b & 0x80));
WRITE(DOGLCD_SCK, HIGH);
Expand All @@ -42,24 +42,24 @@ static inline uint8_t swSpiTransfer_mode_0(uint8_t b, const uint8_t spi_speed, c
}
else {
const uint8_t state = (b & 0x80) ? HIGH : LOW;
for (uint8_t j = 0; j < spi_speed; j++)
LOOP_L_N(j, spi_speed)
WRITE(DOGLCD_MOSI, state);

for (uint8_t j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1); j++)
LOOP_L_N(j, spi_speed + (miso_pin >= 0 ? 0 : 1))
WRITE(DOGLCD_SCK, HIGH);

b <<= 1;
if (miso_pin >= 0 && READ(miso_pin)) b |= 1;

for (uint8_t j = 0; j < spi_speed; j++)
LOOP_L_N(j, spi_speed)
WRITE(DOGLCD_SCK, LOW);
}
}
return b;
}

static inline uint8_t swSpiTransfer_mode_3(uint8_t b, const uint8_t spi_speed, const pin_t miso_pin=-1) {
for (uint8_t i = 0; i < 8; i++) {
LOOP_L_N(i, 8) {
const uint8_t state = (b & 0x80) ? HIGH : LOW;
if (spi_speed == 0) {
WRITE(DOGLCD_SCK, LOW);
Expand All @@ -68,13 +68,13 @@ static inline uint8_t swSpiTransfer_mode_3(uint8_t b, const uint8_t spi_speed, c
WRITE(DOGLCD_SCK, HIGH);
}
else {
for (uint8_t j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1); j++)
LOOP_L_N(j, spi_speed + (miso_pin >= 0 ? 0 : 1))
WRITE(DOGLCD_SCK, LOW);

for (uint8_t j = 0; j < spi_speed; j++)
LOOP_L_N(j, spi_speed)
WRITE(DOGLCD_MOSI, state);

for (uint8_t j = 0; j < spi_speed; j++)
LOOP_L_N(j, spi_speed)
WRITE(DOGLCD_SCK, HIGH);
}
b <<= 1;
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/STM32_F4_F7/EmulatedEeprom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void eeprom_read_block(void *__dst, const void *__src, size_t __n) {

uint16_t data = 0xFF;
uint16_t eeprom_address = unsigned(__src);
for (uint8_t c = 0; c < __n; c++) {
LOOP_L_N(c, __n) {
EE_ReadVariable(eeprom_address+c, &data);
*((uint8_t*)__dst + c) = data;
}
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/shared/servo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ uint8_t ServoCount = 0; // the total number of attached

static boolean isTimerActive(timer16_Sequence_t timer) {
// returns true if any servo is active on this timer
for (uint8_t channel = 0; channel < SERVOS_PER_TIMER; channel++) {
LOOP_L_N(channel, SERVOS_PER_TIMER) {
if (SERVO(timer, channel).Pin.isActive)
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ void setup_powerhold() {

bool pin_is_protected(const pin_t pin) {
static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
for (uint8_t i = 0; i < COUNT(sensitive_pins); i++) {
LOOP_L_N(i, COUNT(sensitive_pins)) {
pin_t sensitive_pin;
memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));
if (pin == sensitive_pin) return true;
Expand Down
5 changes: 5 additions & 0 deletions Marlin/src/core/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@
#define _JOIN_1(O) (O)
#define JOIN_N(N,C,V...) (DO(JOIN,C,LIST_N(N,V)))

#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 NOOP (void(0))

#define CEILING(x,y) (((x) + (y) - 1) / (y))
Expand Down
6 changes: 0 additions & 6 deletions Marlin/src/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ enum AxisEnum : uint8_t {
//
// 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)
Expand Down
18 changes: 9 additions & 9 deletions Marlin/src/feature/bedlevel/abl/abl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ void extrapolate_unprobed_bed_level() {
ylen = ctry1;
#endif

for (uint8_t xo = 0; xo <= xlen; xo++)
for (uint8_t yo = 0; yo <= ylen; yo++) {
LOOP_LE_N(xo, xlen)
LOOP_LE_N(yo, ylen) {
uint8_t x2 = ctrx2 + xo, y2 = ctry2 + yo;
#ifndef HALF_IN_X
const uint8_t x1 = ctrx1 - xo;
Expand Down Expand Up @@ -209,8 +209,8 @@ void print_bilinear_leveling_grid() {

static float bed_level_virt_2cmr(const uint8_t x, const uint8_t y, const float &tx, const float &ty) {
float row[4], column[4];
for (uint8_t i = 0; i < 4; i++) {
for (uint8_t j = 0; j < 4; j++) {
LOOP_L_N(i, 4) {
LOOP_L_N(j, 4) {
column[j] = bed_level_virt_coord(i + x - 1, j + y - 1);
}
row[i] = bed_level_virt_cmr(column, 1, ty);
Expand All @@ -221,11 +221,11 @@ void print_bilinear_leveling_grid() {
void bed_level_virt_interpolate() {
bilinear_grid_spacing_virt = bilinear_grid_spacing / (BILINEAR_SUBDIVISIONS);
bilinear_grid_factor_virt = bilinear_grid_spacing_virt.reciprocal();
for (uint8_t y = 0; y < GRID_MAX_POINTS_Y; y++)
for (uint8_t x = 0; x < GRID_MAX_POINTS_X; x++)
for (uint8_t ty = 0; ty < BILINEAR_SUBDIVISIONS; ty++)
for (uint8_t tx = 0; tx < BILINEAR_SUBDIVISIONS; tx++) {
if ((ty && y == GRID_MAX_POINTS_Y - 1) || (tx && x == GRID_MAX_POINTS_X - 1))
LOOP_L_N(y, GRID_MAX_POINTS_Y)
LOOP_L_N(x, GRID_MAX_POINTS_X)
LOOP_L_N(ty, BILINEAR_SUBDIVISIONS)
LOOP_L_N(tx, BILINEAR_SUBDIVISIONS) {
if ((ty && y == (GRID_MAX_POINTS_Y) - 1) || (tx && x == (GRID_MAX_POINTS_X) - 1))
continue;
z_values_virt[x * (BILINEAR_SUBDIVISIONS) + tx][y * (BILINEAR_SUBDIVISIONS) + ty] =
bed_level_virt_2cmr(
Expand Down
Loading