summaryrefslogtreecommitdiff
path: root/trunk/darwin/firmware/Sanguino/Sanguino3G/SanguinoMaster/SDCard.pde
blob: 01d3c479bbc92feb37ba4404420fbfdda3628c03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Yep, this is actually -*- c++ -*-
#ifdef USE_SD_CARD

void init_sd_card()
{
  if (!card.init_card())
  {
    if (!card.isAvailable())
    {
      Serial.println("No card present"); 
      error = 1;
    }
    else
    {
      Serial.println("Card init failed"); 
      error = 2;
    }
  }
  else if (!card.open_partition())
  {
    Serial.println("No partition"); 
    error = 3;
  }
  else if (!card.open_filesys())
  {
    Serial.println("Can't open filesys"); 
    error = 4;
  }
  else if (!card.open_dir("/"))
  {
    Serial.println("Can't open /");
    error = 5;
  }
  else if (card.isLocked())
  {
    Serial.println("Card is locked");
    error = 6;
  }
}

void open_file()
{
  open_new_file();

  if (error == 0)
  {
    bufferIndex = 0;
    for (char c = 'a'; c<= 'z'; c++)
    {
      buffer[bufferIndex] = c;
      bufferIndex++;
    }

    card.write_file(f, (uint8_t *) buffer, bufferIndex);
    card.close_file(f);
  }

  read_first_file();        
}

void open_new_file()
{
  strcpy(buffer, "RRJOB00.TXT");
  for (buffer[5] = '0'; buffer[5] <= '9'; buffer[5]++)
  {
    for (buffer[6] = '0'; buffer[6] <= '9'; buffer[6]++)
    {
      f = card.open_file(buffer);
      if (!f)
        break;        // found a file!      
      card.close_file(f);
    }
    if (!f) 
      break;
  }

  if(!card.create_file(buffer))
  {
    Serial.print("couldnt create: ");
    Serial.println(buffer);
    error = 7;
  }
  else
  {
    f = card.open_file(buffer);
    if (!f)
    {
      Serial.print("error opening: ");
      Serial.println(buffer);
      error = 8;
    }
    else
    {
      Serial.print("writing to: ");
      Serial.println(buffer);
    }
  }
}

void read_first_file()
{
  strcpy(buffer, "RRJOB00.TXT");
  f = card.open_file(buffer);

  if (!f)
  {
    Serial.print("error opening: ");
    Serial.println(buffer);
    error = 8;
  }
  else
  {
    Serial.print("reading from: ");
    Serial.println(buffer);

    uint8_t result = card.read_file(f, (uint8_t *)buffer, 8);

    if (result >0)
      Serial.print(buffer);
    else if (result == 0)
      Serial.println("end of file.");
    else
    {
      Serial.print("read error: ");
      Serial.println(result, DEC);
    }
  }
}

#endif