Luigi Frunzio / Mbed OS TemperatureButtonFile

Dependencies:   IOTAtelier1819-FileSystem 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 
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 #include "stm32l475e_iot01_tsensor.h"
00025 
00026 // Physical block device, can be any device that supports the BlockDevice API
00027 /*SPIFBlockDevice bd(
00028         MBED_CONF_SPIF_DRIVER_SPI_MOSI,
00029         MBED_CONF_SPIF_DRIVER_SPI_MISO,
00030         MBED_CONF_SPIF_DRIVER_SPI_CLK,
00031         MBED_CONF_SPIF_DRIVER_SPI_CS);*/
00032         
00033 InterruptIn button(USER_BUTTON);
00034 Ticker toggle_led_ticker;
00035 
00036 EventQueue queue(32 * EVENTS_EVENT_SIZE);
00037 Thread t;
00038 
00039 #define BLOCK_SIZE 512
00040 HeapBlockDevice bd(16384, BLOCK_SIZE);
00041 
00042 // File system declaration
00043 LittleFileSystem fs("fs");
00044 
00045 static FILE *f;
00046 volatile int counter = 0;
00047 
00048 void get_data_from_sensors() {
00049     float sensor_value=0;
00050     sensor_value = BSP_TSENSOR_ReadTemp();
00051     fprintf(f, "%f\n", sensor_value);
00052     fflush(f);
00053     fflush(stdout);
00054     printf("Saving temperature...\n");
00055 }
00056 
00057 void print_data_from_file() {
00058     fflush(stdout);
00059     fflush(f);
00060     fseek(f, 0, SEEK_SET);
00061     float value;
00062     while (!feof(f)) {
00063         fscanf(f, "%f", &value); 
00064         printf("%f\n", value);
00065     }
00066     fflush(stdout);
00067 }
00068 
00069 void toggle() {
00070     queue.call(print_data_from_file);
00071 }
00072 
00073 void toggle_led() {
00074     queue.call(get_data_from_sensors);
00075 }
00076 
00077 // Entry point for the example
00078 int main() {
00079     t.start(callback(&queue, &EventQueue::dispatch_forever));
00080     BSP_TSENSOR_Init();
00081     button.rise(&toggle);
00082     
00083     printf("--- Mbed OS filesystem example ---\n");
00084 
00085     // Try to mount the filesystem
00086     printf("Mounting the filesystem... ");
00087     fflush(stdout);
00088     int err = fs.mount(&bd);
00089     printf("%s\n", (err ? "Fail :(" : "OK"));
00090     if (err) {
00091         // Reformat if we can't mount the filesystem
00092         // this should only happen on the first boot
00093         printf("No filesystem found, formatting... ");
00094         fflush(stdout);
00095         err = fs.reformat(&bd);
00096         printf("%s\n", (err ? "Fail :(" : "OK"));
00097         if (err) {
00098             error("error: %s (%d)\n", strerror(-err), err);
00099         }
00100     }
00101 
00102     // Open the numbers file
00103     printf("Opening \"/fs/numbers.txt\"... ");
00104     fflush(stdout);
00105     f = fopen("/fs/numbers.txt", "r +");
00106     printf("%s\n", (!f ? "Fail :(" : "OK"));
00107     if (!f) {
00108         // Create the numbers file if it doesn't exist
00109         printf("No file found, creating a new file... ");
00110         fflush(stdout);
00111         f = fopen("/fs/numbers.txt", "w+");
00112         printf("%s\n", (!f ? "Fail :(" : "OK"));
00113         if (!f) {
00114             error("error: %s (%d)\n", strerror(errno), -errno);
00115         }
00116 
00117         printf("Seeking file... ");
00118         fflush(stdout);
00119         err = fseek(f, 0, SEEK_SET);
00120         printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00121         if (err < 0) {
00122             error("error: %s (%d)\n", strerror(errno), -errno);
00123         }
00124     }
00125     
00126     // Go through and record the acc
00127     toggle_led_ticker.attach(&toggle_led, 60);
00128 
00129 }
00130