SD CARD 

Example

: Mini Shell

This example shows how to interact with Waspmote SD using a smart shell


File:
"WaspSD_4_miniShell.pde"


Code



/*
 *  ------Waspmote SD Mini Shell Example--------
 *
 *  Explanation: This example shows how to interact with Waspmote
 *  SD using a smart shell
 *
 *  Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
 *  http://www.libelium.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see .
 *
 *  Version:                0.1
 *  Design:                 David Gascón
 *  Implementation:         David Cuartielles, Alberto Bielsa
 */



// String to display in case of calling the help command
#define HELP  "Take a look in the code!"

/************* HELP **************************
" - help: prints this message\n" \
 "\n* Disk Functions *\n" \
 " - init: re-initializes the disk\n" \
 " - disk: show disk info\n" \
 "\n* Directory Handling *\n" \
 " - ls: lists directory contents\n" \
 " - at: lists a file with its attributes\n" \
 " - count: shows the amount of files at current dir\n" \
 " - cd: go to a directory\n" \
 " - rm: deletes file/dir\n" \
 " - mkdir: creates a directory\n" \
 "\n* File Handling *\n" \
 " - touch: creates file\n" \
 " - write: writes to file\n" \
 " - append: appends a string to hola.txt\n" \
 " - appendln: appends a string adding EOL\n" \
 " - find: looks for a pattern in hola.txt\n" \
 " - size: size of file\n" \
 " - dump: dumps a file to the end\n" \
 " - cat: dumps the first 10 bytes of a file\n" \
 " - catln: dumps lines 3 to 10 of a file\n" \
 " - numln: amount of lines in file\n" \
 "\n (c) 2009 Libelium Comunicaciones Distribuidas\n"
 */

void setup() {
  USB.begin();
  USB.println("Start WASP FAT 16 SD-Card Test\n");
  
  // Set SD ON
  SD.ON();
}

void loop()
{
  // Init SD
  USB.println("DISK INFO: ");
  USB.println(SD.print_disk_info());

  // provide a simple shell
  USB.println("... Start Shell");
  char buffer[24];
  while(1)
  {

    // print prompt
    USB.print("> ");

    // read command
    char* command = buffer;
    if(read_line(command, sizeof(buffer)) < 1)
      continue;

    // execute command
    if(strcmp_P(command, PSTR("init")) == 0)
    {
      break;
    }
    else if(strcmp_P(command, PSTR("help")) == 0)
    {
      USB.println(HELP);
    }
    else if(strncmp_P(command, PSTR("cd "), 3) == 0)
    {
      command += 3;
      if(command[0] == '\0')
        continue;

      // change directory
      // if no error, continue
      if (!SD.cd(command)) continue;

      USB.print("directory not found: ");
      USB.println(command);
    }
    else if(strcmp_P(command, PSTR("ls")) == 0)
    {
      // print directory listing
      USB.println(SD.ls());
      if (SD.flag & TRUNCATED_DATA)
        USB.println("--> data got truncated");
    }
    else if(strcmp_P(command, PSTR("ls -la")) == 0)
    {
      // print directory listing
      USB.println(SD.ls(0,0,ATTRIBUTES));
      if (SD.flag & TRUNCATED_DATA)
        USB.println("--> data got truncated");
    }
    else if(strncmp_P(command, PSTR("at "), 3) == 0)
    {
      command += 3;
      if(command[0] == '\0')
        continue;

      // print attributes for the file
      USB.println(SD.getAttributes(command));
    }
    else if(strncmp_P(command, PSTR("cat "), 4) == 0)
    {
      command += 4;
      if(command[0] == '\0')
        continue;

      USB.println(SD.cat(command, 0,10));
    }
    else if(strncmp_P(command, PSTR("catln "), 6) == 0)
    {
      command += 6;
      if(command[0] == '\0')
        continue;

      USB.println(SD.catln(command, 3,7));
    }
    else if(strncmp_P(command, PSTR("dump "), 5) == 0)
    {
      command += 5;
      if(command[0] == '\0')
        continue;

      USB.println(SD.cat(command, 0,0));
    }
    else if(strcmp_P(command, PSTR("disk")) == 0)
    {
      if(!SD.print_disk_info())
        USB.println("error reading disk info\n");
      else
        USB.println(SD.buffer);
    }
    else if(strncmp_P(command, PSTR("find "), 5) == 0)
    {
      command += 5;
      if(command[0] == '\0')
        continue;

      // search for hola in whatever file you choose
      long val = SD.indexOf("hola.txt", command, 0);
      if (val > -1)
      {
        USB.print("pattern found in file at: ");
        USB.println(val);
        continue;
      }

      USB.print("pattern not found in file: ");
      USB.println(command);
    }
    else if(strncmp_P(command, PSTR("rm "), 3) == 0)
    {
      command += 3;
      if(command[0] == '\0')
        continue;

      if(SD.del(command))
      {
        USB.print("deleted file: ");
        USB.println(command);
        continue;
      }

      USB.print("error deleting file: ");
      USB.println(command);
    }
    else if(strncmp_P(command, PSTR("touch "), 6) == 0)
    {
      command += 6;
      if(command[0] == '\0')
        continue;

      if(SD.create(command))
      {
        USB.print("created file: ");
        USB.println(command);
        continue;
      }

      USB.print("error creating file: ");
      USB.println(command);
    }
    else if(strncmp_P(command, PSTR("size "), 5) == 0)
    {
      command += 5;
      if(command[0] == '\0')
        continue;

      // search for hola in whatever file you choose
      long val = SD.getFileSize(command);
      if (val > -1)
      {
        USB.println(val);
        continue;
      }

      USB.print("file not found: ");
      USB.println(command);
    }
    else if(strncmp_P(command, PSTR("mkdir "), 6) == 0)
    {
      command += 6;
      if(command[0] == '\0')
        continue;

      if(SD.mkdir(command))
      {
        USB.print("created dir: ");
        USB.println(command);
        continue;
      }

      USB.print("error creating dir: ");
      USB.println(command);
    }
    else if(strncmp_P(command, PSTR("append "), 7) == 0)
    {
      command += 7;
      if(command[0] == '\0')
        continue;

      if(SD.append("hola.txt",command))
      {
        USB.print("success writing: ");
        USB.println(command);
        continue;
      }

      USB.print("error writing: ");
      USB.println(command);
    }
    else if(strncmp_P(command, PSTR("appendln "), 9) == 0)
    {
      command += 9;
      if(command[0] == '\0')
        continue;

      if(SD.appendln("hola.txt",command))
      {
        USB.print("success writing: ");
        USB.println(command);
        continue;
      }

      USB.print("error writing: ");
      USB.println(command);
    }
    else if(strcmp_P(command, PSTR("count")) == 0)
    {
      USB.println(SD.numFiles(),DEC);
    }
    else if(strncmp_P(command, PSTR("numln "), 6) == 0)
    {
      command += 6;
      if(command[0] == '\0')
        continue;

      USB.println(SD.numln(command),DEC);
    }
    else
    {
      USB.print("unknown command: ");
      USB.println(command);
    }

  }
}

// FUNCTIONS //////////////////////////////////////////////////

/*
 * Read a line from the serial port
 *
 */
uint8_t read_line(char* buffer, uint8_t buffer_length)
{
  memset(buffer, 0, buffer_length);

  uint8_t read_length = 0;
  while(read_length < buffer_length - 1)
  {
    while(!USB.available()){
    };
    uint8_t c = USB.read();
    if(c == 0x08 || c == 0x7f)
    {
      if(read_length < 1)
        continue;

      --read_length;
      buffer[read_length] = '\0';

      USB.print(0x08, BYTE);
      USB.print(' ', BYTE);
      USB.print(0x08, BYTE);

      continue;
    }

    USB.print(c, BYTE);

    // here we add the EndOfString trick so that
    // it works from Arduino's command line
#ifdef EndOfString
    if(c == 10 || c == 13 || c == EndOfString)
#else
      if(c == 10 || c == 13)
#endif
      {
        buffer[read_length] = '\0';
        break;
      }
      else
      {
        buffer[read_length] = c;
        ++read_length;
      }
  }

  return read_length;
}
 

Download code

You can download the code of this example.


© Libelium Comunicaciones Distribuidas S.L.

| Terms of use