Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BSP_B-L475E-IOT01
main.cpp
00001 #include "mbed.h" 00002 #include <stdio.h> 00003 #include <errno.h> 00004 00005 // Block devices 00006 #if COMPONENT_SPIF 00007 #include "SPIFBlockDevice.h" 00008 #endif 00009 00010 #if COMPONENT_DATAFLASH 00011 #include "DataFlashBlockDevice.h" 00012 #endif 00013 00014 #if COMPONENT_SD 00015 #include "SDBlockDevice.h" 00016 #endif 00017 00018 #include "HeapBlockDevice.h" 00019 00020 // File systems 00021 #include "LittleFileSystem.h" 00022 #include "FATFileSystem.h" 00023 #include "stm32l475e_iot01_accelero.h" 00024 00025 // Physical block device, can be any device that supports the BlockDevice API 00026 /*SPIFBlockDevice bd( 00027 MBED_CONF_SPIF_DRIVER_SPI_MOSI, 00028 MBED_CONF_SPIF_DRIVER_SPI_MISO, 00029 MBED_CONF_SPIF_DRIVER_SPI_CLK, 00030 MBED_CONF_SPIF_DRIVER_SPI_CS);*/ 00031 00032 DigitalOut led_1(LED1); 00033 DigitalOut led_2(LED2); 00034 DigitalOut led_3(LED3); 00035 Ticker check_led_status; 00036 00037 EventQueue queue(32 * EVENTS_EVENT_SIZE); 00038 Thread t; 00039 00040 #define BLOCK_SIZE 512 00041 HeapBlockDevice bd(32768, BLOCK_SIZE); 00042 00043 #define LED_1_VALUE 1 00044 #define LED_2_VALUE 2 00045 #define LED_3_VALUE 3 00046 00047 // File system declaration 00048 LittleFileSystem fs("fs"); 00049 00050 // Set up the button to trigger an erase 00051 InterruptIn irq(BUTTON1); 00052 void erase() { 00053 printf("Initializing the block device... "); 00054 fflush(stdout); 00055 int err = bd.init(); 00056 printf("%s\n", (err ? "Fail :(" : "OK")); 00057 if (err) { 00058 error("error: %s (%d)\n", strerror(-err), err); 00059 } 00060 00061 printf("Erasing the block device... "); 00062 fflush(stdout); 00063 err = bd.erase(0, bd.size()); 00064 printf("%s\n", (err ? "Fail :(" : "OK")); 00065 if (err) { 00066 error("error: %s (%d)\n", strerror(-err), err); 00067 } 00068 00069 printf("Deinitializing the block device... "); 00070 fflush(stdout); 00071 err = bd.deinit(); 00072 printf("%s\n", (err ? "Fail :(" : "OK")); 00073 if (err) { 00074 error("error: %s (%d)\n", strerror(-err), err); 00075 } 00076 } 00077 00078 static FILE *f; 00079 volatile int counter = 0; 00080 00081 void read_acc() { 00082 int16_t pDataXYZ[3] = {0}; 00083 BSP_ACCELERO_AccGetXYZ(pDataXYZ); 00084 if (abs(pDataXYZ[2]) > 900) { // board laying down 00085 fprintf(f, "%d\n", LED_1_VALUE); 00086 } else if (abs(pDataXYZ[1]) > 900) { // long side 00087 fprintf(f, "%d\n", LED_2_VALUE); 00088 } else if (abs(pDataXYZ[0]) > 900) { // short side 00089 fprintf(f, "%d\n", LED_3_VALUE); 00090 } else { // all other positions 00091 fprintf(f, "%d\n", -1); 00092 } 00093 00094 fflush(f); 00095 fflush(stdout); 00096 } 00097 00098 void read_data_from_file() { 00099 int led1_counter = 0; 00100 int led2_counter = 0; 00101 int led3_counter = 0; 00102 fflush(stdout); 00103 fflush(f); 00104 00105 // read file and count occurrences of position numbers 00106 fseek(f, 0, SEEK_SET); 00107 int number; 00108 while (!feof(f)) { 00109 fscanf(f, "%d", &number); 00110 00111 switch (number){ 00112 case LED_1_VALUE: 00113 led1_counter += 1; 00114 break; 00115 case LED_2_VALUE: 00116 led2_counter += 1; 00117 break; 00118 case LED_3_VALUE: 00119 led3_counter += 1; 00120 break; 00121 default: 00122 break; 00123 } 00124 } 00125 00126 // check which position is the one present the most 00127 if (led1_counter > led2_counter && led1_counter > led3_counter) { 00128 led_1 = 1; 00129 led_2 = 0; 00130 led_3 = 0; 00131 } else if (led2_counter > led1_counter && led2_counter > led3_counter ) { 00132 led_1 = 0; 00133 led_2 = 1; 00134 led_3 = 0; 00135 } else if (led3_counter > led1_counter && led3_counter > led2_counter) { 00136 led_1 = 0; 00137 led_2 = 0; 00138 led_3 = 1; 00139 } 00140 00141 fflush(stdout); 00142 int err = fclose(f); 00143 printf("%s\n", (err < 0 ? "Fail :(" : "OK")); 00144 if (err < 0) { 00145 error("error: %s (%d)\n", strerror(err), -err); 00146 } 00147 err = fs.unmount(); 00148 printf("%s\n", (err < 0 ? "Fail :(" : "OK")); 00149 if (err < 0) { 00150 error("error: %s (%d)\n", strerror(-err), err); 00151 } 00152 00153 if (led_1){ 00154 printf("Lighting up corresponding LED 1!\n"); 00155 } else if (led_2){ 00156 printf("Lighting up corresponding LED 2!\n"); 00157 } else if (led_3){ 00158 printf("Lighting up corresponding LED 3!\n"); 00159 } else { 00160 printf("No valid position detected during the sampling time, no LED lighting up!\n"); 00161 } 00162 00163 } 00164 00165 void toggle_led() { 00166 queue.call(read_acc); 00167 counter++; 00168 if (counter == 100*10) { 00169 check_led_status.detach(); 00170 queue.call(read_data_from_file); 00171 } 00172 00173 } 00174 00175 // Entry point for the example 00176 int main() { 00177 t.start(callback(&queue, &EventQueue::dispatch_forever)); 00178 BSP_ACCELERO_Init(); 00179 00180 00181 printf("--- Mbed OS filesystem example ---\n"); 00182 00183 // Setup the erase event on button press, use the event queue 00184 // to avoid running in interrupt context 00185 irq.fall(mbed_event_queue()->event(erase)); 00186 00187 // Try to mount the filesystem 00188 printf("Mounting the filesystem... "); 00189 fflush(stdout); 00190 int err = fs.mount(&bd); 00191 printf("%s\n", (err ? "Fail :(" : "OK")); 00192 if (err) { 00193 // Reformat if we can't mount the filesystem 00194 // this should only happen on the first boot 00195 printf("No filesystem found, formatting... "); 00196 fflush(stdout); 00197 err = fs.reformat(&bd); 00198 printf("%s\n", (err ? "Fail :(" : "OK")); 00199 if (err) { 00200 error("error: %s (%d)\n", strerror(-err), err); 00201 } 00202 } 00203 00204 // Open the numbers file 00205 printf("Opening \"/fs/numbers.txt\"... "); 00206 fflush(stdout); 00207 f = fopen("/fs/numbers.txt", "r +"); 00208 printf("%s\n", (!f ? "Fail :(" : "OK")); 00209 if (!f) { 00210 // Create the numbers file if it doesn't exist 00211 printf("No file found, creating a new file... "); 00212 fflush(stdout); 00213 f = fopen("/fs/numbers.txt", "w+"); 00214 printf("%s\n", (!f ? "Fail :(" : "OK")); 00215 if (!f) { 00216 error("error: %s (%d)\n", strerror(errno), -errno); 00217 } 00218 00219 printf("Seeking file... "); 00220 fflush(stdout); 00221 err = fseek(f, 0, SEEK_SET); 00222 printf("%s\n", (err < 0 ? "Fail :(" : "OK")); 00223 if (err < 0) { 00224 error("error: %s (%d)\n", strerror(errno), -errno); 00225 } 00226 } 00227 00228 check_led_status.attach(&toggle_led, 0.01); 00229 }
Generated on Fri Jul 15 2022 15:37:40 by
1.7.2