Francesco Arrigo / 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 #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_tsensor.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 InterruptIn button(USER_BUTTON);
00033 Ticker start_read_temp;
00034 
00035 EventQueue queue(32 * EVENTS_EVENT_SIZE);
00036 Thread t;
00037 
00038 #define BLOCK_SIZE 512
00039 HeapBlockDevice bd(32768, BLOCK_SIZE);
00040 
00041 // File system declaration
00042 LittleFileSystem fs("fs");
00043 
00044 static FILE *f;
00045 volatile int counter = 0;
00046 
00047 void read_temp() {
00048     float sensor_value = 0;
00049     sensor_value = BSP_TSENSOR_ReadTemp();
00050     fprintf(f, "%f\n", sensor_value);
00051     fflush(f);
00052     fflush(stdout);
00053     printf("Reading Temp...\n");
00054 }
00055 
00056 void print_data_from_file() {
00057     fflush(stdout);
00058     fflush(f);
00059     fseek(f, 0, SEEK_SET);
00060     float value;
00061     while (!feof(f)) {
00062         fscanf(f, "%f", &value); 
00063         printf("%f\n", value);
00064     }
00065     fflush(stdout);
00066 }
00067 
00068 // needed since we can't have a file open in an interrupt
00069 void button_rising() {
00070     queue.call(print_data_from_file);
00071 }
00072 
00073 // needed since we can't have a file open in an interrupt
00074 void read_temp_call() {
00075     queue.call(read_temp);
00076 }
00077 
00078 // Entry point
00079 int main() {
00080     t.start(callback(&queue, &EventQueue::dispatch_forever));
00081     BSP_TSENSOR_Init();
00082     button.rise(&button_rising);
00083     
00084     printf("--- Mbed OS filesystem example ---\n");
00085 
00086     // Try to mount the filesystem
00087     printf("Mounting the filesystem... ");
00088     fflush(stdout);
00089     int err = fs.mount(&bd);
00090     printf("%s\n", (err ? "Fail :(" : "OK"));
00091     if (err) {
00092         // Reformat if we can't mount the filesystem
00093         // this should only happen on the first boot
00094         printf("No filesystem found, formatting... ");
00095         fflush(stdout);
00096         err = fs.reformat(&bd);
00097         printf("%s\n", (err ? "Fail :(" : "OK"));
00098         if (err) {
00099             error("error: %s (%d)\n", strerror(-err), err);
00100         }
00101     }
00102 
00103     // Open the numbers file
00104     printf("Opening \"/fs/numbers.txt\"... ");
00105     fflush(stdout);
00106     f = fopen("/fs/numbers.txt", "r +");
00107     printf("%s\n", (!f ? "Fail :(" : "OK"));
00108     
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     start_read_temp.attach(&read_temp_call, 60);
00129 
00130 }