Skip to content
This repository has been archived by the owner on Jul 25, 2019. It is now read-only.

Commit

Permalink
Support soft SPI on any pins with Arduino Due
Browse files Browse the repository at this point in the history
Merged SD library from Arduino 1.5.4 with Adafruit SD library.
Resulting library allows soft SPI on any four pins with Arduino Due.
Backwards compatible with former Adfafruit_SD library - Uno, Mega,
Leonardo, etc. still supported.
  • Loading branch information
driverblock committed Nov 5, 2013
1 parent 3216c04 commit d33ff08
Show file tree
Hide file tree
Showing 19 changed files with 4,471 additions and 4,457 deletions.
49 changes: 13 additions & 36 deletions File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
uint8_t nfilecount=0;
*/

File::File(SdFile f, char *n) {
File::File(SdFile f, const char *n) {
// oh man you are kidding me, new() doesnt exist? Ok we do it by hand!
_file = (SdFile *)malloc(sizeof(SdFile));
if (_file) {
Expand Down Expand Up @@ -58,48 +58,25 @@ boolean File::isDirectory(void) {
}


#if ARDUINO >= 100

size_t File::write(uint8_t val) {
if (_file)
return _file->write(val);
else
return 0;
}

size_t File::write(const char *str) {
if (_file)
return _file->write(str);
else
return 0;
return write(&val, 1);
}

size_t File::write(const uint8_t *buf, size_t size) {
if (_file)
return _file->write(buf, size);
else
size_t t;
if (!_file) {
setWriteError();
return 0;
}
_file->clearWriteError();
t = _file->write(buf, size);
if (_file->getWriteError()) {
setWriteError();
return 0;
}
return t;
}

#else

void File::write(uint8_t val) {
if (_file)
_file->write(val);
}

void File::write(const char *str) {
if (_file)
_file->write(str);
}

void File::write(const uint8_t *buf, size_t size) {
if (_file)
_file->write(buf, size);
}

#endif // ARDUINO

int File::peek() {
if (! _file)
return 0;
Expand Down
6 changes: 3 additions & 3 deletions SD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ boolean SDClass::begin(uint8_t csPin, int8_t mosi, int8_t miso, int8_t sck) {


// this little helper is used to traverse paths
SdFile SDClass::getParentDir(char *filepath, int *index) {
SdFile SDClass::getParentDir(const char *filepath, int *index) {
// get parent directory
SdFile d1 = root; // start with the mostparent, root!
SdFile d2;
Expand All @@ -357,7 +357,7 @@ SdFile SDClass::getParentDir(char *filepath, int *index) {
SdFile *parent = &d1;
SdFile *subdir = &d2;

char *origpath = filepath;
const char *origpath = filepath;

while (strchr(filepath, '/')) {

Expand Down Expand Up @@ -404,7 +404,7 @@ SdFile SDClass::getParentDir(char *filepath, int *index) {
}


File SDClass::open(char *filepath, uint8_t mode) {
File SDClass::open(const char *filepath, uint8_t mode) {
/*
Open the supplied file path for reading or writing.
Expand Down
16 changes: 8 additions & 8 deletions SD.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
#define __SD_H__

#if ARDUINO >= 100
#include "Arduino.h"
#include "Arduino.h"
#else
#include "WProgram.h"
#include "WProgram.h"
#endif

#include <utility/SdFat.h>
Expand All @@ -33,16 +33,14 @@ class File : public Stream {
SdFile *_file; // underlying file pointer

public:
File(SdFile f, char *name); // wraps an underlying SdFile
File(SdFile f, const char *name); // wraps an underlying SdFile
File(void); // 'empty' constructor
~File(void); // destructor
#if ARDUINO >= 100
virtual size_t write(uint8_t);
virtual size_t write(const char *str);
virtual size_t write(const uint8_t *buf, size_t size);
#else
virtual void write(uint8_t);
virtual void write(const char *str);
virtual void write(const uint8_t *buf, size_t size);
#endif
virtual int read();
Expand All @@ -60,6 +58,8 @@ class File : public Stream {
boolean isDirectory(void);
File openNextFile(uint8_t mode = O_RDONLY);
void rewindDirectory(void);

using Print::write;
};

class SDClass {
Expand All @@ -71,7 +71,7 @@ class SDClass {
SdFile root;

// my quick&dirty iterator, should be replaced
SdFile getParentDir(char *filepath, int *indx);
SdFile getParentDir(const char *filepath, int *indx);
public:
// This needs to be called to set up the connection to the SD card
// before other methods are used.
Expand All @@ -80,7 +80,7 @@ class SDClass {
// Open the specified file/directory with the supplied mode (e.g. read or
// write, etc). Returns a File object for interacting with the file.
// Note that currently only one file can be open at a time.
File open(char *filename, uint8_t mode = FILE_READ);
File open(const char *filename, uint8_t mode = FILE_READ);

// Methods to determine if the requested file path exists.
boolean exists(char *filepath);
Expand All @@ -93,7 +93,7 @@ class SDClass {
boolean remove(char *filepath);

boolean rmdir(char *filepath);

void enableCRC(boolean mode);

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
modified 9 Apr 2012 by Tom Igoe
*/
// include the SD library:
#include <SPI.h>
#include <SD.h>

// set up variables using the SD utility library functions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

#include <SPI.h>
#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#include <SPI.h>
#include <SD.h>

// change this to match your SD shield or module;
Expand Down
1 change: 1 addition & 0 deletions examples/Files/Files.pde → examples/Files/Files.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>

File myFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include <SPI.h>
#include <SD.h>

File myFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>

File root;
Expand Down
Loading

0 comments on commit d33ff08

Please sign in to comment.