First Commit

This commit is contained in:
MindCreeper03
2025-02-27 19:31:50 +01:00
parent bcbb6aff9a
commit e490df1715
2470 changed files with 1479965 additions and 0 deletions

View File

@@ -0,0 +1,197 @@
// A simple data logger for the Arduino analog pins with optional DS1307
// uses RTClib from https://github.com/adafruit/RTClib
#include <SPI.h>
#include "SdFat.h"
#include "sdios.h"
#include "FreeStack.h"
#define SD_CHIP_SELECT SS // SD chip select pin
#define USE_DS1307 0 // set nonzero to use DS1307 RTC
#define LOG_INTERVAL 1000 // mills between entries
#define SENSOR_COUNT 3 // number of analog pins to log
#define ECHO_TO_SERIAL 1 // echo data to serial port if nonzero
#define WAIT_TO_START 1 // Wait for serial input in setup()
#define ADC_DELAY 10 // ADC delay for high impedence sensors
// file system object
SdFat sd;
// text file for logging
ofstream logfile;
// Serial print stream
ArduinoOutStream cout(Serial);
// buffer to format data - makes it eaiser to echo to Serial
char buf[80];
//------------------------------------------------------------------------------
#if SENSOR_COUNT > 6
#error SENSOR_COUNT too large
#endif // SENSOR_COUNT
//------------------------------------------------------------------------------
// store error strings in flash to save RAM
#define error(s) sd.errorHalt(F(s))
//------------------------------------------------------------------------------
#if USE_DS1307
// use RTClib from Adafruit
// https://github.com/adafruit/RTClib
// The Arduino IDE has a bug that causes Wire and RTClib to be loaded even
// if USE_DS1307 is false.
#error remove this line and uncomment the next two lines.
//#include <Wire.h>
//#include <RTClib.h>
RTC_DS1307 RTC; // define the Real Time Clock object
//------------------------------------------------------------------------------
// call back for file timestamps
void dateTime(uint16_t* date, uint16_t* time) {
DateTime now = RTC.now();
// return date using FAT_DATE macro to format fields
*date = FAT_DATE(now.year(), now.month(), now.day());
// return time using FAT_TIME macro to format fields
*time = FAT_TIME(now.hour(), now.minute(), now.second());
}
//------------------------------------------------------------------------------
// format date/time
ostream& operator << (ostream& os, DateTime& dt) {
os << dt.year() << '/' << int(dt.month()) << '/' << int(dt.day()) << ',';
os << int(dt.hour()) << ':' << setfill('0') << setw(2) << int(dt.minute());
os << ':' << setw(2) << int(dt.second()) << setfill(' ');
return os;
}
#endif // USE_DS1307
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
// Wait for USB Serial.
while (!Serial) {
yield();
}
// F() stores strings in flash to save RAM
cout << endl << F("FreeStack: ") << FreeStack() << endl;
#if WAIT_TO_START
cout << F("Type any character to start\n");
while (!Serial.available()) {
yield();
}
// Discard input.
do {
delay(10);
} while(Serial.available() && Serial.read() >= 0);
#endif // WAIT_TO_START
#if USE_DS1307
// connect to RTC
Wire.begin();
if (!RTC.begin()) {
error("RTC failed");
}
// set date time callback function
SdFile::dateTimeCallback(dateTime);
DateTime now = RTC.now();
cout << now << endl;
#endif // USE_DS1307
// Initialize at the highest speed supported by the board that is
// not over 50 MHz. Try a lower speed if SPI errors occur.
if (!sd.begin(SD_CHIP_SELECT, SD_SCK_MHZ(50))) {
sd.initErrorHalt();
}
// create a new file in root, the current working directory
char name[] = "logger00.csv";
for (uint8_t i = 0; i < 100; i++) {
name[6] = i/10 + '0';
name[7] = i%10 + '0';
if (sd.exists(name)) {
continue;
}
logfile.open(name);
break;
}
if (!logfile.is_open()) {
error("file.open");
}
cout << F("Logging to: ") << name << endl;
cout << F("Type any character to stop\n\n");
// format header in buffer
obufstream bout(buf, sizeof(buf));
bout << F("millis");
#if USE_DS1307
bout << F(",date,time");
#endif // USE_DS1307
for (uint8_t i = 0; i < SENSOR_COUNT; i++) {
bout << F(",sens") << int(i);
}
logfile << buf << endl;
#if ECHO_TO_SERIAL
cout << buf << endl;
#endif // ECHO_TO_SERIAL
}
//------------------------------------------------------------------------------
void loop() {
uint32_t m;
// wait for time to be a multiple of interval
do {
m = millis();
} while (m % LOG_INTERVAL);
// use buffer stream to format line
obufstream bout(buf, sizeof(buf));
// start with time in millis
bout << m;
#if USE_DS1307
DateTime now = RTC.now();
bout << ',' << now;
#endif // USE_DS1307
// read analog pins and format data
for (uint8_t ia = 0; ia < SENSOR_COUNT; ia++) {
#if ADC_DELAY
analogRead(ia);
delay(ADC_DELAY);
#endif // ADC_DELAY
bout << ',' << analogRead(ia);
}
bout << endl;
// log data and flush to SD
logfile << buf << flush;
// check for error
if (!logfile) {
error("write data failed");
}
#if ECHO_TO_SERIAL
cout << buf;
#endif // ECHO_TO_SERIAL
// don't log two points in the same millis
if (m == millis()) {
delay(1);
}
if (!Serial.available()) {
return;
}
logfile.close();
cout << F("Done!");
while (true) {}
}

View File

@@ -0,0 +1,46 @@
/*
* Program to test Short File Name character case flags.
*/
#include <SPI.h>
#include "SdFat.h"
const uint8_t chipSelect = SS;
SdFat sd;
SdFile file;
const char* name[] = {
"low.low", "low.Mix", "low.UP",
"Mix.low", "Mix.Mix", "Mix.UP",
"UP.low", "UP.Mix", "UP.UP"
};
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
// Wait for USB Serial
while (!Serial) {
yield();
}
Serial.println("type any character to start");
while (!Serial.available()) {
yield();
}
if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
Serial.println("begin failed");
return;
}
for (uint8_t i = 0; i < 9; i++) {
sd.remove(name[i]);
if (!file.open(name[i], O_RDWR | O_CREAT | O_EXCL)) {
sd.errorHalt(name[i]);
}
file.println(name[i]);
file.close();
}
sd.ls(LS_DATE|LS_SIZE);
Serial.println("Done");
}
//------------------------------------------------------------------------------
void loop() {}

View File

@@ -0,0 +1,20 @@
#include <SPI.h>
#include "SdFat.h"
#include "sdios.h"
// create a serial output stream
ArduinoOutStream cout(Serial);
void setup() {
Serial.begin(9600);
// Wait for USB Serial
while (!Serial) {
yield();
}
delay(2000);
cout << "Hello, World!\n";
}
void loop() {}

View File

@@ -0,0 +1,29 @@
// This example illustrates use of SdFat's
// minimal unbuffered AVR Serial support.
//
// This is useful for debug and saves RAM
// Will not work on Due, Leonardo, or Teensy
#include <SPI.h>
#include "SdFat.h"
#include "FreeStack.h"
#ifdef UDR0 // Must be AVR with serial port zero.
#include "MinimumSerial.h"
MinimumSerial MiniSerial;
void setup() {
MiniSerial.begin(9600);
MiniSerial.println(FreeStack());
}
void loop() {
int c;
MiniSerial.println(F("Type any Character"));
while ((c = MiniSerial.read()) < 0) {}
MiniSerial.print(F("Read: "));
MiniSerial.println((char)c);
while (MiniSerial.read() >= 0) {}
}
#else // UDR0
#error no AVR serial port 0
#endif // UDR0

View File

@@ -0,0 +1,125 @@
/*
* This program is a simple Print benchmark.
*/
#include <SPI.h>
#include <SD.h>
// SD chip select pin
const uint8_t chipSelect = SS;
// number of lines to print
const uint16_t N_PRINT = 20000;
// test file
File file;
//------------------------------------------------------------------------------
void error(const char* s) {
Serial.println(s);
while (1) {
yield();
}
}
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
// Wait for USB Serial
while (!Serial) {
yield();
}
}
//------------------------------------------------------------------------------
void loop() {
uint32_t maxLatency;
uint32_t minLatency;
uint32_t totalLatency;
// Read any existing Serial data.
do {
delay(10);
} while (Serial.available() && Serial.read() >= 0);
// F() stores strings in flash to save RAM
Serial.println(F("Type any character to start"));
while (!Serial.available()) {
yield();
}
// initialize the SD card
if (!SD.begin(chipSelect)) {
error("begin");
}
Serial.println(F("Starting print test. Please wait.\n"));
// do write test
for (int test = 0; test < 2; test++) {
file = SD.open("bench.txt", FILE_WRITE);
if (!file) {
error("open failed");
}
switch(test) {
case 0:
Serial.println(F("Test of println(uint16_t)"));
break;
case 1:
Serial.println(F("Test of println(double)"));
break;
}
maxLatency = 0;
minLatency = 999999;
totalLatency = 0;
uint32_t t = millis();
for (uint16_t i = 0; i < N_PRINT; i++) {
uint32_t m = micros();
switch(test) {
case 0:
file.println(i);
break;
case 1:
file.println((double)0.01*i);
break;
}
if (file.getWriteError()) {
error("write failed");
}
m = micros() - m;
if (maxLatency < m) {
maxLatency = m;
}
if (minLatency > m) {
minLatency = m;
}
totalLatency += m;
}
file.flush();
t = millis() - t;
double s = file.size();
Serial.print(F("Time "));
Serial.print(0.001*t);
Serial.println(F(" sec"));
Serial.print(F("File size "));
Serial.print(0.001*s);
Serial.print(F(" KB\n"));
Serial.print(F("Write "));
Serial.print(s/t);
Serial.print(F(" KB/sec\n"));
Serial.print(F("Maximum latency: "));
Serial.print(maxLatency);
Serial.print(F(" usec, Minimum Latency: "));
Serial.print(minLatency);
Serial.print(F(" usec, Avg Latency: "));
Serial.print(totalLatency/N_PRINT);
Serial.println(F(" usec\n"));
SD.remove("bench.txt");
}
file.close();
Serial.println(F("Done!\n"));
}

View File

@@ -0,0 +1,30 @@
/*
* Program to compare size of Arduino SD library with SdFat.
* See SdFatSize.ino for SdFat program.
*/
#include <SPI.h>
#include <SD.h>
File file;
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
// Wait for USB Serial
while (!Serial) {
yield();
}
if (!SD.begin()) {
Serial.println("begin failed");
return;
}
file = SD.open("TEST_SD.TXT", FILE_WRITE);
file.println("Hello");
file.close();
Serial.println("Done");
}
//------------------------------------------------------------------------------
void loop() {}

View File

@@ -0,0 +1,33 @@
/*
* Program to compare size of SdFat with Arduino SD library.
* See SD_Size.ino for Arduino SD program.
*
*/
#include <SPI.h>
#include "SdFat.h"
SdFat sd;
SdFile file;
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
// Wait for USB Serial
while (!Serial) {
yield();
}
if (!sd.begin()) {
Serial.println("begin failed");
return;
}
file.open("SizeTest.txt", O_RDWR | O_CREAT | O_AT_END);
file.println("Hello");
file.close();
Serial.println("Done");
}
//------------------------------------------------------------------------------
void loop() {}

View File

@@ -0,0 +1,44 @@
// Simple demo of the Stream parsInt() member function.
#include <SPI.h>
// The next two lines replace #include <SD.h>.
#include "SdFat.h"
SdFat SD;
// SD card chip select pin - Modify the value of csPin for your SD module.
const uint8_t csPin = SS;
File file;
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
// Wait for USB Serial.
while(!Serial) {
yield();
}
Serial.println(F("Type any character to start"));
while (!Serial.available()) {
yield();
}
// Initialize the SD.
if (!SD.begin(csPin)) {
Serial.println(F("begin error"));
return;
}
// Create and open the file. Use flag to truncate an existing file.
file = SD.open("stream.txt", O_RDWR|O_CREAT|O_TRUNC);
if (!file) {
Serial.println(F("open error"));
return;
}
// Write a test number to the file.
file.println("12345");
// Rewind the file and read the number with parseInt().
file.seek(0);
int i = file.parseInt();
Serial.print(F("parseInt: "));
Serial.println(i);
file.close();
}
void loop() {}

View File

@@ -0,0 +1,77 @@
/*
* Append Example
*
* This program shows how to use open for append.
* The program will append 100 line each time it opens the file.
* The program will open and close the file 100 times.
*/
#include <SPI.h>
#include "SdFat.h"
#include "sdios.h"
// SD chip select pin
const uint8_t chipSelect = SS;
// file system object
SdFat sd;
// create Serial stream
ArduinoOutStream cout(Serial);
// store error strings in flash to save RAM
#define error(s) sd.errorHalt(F(s))
//------------------------------------------------------------------------------
void setup() {
// filename for this example
char name[] = "append.txt";
Serial.begin(9600);
// Wait for USB Serial
while (!Serial) {
yield();
}
// F() stores strings in flash to save RAM
cout << endl << F("Type any character to start\n");
while (!Serial.available()) {
yield();
}
// Initialize at the highest speed supported by the board that is
// not over 50 MHz. Try a lower speed if SPI errors occur.
if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
sd.initErrorHalt();
}
cout << F("Appending to: ") << name;
for (uint8_t i = 0; i < 100; i++) {
// open stream for append
ofstream sdout(name, ios::out | ios::app);
if (!sdout) {
error("open failed");
}
// append 100 lines to the file
for (uint8_t j = 0; j < 100; j++) {
// use int() so byte will print as decimal number
sdout << "line " << int(j) << " of pass " << int(i);
sdout << " millis = " << millis() << endl;
}
// close the stream
sdout.close();
if (!sdout) {
error("append data failed");
}
// output progress indicator
if (i % 25 == 0) {
cout << endl;
}
cout << '.';
}
cout << endl << "Done" << endl;
}
//------------------------------------------------------------------------------
void loop() {}

View File

@@ -0,0 +1,82 @@
/*
* Calculate the sum and average of a list of floating point numbers
*/
#include <SPI.h>
#include "SdFat.h"
#include "sdios.h"
// SD chip select pin
const uint8_t chipSelect = SS;
// object for the SD file system
SdFat sd;
// define a serial output stream
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
void writeTestFile() {
// open the output file
ofstream sdout("AvgTest.txt");
// write a series of float numbers
for (int16_t i = -1001; i < 2000; i += 13) {
sdout << 0.1 * i << endl;
}
if (!sdout) {
sd.errorHalt("sdout failed");
}
sdout.close();
}
//------------------------------------------------------------------------------
void calcAverage() {
uint16_t n = 0; // count of input numbers
double num; // current input number
double sum = 0; // sum of input numbers
// open the input file
ifstream sdin("AvgTest.txt");
// check for an open failure
if (!sdin) {
sd.errorHalt("sdin failed");
}
// read and sum the numbers
while (sdin >> num) {
n++;
sum += num;
}
// print the results
cout << "sum of " << n << " numbers = " << sum << endl;
cout << "average = " << sum/n << endl;
}
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
// Wait for USB Serial
while (!Serial) {
yield();
}
// F() stores strings in flash to save RAM
cout << F("Type any character to start\n");
while (!Serial.available()) {
yield();
}
// Initialize at the highest speed supported by the board that is
// not over 50 MHz. Try a lower speed if SPI errors occur.
if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
sd.initErrorHalt();
}
// write the test file
writeTestFile();
// read the test file and calculate the average
calcAverage();
}
//------------------------------------------------------------------------------
void loop() {}

View File

@@ -0,0 +1,149 @@
/*
* This program is a simple binary write/read benchmark
* for the standard Arduino SD.h library.
*/
#include <SPI.h>
#include <SD.h>
// SD chip select pin
const uint8_t chipSelect = SS;
#define FILE_SIZE_MB 5
#define FILE_SIZE (1000000UL*FILE_SIZE_MB)
#define BUF_SIZE 100
uint8_t buf[BUF_SIZE];
// test file
File file;
//------------------------------------------------------------------------------
void error(const char* s) {
Serial.println(s);
while (1) {
yield();
}
}
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
// Wait for USB Serial
while (!Serial) {
yield();
}
}
//------------------------------------------------------------------------------
void loop() {
uint32_t maxLatency;
uint32_t minLatency;
uint32_t totalLatency;
// Discard any input.
do {
delay(10);
} while (Serial.available() && Serial.read() >= 0);
// F() stores strings in flash to save RAM
Serial.println(F("Type any character to start"));
while (!Serial.available()) {
yield();
}
if (!SD.begin(chipSelect)) {
error("begin");
}
// open or create file - truncate existing file.
file = SD.open("Bench.dat", O_RDWR | O_TRUNC | O_CREAT);
if (!file) {
error("open failed");
}
// fill buf with known data
for (size_t_t i = 0; i < (BUF_SIZE-2); i++) {
buf[i] = 'A' + (i % 26);
}
buf[BUF_SIZE-2] = '\r';
buf[BUF_SIZE-1] = '\n';
Serial.print(F("File size "));
Serial.print(FILE_SIZE_MB);
Serial.println(F("MB"));
Serial.print(F("Buffer size "));
Serial.print(BUF_SIZE);
Serial.println(F(" bytes"));
Serial.println(F("Starting write test. Please wait up to a minute"));
// do write test
uint32_t n = FILE_SIZE/sizeof(buf);
maxLatency = 0;
minLatency = 999999;
totalLatency = 0;
uint32_t t = millis();
for (uint32_t i = 0; i < n; i++) {
uint32_t m = micros();
if (file.write(buf, sizeof(buf)) != sizeof(buf)) {
error("write failed");
}
m = micros() - m;
if (maxLatency < m) {
maxLatency = m;
}
if (minLatency > m) {
minLatency = m;
}
totalLatency += m;
}
file.flush();
t = millis() - t;
double s = file.size();
Serial.print(F("Write "));
Serial.print(s/t);
Serial.print(F(" KB/sec\n"));
Serial.print(F("Maximum latency: "));
Serial.print(maxLatency);
Serial.print(F(" usec, Minimum Latency: "));
Serial.print(minLatency);
Serial.print(F(" usec, Avg Latency: "));
Serial.print(totalLatency/n);
Serial.print(F(" usec\n\n"));
Serial.println(F("Starting read test. Please wait up to a minute"));
// do read test
file.seek(0);
maxLatency = 0;
minLatency = 99999;
totalLatency = 0;
t = millis();
for (uint32_t i = 0; i < n; i++) {
buf[BUF_SIZE-1] = 0;
uint32_t m = micros();
if (file.read(buf, sizeof(buf)) != sizeof(buf)) {
error("read failed");
}
m = micros() - m;
if (maxLatency < m) {
maxLatency = m;
}
if (minLatency > m) {
minLatency = m;
}
totalLatency += m;
if (buf[BUF_SIZE-1] != '\n') {
error("data check");
}
}
t = millis() - t;
Serial.print(F("Read "));
Serial.print(s/t);
Serial.print(F(" KB/sec\n"));
Serial.print(F("Maximum latency: "));
Serial.print(maxLatency);
Serial.print(F(" usec, Minimum Latency: "));
Serial.print(minLatency);
Serial.print(F(" usec, Avg Latency: "));
Serial.print(totalLatency/n);
Serial.print(F(" usec\n\n"));
Serial.print(F("Done\n\n"));
file.close();
}

View File

@@ -0,0 +1,39 @@
/*
* Use of ibufsteam to parse a line and obufstream to format a line
*/
#include <SPI.h>
#include "SdFat.h"
#include "sdios.h"
// create a serial output stream
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
void setup() {
char buf[20]; // buffer for formatted line
int i, j, k; // values from parsed line
Serial.begin(9600);
// Wait for USB Serial
while (!Serial) {
yield();
}
delay(2000);
// initialize input string
ibufstream bin("123 456 789");
// parse the string "123 456 789"
bin >> i >> j >> k;
// initialize output buffer
obufstream bout(buf, sizeof(buf));
// format the output string
bout << k << ',' << j << ',' << i << endl;
// write the string to serial
cout << buf;
}
void loop() {}

View File

@@ -0,0 +1,39 @@
/*
* Demo of ArduinoInStream and ArduinoOutStream
*/
#include <SPI.h>
#include "SdFat.h"
#include "sdios.h"
// create serial output stream
ArduinoOutStream cout(Serial);
// input line buffer
char cinBuf[40];
// create serial input stream
ArduinoInStream cin(Serial, cinBuf, sizeof(cinBuf));
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
// Wait for USB Serial
while (!Serial) {
yield();
}
}
//------------------------------------------------------------------------------
void loop() {
int32_t n = 0;
cout << "\nenter an integer\n";
cin.readline();
if (cin >> n) {
cout << "The number is: " << n;
} else {
// will fail if no digits or not in range [-2147483648, 2147483647]
cout << "Invalid input: " << cinBuf;
}
cout << endl;
}

View File

@@ -0,0 +1,62 @@
/*
* Append a line to a file - demo of pathnames and streams
*/
#include <SPI.h>
#include "SdFat.h"
#include "sdios.h"
// SD chip select pin
const uint8_t chipSelect = SS;
// file system object
SdFat sd;
// define a serial output stream
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
/*
* Append a line to logfile.txt
*/
void logEvent(const char *msg) {
// create dir if needed
sd.mkdir("logs/2014/Jan");
// create or open a file for append
ofstream sdlog("logs/2014/Jan/logfile.txt", ios::out | ios::app);
// append a line to the file
sdlog << msg << endl;
// check for errors
if (!sdlog) {
sd.errorHalt("append failed");
}
sdlog.close();
}
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
// Wait for USB Serial
while (!Serial) {
yield();
}
// F() stores strings in flash to save RAM
cout << F("Type any character to start\n");
while (!Serial.available()) {
yield();
}
delay(400); // catch Due reset problem
// Initialize at the highest speed supported by the board that is
// not over 50 MHz. Try a lower speed if SPI errors occur.
if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
sd.initErrorHalt();
}
// append a line to the logfile
logEvent("Another line for the logfile");
cout << F("Done - check /logs/2014/Jan/logfile.txt on the SD") << endl;
}
//------------------------------------------------------------------------------
void loop() {}

View File

@@ -0,0 +1,111 @@
// Demo of rewriting a line read by fgets
#include <SPI.h>
#include "SdFat.h"
#include "sdios.h"
// SD card chip select pin
const uint8_t chipSelect = SS;
// file system
SdFat sd;
// print stream
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
// store error strings in flash memory
#define error(s) sd.errorHalt(F(s))
//------------------------------------------------------------------------------
void demoFgets() {
char line[25];
int c;
uint32_t pos;
// open test file
SdFile rdfile("fgets.txt", O_RDWR);
// check for open error
if (!rdfile.isOpen()) {
error("demoFgets");
}
// list file
cout << F("-----Before Rewrite\r\n");
while ((c = rdfile.read()) >= 0) {
Serial.write(c);
}
rdfile.rewind();
// read lines from the file to get position
while (1) {
pos = rdfile.curPosition();
if (rdfile.fgets(line, sizeof(line)) < 0) {
error("Line not found");
}
// find line that contains "Line C"
if (strstr(line, "Line C")) {
break;
}
}
// rewrite line with 'C'
if (!rdfile.seekSet(pos)) {
error("seekSet");
}
rdfile.println("Line R");
rdfile.rewind();
// list file
cout << F("\r\n-----After Rewrite\r\n");
while ((c = rdfile.read()) >= 0) {
Serial.write(c);
}
// close so rewrite is not lost
rdfile.close();
}
//------------------------------------------------------------------------------
void makeTestFile() {
// create or open test file
SdFile wrfile("fgets.txt", O_WRONLY | O_CREAT | O_TRUNC);
// check for open error
if (!wrfile.isOpen()) {
error("MakeTestFile");
}
// write test file
wrfile.print(F(
"Line A\r\n"
"Line B\r\n"
"Line C\r\n"
"Line D\r\n"
"Line E\r\n"
));
wrfile.close();
}
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
// Wait for USB Serial
while (!Serial) {
yield();
}
cout << F("Type any character to start\n");
while (!Serial.available()) {
yield();
}
// Initialize at the highest speed supported by the board that is
// not over 50 MHz. Try a lower speed if SPI errors occur.
if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
sd.initErrorHalt();
}
makeTestFile();
demoFgets();
cout << F("\nDone\n");
}
void loop() {}

View File

@@ -0,0 +1,51 @@
/*
* Read the logfile created by the eventlog.ino example.
* Demo of pathnames and working directories
*/
#include <SPI.h>
#include "SdFat.h"
#include "sdios.h"
// SD chip select pin
const uint8_t chipSelect = SS;
// file system object
SdFat sd;
// define a serial output stream
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
void setup() {
int c;
Serial.begin(9600);
// Wait for USB Serial
while (!Serial) {
yield();
}
// Initialize at the highest speed supported by the board that is
// not over 50 MHz. Try a lower speed if SPI errors occur.
if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
sd.initErrorHalt();
}
// set current working directory
if (!sd.chdir("logs/2014/Jan/")) {
sd.errorHalt("chdir failed. Did you run eventlog.ino?");
}
// open file in current working directory
ifstream file("logfile.txt");
if (!file.is_open()) {
sd.errorHalt("open failed");
}
// copy the file to Serial
while ((c = file.get()) >= 0) {
cout << (char)c;
}
cout << "Done" << endl;
}
//------------------------------------------------------------------------------
void loop() {}

View File

@@ -0,0 +1,34 @@
Old and debug examples.
AnalogLogger - A simple data logger for one or more analog pins.
append - This sketch creates a large file by successive
open/write/close operations.
average - A demonstration of parsing floating point numbers.
BaseExtCaseTest - Long file name test.
benchSD - A read/write benchmark for the standard Arduino SD.h library.
bufstream - ibufsteam to parse a line and obufstream to format a line.
cin_cout - Demo of ArduinoInStream and ArduinoOutStream.
eventlog - Append a line to a file - demo of pathnames and streams.
fgetsRewrite - Demo of rewriting a line read by fgets.
HelloWorld - Create a serial output stream.
MiniSerial - SdFat minimal serial support for debug.
PrintBenchmarkSD - Bench mark SD.h print.
readlog - Read file. Demo of pathnames and current working directory.
SD_Size - Determine flash used by SD.h example.
SdFatSize - Determine flash used by SdFat.
StreamParseInt - Simple demo of the Stream parsInt() member function.