sd-append

peterfun/sd-card/sd-append

Append data to a file on the SD-Card. Will create the file if it does not exist.
sd-append
@/sd-append
Append data to a file on the SD-Card. Will create the file if it does not exist.
CSport
CS (chip select) pin the SD card reader is connected to. Also known as SS (slave select).
FILEstring
File name to append to
LINEstring
Line to append
Apulse
Perform file open, write, flush, close cycle
sd-append
CS
FILE
LINE
A
DONE
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_A
#pragma XOD error_raise enable

//{{#global}}
#include <SPI.h>
#include <SD.h>
//{{/global}}

//struct State {
//    bool begun;
//};

//{{ GENERATED_CODE }}
node {
    
bool begun;
    
void evaluate(Context ctx) {
    if (!isInputDirty<input_A>(ctx))
        return;

    //auto state = getState(ctx);

    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 file = SD.open(filename, O_WRITE | O_CREAT | O_APPEND);
    if (!file) {
        // Failed to open the file. Maybe, SD card gone,
        // try to reinit next time
        begun = false;
        raiseError(ctx); // Can't open file
        return;
    }

    XString line = getValue<input_LINE>(ctx);
    size_t lastWriteSize;
    for (auto it = line.iterate(); it; ++it) {
        lastWriteSize = file.print(*it);
        if (lastWriteSize == 0) {
            begun = false;
            raiseError(ctx); // No bytes written
            return;
        }
    }

    file.print('\n');
    file.flush();
    file.close();
    emitValue<output_DONE>(ctx, 1);
}
}