Exercise 1
Dependencies: BSP_B-L475E-IOT01
main.cpp
00001 00002 #include "mbed.h" 00003 #include <stdio.h> 00004 #include <errno.h> 00005 00006 // Block devices 00007 #if COMPONENT_SPIF 00008 #include "SPIFBlockDevice.h" 00009 #endif 00010 00011 #if COMPONENT_DATAFLASH 00012 #include "DataFlashBlockDevice.h" 00013 #endif 00014 00015 #if COMPONENT_SD 00016 #include "SDBlockDevice.h" 00017 #endif 00018 00019 #include "HeapBlockDevice.h" 00020 00021 // File systems 00022 #include "LittleFileSystem.h" 00023 #include "FATFileSystem.h" 00024 00025 #include "stm32l475e_iot01_accelero.h" 00026 00027 // Physical block device, can be any device that supports the BlockDevice API 00028 /*SPIFBlockDevice bd( 00029 MBED_CONF_SPIF_DRIVER_SPI_MOSI, 00030 MBED_CONF_SPIF_DRIVER_SPI_MISO, 00031 MBED_CONF_SPIF_DRIVER_SPI_CLK, 00032 MBED_CONF_SPIF_DRIVER_SPI_CS);*/ 00033 00034 #define BLOCK_SIZE 512 00035 HeapBlockDevice bd(4096, BLOCK_SIZE); 00036 00037 // File system declaration 00038 LittleFileSystem fs("fs"); 00039 00040 DigitalOut led1(LED1); 00041 DigitalOut led2(LED2); 00042 DigitalOut led3(LED3); 00043 00044 // Set up the button to trigger an erase 00045 InterruptIn irq(BUTTON1); 00046 void erase() { 00047 printf("Initializing the block device... "); 00048 fflush(stdout); 00049 int err = bd.init(); 00050 printf("%s\n", (err ? "Fail :(" : "OK")); 00051 if (err) { 00052 error("error: %s (%d)\n", strerror(-err), err); 00053 } 00054 00055 printf("Erasing the block device... "); 00056 fflush(stdout); 00057 err = bd.erase(0, bd.size()); 00058 printf("%s\n", (err ? "Fail :(" : "OK")); 00059 if (err) { 00060 error("error: %s (%d)\n", strerror(-err), err); 00061 } 00062 00063 printf("Deinitializing the block device... "); 00064 fflush(stdout); 00065 err = bd.deinit(); 00066 printf("%s\n", (err ? "Fail :(" : "OK")); 00067 if (err) { 00068 error("error: %s (%d)\n", strerror(-err), err); 00069 } 00070 } 00071 00072 00073 // Entry point for the example 00074 00075 Ticker toggle_write_to_file; 00076 EventQueue queue(16 * EVENTS_EVENT_SIZE); 00077 Thread t; 00078 static FILE *f; 00079 volatile int counter = 0; 00080 time_t begin = time(NULL); 00081 00082 00083 00084 void writeAccToFile(){ 00085 int16_t pDataXYZ[3] = {0}; 00086 BSP_ACCELERO_AccGetXYZ(pDataXYZ); 00087 int x = abs(pDataXYZ[0]); 00088 int y = abs(pDataXYZ[1]); 00089 int z = abs(pDataXYZ[2]); 00090 char c; 00091 if(z > 950 && z < 1050){ 00092 c = 'z'; 00093 } else if(y > 950 && y < 1050){ 00094 c = 'y'; 00095 }else if(x > 950 && x < 1050){ 00096 c = 'x'; 00097 }else{ 00098 c = 'o'; 00099 } 00100 fprintf(f, "%c", c); 00101 fflush(stdout); 00102 } 00103 00104 void readAccFile(){ 00105 printf("end sampling \n"); 00106 00107 // Close the file which also flushes any cached writes 00108 printf("Closing \"/fs/numbers.txt\"... "); 00109 fflush(stdout); 00110 int err = fclose(f); 00111 printf("%s\n", (err < 0 ? "Fail :(" : "OK")); 00112 if (err < 0) { 00113 error("error: %s (%d)\n", strerror(errno), -errno); 00114 } 00115 00116 printf("Opening \"/fs/numbers.txt\"... "); 00117 fflush(stdout); 00118 f = fopen("/fs/numbers.txt", "r"); 00119 printf("%s\n", (!f ? "Fail :(" : "OK")); 00120 if (!f) { 00121 error("error: %s (%d)\n", strerror(errno), -errno); 00122 } 00123 00124 int xCount = 0; 00125 int yCount = 0; 00126 int zCount = 0; 00127 00128 while (!feof(f)) { 00129 char c = fgetc(f); 00130 if(c == 'x'){ 00131 xCount++; 00132 }else if(c == 'y'){ 00133 yCount++; 00134 }else if(c == 'z'){ 00135 zCount++; 00136 } 00137 } 00138 printf("Values: x: %d, y:%d, z: %d \n", xCount, yCount, zCount); 00139 00140 //Switch on the LED. x is default Led. 00141 if(xCount >= yCount && xCount >= zCount){ 00142 led3 = 1; 00143 }else if(yCount > xCount && yCount > zCount){ 00144 led2 = 1; 00145 }else{ 00146 led1 = 1; 00147 } 00148 00149 printf("\rClosing \"/fs/numbers.txt\"... "); 00150 fflush(stdout); 00151 err = fclose(f); 00152 printf("%s\n", (err < 0 ? "Fail :(" : "OK")); 00153 if (err < 0) { 00154 error("error: %s (%d)\n", strerror(errno), -errno); 00155 } 00156 00157 // Tidy up 00158 printf("Unmounting... "); 00159 fflush(stdout); 00160 err = fs.unmount(); 00161 printf("%s\n", (err < 0 ? "Fail :(" : "OK")); 00162 if (err < 0) { 00163 error("error: %s (%d)\n", strerror(-err), err); 00164 } 00165 printf("Mbed OS filesystem example done!\n"); 00166 00167 } 00168 00169 void readSensorsInterupt() { 00170 queue.call(writeAccToFile); 00171 counter++; 00172 if (counter == 1000) { 00173 toggle_write_to_file.detach(); 00174 queue.call(readAccFile); 00175 } 00176 } 00177 00178 int main() { 00179 // Setup the erase event on button press, use the event queue 00180 // to avoid running in interrupt context 00181 irq.fall(mbed_event_queue()->event(erase)); 00182 00183 // Try to mount the filesystem 00184 printf("Mounting the filesystem... "); 00185 fflush(stdout); 00186 int err = fs.mount(&bd); 00187 printf("%s\n", (err ? "Fail :(" : "OK")); 00188 if (err) { 00189 // Reformat if we can't mount the filesystem 00190 // this should only happen on the first boot 00191 printf("No filesystem found, formatting... "); 00192 fflush(stdout); 00193 err = fs.reformat(&bd); 00194 printf("%s\n", (err ? "Fail :(" : "OK")); 00195 if (err) { 00196 error("error: %s (%d)\n", strerror(-err), err); 00197 } 00198 } 00199 00200 // Open the numbers file 00201 printf("Opening \"/fs/numbers.txt\"... "); 00202 fflush(stdout); 00203 f = fopen("/fs/numbers.txt", "r +"); 00204 printf("%s\n", (!f ? "Fail :(" : "OK")); 00205 if (!f) { 00206 // Create the numbers file if it doesn't exist 00207 printf("No file found, creating a new file... "); 00208 fflush(stdout); 00209 f = fopen("/fs/numbers.txt", "w+"); 00210 printf("%s\n", (!f ? "Fail :(" : "OK")); 00211 if (!f) { 00212 error("error: %s (%d)\n", strerror(errno), -errno); 00213 } 00214 00215 printf("Seeking file... "); 00216 fflush(stdout); 00217 err = fseek(f, 0, SEEK_SET); 00218 printf("%s\n", (err < 0 ? "Fail :(" : "OK")); 00219 if (err < 0) { 00220 error("error: %s (%d)\n", strerror(errno), -errno); 00221 } 00222 } 00223 00224 t.start(callback(&queue, &EventQueue::dispatch_forever)); 00225 BSP_ACCELERO_Init(); 00226 led1 = 0; 00227 led2 = 0; 00228 led3 = 0; 00229 printf("Start sampling... \n"); 00230 begin = time(NULL); 00231 toggle_write_to_file.attach(&readSensorsInterupt, 0.01); 00232 } 00233 00234 00235 00236 00237 00238 00239 00240 00241 00242 00243 00244
Generated on Thu Jul 14 2022 23:03:31 by 1.7.2