Help with String Out on old C++ Style Node, for SD-Read

I’m trying make the SD-Read node that nobody has made, yet. I can’t seem to get a string output on the String Out node… I’ve tried for several days, and the following program should work.
Please Help… How to get text to output on a new node based on SD-Write

I copied the SD-write node, I added a String-Out node to the patch “STR”
image

I went inside and changed it to this
image

It gives this error

Really, the end goal is to read a file and put it in the watch node… The XOD community has been asking for this for years!!! Really, we need a way to lookup data in a table.

Please Help!!
sd-read.xodball (12.4 KB)

Re: Migrating C++ Implementations to v0.35 Syntax, Migrating C++ Implementations to v0.35 Syntax — XOD

I was able to do it, but I had to migrate the old style to the new style v0.35. In this post I’m just showing the migrated sd-read code and an example of using it.

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

#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>

node {
    
bool begun;
    
void evaluate(Context ctx) {
    if (!isInputDirty<input_W>(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 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);
}
}

sd-log-new syntax.xodball (15.5 KB)

This is as far as I got… I can get the file position set (which is the major hangup for most people), and I can read some of the file, but the string is coming out right.


sd-read.xodball (22.7 KB)

We would like to set up the method to be able to process lookup tables. So, you need a start and cap input…

Should be something like this

char val[20];
float v;
int i=0;
//...
while(val[i-1]!='\r'){
      val[i]=myFile.read();
      i++;
}
v=atoi(val);
//....the rest of program

Also, see this webpage… Serial Input Basics - updated - Introductory Tutorials - Arduino Forum

Update: I can read diamonds with question marks from the file… It’s really wierd… The string out node appears to only take a pointer to some kind of string… there’s not good enough documentation on how to consolidate the string…

CAN SOMEBODY HELP ME READ THIS FILE AND OUTPUT IT ON THE STRING OUT??

// 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_W
#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_W>(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_WRITE | O_CREAT | O_APPEND);
    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);


}
}


sd-read-4.xodball (23.9 KB)

PLEASE HELP!

OMG… I finally got it… I was onto the right path, but have been shooting myself in the foot with a stupid mistake! I never opened the file to read!!

File myFile = SD.open(filename, O_READ);

Those non-printable characters were showing up because it wasn’t actually reading the file.

face-palm

image

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.