mardi 14 juin 2016

if statement conditional flow using ! operator

I am looking at some Arduino compatible code specifically designed for working with Serial Flash Memory, but I'm coming up against a more general question about if() statements and conditional flow of a program. It is a new technique I have not seen before. The original code can be accessed here:http://ift.tt/1XnHj8H

This code works fine, and does what it is intended to do, that is not the question here...just FYI.

The code I am having a hard time understanding, in the void loop() is as follows:

logging = true;
    if (logging) {
          logString = valueToLogString (temperature, currentTime);

          if (!dataLogger.writeData (logString.c_str(), logString.length())) {
            if (!flashFull) {
              Serial.print ("Flash data is full - stopped writing\n");
              flashFull = true;
            }
          }
          // also print to serial port
          Serial.print(logString);
        }
      }

The function dataLogger.writeData, called in the code above, is as follows:

bool SerialFlashDataLogger::writeData (const void *buf, uint32_t wrlen) {

  uint32_t lengthWritten = 0;

  lengthWritten = dataFile.write (buf, wrlen);

  // return false if all the data was not written
  return (lengthWritten == wrlen);
}

I am having a hard time understanding the if() statement:

if (!dataLogger.writeData (logString.c_str(), logString.length()))

The code is designed to continuously log data, so why is there an if(!dataLogger.writeData.....) -- which reads if ("NOT"dataLogger.writeData....)

I think it has something to do with how the "writeData" function is defined and the fact that it returns a boolean value, but I just can't seem to get my head around how this helps the writeData function continuously accept inputs in order to write to the memory chip?

There is something I'm missing in the logic here.

I know this is perhaps a confusing post...hopefully it is clear to someone ;0

Aucun commentaire:

Enregistrer un commentaire