greenhouse
Public Member Functions
Tasks::Storage Class Reference

#include <storage_task.h>

Public Member Functions

void example (UI::Display *display)
 

Detailed Description

Definition at line 6 of file storage_task.h.

Member Function Documentation

◆ example()

void Tasks::Storage::example ( UI::Display display)

Definition at line 21 of file storage_task.cpp.

21  {
22  // Try to mount the filesystem
23  display->log((uint8_t *)"Mounting the filesystem...");
24  fflush(stdout);
25  int err = fs.mount(bd);
26  printf("%s\n", (err ? "Fail :(" : "OK"));
27  if (err) {
28  display->log((uint8_t *)"Could not mount SD card");
29  return;
30  }
31 
32  display->log((uint8_t *)"SD Card mounted successfully");
33 
34  // Open the numbers file
35  printf("Opening \"/fs/numbers.txt\"... ");
36  fflush(stdout);
37  FILE *f = fopen("/fs/numbers.txt", "r+");
38  printf("%s\n", (!f ? "Fail :(" : "OK"));
39  if (!f) {
40  // Create the numbers file if it doesn't exist
41  printf("No file found, creating a new file... ");
42  fflush(stdout);
43  f = fopen("/fs/numbers.txt", "w+");
44  printf("%s\n", (!f ? "Fail :(" : "OK"));
45  if (!f) {
46  error("error: %s (%d)\n", strerror(errno), -errno);
47  }
48 
49  for (int i = 0; i < 10; i++) {
50  printf("\rWriting numbers (%d/%d)... ", i, 10);
51  fflush(stdout);
52  err = fprintf(f, " %d\n", i);
53  if (err < 0) {
54  printf("Fail :(\n");
55  error("error: %s (%d)\n", strerror(errno), -errno);
56  }
57  }
58  printf("\rWriting numbers (%d/%d)... OK\n", 10, 10);
59 
60  printf("Seeking file... ");
61  fflush(stdout);
62  err = fseek(f, 0, SEEK_SET);
63  printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
64  if (err < 0) {
65  error("error: %s (%d)\n", strerror(errno), -errno);
66  }
67  }
68 
69  // Go through and increment the numbers
70  for (int i = 0; i < 10; i++) {
71  printf("\rIncrementing numbers (%d/%d)... ", i, 10);
72  fflush(stdout);
73 
74  // Get current stream position
75  long pos = ftell(f);
76 
77  // Parse out the number and increment
78  char buf[BUFFER_MAX_LEN];
79  if (!fgets(buf, BUFFER_MAX_LEN, f)) {
80  error("error: %s (%d)\n", strerror(errno), -errno);
81  }
82  char *endptr;
83  int32_t number = strtol(buf, &endptr, 10);
84  if ((errno == ERANGE) || // The number is too small/large
85  (endptr == buf) || // No character was read
86  (*endptr && *endptr != '\n') // The whole input was not converted
87  ) {
88  continue;
89  }
90  number += 1;
91 
92  // Seek to beginning of number
93  fseek(f, pos, SEEK_SET);
94 
95  // Store number
96  fprintf(f, " %d\n", number);
97 
98  // Flush between write and read on same file
99  fflush(f);
100  }
101  printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10);
102 
103  // Close the file which also flushes any cached writes
104  printf("Closing \"/fs/numbers.txt\"... ");
105  fflush(stdout);
106  err = fclose(f);
107  printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
108  if (err < 0) {
109  error("error: %s (%d)\n", strerror(errno), -errno);
110  }
111 
112  // Display the root directory
113  printf("Opening the root directory... ");
114  fflush(stdout);
115  DIR *d = opendir("/fs/");
116  printf("%s\n", (!d ? "Fail :(" : "OK"));
117  if (!d) {
118  error("error: %s (%d)\n", strerror(errno), -errno);
119  }
120 
121  printf("Path: / \n");
122  display->log((uint8_t *)"Path: /");
123  while (true) {
124  struct dirent *e = readdir(d);
125  if (!e) {
126  break;
127  }
128 
129  printf(" %s\n", e->d_name);
130 
131  std::string file_name = std::string(" ");
132  file_name.append(e->d_name);
133  file_name.append("\n");
134  display->log((uint8_t *)file_name.c_str());
135  }
136 
137  display->log((uint8_t *)"Closing the root directory");
138  fflush(stdout);
139  err = closedir(d);
140  printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
141  if (err < 0) {
142  error("error: %s (%d)\n", strerror(errno), -errno);
143  }
144 
145  // Display the numbers file
146  display->log((uint8_t *)"Opening /fs/numbers.txt");
147  fflush(stdout);
148  f = fopen("/fs/numbers.txt", "r");
149 
150  if (!f) {
151  display->log((uint8_t *)"Operation failed");
152  error("error: %s (%d)\n", strerror(errno), -errno);
153  }
154 
155  display->log((uint8_t *)"numbers:");
156  while (!feof(f)) {
157  int c = fgetc(f);
158  printf("%c", c);
159  }
160 
161  printf("\rClosing \"/fs/numbers.txt\"... ");
162  fflush(stdout);
163  err = fclose(f);
164  printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
165  if (err < 0) {
166  error("error: %s (%d)\n", strerror(errno), -errno);
167  }
168 
169  // Tidy up
170  printf("Unmounting... ");
171  fflush(stdout);
172  err = fs.unmount();
173  printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
174  if (err < 0) {
175  error("error: %s (%d)\n", strerror(-err), err);
176  }
177 }
void log(uint8_t *text)
Definition: ui.cpp:53
UI::Display * display
Definition: main.cpp:19
#define BUFFER_MAX_LEN
FATFileSystem fs("fs")
BlockDevice * bd

References bd, BUFFER_MAX_LEN, display, fs(), and UI::Display::log().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: