a
Dependencies: BSP_B-L475E-IOT01
main.cpp@1:57a53e16f6e6, 2018-12-06 (annotated)
- Committer:
- iandil
- Date:
- Thu Dec 06 16:30:27 2018 +0000
- Revision:
- 1:57a53e16f6e6
- Parent:
- 0:c105215e1c51
- Child:
- 2:6bc8f96eb54f
fixed time interval;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
iandil | 0:c105215e1c51 | 1 | |
iandil | 0:c105215e1c51 | 2 | #include "mbed.h" |
iandil | 0:c105215e1c51 | 3 | #include <stdio.h> |
iandil | 0:c105215e1c51 | 4 | #include <errno.h> |
iandil | 0:c105215e1c51 | 5 | #include "stm32l475e_iot01_tsensor.h" |
iandil | 0:c105215e1c51 | 6 | #include "nvstore.h" |
iandil | 0:c105215e1c51 | 7 | |
iandil | 0:c105215e1c51 | 8 | // Block devices |
iandil | 0:c105215e1c51 | 9 | #if COMPONENT_SPIF |
iandil | 0:c105215e1c51 | 10 | #include "SPIFBlockDevice.h" |
iandil | 0:c105215e1c51 | 11 | #endif |
iandil | 0:c105215e1c51 | 12 | |
iandil | 0:c105215e1c51 | 13 | #if COMPONENT_DATAFLASH |
iandil | 0:c105215e1c51 | 14 | #include "DataFlashBlockDevice.h" |
iandil | 0:c105215e1c51 | 15 | #endif |
iandil | 0:c105215e1c51 | 16 | |
iandil | 0:c105215e1c51 | 17 | #if COMPONENT_SD |
iandil | 0:c105215e1c51 | 18 | #include "SDBlockDevice.h" |
iandil | 0:c105215e1c51 | 19 | #endif |
iandil | 0:c105215e1c51 | 20 | |
iandil | 0:c105215e1c51 | 21 | #include "HeapBlockDevice.h" |
iandil | 0:c105215e1c51 | 22 | // File systems |
iandil | 0:c105215e1c51 | 23 | #include "LittleFileSystem.h" |
iandil | 0:c105215e1c51 | 24 | #include "FATFileSystem.h" |
iandil | 0:c105215e1c51 | 25 | |
iandil | 0:c105215e1c51 | 26 | EventQueue queue(32 * EVENTS_EVENT_SIZE); |
iandil | 0:c105215e1c51 | 27 | Thread thread; |
iandil | 0:c105215e1c51 | 28 | Ticker ticker; |
iandil | 0:c105215e1c51 | 29 | |
iandil | 0:c105215e1c51 | 30 | |
iandil | 0:c105215e1c51 | 31 | // Physical block device, can be any device that supports the BlockDevice API |
iandil | 0:c105215e1c51 | 32 | /*SPIFBlockDevice bd( |
iandil | 0:c105215e1c51 | 33 | MBED_CONF_SPIF_DRIVER_SPI_MOSI, |
iandil | 0:c105215e1c51 | 34 | MBED_CONF_SPIF_DRIVER_SPI_MISO, |
iandil | 0:c105215e1c51 | 35 | MBED_CONF_SPIF_DRIVER_SPI_CLK, |
iandil | 0:c105215e1c51 | 36 | MBED_CONF_SPIF_DRIVER_SPI_CS);*/ |
iandil | 0:c105215e1c51 | 37 | |
iandil | 0:c105215e1c51 | 38 | #define BLOCK_SIZE 512 |
iandil | 0:c105215e1c51 | 39 | HeapBlockDevice bd(16384, BLOCK_SIZE); |
iandil | 0:c105215e1c51 | 40 | |
iandil | 0:c105215e1c51 | 41 | // File system declaration |
iandil | 0:c105215e1c51 | 42 | LittleFileSystem fs("fs"); |
iandil | 0:c105215e1c51 | 43 | |
iandil | 0:c105215e1c51 | 44 | InterruptIn button(USER_BUTTON); |
iandil | 0:c105215e1c51 | 45 | |
iandil | 0:c105215e1c51 | 46 | |
iandil | 0:c105215e1c51 | 47 | static FILE *file; |
iandil | 0:c105215e1c51 | 48 | volatile int counter = 0; |
iandil | 0:c105215e1c51 | 49 | |
iandil | 0:c105215e1c51 | 50 | void readSensors() { |
iandil | 0:c105215e1c51 | 51 | float temperature = 0; |
iandil | 0:c105215e1c51 | 52 | temperature = BSP_TSENSOR_ReadTemp(); |
iandil | 0:c105215e1c51 | 53 | fprintf(file, "%f\n", temperature); |
iandil | 0:c105215e1c51 | 54 | fflush(file); |
iandil | 0:c105215e1c51 | 55 | fflush(stdout); |
iandil | 0:c105215e1c51 | 56 | printf("Temperature Saved\n"); |
iandil | 0:c105215e1c51 | 57 | } |
iandil | 0:c105215e1c51 | 58 | |
iandil | 0:c105215e1c51 | 59 | void readAndPrintFile() { |
iandil | 0:c105215e1c51 | 60 | fflush(stdout); |
iandil | 0:c105215e1c51 | 61 | fflush(file); |
iandil | 0:c105215e1c51 | 62 | fseek(file, 0, SEEK_SET); |
iandil | 0:c105215e1c51 | 63 | float temperature; |
iandil | 0:c105215e1c51 | 64 | while(!feof(file)) { |
iandil | 0:c105215e1c51 | 65 | fscanf(file, "%f", &temperature); |
iandil | 0:c105215e1c51 | 66 | printf("Temperature: %f\n", temperature); |
iandil | 0:c105215e1c51 | 67 | } |
iandil | 0:c105215e1c51 | 68 | fflush(stdout); |
iandil | 0:c105215e1c51 | 69 | } |
iandil | 0:c105215e1c51 | 70 | |
iandil | 0:c105215e1c51 | 71 | |
iandil | 0:c105215e1c51 | 72 | void printData() { |
iandil | 0:c105215e1c51 | 73 | queue.call(readAndPrintFile); |
iandil | 0:c105215e1c51 | 74 | } |
iandil | 0:c105215e1c51 | 75 | void readData() { |
iandil | 0:c105215e1c51 | 76 | queue.call(readSensors); |
iandil | 0:c105215e1c51 | 77 | } |
iandil | 0:c105215e1c51 | 78 | |
iandil | 0:c105215e1c51 | 79 | |
iandil | 0:c105215e1c51 | 80 | // Entry point for the example |
iandil | 0:c105215e1c51 | 81 | int main() { |
iandil | 0:c105215e1c51 | 82 | thread.start(callback(&queue, &EventQueue::dispatch_forever)); |
iandil | 0:c105215e1c51 | 83 | BSP_TSENSOR_Init(); |
iandil | 0:c105215e1c51 | 84 | button.rise(&printData); |
iandil | 0:c105215e1c51 | 85 | |
iandil | 0:c105215e1c51 | 86 | printf("--- Mbed OS filesystem example ---\n"); |
iandil | 0:c105215e1c51 | 87 | |
iandil | 0:c105215e1c51 | 88 | // Try to mount the filesystem |
iandil | 0:c105215e1c51 | 89 | printf("Mounting the filesystem... "); |
iandil | 0:c105215e1c51 | 90 | fflush(stdout); |
iandil | 0:c105215e1c51 | 91 | int err = fs.mount(&bd); |
iandil | 0:c105215e1c51 | 92 | printf("%s\n", (err ? "Fail :(" : "OK")); |
iandil | 0:c105215e1c51 | 93 | if (err) { |
iandil | 0:c105215e1c51 | 94 | // Reformat if we can't mount the filesystem |
iandil | 0:c105215e1c51 | 95 | // this should only happen on the first boot |
iandil | 0:c105215e1c51 | 96 | printf("No filesystem found, formatting... "); |
iandil | 0:c105215e1c51 | 97 | fflush(stdout); |
iandil | 0:c105215e1c51 | 98 | err = fs.reformat(&bd); |
iandil | 0:c105215e1c51 | 99 | printf("%s\n", (err ? "Fail :(" : "OK")); |
iandil | 0:c105215e1c51 | 100 | if (err) { |
iandil | 0:c105215e1c51 | 101 | error("error: %s (%d)\n", strerror(-err), err); |
iandil | 0:c105215e1c51 | 102 | } |
iandil | 0:c105215e1c51 | 103 | } |
iandil | 0:c105215e1c51 | 104 | |
iandil | 0:c105215e1c51 | 105 | // Open the numbers file |
iandil | 0:c105215e1c51 | 106 | printf("Opening \"/fs/numbers.txt\"... "); |
iandil | 0:c105215e1c51 | 107 | fflush(stdout); |
iandil | 0:c105215e1c51 | 108 | file = fopen("/fs/numbers.txt", "r+"); |
iandil | 0:c105215e1c51 | 109 | printf("%s\n", (!file ? "Fail :(" : "OK")); |
iandil | 0:c105215e1c51 | 110 | if (!file) { |
iandil | 0:c105215e1c51 | 111 | // Create the numbers file if it doesn't exist |
iandil | 0:c105215e1c51 | 112 | printf("No file found, creating a new file... "); |
iandil | 0:c105215e1c51 | 113 | fflush(stdout); |
iandil | 0:c105215e1c51 | 114 | file = fopen("/fs/numbers.txt", "w+"); |
iandil | 0:c105215e1c51 | 115 | printf("%s\n", (!file ? "Fail :(" : "OK")); |
iandil | 0:c105215e1c51 | 116 | if (!file) { |
iandil | 0:c105215e1c51 | 117 | error("error: %s (%d)\n", strerror(errno), -errno); |
iandil | 0:c105215e1c51 | 118 | } |
iandil | 0:c105215e1c51 | 119 | |
iandil | 0:c105215e1c51 | 120 | printf("Seeking file... "); |
iandil | 0:c105215e1c51 | 121 | fflush(stdout); |
iandil | 0:c105215e1c51 | 122 | err = fseek(file, 0, SEEK_SET); |
iandil | 0:c105215e1c51 | 123 | printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
iandil | 0:c105215e1c51 | 124 | if (err < 0) { |
iandil | 0:c105215e1c51 | 125 | error("error: %s (%d)\n", strerror(errno), -errno); |
iandil | 0:c105215e1c51 | 126 | } |
iandil | 0:c105215e1c51 | 127 | } |
iandil | 0:c105215e1c51 | 128 | |
iandil | 0:c105215e1c51 | 129 | |
iandil | 0:c105215e1c51 | 130 | |
iandil | 1:57a53e16f6e6 | 131 | ticker.attach(&readData, 60); |
iandil | 0:c105215e1c51 | 132 | |
iandil | 0:c105215e1c51 | 133 | |
iandil | 0:c105215e1c51 | 134 | } |