Redona Kembora / 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 // File system declaration
00037 LittleFileSystem fs("fs");
00038 
00039 Ticker ticker;
00040 Thread t;
00041 static FILE *f;
00042 EventQueue queue(16 * EVENTS_EVENT_SIZE);
00043 InterruptIn button(USER_BUTTON);
00044 
00045 
00046 void display_temperature() {
00047     int count = 1;
00048     fflush(stdout);
00049     fflush(f);
00050     fseek(f, 0, SEEK_SET);
00051     float value;
00052     while (!feof(f)) {
00053         fscanf(f, "%f", &value); 
00054         printf("The %dst/nd temperature recorded was %f degress.\n", count, value);
00055         count++;
00056     }
00057     fflush(stdout);
00058 }
00059 
00060 
00061 void store_temperature() {
00062     float temperature = 0;
00063     temperature = BSP_TSENSOR_ReadTemp();
00064     fprintf(f, "%f\n", temperature);
00065     fflush(f);
00066     fflush(stdout);
00067     printf("A new temperature is being stored...\n");
00068 }
00069 
00070 void rise_call() {
00071     queue.call(display_temperature);
00072 }
00073 
00074 void ticker_call() {    
00075     queue.call(store_temperature);
00076 }
00077 
00078 int main(){
00079     t.start(callback(&queue, &EventQueue::dispatch_forever));
00080     BSP_TSENSOR_Init();
00081     button.rise(&rise_call);
00082     
00083     // Try to mount the filesystem
00084     printf("Mounting the filesystem... ");
00085     fflush(stdout);
00086     int err = fs.mount(&bd);
00087     printf("%s\n", (err ? "Fail :(" : "OK"));
00088     if (err) {
00089         // Reformat if we can't mount the filesystem
00090         // this should only happen on the first boot
00091         printf("No filesystem found, formatting... ");
00092         fflush(stdout);
00093         err = fs.reformat(&bd);
00094         printf("%s\n", (err ? "Fail :(" : "OK"));
00095         if (err) {
00096             error("error: %s (%d)\n", strerror(-err), err);
00097         }
00098     }
00099     
00100     // Open the numbers file
00101     printf("Opening \"/fs/numbers.txt\"... ");
00102     fflush(stdout);
00103     f = fopen("/fs/numbers.txt", "r+");
00104     printf("%s\n", (!f ? "Fail :(" : "OK"));
00105     if (!f) {
00106         // Create the numbers file if it doesn't exist
00107         printf("No file found, creating a new file... ");
00108         fflush(stdout);
00109         f = fopen("/fs/numbers.txt", "w+");
00110         printf("%s\n", (!f ? "Fail :(" : "OK"));
00111         if (!f) {
00112             error("error: %s (%d)\n", strerror(errno), -errno);
00113         }
00114     }
00115 
00116      ticker.attach(&ticker_call, 5);
00117 }