sd-log-sense-cap-indicator

wayland/rp2040/sd-log-sense-cap-indicator

Appends lines of text to a file on SD card. Possible errors: — Can't open a file — Initialization failed or no SD card — Can't write data to SD card
sd-log-sense-cap-indicator
@/sd-log-sense-cap-indicator
Appends lines of text to a file on SD card. Possible errors: — Can't open a file — Initialization failed or no SD card — Can't write data to SD card
FILEstring
File name to append to
LINEstring
Line to append
Wpulse
Perform file open, write, flush, close cycle
sd-log-sense-cap-indicator
DONE
FILE
LINE
W
DONEpulse
Fires when write is done
To use the node in your project you should have the wayland/rp2040 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

#pragma XOD evaluate_on_pin disable
#pragma XOD evaluate_on_pin enable input_W
#pragma XOD error_raise enable

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


struct State {
    bool begun;
};

node {
    bool begun;
    const int csPin = 13;

    void evaluate(Context ctx) {
        if (!isInputDirty<input_W>(ctx))
            return;

        if (!begun) {
            // First time use, initialize
            begun = SD.begin(csPin, 1000000, SPI1);
        }

        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);
    }
}