arduino-libraries / SD

SD Library for Arduino
http://arduino.cc/
GNU General Public License v3.0
182 stars 154 forks source link

Update ReadWrite.ino #73

Closed lorforlinux closed 4 years ago

lorforlinux commented 4 years ago

There is an incompatibility issue for showing the initialization error message for the examples.

The example CardInfo.ino has this code (which i personally like)

if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    while (1);
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }

While the other example, like the ReadWrite.ino uses this code

if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

It's a better idea to make one function in the library for doing this instead of writing similar code in all the examples. I will work on updating the library code if others agree with me on this :)

TravisBuddy commented 4 years ago

Travis tests have failed

Hey @deepaklorkhatri007, Please read the following log in order to understand the failure reason. It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: e9d27ce0-5572-11ea-850c-e5458713aaa0
lorforlinux commented 4 years ago

Hi, how can I create a local setup for fixing the issue of formatting in examples?

per1234 commented 4 years ago

@deepaklorkhatri007 in most cases, including this one (trailing space on line 44 of examples/ReadWrite/ReadWrite.ino), just doing a Tools > Auto Format in the Arduino IDE will provide compliant formatting.

I think it would be nice to fix the trailing space in the example, but you don't need to worry about the non-compliant formatting in src/utility/SdFat.h, as that was not caused by your PR.


Just in case you're interested in the details, or don't want to use the Arduino IDE:

The same formatter tool is used for this CI build as the Arduino IDE: Artistic Style. You can download it here: https://sourceforge.net/projects/astyle/files installation instructions here: http://astyle.sourceforge.net/install.html

The configuration file we use for the CI builds is available here: https://raw.githubusercontent.com/arduino/Arduino/master/build/shared/examples_formatter.conf

The documentation for Artistic Style is here: http://astyle.sourceforge.net/astyle.html

If you wanted to format all .ino files recursively from the current directory, using the examples_formatter.conf configuration file, without making backups, the command would look something like this:

astyle --suffix=none --options="examples_formatter.conf" --recursive  ./*.pde,*.ino
lorforlinux commented 4 years ago

Hi @per1234, thank you so much for the details.

I think it would be nice to fix the trailing space in the example, but you don't need to worry about the non-compliant formatting in src/utility/SdFat.h, as that was not caused by your PR.

yes, the build was failing before my pull request.

just doing a Tools > Auto Format in the Arduino IDE will provide compliant formatting.

I will try to fix the issue :)

lorforlinux commented 4 years ago

All checks are passing now, I am using Fedora Linux and astyle is already installed on my machine. If it was not installed i may have used

sudo dnf install astyle

For fixing the formatting we can directly use the command suggested by @per1234 (I did it one by one to get to know the tool better)

For formatting all the files from in SD/ directory run

$ cd SD/
$ astyle --suffix=none --options="examples_formatter.conf" --recursive  ./*.pde,*.ino -v

For doing it one by one run

$ cd {folder}
$ astyle --suffix=none --options="examples_formatter.conf" --recursive  {file}.ino -v

Now I want to fix the code redundancy of the error message if everybody agrees on it. Basically I am going to create one function which we can use to see if the initialization is successful or not.