aaa
Dependencies: BSP_B-L475E-IOT01
main.cpp@0:11c09159d5c4, 2018-12-06 (annotated)
- Committer:
- frunzl
- Date:
- Thu Dec 06 12:14:53 2018 +0000
- Revision:
- 0:11c09159d5c4
aaa
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
frunzl | 0:11c09159d5c4 | 1 | |
frunzl | 0:11c09159d5c4 | 2 | #include "mbed.h" |
frunzl | 0:11c09159d5c4 | 3 | #include <stdio.h> |
frunzl | 0:11c09159d5c4 | 4 | #include <errno.h> |
frunzl | 0:11c09159d5c4 | 5 | |
frunzl | 0:11c09159d5c4 | 6 | // Block devices |
frunzl | 0:11c09159d5c4 | 7 | #if COMPONENT_SPIF |
frunzl | 0:11c09159d5c4 | 8 | #include "SPIFBlockDevice.h" |
frunzl | 0:11c09159d5c4 | 9 | #endif |
frunzl | 0:11c09159d5c4 | 10 | |
frunzl | 0:11c09159d5c4 | 11 | #if COMPONENT_DATAFLASH |
frunzl | 0:11c09159d5c4 | 12 | #include "DataFlashBlockDevice.h" |
frunzl | 0:11c09159d5c4 | 13 | #endif |
frunzl | 0:11c09159d5c4 | 14 | |
frunzl | 0:11c09159d5c4 | 15 | #if COMPONENT_SD |
frunzl | 0:11c09159d5c4 | 16 | #include "SDBlockDevice.h" |
frunzl | 0:11c09159d5c4 | 17 | #endif |
frunzl | 0:11c09159d5c4 | 18 | |
frunzl | 0:11c09159d5c4 | 19 | #include "HeapBlockDevice.h" |
frunzl | 0:11c09159d5c4 | 20 | |
frunzl | 0:11c09159d5c4 | 21 | // File systems |
frunzl | 0:11c09159d5c4 | 22 | #include "LittleFileSystem.h" |
frunzl | 0:11c09159d5c4 | 23 | #include "FATFileSystem.h" |
frunzl | 0:11c09159d5c4 | 24 | #include "stm32l475e_iot01_accelero.h" |
frunzl | 0:11c09159d5c4 | 25 | #include "nvstore.h" |
frunzl | 0:11c09159d5c4 | 26 | |
frunzl | 0:11c09159d5c4 | 27 | // Physical block device, can be any device that supports the BlockDevice API |
frunzl | 0:11c09159d5c4 | 28 | /*SPIFBlockDevice bd( |
frunzl | 0:11c09159d5c4 | 29 | MBED_CONF_SPIF_DRIVER_SPI_MOSI, |
frunzl | 0:11c09159d5c4 | 30 | MBED_CONF_SPIF_DRIVER_SPI_MISO, |
frunzl | 0:11c09159d5c4 | 31 | MBED_CONF_SPIF_DRIVER_SPI_CLK, |
frunzl | 0:11c09159d5c4 | 32 | MBED_CONF_SPIF_DRIVER_SPI_CS);*/ |
frunzl | 0:11c09159d5c4 | 33 | |
frunzl | 0:11c09159d5c4 | 34 | DigitalOut led_1(LED1); |
frunzl | 0:11c09159d5c4 | 35 | DigitalOut led_2(LED2); |
frunzl | 0:11c09159d5c4 | 36 | DigitalOut led_3(LED3); |
frunzl | 0:11c09159d5c4 | 37 | Ticker toggle_led_ticker; |
frunzl | 0:11c09159d5c4 | 38 | |
frunzl | 0:11c09159d5c4 | 39 | |
frunzl | 0:11c09159d5c4 | 40 | EventQueue queue(32 * EVENTS_EVENT_SIZE); |
frunzl | 0:11c09159d5c4 | 41 | Thread t; |
frunzl | 0:11c09159d5c4 | 42 | |
frunzl | 0:11c09159d5c4 | 43 | #define BLOCK_SIZE 512 |
frunzl | 0:11c09159d5c4 | 44 | HeapBlockDevice bd(16384, BLOCK_SIZE); |
frunzl | 0:11c09159d5c4 | 45 | |
frunzl | 0:11c09159d5c4 | 46 | // File system declaration |
frunzl | 0:11c09159d5c4 | 47 | LittleFileSystem fs("fs"); |
frunzl | 0:11c09159d5c4 | 48 | |
frunzl | 0:11c09159d5c4 | 49 | // Set up the button to trigger an erase |
frunzl | 0:11c09159d5c4 | 50 | InterruptIn irq(BUTTON1); |
frunzl | 0:11c09159d5c4 | 51 | void erase() { |
frunzl | 0:11c09159d5c4 | 52 | printf("Initializing the block device... "); |
frunzl | 0:11c09159d5c4 | 53 | fflush(stdout); |
frunzl | 0:11c09159d5c4 | 54 | int err = bd.init(); |
frunzl | 0:11c09159d5c4 | 55 | printf("%s\n", (err ? "Fail :(" : "OK")); |
frunzl | 0:11c09159d5c4 | 56 | if (err) { |
frunzl | 0:11c09159d5c4 | 57 | error("error: %s (%d)\n", strerror(-err), err); |
frunzl | 0:11c09159d5c4 | 58 | } |
frunzl | 0:11c09159d5c4 | 59 | |
frunzl | 0:11c09159d5c4 | 60 | printf("Erasing the block device... "); |
frunzl | 0:11c09159d5c4 | 61 | fflush(stdout); |
frunzl | 0:11c09159d5c4 | 62 | err = bd.erase(0, bd.size()); |
frunzl | 0:11c09159d5c4 | 63 | printf("%s\n", (err ? "Fail :(" : "OK")); |
frunzl | 0:11c09159d5c4 | 64 | if (err) { |
frunzl | 0:11c09159d5c4 | 65 | error("error: %s (%d)\n", strerror(-err), err); |
frunzl | 0:11c09159d5c4 | 66 | } |
frunzl | 0:11c09159d5c4 | 67 | |
frunzl | 0:11c09159d5c4 | 68 | printf("Deinitializing the block device... "); |
frunzl | 0:11c09159d5c4 | 69 | fflush(stdout); |
frunzl | 0:11c09159d5c4 | 70 | err = bd.deinit(); |
frunzl | 0:11c09159d5c4 | 71 | printf("%s\n", (err ? "Fail :(" : "OK")); |
frunzl | 0:11c09159d5c4 | 72 | if (err) { |
frunzl | 0:11c09159d5c4 | 73 | error("error: %s (%d)\n", strerror(-err), err); |
frunzl | 0:11c09159d5c4 | 74 | } |
frunzl | 0:11c09159d5c4 | 75 | } |
frunzl | 0:11c09159d5c4 | 76 | static FILE *f; |
frunzl | 0:11c09159d5c4 | 77 | static uint16_t key_x = 1; |
frunzl | 0:11c09159d5c4 | 78 | static uint16_t key_y = 2; |
frunzl | 0:11c09159d5c4 | 79 | static uint16_t key_z = 3; |
frunzl | 0:11c09159d5c4 | 80 | static uint32_t value_x; |
frunzl | 0:11c09159d5c4 | 81 | static uint32_t value_y; |
frunzl | 0:11c09159d5c4 | 82 | static uint32_t value_z; |
frunzl | 0:11c09159d5c4 | 83 | volatile int counter = 0; |
frunzl | 0:11c09159d5c4 | 84 | |
frunzl | 0:11c09159d5c4 | 85 | void get_data_from_sensors() { |
frunzl | 0:11c09159d5c4 | 86 | int16_t pDataXYZ[3] = {0}; |
frunzl | 0:11c09159d5c4 | 87 | BSP_ACCELERO_AccGetXYZ(pDataXYZ); |
frunzl | 0:11c09159d5c4 | 88 | if (abs(pDataXYZ[0]) > 900) { |
frunzl | 0:11c09159d5c4 | 89 | fprintf(f, "%d\n", 1); |
frunzl | 0:11c09159d5c4 | 90 | } else if (abs(pDataXYZ[1]) > 900) { |
frunzl | 0:11c09159d5c4 | 91 | fprintf(f, "%d\n", 2); |
frunzl | 0:11c09159d5c4 | 92 | } else if (abs(pDataXYZ[2]) > 900) { |
frunzl | 0:11c09159d5c4 | 93 | fprintf(f, "%d\n", 3); |
frunzl | 0:11c09159d5c4 | 94 | } else { |
frunzl | 0:11c09159d5c4 | 95 | fprintf(f, "%d\n", -1); |
frunzl | 0:11c09159d5c4 | 96 | } |
frunzl | 0:11c09159d5c4 | 97 | fflush(f); |
frunzl | 0:11c09159d5c4 | 98 | fflush(stdout); |
frunzl | 0:11c09159d5c4 | 99 | } |
frunzl | 0:11c09159d5c4 | 100 | |
frunzl | 0:11c09159d5c4 | 101 | void read_data_from_file() { |
frunzl | 0:11c09159d5c4 | 102 | int led1 = 0; |
frunzl | 0:11c09159d5c4 | 103 | int led2 = 0; |
frunzl | 0:11c09159d5c4 | 104 | int led3 = 0; |
frunzl | 0:11c09159d5c4 | 105 | fflush(stdout); |
frunzl | 0:11c09159d5c4 | 106 | fflush(f); |
frunzl | 0:11c09159d5c4 | 107 | |
frunzl | 0:11c09159d5c4 | 108 | fseek(f, 0, SEEK_SET); |
frunzl | 0:11c09159d5c4 | 109 | int number; |
frunzl | 0:11c09159d5c4 | 110 | while (!feof(f)) { |
frunzl | 0:11c09159d5c4 | 111 | fscanf(f, "%d", &number); |
frunzl | 0:11c09159d5c4 | 112 | if (number == 1) { |
frunzl | 0:11c09159d5c4 | 113 | led3 +=1; |
frunzl | 0:11c09159d5c4 | 114 | } else if(number == 2) { |
frunzl | 0:11c09159d5c4 | 115 | led2 +=1; |
frunzl | 0:11c09159d5c4 | 116 | } else if(number == 3) { |
frunzl | 0:11c09159d5c4 | 117 | led1 +=1; |
frunzl | 0:11c09159d5c4 | 118 | } |
frunzl | 0:11c09159d5c4 | 119 | } |
frunzl | 0:11c09159d5c4 | 120 | |
frunzl | 0:11c09159d5c4 | 121 | NVStore &nvstore = NVStore::get_instance(); |
frunzl | 0:11c09159d5c4 | 122 | // Z Horizontal LED1, X short LED3, Y long LED2 |
frunzl | 0:11c09159d5c4 | 123 | if (led1 >= led2 && led1 >= led3) { |
frunzl | 0:11c09159d5c4 | 124 | led_1 = 1; |
frunzl | 0:11c09159d5c4 | 125 | led_2 = 0; |
frunzl | 0:11c09159d5c4 | 126 | led_3 = 0; |
frunzl | 0:11c09159d5c4 | 127 | value_x++; |
frunzl | 0:11c09159d5c4 | 128 | nvstore.set(key_x, sizeof(value_x), &value_x); |
frunzl | 0:11c09159d5c4 | 129 | } else if (led2 >= led1 && led2 >= led3 ) { |
frunzl | 0:11c09159d5c4 | 130 | led_1 = 0; |
frunzl | 0:11c09159d5c4 | 131 | led_2 = 1; |
frunzl | 0:11c09159d5c4 | 132 | led_3 = 0; |
frunzl | 0:11c09159d5c4 | 133 | value_y++; |
frunzl | 0:11c09159d5c4 | 134 | nvstore.set(key_y, sizeof(value_y), &value_y); |
frunzl | 0:11c09159d5c4 | 135 | } else if (led3 >= led1 && led3 >= led2) { |
frunzl | 0:11c09159d5c4 | 136 | led_1 = 0; |
frunzl | 0:11c09159d5c4 | 137 | led_2 = 0; |
frunzl | 0:11c09159d5c4 | 138 | led_3 = 1; |
frunzl | 0:11c09159d5c4 | 139 | value_z++; |
frunzl | 0:11c09159d5c4 | 140 | nvstore.set(key_z, sizeof(value_z), &value_z); |
frunzl | 0:11c09159d5c4 | 141 | } |
frunzl | 0:11c09159d5c4 | 142 | |
frunzl | 0:11c09159d5c4 | 143 | fflush(stdout); |
frunzl | 0:11c09159d5c4 | 144 | int err = fclose(f); |
frunzl | 0:11c09159d5c4 | 145 | printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
frunzl | 0:11c09159d5c4 | 146 | if (err < 0) { |
frunzl | 0:11c09159d5c4 | 147 | error("error: %s (%d)\n", strerror(err), -err); |
frunzl | 0:11c09159d5c4 | 148 | } |
frunzl | 0:11c09159d5c4 | 149 | err = fs.unmount(); |
frunzl | 0:11c09159d5c4 | 150 | printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
frunzl | 0:11c09159d5c4 | 151 | if (err < 0) { |
frunzl | 0:11c09159d5c4 | 152 | error("error: %s (%d)\n", strerror(-err), err); |
frunzl | 0:11c09159d5c4 | 153 | } |
frunzl | 0:11c09159d5c4 | 154 | |
frunzl | 0:11c09159d5c4 | 155 | printf("Mbed OS filesystem example done!\n"); |
frunzl | 0:11c09159d5c4 | 156 | |
frunzl | 0:11c09159d5c4 | 157 | } |
frunzl | 0:11c09159d5c4 | 158 | |
frunzl | 0:11c09159d5c4 | 159 | void toggle_led() { |
frunzl | 0:11c09159d5c4 | 160 | queue.call(get_data_from_sensors); |
frunzl | 0:11c09159d5c4 | 161 | counter++; |
frunzl | 0:11c09159d5c4 | 162 | if (counter == 100*10) { |
frunzl | 0:11c09159d5c4 | 163 | toggle_led_ticker.detach(); |
frunzl | 0:11c09159d5c4 | 164 | queue.call(read_data_from_file); |
frunzl | 0:11c09159d5c4 | 165 | } |
frunzl | 0:11c09159d5c4 | 166 | |
frunzl | 0:11c09159d5c4 | 167 | } |
frunzl | 0:11c09159d5c4 | 168 | |
frunzl | 0:11c09159d5c4 | 169 | // Entry point for the example |
frunzl | 0:11c09159d5c4 | 170 | int main() { |
frunzl | 0:11c09159d5c4 | 171 | t.start(callback(&queue, &EventQueue::dispatch_forever)); |
frunzl | 0:11c09159d5c4 | 172 | BSP_ACCELERO_Init(); |
frunzl | 0:11c09159d5c4 | 173 | |
frunzl | 0:11c09159d5c4 | 174 | NVStore &nvstore = NVStore::get_instance(); |
frunzl | 0:11c09159d5c4 | 175 | |
frunzl | 0:11c09159d5c4 | 176 | uint16_t actual_len_bytes = 0; |
frunzl | 0:11c09159d5c4 | 177 | int rc; |
frunzl | 0:11c09159d5c4 | 178 | |
frunzl | 0:11c09159d5c4 | 179 | rc = nvstore.init(); |
frunzl | 0:11c09159d5c4 | 180 | printf("Init NVStore. \n"); |
frunzl | 0:11c09159d5c4 | 181 | |
frunzl | 0:11c09159d5c4 | 182 | rc = nvstore.get(key_x, sizeof(value_x), &value_x, actual_len_bytes); |
frunzl | 0:11c09159d5c4 | 183 | if (rc == NVSTORE_NOT_FOUND) { |
frunzl | 0:11c09159d5c4 | 184 | value_x = 0; |
frunzl | 0:11c09159d5c4 | 185 | value_y = 0; |
frunzl | 0:11c09159d5c4 | 186 | value_z = 0; |
frunzl | 0:11c09159d5c4 | 187 | nvstore.set(key_x, sizeof(value_x), &value_x); |
frunzl | 0:11c09159d5c4 | 188 | nvstore.set(key_y, sizeof(value_y), &value_y); |
frunzl | 0:11c09159d5c4 | 189 | nvstore.set(key_z, sizeof(value_z), &value_z); |
frunzl | 0:11c09159d5c4 | 190 | } else { |
frunzl | 0:11c09159d5c4 | 191 | nvstore.get(key_y, sizeof(value_y), &value_y, actual_len_bytes); |
frunzl | 0:11c09159d5c4 | 192 | nvstore.get(key_z, sizeof(value_z), &value_z, actual_len_bytes); |
frunzl | 0:11c09159d5c4 | 193 | } |
frunzl | 0:11c09159d5c4 | 194 | |
frunzl | 0:11c09159d5c4 | 195 | printf("LED1: %d\n LED2: %d\n LED3: %d\n", value_x, value_y, value_z); |
frunzl | 0:11c09159d5c4 | 196 | |
frunzl | 0:11c09159d5c4 | 197 | printf("--- Mbed OS AccelleratorReadNV ---\n"); |
frunzl | 0:11c09159d5c4 | 198 | |
frunzl | 0:11c09159d5c4 | 199 | // Setup the erase event on button press, use the event queue |
frunzl | 0:11c09159d5c4 | 200 | // to avoid running in interrupt context |
frunzl | 0:11c09159d5c4 | 201 | irq.fall(mbed_event_queue()->event(erase)); |
frunzl | 0:11c09159d5c4 | 202 | |
frunzl | 0:11c09159d5c4 | 203 | // Try to mount the filesystem |
frunzl | 0:11c09159d5c4 | 204 | printf("Mounting the filesystem... "); |
frunzl | 0:11c09159d5c4 | 205 | fflush(stdout); |
frunzl | 0:11c09159d5c4 | 206 | int err = fs.mount(&bd); |
frunzl | 0:11c09159d5c4 | 207 | printf("%s\n", (err ? "Fail :(" : "OK")); |
frunzl | 0:11c09159d5c4 | 208 | if (err) { |
frunzl | 0:11c09159d5c4 | 209 | // Reformat if we can't mount the filesystem |
frunzl | 0:11c09159d5c4 | 210 | // this should only happen on the first boot |
frunzl | 0:11c09159d5c4 | 211 | printf("No filesystem found, formatting... "); |
frunzl | 0:11c09159d5c4 | 212 | fflush(stdout); |
frunzl | 0:11c09159d5c4 | 213 | err = fs.reformat(&bd); |
frunzl | 0:11c09159d5c4 | 214 | printf("%s\n", (err ? "Fail :(" : "OK")); |
frunzl | 0:11c09159d5c4 | 215 | if (err) { |
frunzl | 0:11c09159d5c4 | 216 | error("error: %s (%d)\n", strerror(-err), err); |
frunzl | 0:11c09159d5c4 | 217 | } |
frunzl | 0:11c09159d5c4 | 218 | } |
frunzl | 0:11c09159d5c4 | 219 | |
frunzl | 0:11c09159d5c4 | 220 | // Open the numbers file |
frunzl | 0:11c09159d5c4 | 221 | printf("Opening \"/fs/numbers.txt\"... "); |
frunzl | 0:11c09159d5c4 | 222 | fflush(stdout); |
frunzl | 0:11c09159d5c4 | 223 | f = fopen("/fs/numbers.txt", "r +"); |
frunzl | 0:11c09159d5c4 | 224 | printf("%s\n", (!f ? "Fail :(" : "OK")); |
frunzl | 0:11c09159d5c4 | 225 | if (!f) { |
frunzl | 0:11c09159d5c4 | 226 | // Create the numbers file if it doesn't exist |
frunzl | 0:11c09159d5c4 | 227 | printf("No file found, creating a new file... "); |
frunzl | 0:11c09159d5c4 | 228 | fflush(stdout); |
frunzl | 0:11c09159d5c4 | 229 | f = fopen("/fs/numbers.txt", "w+"); |
frunzl | 0:11c09159d5c4 | 230 | printf("%s\n", (!f ? "Fail :(" : "OK")); |
frunzl | 0:11c09159d5c4 | 231 | if (!f) { |
frunzl | 0:11c09159d5c4 | 232 | error("error: %s (%d)\n", strerror(errno), -errno); |
frunzl | 0:11c09159d5c4 | 233 | } |
frunzl | 0:11c09159d5c4 | 234 | |
frunzl | 0:11c09159d5c4 | 235 | // for (int i = 0; i < 10; i++) { |
frunzl | 0:11c09159d5c4 | 236 | // printf("\rWriting numbers (%d/%d)... ", i, 10); |
frunzl | 0:11c09159d5c4 | 237 | // fflush(stdout); |
frunzl | 0:11c09159d5c4 | 238 | // err = fprintf(f, " %d\n", i); |
frunzl | 0:11c09159d5c4 | 239 | // if (err < 0) { |
frunzl | 0:11c09159d5c4 | 240 | // printf("Fail :(\n"); |
frunzl | 0:11c09159d5c4 | 241 | // error("error: %s (%d)\n", strerror(errno), -errno); |
frunzl | 0:11c09159d5c4 | 242 | // } |
frunzl | 0:11c09159d5c4 | 243 | // } |
frunzl | 0:11c09159d5c4 | 244 | // printf("\rWriting numbers (%d/%d)... OK\n", 10, 10); |
frunzl | 0:11c09159d5c4 | 245 | |
frunzl | 0:11c09159d5c4 | 246 | printf("Seeking file... "); |
frunzl | 0:11c09159d5c4 | 247 | fflush(stdout); |
frunzl | 0:11c09159d5c4 | 248 | err = fseek(f, 0, SEEK_SET); |
frunzl | 0:11c09159d5c4 | 249 | printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
frunzl | 0:11c09159d5c4 | 250 | if (err < 0) { |
frunzl | 0:11c09159d5c4 | 251 | error("error: %s (%d)\n", strerror(errno), -errno); |
frunzl | 0:11c09159d5c4 | 252 | } |
frunzl | 0:11c09159d5c4 | 253 | } |
frunzl | 0:11c09159d5c4 | 254 | |
frunzl | 0:11c09159d5c4 | 255 | // Go through and record the acc |
frunzl | 0:11c09159d5c4 | 256 | toggle_led_ticker.attach(&toggle_led, 0.01); |
frunzl | 0:11c09159d5c4 | 257 | |
frunzl | 0:11c09159d5c4 | 258 | |
frunzl | 0:11c09159d5c4 | 259 | // for (int i = 0; i < 10; i++) { |
frunzl | 0:11c09159d5c4 | 260 | // printf("\rIncrementing numbers (%d/%d)... ", i, 10); |
frunzl | 0:11c09159d5c4 | 261 | // fflush(stdout); |
frunzl | 0:11c09159d5c4 | 262 | // |
frunzl | 0:11c09159d5c4 | 263 | // // Get current stream position |
frunzl | 0:11c09159d5c4 | 264 | // long pos = ftell(f); |
frunzl | 0:11c09159d5c4 | 265 | // |
frunzl | 0:11c09159d5c4 | 266 | // // Parse out the number and increment |
frunzl | 0:11c09159d5c4 | 267 | // int32_t number; |
frunzl | 0:11c09159d5c4 | 268 | // fscanf(f, "%d", &number); |
frunzl | 0:11c09159d5c4 | 269 | // number += 1; |
frunzl | 0:11c09159d5c4 | 270 | // |
frunzl | 0:11c09159d5c4 | 271 | // // Seek to beginning of number |
frunzl | 0:11c09159d5c4 | 272 | // fseek(f, pos, SEEK_SET); |
frunzl | 0:11c09159d5c4 | 273 | // |
frunzl | 0:11c09159d5c4 | 274 | // // Store number |
frunzl | 0:11c09159d5c4 | 275 | // fprintf(f, " %d\n", number); |
frunzl | 0:11c09159d5c4 | 276 | // |
frunzl | 0:11c09159d5c4 | 277 | // // Flush between write and read on same file |
frunzl | 0:11c09159d5c4 | 278 | // fflush(f); |
frunzl | 0:11c09159d5c4 | 279 | // } |
frunzl | 0:11c09159d5c4 | 280 | // printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10); |
frunzl | 0:11c09159d5c4 | 281 | |
frunzl | 0:11c09159d5c4 | 282 | // Close the file which also flushes any cached writes |
frunzl | 0:11c09159d5c4 | 283 | //printf("Closing \"/fs/numbers.txt\"... "); |
frunzl | 0:11c09159d5c4 | 284 | // fflush(stdout); |
frunzl | 0:11c09159d5c4 | 285 | // err = fclose(f); |
frunzl | 0:11c09159d5c4 | 286 | // printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
frunzl | 0:11c09159d5c4 | 287 | // if (err < 0) { |
frunzl | 0:11c09159d5c4 | 288 | // error("error: %s (%d)\n", strerror(errno), -errno); |
frunzl | 0:11c09159d5c4 | 289 | // } |
frunzl | 0:11c09159d5c4 | 290 | // |
frunzl | 0:11c09159d5c4 | 291 | // // Display the root directory |
frunzl | 0:11c09159d5c4 | 292 | // printf("Opening the root directory... "); |
frunzl | 0:11c09159d5c4 | 293 | // fflush(stdout); |
frunzl | 0:11c09159d5c4 | 294 | // DIR *d = opendir("/fs/"); |
frunzl | 0:11c09159d5c4 | 295 | // printf("%s\n", (!d ? "Fail :(" : "OK")); |
frunzl | 0:11c09159d5c4 | 296 | // if (!d) { |
frunzl | 0:11c09159d5c4 | 297 | // error("error: %s (%d)\n", strerror(errno), -errno); |
frunzl | 0:11c09159d5c4 | 298 | // } |
frunzl | 0:11c09159d5c4 | 299 | // |
frunzl | 0:11c09159d5c4 | 300 | // printf("root directory:\n"); |
frunzl | 0:11c09159d5c4 | 301 | // while (true) { |
frunzl | 0:11c09159d5c4 | 302 | // struct dirent *e = readdir(d); |
frunzl | 0:11c09159d5c4 | 303 | // if (!e) { |
frunzl | 0:11c09159d5c4 | 304 | // break; |
frunzl | 0:11c09159d5c4 | 305 | // } |
frunzl | 0:11c09159d5c4 | 306 | // |
frunzl | 0:11c09159d5c4 | 307 | // printf(" %s\n", e->d_name); |
frunzl | 0:11c09159d5c4 | 308 | // } |
frunzl | 0:11c09159d5c4 | 309 | // |
frunzl | 0:11c09159d5c4 | 310 | // printf("Closing the root directory... "); |
frunzl | 0:11c09159d5c4 | 311 | // fflush(stdout); |
frunzl | 0:11c09159d5c4 | 312 | // err = closedir(d); |
frunzl | 0:11c09159d5c4 | 313 | // printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
frunzl | 0:11c09159d5c4 | 314 | // if (err < 0) { |
frunzl | 0:11c09159d5c4 | 315 | // error("error: %s (%d)\n", strerror(errno), -errno); |
frunzl | 0:11c09159d5c4 | 316 | // } |
frunzl | 0:11c09159d5c4 | 317 | // |
frunzl | 0:11c09159d5c4 | 318 | // // Display the numbers file |
frunzl | 0:11c09159d5c4 | 319 | // printf("Opening \"/fs/numbers.txt\"... "); |
frunzl | 0:11c09159d5c4 | 320 | // fflush(stdout); |
frunzl | 0:11c09159d5c4 | 321 | // f = fopen("/fs/numbers.txt", "r"); |
frunzl | 0:11c09159d5c4 | 322 | // printf("%s\n", (!f ? "Fail :(" : "OK")); |
frunzl | 0:11c09159d5c4 | 323 | // if (!f) { |
frunzl | 0:11c09159d5c4 | 324 | // error("error: %s (%d)\n", strerror(errno), -errno); |
frunzl | 0:11c09159d5c4 | 325 | // } |
frunzl | 0:11c09159d5c4 | 326 | // |
frunzl | 0:11c09159d5c4 | 327 | // printf("numbers:\n"); |
frunzl | 0:11c09159d5c4 | 328 | // while (!feof(f)) { |
frunzl | 0:11c09159d5c4 | 329 | // int c = fgetc(f); |
frunzl | 0:11c09159d5c4 | 330 | // printf("%c", c); |
frunzl | 0:11c09159d5c4 | 331 | // } |
frunzl | 0:11c09159d5c4 | 332 | // |
frunzl | 0:11c09159d5c4 | 333 | // printf("\rClosing \"/fs/numbers.txt\"... "); |
frunzl | 0:11c09159d5c4 | 334 | // fflush(stdout); |
frunzl | 0:11c09159d5c4 | 335 | // err = fclose(f); |
frunzl | 0:11c09159d5c4 | 336 | // printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
frunzl | 0:11c09159d5c4 | 337 | // if (err < 0) { |
frunzl | 0:11c09159d5c4 | 338 | // error("error: %s (%d)\n", strerror(errno), -errno); |
frunzl | 0:11c09159d5c4 | 339 | // } |
frunzl | 0:11c09159d5c4 | 340 | // |
frunzl | 0:11c09159d5c4 | 341 | // // Tidy up |
frunzl | 0:11c09159d5c4 | 342 | // printf("Unmounting... "); |
frunzl | 0:11c09159d5c4 | 343 | // fflush(stdout); |
frunzl | 0:11c09159d5c4 | 344 | // err = fs.unmount(); |
frunzl | 0:11c09159d5c4 | 345 | // printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
frunzl | 0:11c09159d5c4 | 346 | // if (err < 0) { |
frunzl | 0:11c09159d5c4 | 347 | // error("error: %s (%d)\n", strerror(-err), err); |
frunzl | 0:11c09159d5c4 | 348 | // } |
frunzl | 0:11c09159d5c4 | 349 | // |
frunzl | 0:11c09159d5c4 | 350 | // printf("Mbed OS filesystem example done!\n"); |
frunzl | 0:11c09159d5c4 | 351 | } |
frunzl | 0:11c09159d5c4 | 352 |