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 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_tsensor.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 // Set up the button to trigger an erase 00041 InterruptIn irq(BUTTON1); 00042 void erase() { 00043 printf("Initializing the block device... "); 00044 fflush(stdout); 00045 int err = bd.init(); 00046 printf("%s\n", (err ? "Fail :(" : "OK")); 00047 if (err) { 00048 error("error: %s (%d)\n", strerror(-err), err); 00049 } 00050 00051 printf("Erasing the block device... "); 00052 fflush(stdout); 00053 err = bd.erase(0, bd.size()); 00054 printf("%s\n", (err ? "Fail :(" : "OK")); 00055 if (err) { 00056 error("error: %s (%d)\n", strerror(-err), err); 00057 } 00058 00059 printf("Deinitializing the block device... "); 00060 fflush(stdout); 00061 err = bd.deinit(); 00062 printf("%s\n", (err ? "Fail :(" : "OK")); 00063 if (err) { 00064 error("error: %s (%d)\n", strerror(-err), err); 00065 } 00066 } 00067 00068 00069 // Entry point for the example 00070 00071 Ticker toggle_write_to_file; 00072 EventQueue queue(16 * EVENTS_EVENT_SIZE); 00073 Thread t; 00074 static FILE *f; 00075 InterruptIn button(USER_BUTTON); 00076 00077 void writeTemToFile(){ 00078 float temp = BSP_TSENSOR_ReadTemp(); 00079 printf("\nTEMPERATURE = %.2f degC\n", temp); 00080 00081 fprintf(f, "%f\n", temp); 00082 fflush(stdout); 00083 fflush(f); 00084 } 00085 00086 void readTempFile(){ 00087 fflush(stdout); 00088 fflush(f); 00089 fseek(f, 0, SEEK_SET); 00090 printf("Temperature time line: \n"); 00091 while (!feof(f)) { 00092 float temp; 00093 fscanf(f, "%f", &temp); 00094 printf("%f degC ", temp); 00095 } 00096 printf("\n"); 00097 fflush(stdout); 00098 } 00099 00100 void readSensorInterupt() { 00101 queue.call(writeTemToFile); 00102 } 00103 00104 void printSensorInterupt() { 00105 queue.call(readTempFile); 00106 } 00107 00108 int main() { 00109 // Setup the erase event on button press, use the event queue 00110 // to avoid running in interrupt context 00111 irq.fall(mbed_event_queue()->event(erase)); 00112 00113 // Try to mount the filesystem 00114 printf("Mounting the filesystem... "); 00115 fflush(stdout); 00116 int err = fs.mount(&bd); 00117 printf("%s\n", (err ? "Fail :(" : "OK")); 00118 if (err) { 00119 // Reformat if we can't mount the filesystem 00120 // this should only happen on the first boot 00121 printf("No filesystem found, formatting... "); 00122 fflush(stdout); 00123 err = fs.reformat(&bd); 00124 printf("%s\n", (err ? "Fail :(" : "OK")); 00125 if (err) { 00126 error("error: %s (%d)\n", strerror(-err), err); 00127 } 00128 } 00129 00130 // Open the numbers file 00131 printf("Opening \"/fs/numbers.txt\"... "); 00132 fflush(stdout); 00133 f = fopen("/fs/numbers.txt", "r +"); 00134 printf("%s\n", (!f ? "Fail :(" : "OK")); 00135 if (!f) { 00136 // Create the numbers file if it doesn't exist 00137 printf("No file found, creating a new file... "); 00138 fflush(stdout); 00139 f = fopen("/fs/numbers.txt", "w+"); 00140 printf("%s\n", (!f ? "Fail :(" : "OK")); 00141 if (!f) { 00142 error("error: %s (%d)\n", strerror(errno), -errno); 00143 } 00144 00145 printf("Seeking file... "); 00146 fflush(stdout); 00147 err = fseek(f, 0, SEEK_SET); 00148 printf("%s\n", (err < 0 ? "Fail :(" : "OK")); 00149 if (err < 0) { 00150 error("error: %s (%d)\n", strerror(errno), -errno); 00151 } 00152 } 00153 00154 t.start(callback(&queue, &EventQueue::dispatch_forever)); 00155 printf("Start sampling... \n"); 00156 BSP_TSENSOR_Init(); 00157 toggle_write_to_file.attach(&readSensorInterupt, 5); 00158 button.rise(&printSensorInterupt); 00159 }
Generated on Wed Jul 13 2022 20:18:38 by
1.7.2