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

🧑‍💻 Update pin formatting tools #26450

Merged
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CONTAINER_IMAGE := marlin-dev

help:
@echo "Tasks for local development:"
@echo "* format-pins: Reformat all pins files
@echo "* tests-single-ci: Run a single test from inside the CI"
@echo "* tests-single-local: Run a single test locally"
@echo "* tests-single-local-docker: Run a single test locally, using docker"
Expand Down Expand Up @@ -57,3 +58,12 @@ tests-all-local-docker:
setup-local-docker:
$(CONTAINER_RT_BIN) build -t $(CONTAINER_IMAGE) -f docker/Dockerfile .
.PHONY: setup-local-docker

PINS := $(shell find Marlin/src/pins -mindepth 2 -name '*.h')

.PHONY: $(PINS)

$(PINS): %:
@echo "Formatting $@" && node buildroot/share/scripts/pinsformat.js $@

format-pins: $(PINS)
2 changes: 1 addition & 1 deletion Marlin/src/pins/gd32f1/pins_TRIGORILLA_V006.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
//
// SPI
//
#define SPI_DEVICE -1 // Maple
#define SPI_DEVICE -1 // Maple
#define SCK_PIN -1
#define MISO_PIN -1
#define MOSI_PIN -1
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/pins/lpc1768/pins_BTT_SKR_V1_3.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@
#define TFT_BACKLIGHT_PIN EXP1_03_PIN

#define TOUCH_BUTTONS_HW_SPI
#define TOUCH_BUTTONS_HW_SPI_DEVICE 1
#define TOUCH_BUTTONS_HW_SPI_DEVICE 1

//#define TFT_RST_PIN EXP2_07_PIN
#define TFT_SCK_PIN EXP2_02_PIN
Expand Down Expand Up @@ -422,7 +422,7 @@

#define SD_DETECT_PIN EXP2_07_PIN

#else // !CR10_STOCKDISPLAY
#else // !CR10_STOCKDISPLAY

#define LCD_PINS_RS EXP1_04_PIN

Expand Down Expand Up @@ -463,7 +463,7 @@
#define NEOPIXEL_PIN EXP1_06_PIN
#endif

#else // !FYSETC_MINI_12864
#else // !FYSETC_MINI_12864

#if ENABLED(MKS_MINI_12864)

Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/pins/mega/pins_GT2560_REV_A.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
// Power monitoring pins - set to 0 for main VIN, 1 for dedicated bed supply rail.
// Don't forget to enable POWER_MONITOR_VOLTAGE in Configuration_adv.h
// and set POWER_MONITOR_VOLTS_PER_VOLT to 0.090909.
#define POWER_MONITOR_VOLTAGE_PIN 0
#define POWER_MONITOR_VOLTAGE_PIN 0

/** LCD SDCARD
* ------ ------
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/pins/mega/pins_GT2560_REV_A_PLUS.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@

#define BOARD_INFO_NAME "GT2560 Rev.A+"

#define SERVO0_PIN 11
#define SERVO0_PIN 11

#include "pins_GT2560_REV_A.h"
8 changes: 4 additions & 4 deletions Marlin/src/pins/mega/pins_PICAOLD.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
// Origin: https://github.com/mjrice/PICA/blob/97ab9e7771a8e5eef97788f3adcc17a9fa9de9b9/pica_schematic.pdf
// ATmega2560

#define HEATER_0_PIN 9 // E0
#define HEATER_1_PIN 10 // E1
#define FAN0_PIN 11
#define FAN2_PIN 12
#define HEATER_0_PIN 9 // E0
#define HEATER_1_PIN 10 // E1
#define FAN0_PIN 11
#define FAN2_PIN 12

#include "pins_PICA.h"
8 changes: 4 additions & 4 deletions Marlin/src/pins/ramps/pins_FELIX2.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@
//
// Heaters / Fans
//
#define MOSFET_D_PIN 7
#define MOSFET_D_PIN 7

#include "pins_RAMPS.h"

//
// Misc. Functions
//
#define SDPOWER_PIN 1
#define SDPOWER_PIN 1

#define PS_ON_PIN 12
#define PS_ON_PIN 12

//
// LCD / Controller
//
#if HAS_WIRED_LCD && IS_NEWPANEL

#define SD_DETECT_PIN 6
#define SD_DETECT_PIN 6

#endif

Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/pins/ramps/pins_MAKEBOARD_MINI.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
//
// Only 3 Limit Switch plugs on Micromake C1
//
#define X_STOP_PIN 2
#define Y_STOP_PIN 15
#define Z_STOP_PIN 19
#define X_STOP_PIN 2
#define Y_STOP_PIN 15
#define Z_STOP_PIN 19

#include "pins_RAMPS.h"
4 changes: 2 additions & 2 deletions Marlin/src/pins/ramps/pins_RUMBA_RAISE3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#define DEFAULT_MACHINE_NAME "Raise3D N Series"

// Raise3D uses thermocouples on the standard input pins
#define TEMP_0_PIN 15 // Analog Input
#define TEMP_1_PIN 14 // Analog Input
#define TEMP_0_PIN 15 // Analog Input
#define TEMP_1_PIN 14 // Analog Input

#include "pins_RUMBA.h"
2 changes: 1 addition & 1 deletion Marlin/src/pins/ramps/pins_TRONXY_V3_1_0.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
//
// Servos
//
#define SERVO1_PIN 12 // 2560 PIN 25/PB6
#define SERVO1_PIN 12 // 2560 PIN 25/PB6

//
// Import RAMPS 1.4 pins
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/pins/sam/pins_RAMPS4DUE.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
//
// Temperature Sensors
//
#define TEMP_0_PIN 9 // Analog Input
#define TEMP_1_PIN -1 // Analog Input
#define TEMP_BED_PIN 10 // Analog Input
#define TEMP_0_PIN 9 // Analog Input
#define TEMP_1_PIN -1 // Analog Input
#define TEMP_BED_PIN 10 // Analog Input

#include "../ramps/pins_RAMPS.h"
6 changes: 3 additions & 3 deletions Marlin/src/pins/sam/pins_RAMPS_FD_V2.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#define BOARD_INFO_NAME "RAMPS-FD v2"

#ifndef E0_CS_PIN
#define E0_CS_PIN 69 // moved from A13 to A15 on v2.2, if not earlier
#define E0_CS_PIN 69 // moved from A13 to A15 on v2.2, if not earlier
#endif

#include "pins_RAMPS_FD_V1.h"
Expand All @@ -44,9 +44,9 @@
#define MARLIN_EEPROM_SIZE 0x10000 // 64K in a 24C512

#ifndef PS_ON_PIN
#define PS_ON_PIN 12
#define PS_ON_PIN 12
#endif

#ifndef FILWIDTH_PIN
#define FILWIDTH_PIN 5 // Analog Input on AUX2
#define FILWIDTH_PIN 5 // Analog Input on AUX2
#endif
2 changes: 1 addition & 1 deletion Marlin/src/pins/sanguino/pins_AZTEEG_X1.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@

#define BOARD_INFO_NAME "Azteeg X1"

#define FAN0_PIN 4
#define FAN0_PIN 4

#include "pins_SANGUINOLOLU_12.h" // ... SANGUINOLOLU_11
2 changes: 1 addition & 1 deletion Marlin/src/pins/sanguino/pins_GEN7_13.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@

#define BOARD_INFO_NAME "Gen7 v1.3"

#define GEN7_VERSION 13 // v1.3
#define GEN7_VERSION 13 // v1.3
#include "pins_GEN7_12.h"
2 changes: 1 addition & 1 deletion Marlin/src/pins/stm32f1/pins_MKS_ROBIN_E3_V1_1_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
#define Z_STEP_PIN PC14
#define Z_DIR_PIN PC15

#define BTN_ENC_EN -1
#define BTN_ENC_EN -1

#include "pins_MKS_ROBIN_E3_common.h"
6 changes: 3 additions & 3 deletions Marlin/src/pins/stm32f4/pins_BTT_SKR_V2_0_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,10 @@
#define BTN_EN2 EXP2_05_PIN

#ifndef TFT_WIDTH
#define TFT_WIDTH 480
#define TFT_WIDTH 480
#endif
#ifndef TFT_HEIGHT
#define TFT_HEIGHT 320
#define TFT_HEIGHT 320
#endif

#if ENABLED(BTT_TFT35_SPI_V1_0)
Expand Down Expand Up @@ -592,7 +592,7 @@
#define TFT_BACKLIGHT_PIN LCD_BACKLIGHT_PIN

#define TOUCH_BUTTONS_HW_SPI
#define TOUCH_BUTTONS_HW_SPI_DEVICE 1
#define TOUCH_BUTTONS_HW_SPI_DEVICE 1

#define TOUCH_CS_PIN EXP1_05_PIN // SPI1_NSS
#define TOUCH_SCK_PIN EXP2_02_PIN // SPI1_SCK
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/pins/stm32h7/pins_BTT_SKR_V3_0_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@
#define TFT_BACKLIGHT_PIN LCD_BACKLIGHT_PIN

#define TOUCH_BUTTONS_HW_SPI
#define TOUCH_BUTTONS_HW_SPI_DEVICE 1
#define TOUCH_BUTTONS_HW_SPI_DEVICE 1

#define TOUCH_CS_PIN EXP1_05_PIN // SPI1_NSS
#define TOUCH_SCK_PIN EXP2_02_PIN // SPI1_SCK
Expand Down
47 changes: 34 additions & 13 deletions buildroot/share/scripts/pinsformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ String.prototype.rpad = function(len, chr) {
return s;
};

// Concatenate a string, adding a space if necessary
// to avoid merging two words
String.prototype.concat_with_space = function(str) {
Copy link
Member

@thinkyhead thinkyhead Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason I have an urge to rename this "spoin".

if (this.charAt(this.length - 1) !== ' ' && str.charAt(0) !== ' ') {
return this + ' ' + str;
}
return this + str;
};

const mpatt = [ '-?\\d{1,3}', 'P[A-I]\\d+', 'P\\d_\\d+', 'Pin[A-Z]\\d\\b' ],
definePatt = new RegExp(`^\\s*(//)?#define\\s+[A-Z_][A-Z0-9_]+\\s+(${mpatt[0]}|${mpatt[1]}|${mpatt[2]}|${mpatt[3]})\\s*(//.*)?$`, 'gm'),
ppad = [ 3, 4, 5, 5 ],
Expand Down Expand Up @@ -56,6 +65,7 @@ else
// Find the pin pattern so non-pin defines can be skipped
function get_pin_pattern(txt) {
var r, m = 0, match_count = [ 0, 0, 0, 0 ];
var max_match_count = 0, max_match_index = -1;
definePatt.lastIndex = 0;
while ((r = definePatt.exec(txt)) !== null) {
let ind = -1;
Expand All @@ -65,11 +75,20 @@ function get_pin_pattern(txt) {
return r[2].match(p);
}) ) {
const m = ++match_count[ind];
if (m >= 5) {
return { match: mpatt[ind], pad:ppad[ind] };
if (m > max_match_count) {
max_match_count = m;
max_match_index = ind;
}
}
}
if (max_match_index !== -1) {
if (do_log) {
match_count.forEach((count, index) => {
console.log("Index: " + index, "Count: " + count, "Pattern: " + mpatt[index]);
});
}
return { match: mpatt[max_match_index], pad:ppad[max_match_index] };
}
return null;
}

Expand All @@ -79,7 +98,7 @@ function process_text(txt) {
if (!patt) return txt;
const pindefPatt = new RegExp(`^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+(${patt.match})\\s*(//.*)?$`),
noPinPatt = new RegExp(`^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+(-1)\\s*(//.*)?$`),
skipPatt1 = new RegExp('^(\\s*(//)?#define)\\s+(AT90USB|USBCON|(BOARD|DAC|FLASH|HAS|IS|USE)_.+|.+_(ADDRESS|AVAILABLE|BAUDRATE|CLOCK|CONNECTION|DEFAULT|FREQ|ITEM|MODULE|NAME|ONLY|PERIOD|RANGE|RATE|SERIAL|SIZE|SPI|STATE|STEP|TIMER))\\s+(.+)\\s*(//.*)?$'),
skipPatt1 = new RegExp('^(\\s*(//)?#define)\\s+(AT90USB|USBCON|(BOARD|DAC|FLASH|HAS|IS|USE)_.+|.+_(ADDRESS|AVAILABLE|BASE_VERSION|BAUDRATE|CLOCK|CONNECTION|DEFAULT|ERROR|EXTRUDERS|FREQ|ITEM|MKS_BASE_VERSION|MODULE|NAME|ONLY|ORIENTATION|PERIOD|RANGE|RATE|READ_RETRIES|SERIAL|SIZE|SPI|STATE|STEP|TIMER))\\s+(.+)\\s*(//.*)?$'),
skipPatt2 = new RegExp('^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+(0x[0-9A-Fa-f]+|\d+|.+[a-z].+)\\s*(//.*)?$'),
aliasPatt = new RegExp('^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+([A-Z_][A-Z0-9_()]+)\\s*(//.*)?$'),
switchPatt = new RegExp('^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s*(//.*)?$'),
Expand Down Expand Up @@ -110,17 +129,17 @@ function process_text(txt) {
if (do_log) console.log("pin:", line);
const pinnum = r[4].charAt(0) == 'P' ? r[4] : r[4].lpad(patt.pad);
line = r[1] + ' ' + r[3];
line = line.rpad(col_value_lj) + pinnum;
if (r[5]) line = line.rpad(col_comment) + r[5];
line = line.rpad(col_value_lj).concat_with_space(pinnum);
if (r[5]) line = line.rpad(col_comment).concat_with_space(r[5]);
}
else if ((r = noPinPatt.exec(line)) !== null) {
//
// #define MY_PIN -1
//
if (do_log) console.log("pin -1:", line);
line = r[1] + ' ' + r[3];
line = line.rpad(col_value_lj) + '-1';
if (r[5]) line = line.rpad(col_comment) + r[5];
line = line.rpad(col_value_lj).concat_with_space('-1');
if (r[5]) line = line.rpad(col_comment).concat_with_space(r[5]);
}
else if (skipPatt2.exec(line) !== null) {
//
Expand All @@ -134,16 +153,18 @@ function process_text(txt) {
//
if (do_log) console.log("alias:", line);
line = r[1] + ' ' + r[3];
line += r[4].lpad(col_value_rj + 1 - line.length);
if (r[5]) line = line.rpad(col_comment) + r[5];
line = line.concat_with_space(r[4].lpad(col_value_rj + 1 - line.length));
if (r[5]) {
line = line.rpad(col_comment).concat_with_space(r[5]);
}
sjasonsmith marked this conversation as resolved.
Show resolved Hide resolved
}
else if ((r = switchPatt.exec(line)) !== null) {
//
// #define SWITCH
//
if (do_log) console.log("switch:", line);
line = r[1] + ' ' + r[3];
if (r[4]) line = line.rpad(col_comment) + r[4];
if (r[4]) line = line.rpad(col_comment).concat_with_space(r[4]);
check_comment_next = true;
}
else if ((r = defPatt.exec(line)) !== null) {
Expand All @@ -152,7 +173,7 @@ function process_text(txt) {
//
if (do_log) console.log("def:", line);
line = r[1] + ' ' + r[3] + ' ';
line += r[4].lpad(col_value_rj + 1 - line.length);
line = line.concat_with_space(r[4].lpad(col_value_rj + 1 - line.length));
if (r[5]) line = line.rpad(col_comment - 1) + ' ' + r[5];
}
else if ((r = undefPatt.exec(line)) !== null) {
Expand All @@ -161,14 +182,14 @@ function process_text(txt) {
//
if (do_log) console.log("undef:", line);
line = r[1] + ' ' + r[3];
if (r[4]) line = line.rpad(col_comment) + r[4];
if (r[4]) line = line.rpad(col_comment).concat_with_space(r[4]);
}
else if ((r = condPatt.exec(line)) !== null) {
//
// #if ...
//
if (do_log) console.log("cond:", line);
line = r[1].rpad(col_comment) + r[5];
line = r[1].rpad(col_comment).concat_with_space(r[5]);
check_comment_next = true;
}
out += line + '\n';
Expand Down