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