Lucas Pennati / 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 thread;
00028 Ticker ticker;
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 *file;
00048 volatile int counter = 0;
00049 
00050 void readSensors() {
00051     float temperature = 0;
00052     temperature = BSP_TSENSOR_ReadTemp();
00053     fprintf(file, "%f\n", temperature);
00054     fflush(file);
00055     fflush(stdout);    
00056     printf("Temperature Saved\n");
00057 }
00058 
00059 void readAndPrintFile() {
00060     fflush(stdout);
00061     fflush(file);
00062     fseek(file, 0, SEEK_SET);
00063     float temperature;
00064     while(!feof(file)) {
00065         fscanf(file, "%f", &temperature);
00066         printf("Temperature: %f\n", temperature);
00067     }
00068     fflush(stdout);
00069 }
00070 
00071 
00072 void printData() {
00073     queue.call(readAndPrintFile);    
00074 }
00075 void readData() {
00076     queue.call(readSensors);
00077 }
00078 
00079 
00080 // Entry point for the example
00081 int main() {
00082     thread.start(callback(&queue, &EventQueue::dispatch_forever));
00083     BSP_TSENSOR_Init();
00084     button.rise(&printData);
00085     
00086     printf("--- Mbed OS filesystem example ---\n");
00087 
00088     // Mount the filesystem
00089     printf("Mounting the filesystem\n");
00090     fflush(stdout);
00091     int err = fs.mount(&bd);
00092     printf("%s\n", (err ? "Fail :(" : "OK"));
00093     if (err) {
00094         // Reformat if we can't mount the filesystem
00095         // this should only happen on the first boot
00096         printf("ERROR -> Formatting due to no filesystem exists");
00097         fflush(stdout);
00098         err = fs.reformat(&bd);
00099         printf("%s\n", (err ? "Fail :(" : "OK"));
00100         if (err) {
00101             error("ERROR -> %s (%d)\n", strerror(-err), err);
00102         }
00103     }
00104 
00105     // Open the numbers file
00106     printf("Opening \"/fs/numbers.txt\"\n");
00107     fflush(stdout);
00108     file = fopen("/fs/numbers.txt", "r+");
00109     printf("%s\n", (!file ? "Fail :(" : "OK"));
00110     if (!file) {
00111         // Create the numbers file if it doesn't exist
00112         printf("Creating a new file\n");
00113         fflush(stdout);
00114         file = fopen("/fs/numbers.txt", "w+");
00115         printf("%s\n", (!file ? "Fail :(" : "OK"));
00116         if (!file) {
00117             error("ERROR -> %s (%d)\n", strerror(errno), -errno);
00118         }
00119         
00120         printf("Seeking file\n");
00121         fflush(stdout);
00122         err = fseek(file, 0, SEEK_SET);
00123         printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00124         if (err < 0) {
00125             error("ERROR -> %s (%d)\n", strerror(errno), -errno);
00126         }
00127     }
00128     
00129 
00130     ticker.attach(&readData, 60);
00131 }