sd-read

peterfun/sd-card/sd-read

Read data from a file on the SD-Card
sd-read
@/sd-read
Read data from a file on the SD-Card
CSport
CS (chip select) pin the SD card reader is connected to. Also known as SS (slave select).
FILEstring
File name to append to
STARTnumber
CAPnumber
Rpulse
Perform file open, read, close cycle
sd-read
CS
FILE
START
CAP
R
DONE
STR
POS
POSnumber
STRstring
DONEpulse
Fires when write is done
To use the node in your project you should have the peterfun/sd-card library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

C++ implementation

// This is the old sd-log patch migrated to the new v0.35 syntax method according to https://xod.io/docs/guide/migrating-to-v035/

#pragma XOD evaluate_on_pin disable
#pragma XOD evaluate_on_pin enable input_R
#pragma XOD error_raise enable



#include <SPI.h>
#include <SD.h>
size_t strlcpy(char *dst, const char *src, size_t size)
// Thanks to Adam Rosenfield for this funcion, strlcpy
{
    size_t len = 0;
    while(size > 1 && *src)
    {
        *dst++ = *src++;
        size--;
        len++;
    }
    if(size > 0)
        *dst = 0;
    return len + strlen(src);    
}


node {
    
    bool begun;
    char str[64] ;
 
    CStringView view = CStringView(str);
    
void evaluate(Context ctx) {
    if (!isInputDirty<input_R>(ctx))
        return;


    if (!begun) {
        // First time use, initialize
        auto csPin = getValue<input_CS>(ctx);
        begun = SD.begin(csPin);
    }

    if (!begun) {
        // Initialization failed (wrong connection, no SD card)
        raiseError(ctx);
        return;
    }
    char filename[16] = { 0 };
    dump(getValue<input_FILE>(ctx), filename);
    File myFile = SD.open(filename, O_READ);
    if (!myFile) {
        // Failed to open the file. Maybe, SD card gone,
        // try to reinit next time
        begun = false;
        raiseError(ctx); // Can't open file
        return;
    }

    uint8_t START = getValue<input_START>(ctx);
    uint8_t CAP = getValue<input_CAP>(ctx);
    
    myFile.seek(START);

    uint8_t index = 0;
    //char ch[CAP+1];
   while (myFile.available() && index < CAP){
       char someChar = myFile.read();
      
       str[index] =someChar;
     
        index++;
       str[index] = '\0';
   }
  
    int pos = myFile.position();
    
    //strlcpy(str, ch, sizeof(str));
    
    myFile.close();
    
   emitValue<output_STR>(ctx, XString(&view));
   emitValue<output_DONE>(ctx, 1);
   emitValue<output_POS>(ctx, pos);


}
}