Alessio Buratti / Mbed OS TemperatureButtonFile

Dependencies:   BSP_B-L475E-IOT01

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include <stdio.h>
00004 #include <errno.h>
00005 #include "stm32l475e_iot01_tsensor.h"
00006 #include "nvstore.h"
00007 
00008     // Block devices
00009 #if COMPONENT_SPIF
00010 #include "SPIFBlockDevice.h"
00011 #endif
00012 
00013 #if COMPONENT_DATAFLASH
00014 #include "DataFlashBlockDevice.h"
00015 #endif
00016 
00017 #if COMPONENT_SD
00018 #include "SDBlockDevice.h"
00019 #endif
00020 
00021 #include "HeapBlockDevice.h"
00022     // File systems
00023 #include "LittleFileSystem.h"
00024 #include "FATFileSystem.h"
00025 
00026 EventQueue queue(32 * EVENTS_EVENT_SIZE);
00027 Thread t;
00028 Ticker tTicker;
00029 
00030 
00031     // Physical block device, can be any device that supports the BlockDevice API
00032 /*SPIFBlockDevice bd(
00033  MBED_CONF_SPIF_DRIVER_SPI_MOSI,
00034  MBED_CONF_SPIF_DRIVER_SPI_MISO,
00035  MBED_CONF_SPIF_DRIVER_SPI_CLK,
00036  MBED_CONF_SPIF_DRIVER_SPI_CS);*/
00037 
00038 #define BLOCK_SIZE 512
00039 HeapBlockDevice bd(16384, BLOCK_SIZE);
00040 
00041     // File system declaration
00042 LittleFileSystem fs("fs");
00043 
00044 InterruptIn button(USER_BUTTON);
00045 
00046 
00047 static FILE *f;
00048 volatile int counter = 0;
00049 
00050 void saveTemperature() {
00051     float temperature = 0;
00052     temperature = BSP_TSENSOR_ReadTemp();
00053     fprintf(f, "%f\n", temperature);
00054     fflush(f);
00055     fflush(stdout);
00056 }
00057 
00058 void printTemperatures() {
00059     fflush(stdout);
00060     fflush(f);
00061     fseek(f, 0, SEEK_SET);
00062     float temperature;
00063     while(!feof(f)) {
00064         fscanf(f, "%f", &temperature);
00065         printf("Temperature: %f\n", temperature);
00066     }
00067     fflush(stdout);
00068 }
00069 
00070 
00071 void getTempQueueCall() {
00072     queue.call(printTemperatures);
00073 }
00074 void tickerBlock() {
00075     queue.call(saveTemperature);
00076 }
00077 
00078     // Entry point for the example
00079 int main() {
00080     t.start(callback(&queue, &EventQueue::dispatch_forever));
00081     BSP_TSENSOR_Init();
00082     
00083     button.rise(&getTempQueueCall);
00084     
00085     printf("--- Mbed OS filesystem example ---\n");
00086     
00087         // Try to mount the filesystem
00088     printf("Mounting the filesystem... ");
00089     fflush(stdout);
00090     int err = fs.mount(&bd);
00091     printf("%s\n", (err ? "Fail :(" : "OK"));
00092     if (err) {
00093             // Reformat if we can't mount the filesystem
00094             // this should only happen on the first boot
00095         printf("No filesystem found, formatting... ");
00096         fflush(stdout);
00097         err = fs.reformat(&bd);
00098         printf("%s\n", (err ? "Fail :(" : "OK"));
00099         if (err) {
00100             error("error: %s (%d)\n", strerror(-err), err);
00101         }
00102     }
00103     
00104         // Open the numbers file
00105     printf("Opening \"/fs/numbers.txt\"... ");
00106     fflush(stdout);
00107     f = fopen("/fs/numbers.txt", "r+");
00108     printf("%s\n", (!f ? "Fail :(" : "OK"));
00109     if (!f) {
00110             // Create the numbers file if it doesn't exist
00111         printf("No file found, creating a new file... ");
00112         fflush(stdout);
00113         f = fopen("/fs/numbers.txt", "w+");
00114         printf("%s\n", (!f ? "Fail :(" : "OK"));
00115         if (!f) {
00116             error("error: %s (%d)\n", strerror(errno), -errno);
00117         }
00118         
00119         printf("Seeking file... ");
00120         fflush(stdout);
00121         err = fseek(f, 0, SEEK_SET);
00122         printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00123         if (err < 0) {
00124             error("error: %s (%d)\n", strerror(errno), -errno);
00125         }
00126     }
00127     
00128     tTicker.attach(&tickerBlock, 60);
00129     
00130 }