assignment
Dependencies: BSP_B-L475E-IOT01
Diff: main.cpp
- Revision:
- 0:746ae63195a5
- Child:
- 1:14aa85a5009a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Dec 06 16:47:50 2018 +0000 @@ -0,0 +1,112 @@ + +#include "mbed.h" + +#include "stm32l475e_iot01_tsensor.h" + +#include <stdio.h> +#include <errno.h> + + // Block devices +#if COMPONENT_SPIF +#include "SPIFBlockDevice.h" +#endif + +#if COMPONENT_DATAFLASH +#include "DataFlashBlockDevice.h" +#endif + +#if COMPONENT_SD +#include "SDBlockDevice.h" +#endif + +#include "HeapBlockDevice.h" + + // File systems +#include "LittleFileSystem.h" +#include "FATFileSystem.h" + + // Physical block device, can be any device that supports the BlockDevice API +/*SPIFBlockDevice bd( + MBED_CONF_SPIF_DRIVER_SPI_MOSI, + MBED_CONF_SPIF_DRIVER_SPI_MISO, + MBED_CONF_SPIF_DRIVER_SPI_CLK, + MBED_CONF_SPIF_DRIVER_SPI_CS);*/ + + +#define BLOCK_SIZE 512 +HeapBlockDevice bd(16384, BLOCK_SIZE); + + // File system declaration +LittleFileSystem fs("fs"); + +Ticker tTicker; +Thread t; + +static FILE *f; +volatile int counter = 0; +EventQueue queue(32 * EVENTS_EVENT_SIZE); + +InterruptIn button(USER_BUTTON); + +void printTemperatures() { + fflush(stdout); + fflush(f); + fseek(f, 0, SEEK_SET); + float temperature; + while (!feof(f)) { + fscanf(f, "%f", &temperature); + printf("Temperature: %f\n", temperature); + } + fflush(f); +} + +void getTemperature() { + float temperature = BSP_TSENSOR_ReadTemp(); + fprintf(f, "%f\n", temperature); + fflush(f); + fflush(stdout); +} + +void tickerBlock() { + queue.call(getTemperature); +} + +int main(){ + t.start(callback(&queue, &EventQueue::dispatch_forever)); + BSP_TSENSOR_Init(); + button.rise(&printTemperatures); + // Try to mount the filesystem + printf("Mounting the filesystem... "); + fflush(stdout); + int err = fs.mount(&bd); + printf("%s\n", (err ? "Fail :(" : "OK")); + if (err) { + // Reformat if we can't mount the filesystem + // this should only happen on the first boot + printf("No filesystem found, formatting... "); + fflush(stdout); + err = fs.reformat(&bd); + printf("%s\n", (err ? "Fail :(" : "OK")); + if (err) { + error("error: %s (%d)\n", strerror(-err), err); + } + } + + // Open the numbers file + printf("Opening \"/fs/numbers.txt\"... "); + fflush(stdout); + f = fopen("/fs/numbers.txt", "r+"); + printf("%s\n", (!f ? "Fail :(" : "OK")); + if (!f) { + // Create the numbers file if it doesn't exist + printf("No file found, creating a new file... "); + fflush(stdout); + f = fopen("/fs/numbers.txt", "w+"); + printf("%s\n", (!f ? "Fail :(" : "OK")); + if (!f) { + error("error: %s (%d)\n", strerror(errno), -errno); + } + } + + tTicker.attach(&tickerBlock, 60); +}