TemperatureButtonFile Assignment

Dependencies:   BSP_B-L475E-IOT01

Committer:
framtk
Date:
Thu Dec 06 22:09:12 2018 +0000
Revision:
28:707dc153dfad
Parent:
27:0cf170538518
done

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:8e251d9511b8 1 #include "mbed.h"
mbed_official 0:8e251d9511b8 2 #include <stdio.h>
mbed_official 0:8e251d9511b8 3 #include <errno.h>
mbed_official 0:8e251d9511b8 4
mbed_official 0:8e251d9511b8 5 // Block devices
mbed_official 25:65a9183a2178 6 #if COMPONENT_SPIF
mbed_official 0:8e251d9511b8 7 #include "SPIFBlockDevice.h"
mbed_official 25:65a9183a2178 8 #endif
mbed_official 25:65a9183a2178 9
mbed_official 25:65a9183a2178 10 #if COMPONENT_DATAFLASH
mbed_official 1:2bfc377bcc2a 11 #include "DataFlashBlockDevice.h"
mbed_official 25:65a9183a2178 12 #endif
mbed_official 25:65a9183a2178 13
mbed_official 25:65a9183a2178 14 #if COMPONENT_SD
mbed_official 0:8e251d9511b8 15 #include "SDBlockDevice.h"
mbed_official 25:65a9183a2178 16 #endif
mbed_official 25:65a9183a2178 17
mbed_official 0:8e251d9511b8 18 #include "HeapBlockDevice.h"
mbed_official 0:8e251d9511b8 19
mbed_official 0:8e251d9511b8 20 // File systems
mbed_official 0:8e251d9511b8 21 #include "LittleFileSystem.h"
mbed_official 0:8e251d9511b8 22 #include "FATFileSystem.h"
framtk 28:707dc153dfad 23 #include "stm32l475e_iot01_tsensor.h"
mbed_official 0:8e251d9511b8 24
mbed_official 0:8e251d9511b8 25 // Physical block device, can be any device that supports the BlockDevice API
lmottola 26:130bb1173866 26 /*SPIFBlockDevice bd(
mbed_official 0:8e251d9511b8 27 MBED_CONF_SPIF_DRIVER_SPI_MOSI,
mbed_official 0:8e251d9511b8 28 MBED_CONF_SPIF_DRIVER_SPI_MISO,
mbed_official 0:8e251d9511b8 29 MBED_CONF_SPIF_DRIVER_SPI_CLK,
lmottola 26:130bb1173866 30 MBED_CONF_SPIF_DRIVER_SPI_CS);*/
framtk 28:707dc153dfad 31
framtk 28:707dc153dfad 32 InterruptIn button(USER_BUTTON);
framtk 28:707dc153dfad 33 Ticker start_read_temp;
framtk 27:0cf170538518 34
framtk 27:0cf170538518 35 EventQueue queue(32 * EVENTS_EVENT_SIZE);
framtk 27:0cf170538518 36 Thread t;
framtk 27:0cf170538518 37
lmottola 26:130bb1173866 38 #define BLOCK_SIZE 512
framtk 27:0cf170538518 39 HeapBlockDevice bd(32768, BLOCK_SIZE);
framtk 27:0cf170538518 40
mbed_official 0:8e251d9511b8 41 // File system declaration
mbed_official 0:8e251d9511b8 42 LittleFileSystem fs("fs");
mbed_official 0:8e251d9511b8 43
framtk 27:0cf170538518 44 static FILE *f;
framtk 27:0cf170538518 45 volatile int counter = 0;
framtk 27:0cf170538518 46
framtk 28:707dc153dfad 47 void read_temp() {
framtk 28:707dc153dfad 48 float sensor_value = 0;
framtk 28:707dc153dfad 49 sensor_value = BSP_TSENSOR_ReadTemp();
framtk 28:707dc153dfad 50 fprintf(f, "%f\n", sensor_value);
framtk 27:0cf170538518 51 fflush(f);
framtk 27:0cf170538518 52 fflush(stdout);
framtk 28:707dc153dfad 53 printf("Reading Temp...\n");
framtk 28:707dc153dfad 54 }
framtk 28:707dc153dfad 55
framtk 28:707dc153dfad 56 void print_data_from_file() {
framtk 28:707dc153dfad 57 fflush(stdout);
framtk 28:707dc153dfad 58 fflush(f);
framtk 28:707dc153dfad 59 fseek(f, 0, SEEK_SET);
framtk 28:707dc153dfad 60 float value;
framtk 28:707dc153dfad 61 while (!feof(f)) {
framtk 28:707dc153dfad 62 fscanf(f, "%f", &value);
framtk 28:707dc153dfad 63 printf("%f\n", value);
framtk 28:707dc153dfad 64 }
framtk 28:707dc153dfad 65 fflush(stdout);
framtk 27:0cf170538518 66 }
framtk 27:0cf170538518 67
framtk 28:707dc153dfad 68 // needed since we can't have a file open in an interrupt
framtk 28:707dc153dfad 69 void button_rising() {
framtk 28:707dc153dfad 70 queue.call(print_data_from_file);
framtk 27:0cf170538518 71 }
framtk 27:0cf170538518 72
framtk 28:707dc153dfad 73 // needed since we can't have a file open in an interrupt
framtk 28:707dc153dfad 74 void read_temp_call() {
framtk 28:707dc153dfad 75 queue.call(read_temp);
framtk 27:0cf170538518 76 }
mbed_official 0:8e251d9511b8 77
framtk 28:707dc153dfad 78 // Entry point
mbed_official 0:8e251d9511b8 79 int main() {
framtk 27:0cf170538518 80 t.start(callback(&queue, &EventQueue::dispatch_forever));
framtk 28:707dc153dfad 81 BSP_TSENSOR_Init();
framtk 28:707dc153dfad 82 button.rise(&button_rising);
framtk 27:0cf170538518 83
mbed_official 0:8e251d9511b8 84 printf("--- Mbed OS filesystem example ---\n");
mbed_official 0:8e251d9511b8 85
mbed_official 0:8e251d9511b8 86 // Try to mount the filesystem
mbed_official 0:8e251d9511b8 87 printf("Mounting the filesystem... ");
mbed_official 0:8e251d9511b8 88 fflush(stdout);
mbed_official 0:8e251d9511b8 89 int err = fs.mount(&bd);
mbed_official 0:8e251d9511b8 90 printf("%s\n", (err ? "Fail :(" : "OK"));
mbed_official 0:8e251d9511b8 91 if (err) {
mbed_official 0:8e251d9511b8 92 // Reformat if we can't mount the filesystem
mbed_official 0:8e251d9511b8 93 // this should only happen on the first boot
mbed_official 0:8e251d9511b8 94 printf("No filesystem found, formatting... ");
mbed_official 0:8e251d9511b8 95 fflush(stdout);
mbed_official 0:8e251d9511b8 96 err = fs.reformat(&bd);
mbed_official 0:8e251d9511b8 97 printf("%s\n", (err ? "Fail :(" : "OK"));
mbed_official 0:8e251d9511b8 98 if (err) {
mbed_official 0:8e251d9511b8 99 error("error: %s (%d)\n", strerror(-err), err);
mbed_official 0:8e251d9511b8 100 }
mbed_official 0:8e251d9511b8 101 }
mbed_official 0:8e251d9511b8 102
mbed_official 0:8e251d9511b8 103 // Open the numbers file
mbed_official 0:8e251d9511b8 104 printf("Opening \"/fs/numbers.txt\"... ");
mbed_official 0:8e251d9511b8 105 fflush(stdout);
framtk 27:0cf170538518 106 f = fopen("/fs/numbers.txt", "r +");
mbed_official 0:8e251d9511b8 107 printf("%s\n", (!f ? "Fail :(" : "OK"));
framtk 28:707dc153dfad 108
mbed_official 0:8e251d9511b8 109 if (!f) {
mbed_official 0:8e251d9511b8 110 // Create the numbers file if it doesn't exist
mbed_official 0:8e251d9511b8 111 printf("No file found, creating a new file... ");
mbed_official 0:8e251d9511b8 112 fflush(stdout);
mbed_official 0:8e251d9511b8 113 f = fopen("/fs/numbers.txt", "w+");
mbed_official 0:8e251d9511b8 114 printf("%s\n", (!f ? "Fail :(" : "OK"));
mbed_official 0:8e251d9511b8 115 if (!f) {
mbed_official 0:8e251d9511b8 116 error("error: %s (%d)\n", strerror(errno), -errno);
mbed_official 0:8e251d9511b8 117 }
mbed_official 0:8e251d9511b8 118
mbed_official 0:8e251d9511b8 119 printf("Seeking file... ");
mbed_official 0:8e251d9511b8 120 fflush(stdout);
mbed_official 0:8e251d9511b8 121 err = fseek(f, 0, SEEK_SET);
mbed_official 0:8e251d9511b8 122 printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
mbed_official 0:8e251d9511b8 123 if (err < 0) {
mbed_official 0:8e251d9511b8 124 error("error: %s (%d)\n", strerror(errno), -errno);
mbed_official 0:8e251d9511b8 125 }
mbed_official 0:8e251d9511b8 126 }
mbed_official 0:8e251d9511b8 127
framtk 28:707dc153dfad 128 start_read_temp.attach(&read_temp_call, 60);
framtk 28:707dc153dfad 129
framtk 27:0cf170538518 130 }