From ed672deb7447e8c229fa5bfef54189dceb0841a8 Mon Sep 17 00:00:00 2001 From: Fede85 Date: Thu, 20 Jun 2013 13:24:27 +0200 Subject: modified SDclass.begin() check for sd presence and added the Datalogger example --- libraries/Bridge/FileIO.cpp | 8 +- .../Bridge/examples/Datalogger/Datalogger.ino | 96 ++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 libraries/Bridge/examples/Datalogger/Datalogger.ino (limited to 'libraries') diff --git a/libraries/Bridge/FileIO.cpp b/libraries/Bridge/FileIO.cpp index c66f937..c49c2b4 100644 --- a/libraries/Bridge/FileIO.cpp +++ b/libraries/Bridge/FileIO.cpp @@ -160,7 +160,13 @@ const char *File::name() { boolean SDClass::begin() { - return true; + Process chksd; + int res = chksd.runShellCommand(F("cat /sys/bus/scsi/drivers/sd/*/block/sda/size")); + if (res == 0) { + if (chksd.peek() != '0') + return true; + } + return false; } File SDClass::open(const char *filename, uint8_t mode) { diff --git a/libraries/Bridge/examples/Datalogger/Datalogger.ino b/libraries/Bridge/examples/Datalogger/Datalogger.ino new file mode 100644 index 0000000..5412631 --- /dev/null +++ b/libraries/Bridge/examples/Datalogger/Datalogger.ino @@ -0,0 +1,96 @@ +/* + SD card datalogger + + This example shows how to log data from three analog sensors + to an SD card mounted on the Linux using the Bridge library. + + The circuit: + * analog sensors on analog ins 0, 1, and 2 + * SD card attached to SD card slot of the Arduino Yun + + You are allowed to remove the SD card while the Linux and the + sketch is running but becareful to don't remove it while + the system is writing on it. + + created 24 Nov 2010 + modified 9 Apr 2012 + by Tom Igoe + adapted to the Yun Bridge library 20 Jun 2013 + by Federico Vanzati + + This example code is in the public domain. + + */ + +#include +#include + +void setup() { + // Initialize the Bridge and the Console + Bridge.begin(); + Console.begin(); + + while(!Console){ + ; // wait for Console port to connect. + } + + // see if the card is present and can be initialized: + if (!SD.begin()) { + Console.println("SD card failed, or not present"); + // don't do anything more: + return; + } + Console.println("SD card initialized."); + +} + + +void loop () { + // make a string that start with a timestamp for assembling the data to log: + String dataString = ""; + addTimeStamp(dataString); + dataString += " = "; + + // read three sensors and append to the string: + for (int analogPin = 0; analogPin < 3; analogPin++) { + int sensor = analogRead(analogPin); + dataString += String(sensor); + if (analogPin < 2) { + dataString += ","; + } + } + + // open the file. note that only one file can be open at a time, + // so you have to close this one before opening another. + // The SD card is mounted at the following "/mnt/sda1" + File dataFile = SD.open("/mnt/sda1/datalog.txt", FILE_APPEND); + + // if the file is available, write to it: + if (dataFile) { + dataFile.println(dataString); + dataFile.close(); + // print to the serial port too: + Console.println(dataString); + } + // if the file isn't open, pop up an error: + else { + Console.println("error opening datalog.txt"); + } + + delay(15000); + +} + +// This function append a time stamp to the string passed as argument +void addTimeStamp(String &string) { + Process time; + time.begin("date"); + time.addParameter("+%D-%T"); + time.run(); + + while(time.available()>0) { + char c = time.read(); + if(c != '\n') + string += c; + } +} -- cgit v1.2.3-18-g5258