Skip to content

Commit 4bbbf5c

Browse files
committed
Use ClangFormat as code formatter for examples
The code formatting tool used by the Arduino IDE 1.x "Auto Format" feature is "Artistic Style". This was changed to ClangFormat in Arduino IDE 2.x. Now that the stable Arduino IDE 2.0.0 release has been made, this is now the official code formatting tool for use with Arduino sketches. Due to differences in the two tools, some unavoidable minor changes occurred in the formatting style. This meant that the Arduino IDE 2.x user may experience some unexpected changes to the unmodified official example sketches when running an "Auto Format". So the examples in this repository must be formatted to comply with the updated Arduino code style. The changes are mostly the result of ClangFormat being more "opinionated" than "Artistic Style" was under the fairly light-handed formatter configuration used by Arduino IDE 1.x Auto Format. Due to this factor, formatting changes required to bring the examples into compliance with the new style mostly do not conflict with the Arduino IDE 1.x style. The only exception is the more complex code of the ArduinoISP sketch. In this case, the sketch is more of a utility program than an example, so the formatting changes will not be impactful for users who continue to use the outdated Arduino IDE version.
1 parent f109942 commit 4bbbf5c

File tree

75 files changed

+701
-556
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+701
-556
lines changed

Diff for: .clang-format

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/tree/main/other/clang-format-configuration
2+
---
3+
AccessModifierOffset: -2
4+
AlignAfterOpenBracket: Align
5+
AlignArrayOfStructures: None
6+
AlignConsecutiveAssignments: None
7+
AlignConsecutiveBitFields: None
8+
AlignConsecutiveDeclarations: None
9+
AlignConsecutiveMacros: None
10+
AlignEscapedNewlines: DontAlign
11+
AlignOperands: Align
12+
AlignTrailingComments: true
13+
AllowAllArgumentsOnNextLine: true
14+
AllowAllConstructorInitializersOnNextLine: true
15+
AllowAllParametersOfDeclarationOnNextLine: true
16+
AllowShortBlocksOnASingleLine: Always
17+
AllowShortCaseLabelsOnASingleLine: true
18+
AllowShortEnumsOnASingleLine: true
19+
AllowShortFunctionsOnASingleLine: Empty
20+
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
21+
AllowShortLambdasOnASingleLine: Empty
22+
AllowShortLoopsOnASingleLine: true
23+
AlwaysBreakAfterDefinitionReturnType: None
24+
AlwaysBreakAfterReturnType: None
25+
AlwaysBreakBeforeMultilineStrings: false
26+
AlwaysBreakTemplateDeclarations: No
27+
AttributeMacros:
28+
- __capability
29+
BasedOnStyle: LLVM
30+
BinPackArguments: true
31+
BinPackParameters: true
32+
BitFieldColonSpacing: Both
33+
BraceWrapping:
34+
AfterCaseLabel: false
35+
AfterClass: false
36+
AfterControlStatement: Never
37+
AfterEnum: false
38+
AfterFunction: false
39+
AfterNamespace: false
40+
AfterObjCDeclaration: false
41+
AfterStruct: false
42+
AfterUnion: false
43+
AfterExternBlock: false
44+
BeforeCatch: false
45+
BeforeElse: false
46+
BeforeLambdaBody: false
47+
BeforeWhile: false
48+
IndentBraces: false
49+
SplitEmptyFunction: true
50+
SplitEmptyRecord: true
51+
SplitEmptyNamespace: true
52+
BreakAfterJavaFieldAnnotations: false
53+
BreakBeforeBinaryOperators: NonAssignment
54+
BreakBeforeBraces: Attach
55+
BreakBeforeConceptDeclarations: false
56+
BreakBeforeInheritanceComma: false
57+
BreakBeforeTernaryOperators: true
58+
BreakConstructorInitializers: BeforeColon
59+
BreakConstructorInitializersBeforeComma: false
60+
BreakInheritanceList: BeforeColon
61+
BreakStringLiterals: false
62+
ColumnLimit: 0
63+
CommentPragmas: ''
64+
CompactNamespaces: false
65+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
66+
ConstructorInitializerIndentWidth: 2
67+
ContinuationIndentWidth: 2
68+
Cpp11BracedListStyle: false
69+
DeriveLineEnding: true
70+
DerivePointerAlignment: true
71+
DisableFormat: false
72+
EmptyLineAfterAccessModifier: Leave
73+
EmptyLineBeforeAccessModifier: Leave
74+
ExperimentalAutoDetectBinPacking: false
75+
FixNamespaceComments: false
76+
ForEachMacros:
77+
- foreach
78+
- Q_FOREACH
79+
- BOOST_FOREACH
80+
IfMacros:
81+
- KJ_IF_MAYBE
82+
IncludeBlocks: Preserve
83+
IncludeCategories:
84+
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
85+
Priority: 2
86+
SortPriority: 0
87+
CaseSensitive: false
88+
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
89+
Priority: 3
90+
SortPriority: 0
91+
CaseSensitive: false
92+
- Regex: '.*'
93+
Priority: 1
94+
SortPriority: 0
95+
CaseSensitive: false
96+
IncludeIsMainRegex: ''
97+
IncludeIsMainSourceRegex: ''
98+
IndentAccessModifiers: false
99+
IndentCaseBlocks: true
100+
IndentCaseLabels: true
101+
IndentExternBlock: Indent
102+
IndentGotoLabels: false
103+
IndentPPDirectives: None
104+
IndentRequires: true
105+
IndentWidth: 2
106+
IndentWrappedFunctionNames: false
107+
InsertTrailingCommas: None
108+
JavaScriptQuotes: Leave
109+
JavaScriptWrapImports: true
110+
KeepEmptyLinesAtTheStartOfBlocks: true
111+
LambdaBodyIndentation: Signature
112+
Language: Cpp
113+
MacroBlockBegin: ''
114+
MacroBlockEnd: ''
115+
MaxEmptyLinesToKeep: 100000
116+
NamespaceIndentation: None
117+
ObjCBinPackProtocolList: Auto
118+
ObjCBlockIndentWidth: 2
119+
ObjCBreakBeforeNestedBlockParam: true
120+
ObjCSpaceAfterProperty: false
121+
ObjCSpaceBeforeProtocolList: true
122+
PPIndentWidth: -1
123+
PackConstructorInitializers: BinPack
124+
PenaltyBreakAssignment: 1
125+
PenaltyBreakBeforeFirstCallParameter: 1
126+
PenaltyBreakComment: 1
127+
PenaltyBreakFirstLessLess: 1
128+
PenaltyBreakOpenParenthesis: 1
129+
PenaltyBreakString: 1
130+
PenaltyBreakTemplateDeclaration: 1
131+
PenaltyExcessCharacter: 1
132+
PenaltyIndentedWhitespace: 1
133+
PenaltyReturnTypeOnItsOwnLine: 1
134+
PointerAlignment: Right
135+
QualifierAlignment: Leave
136+
ReferenceAlignment: Pointer
137+
ReflowComments: false
138+
RemoveBracesLLVM: false
139+
SeparateDefinitionBlocks: Leave
140+
ShortNamespaceLines: 0
141+
SortIncludes: Never
142+
SortJavaStaticImport: Before
143+
SortUsingDeclarations: false
144+
SpaceAfterCStyleCast: false
145+
SpaceAfterLogicalNot: false
146+
SpaceAfterTemplateKeyword: false
147+
SpaceAroundPointerQualifiers: Default
148+
SpaceBeforeAssignmentOperators: true
149+
SpaceBeforeCaseColon: false
150+
SpaceBeforeCpp11BracedList: false
151+
SpaceBeforeCtorInitializerColon: true
152+
SpaceBeforeInheritanceColon: true
153+
SpaceBeforeParens: ControlStatements
154+
SpaceBeforeParensOptions:
155+
AfterControlStatements: true
156+
AfterForeachMacros: true
157+
AfterFunctionDefinitionName: false
158+
AfterFunctionDeclarationName: false
159+
AfterIfMacros: true
160+
AfterOverloadedOperator: false
161+
BeforeNonEmptyParentheses: false
162+
SpaceBeforeRangeBasedForLoopColon: true
163+
SpaceBeforeSquareBrackets: false
164+
SpaceInEmptyBlock: false
165+
SpaceInEmptyParentheses: false
166+
SpacesBeforeTrailingComments: 2
167+
SpacesInAngles: Leave
168+
SpacesInCStyleCastParentheses: false
169+
SpacesInConditionalStatement: false
170+
SpacesInContainerLiterals: false
171+
SpacesInLineCommentPrefix:
172+
Minimum: 0
173+
Maximum: -1
174+
SpacesInParentheses: false
175+
SpacesInSquareBrackets: false
176+
Standard: Auto
177+
StatementAttributeLikeMacros:
178+
- Q_EMIT
179+
StatementMacros:
180+
- Q_UNUSED
181+
- QT_REQUIRE_VERSION
182+
TabWidth: 2
183+
UseCRLF: false
184+
UseTab: Never
185+
WhitespaceSensitiveMacros:
186+
- STRINGIZE
187+
- PP_STRINGIZE
188+
- BOOST_PP_STRINGIZE
189+
- NS_SWIFT_NAME
190+
- CF_SWIFT_NAME

Diff for: .github/workflows/code-formatting-check.yml

+20-17
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ on:
44
pull_request:
55
paths:
66
- ".github/workflows/code-formatting-check.yml"
7-
- "examples_formatter.conf"
7+
- ".clang-format"
88
- "examples_formatter.sh"
99
- "examples/**"
1010
push:
1111
paths:
1212
- ".github/workflows/code-formatting-check.yml"
13-
- "examples_formatter.conf"
13+
- ".clang-format"
1414
- "examples_formatter.sh"
1515
- "examples/**"
1616

@@ -19,31 +19,34 @@ jobs:
1919
runs-on: ubuntu-latest
2020

2121
env:
22-
# See: https://sourceforge.net/projects/astyle/files/astyle/
23-
ASTYLE_VERSION: 3.1
22+
# See: https://github.com/arduino/arduino-ide/blob/main/arduino-ide-extension/package.json
23+
CLANG_FORMAT_VERSION: 14.0.0
2424

2525
steps:
26+
- name: Set environment variables
27+
run: |
28+
# See: https://docs.github.com/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable
29+
echo "CLANG_FORMAT_INSTALL_PATH=${{ runner.temp }}/clang-format" >> "$GITHUB_ENV"
30+
2631
- name: Checkout
2732
uses: actions/checkout@v3
2833

29-
# See: http://astyle.sourceforge.net/
30-
- name: Download Artistic Style
31-
uses: carlosperate/download-file-action@v1
34+
- name: Download ClangFormat
35+
id: download
36+
uses: MrOctopus/download-asset-action@1.0
3237
with:
33-
file-url: https://cfhcable.dl.sourceforge.net/project/astyle/astyle/astyle%20${{ env.ASTYLE_VERSION }}/astyle_${{ env.ASTYLE_VERSION }}_linux.tar.gz
34-
location: ${{ runner.temp }}/astyle
38+
repository: arduino/clang-static-binaries
39+
tag: ${{ env.CLANG_FORMAT_VERSION }}
40+
asset: clang-format_${{ env.CLANG_FORMAT_VERSION }}_Linux_64bit.tar.bz2
41+
target: ${{ env.CLANG_FORMAT_INSTALL_PATH }}
3542

36-
# See: http://astyle.sourceforge.net/install.html#_GCC_Makefile
37-
- name: Build Artistic Style
43+
- name: Install ClangFormat
3844
run: |
39-
# Build Artistic Style
40-
cd "${{ runner.temp }}/astyle"
41-
tar --extract --file="astyle_${{ env.ASTYLE_VERSION }}_linux.tar.gz"
42-
cd "astyle/build/gcc"
43-
make
45+
cd "${{ env.CLANG_FORMAT_INSTALL_PATH }}"
46+
tar --extract --file="${{ steps.download.outputs.name }}"
4447
# Add installation to PATH:
4548
# See: https://docs.github.com/actions/using-workflows/workflow-commands-for-github-actions#adding-a-system-path
46-
echo "${{ runner.temp }}/astyle/astyle/build/gcc/bin" >> "$GITHUB_PATH"
49+
echo "${{ env.CLANG_FORMAT_INSTALL_PATH }}/clang_Linux_64bit" >> "$GITHUB_PATH"
4750
4851
- name: Format examples
4952
run: ./examples_formatter.sh

Diff for: examples/01.Basics/AnalogReadSerial/AnalogReadSerial.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ void loop() {
2222
int sensorValue = analogRead(A0);
2323
// print out the value you read:
2424
Serial.println(sensorValue);
25-
delay(1); // delay in between reads for stability
25+
delay(1); // delay in between reads for stability
2626
}

Diff for: examples/01.Basics/BareMinimum/BareMinimum.ino

-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
void setup() {
22
// put your setup code here, to run once:
3-
43
}
54

65
void loop() {
76
// put your main code here, to run repeatedly:
8-
97
}

Diff for: examples/01.Basics/Blink/Blink.ino

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ void setup() {
3030

3131
// the loop function runs over and over again forever
3232
void loop() {
33-
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
34-
delay(1000); // wait for a second
35-
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
36-
delay(1000); // wait for a second
33+
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
34+
delay(1000); // wait for a second
35+
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
36+
delay(1000); // wait for a second
3737
}

Diff for: examples/01.Basics/DigitalReadSerial/DigitalReadSerial.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ void loop() {
2525
int buttonState = digitalRead(pushButton);
2626
// print out the state of the button:
2727
Serial.println(buttonState);
28-
delay(1); // delay in between reads for stability
28+
delay(1); // delay in between reads for stability
2929
}

Diff for: examples/01.Basics/Fade/Fade.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Fade
1414
*/
1515

16-
int led = 9; // the PWM pin the LED is attached to
17-
int brightness = 0; // how bright the LED is
18-
int fadeAmount = 5; // how many points to fade the LED by
16+
int led = 9; // the PWM pin the LED is attached to
17+
int brightness = 0; // how bright the LED is
18+
int fadeAmount = 5; // how many points to fade the LED by
1919

2020
// the setup routine runs once when you press reset:
2121
void setup() {

Diff for: examples/02.Digital/BlinkWithoutDelay/BlinkWithoutDelay.ino

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@
2929
*/
3030

3131
// constants won't change. Used here to set a pin number:
32-
const int ledPin = LED_BUILTIN;// the number of the LED pin
32+
const int ledPin = LED_BUILTIN; // the number of the LED pin
3333

3434
// Variables will change:
35-
int ledState = LOW; // ledState used to set the LED
35+
int ledState = LOW; // ledState used to set the LED
3636

3737
// Generally, you should use "unsigned long" for variables that hold time
3838
// The value will quickly become too large for an int to store
39-
unsigned long previousMillis = 0; // will store last time LED was updated
39+
unsigned long previousMillis = 0; // will store last time LED was updated
4040

4141
// constants won't change:
42-
const long interval = 1000; // interval at which to blink (milliseconds)
42+
const long interval = 1000; // interval at which to blink (milliseconds)
4343

4444
void setup() {
4545
// set the digital pin as output:

Diff for: examples/02.Digital/Button/Button.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
*/
2424

2525
// constants won't change. They're used here to set pin numbers:
26-
const int buttonPin = 2; // the number of the pushbutton pin
27-
const int ledPin = 13; // the number of the LED pin
26+
const int buttonPin = 2; // the number of the pushbutton pin
27+
const int ledPin = 13; // the number of the LED pin
2828

2929
// variables will change:
30-
int buttonState = 0; // variable for reading the pushbutton status
30+
int buttonState = 0; // variable for reading the pushbutton status
3131

3232
void setup() {
3333
// initialize the LED pin as an output:

Diff for: examples/02.Digital/Debounce/Debounce.ino

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
*/
2929

3030
// constants won't change. They're used here to set pin numbers:
31-
const int buttonPin = 2; // the number of the pushbutton pin
32-
const int ledPin = 13; // the number of the LED pin
31+
const int buttonPin = 2; // the number of the pushbutton pin
32+
const int ledPin = 13; // the number of the LED pin
3333

3434
// Variables will change:
35-
int ledState = HIGH; // the current state of the output pin
36-
int buttonState; // the current reading from the input pin
37-
int lastButtonState = LOW; // the previous reading from the input pin
35+
int ledState = HIGH; // the current state of the output pin
36+
int buttonState; // the current reading from the input pin
37+
int lastButtonState = LOW; // the previous reading from the input pin
3838

3939
// the following variables are unsigned longs because the time, measured in
4040
// milliseconds, will quickly become a bigger number than can be stored in an int.

Diff for: examples/02.Digital/DigitalInputPullup/DigitalInputPullup.ino

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ void setup() {
2626
//configure pin 2 as an input and enable the internal pull-up resistor
2727
pinMode(2, INPUT_PULLUP);
2828
pinMode(13, OUTPUT);
29-
3029
}
3130

3231
void loop() {

Diff for: examples/02.Digital/StateChangeDetection/StateChangeDetection.ino

+5-6
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
*/
2626

2727
// this constant won't change:
28-
const int buttonPin = 2; // the pin that the pushbutton is attached to
29-
const int ledPin = 13; // the pin that the LED is attached to
28+
const int buttonPin = 2; // the pin that the pushbutton is attached to
29+
const int ledPin = 13; // the pin that the LED is attached to
3030

3131
// Variables will change:
32-
int buttonPushCounter = 0; // counter for the number of button presses
33-
int buttonState = 0; // current state of the button
34-
int lastButtonState = 0; // previous state of the button
32+
int buttonPushCounter = 0; // counter for the number of button presses
33+
int buttonState = 0; // current state of the button
34+
int lastButtonState = 0; // previous state of the button
3535

3636
void setup() {
3737
// initialize the button pin as a input:
@@ -75,5 +75,4 @@ void loop() {
7575
} else {
7676
digitalWrite(ledPin, LOW);
7777
}
78-
7978
}

0 commit comments

Comments
 (0)