Andrea Vicari / 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 #include "mbed.h"
00002 
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 // 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 #define BLOCK_SIZE 512
00033 HeapBlockDevice bd(16384, BLOCK_SIZE);
00034 #include "stm32l475e_iot01_tsensor.h"
00035 
00036 
00037 // File system declaration
00038 LittleFileSystem fs("fs");
00039 
00040 Ticker timeout_ticker;
00041 Thread t;
00042 
00043 static FILE *f;
00044 volatile int seconds_passed = 0;
00045 EventQueue queue(32 * EVENTS_EVENT_SIZE);
00046 
00047 void write_temperature_on_file() {
00048     float temperature = 0;
00049     temperature = BSP_TSENSOR_ReadTemp();
00050     fprintf(f, "%f\n", temperature);
00051     fflush(f);
00052     fflush(stdout);
00053     printf("New temperature stored in the file\n");
00054 }
00055 
00056 void print_temperature_from_file() {
00057     printf("\n====== PRINTING TEMPERATURE RECORDED SO FAR ======\n");
00058     int order = 1;
00059     fflush(stdout);
00060     fflush(f);
00061     fseek(f, 0, SEEK_SET);
00062     float value;
00063     while (!feof(f)) {
00064         fscanf(f, "%f", &value); 
00065         printf("  %d: %f\n", order, value);
00066         order++;
00067     }
00068     fflush(stdout);
00069     printf("\n");
00070 }
00071 
00072 InterruptIn button(USER_BUTTON);
00073 
00074 void toggle() {
00075     queue.call(print_temperature_from_file);
00076 }
00077 
00078 void ticker_attach() {    
00079     queue.call(write_temperature_on_file);
00080 }
00081 
00082 int main(){
00083     t.start(callback(&queue, &EventQueue::dispatch_forever));
00084     BSP_TSENSOR_Init();
00085     button.rise(&toggle);
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 
00120     timeout_ticker.attach(&ticker_attach, 60);
00121 }