Read Config File from an SD Card

This is a example sketch to show how to read a configuration file from an SD card.

ConfigRead.pde
// Config file example
//
// Reads a config file line by line, until EOF
// This example has each config variable defined by a single character at
// the beginning of the line followed by an integer value.
// There can be optional whitespace (spaces or tabs) between the var name.
// Values may be negative.
// Unrecognized variable names == ignored line.
// Latest definition of a var name supercedes previous values.
// Maximum line length, including comments, is 100 bytes (can be adjusted)
 
// Example config file:
/*
# This line is ignored
 So is this line (since we don't define a single space as a var name)
N123 # Comments can go after 
N-1
N -2
N    10
NXXXX # this line will result in a 0 value
N234234234234234  # there is a limit to the size (32 bit signed value)
*/
 
#include <NewSoftSerial.h>
#include <RogueSD.h>
 
NewSoftSerial ummc_s(6, 7);
RogueSD ummc(ummc_s);
 
#define IGNOREWHITESPACE 1
 
int32_t getInt(char *line, uint8_t base)
{
  uint8_t c, neg = 0;
  uint8_t i = 0;
  uint32_t val;
 
  val = 0;
 
#if IGNOREWHITESPACE == 1
  while (line[i] == ' ' || line[i] == 0x09)
    i++;
#endif
 
  if (line[i] == '-')
  {
    neg = 1;
    i++;
  }
 
  c = line[i];
 
  while (((c >= 'A') && (c <= 'Z'))
      || ((c >= 'a') && (c <= 'z'))
      || ((c >= '0') && (c <= '9')))
  {
    if (c >= 'a') c -= 0x57;             // c = c - 'a' + 0x0a, c = c - ('a' - 0x0a)
    else if (c >= 'A') c -= 0x37;        // c = c - 'A' + 0x0A
    else c -= '0';
    if (c >= base) break;
 
    val *= base;
    val += c;
    c = line[++i];
  }
 
  return neg ? -val : val;
}
 
void setup(void)
{
  char linebuffer[100];
  int8_t filehandle = 0;
  int32_t value = 0;
 
  Serial.begin(9600);
  ummc_s.begin(9600);
 
  Serial.println("Initializing uMMC");
  ummc.sync();
  ummc.closeall();
 
 
  Serial.println("Opening /CONFIG.CFG");
  filehandle = ummc.open_P(PSTR("/CONFIG.CFG"), OPEN_READ);
 
  if (filehandle > 0)
  {
    while (ummc.readln(filehandle, 100, linebuffer) > 0)
    {
      if (linebuffer[0] == 'N')
      {
        value = getInt(&linebuffer[1], 10);
        Serial.print("N: ");
        Serial.println(value);
      }
      // else if (linebuffer[0] == 'X')
      // etc...
      // everything else is effectively ignored
      // This is an easy way to get 26 (or more if you use non-alpha chars for var names)
      // config vars.
    }
 
    ummc.close(filehandle);
  }
  else
  {
    Serial.println("Couldn't open /CONFIG.CFG");
  }
}
 
void loop(void)
{
}
You could leave a comment if you were logged in.
  • Bookmark at
  • Bookmark "Read Config File from an SD Card" at del.icio.us
  • Bookmark "Read Config File from an SD Card" at Digg
  • Bookmark "Read Config File from an SD Card" at Google
  • Bookmark "Read Config File from an SD Card" at StumbleUpon
  • Bookmark "Read Config File from an SD Card" at Technorati
  • Bookmark "Read Config File from an SD Card" at Facebook
  • Bookmark "Read Config File from an SD Card" at Twitter
  • Bookmark "Read Config File from an SD Card" at Slashdot
  • Bookmark "Read Config File from an SD Card" at Yahoo! Bookmarks
  • Bookmark "Read Config File from an SD Card" at Furl
  • Bookmark "Read Config File from an SD Card" at Reddit
  • Bookmark "Read Config File from an SD Card" at Ask
  • Bookmark "Read Config File from an SD Card" at BlinkList
  • Bookmark "Read Config File from an SD Card" at blogmarks
  • Bookmark "Read Config File from an SD Card" at Ma.gnolia
  • Bookmark "Read Config File from an SD Card" at Netscape
  • Bookmark "Read Config File from an SD Card" at ppnow
  • Bookmark "Read Config File from an SD Card" at Rojo
  • Bookmark "Read Config File from an SD Card" at Shadows
  • Bookmark "Read Config File from an SD Card" at Simpy
  • Bookmark "Read Config File from an SD Card" at Socializer
  • Bookmark "Read Config File from an SD Card" at Spurl
  • Bookmark "Read Config File from an SD Card" at Tailrank
  • Bookmark "Read Config File from an SD Card" at Live Bookmarks
  • Bookmark "Read Config File from an SD Card" at Wists
  • Bookmark "Read Config File from an SD Card" at Yahoo! Myweb
  • Bookmark "Read Config File from an SD Card" at BobrDobr
  • Bookmark "Read Config File from an SD Card" at Memori
  • Bookmark "Read Config File from an SD Card" at Faves
  • Bookmark "Read Config File from an SD Card" at Favorites
  • Bookmark "Read Config File from an SD Card" at Newsvine
  • Bookmark "Read Config File from an SD Card" at myAOL
  • Bookmark "Read Config File from an SD Card" at Fark
  • Bookmark "Read Config File from an SD Card" at RawSugar
  • Bookmark "Read Config File from an SD Card" at LinkaGoGo
  • Bookmark "Read Config File from an SD Card" at Mister Wong
  • Bookmark "Read Config File from an SD Card" at Wink
  • Bookmark "Read Config File from an SD Card" at BackFlip
  • Bookmark "Read Config File from an SD Card" at Diigo
  • Bookmark "Read Config File from an SD Card" at Segnalo
  • Bookmark "Read Config File from an SD Card" at Netvouz
  • Bookmark "Read Config File from an SD Card" at DropJack
  • Bookmark "Read Config File from an SD Card" at Feed Me Links
  • Bookmark "Read Config File from an SD Card" at funP
  • Bookmark "Read Config File from an SD Card" at HEMiDEMi
code/arduino/read_config_file_from_sd_card.txt · Last modified: 2011/01/03 22:21 by bhagman
Valid CSS Recent changes RSS feed Valid XHTML 1.0