bbb

Dependencies:   IOTAtelier1819-FileSystem BSP_B-L475E-IOT01

Committer:
frunzl
Date:
Thu Dec 06 12:15:42 2018 +0000
Revision:
27:6961ff206736
Parent:
26:130bb1173866
bbbb

Who changed what in which revision?

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