TemperatureButtonFile Assignment
Dependencies: BSP_B-L475E-IOT01
Revision 28:707dc153dfad, committed 2018-12-06
- Comitter:
- framtk
- Date:
- Thu Dec 06 22:09:12 2018 +0000
- Parent:
- 27:0cf170538518
- Commit message:
- done
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Dec 06 20:28:08 2018 +0000 +++ b/main.cpp Thu Dec 06 22:09:12 2018 +0000 @@ -20,7 +20,7 @@ // File systems #include "LittleFileSystem.h" #include "FATFileSystem.h" -#include "stm32l475e_iot01_accelero.h" +#include "stm32l475e_iot01_tsensor.h" // Physical block device, can be any device that supports the BlockDevice API /*SPIFBlockDevice bd( @@ -28,11 +28,9 @@ MBED_CONF_SPIF_DRIVER_SPI_MISO, MBED_CONF_SPIF_DRIVER_SPI_CLK, MBED_CONF_SPIF_DRIVER_SPI_CS);*/ - -DigitalOut led_1(LED1); -DigitalOut led_2(LED2); -DigitalOut led_3(LED3); -Ticker check_led_status; + +InterruptIn button(USER_BUTTON); +Ticker start_read_temp; EventQueue queue(32 * EVENTS_EVENT_SIZE); Thread t; @@ -40,150 +38,51 @@ #define BLOCK_SIZE 512 HeapBlockDevice bd(32768, BLOCK_SIZE); -#define LED_1_VALUE 1 -#define LED_2_VALUE 2 -#define LED_3_VALUE 3 - // File system declaration LittleFileSystem fs("fs"); -// Set up the button to trigger an erase -InterruptIn irq(BUTTON1); -void erase() { - printf("Initializing the block device... "); - fflush(stdout); - int err = bd.init(); - printf("%s\n", (err ? "Fail :(" : "OK")); - if (err) { - error("error: %s (%d)\n", strerror(-err), err); - } - - printf("Erasing the block device... "); - fflush(stdout); - err = bd.erase(0, bd.size()); - printf("%s\n", (err ? "Fail :(" : "OK")); - if (err) { - error("error: %s (%d)\n", strerror(-err), err); - } - - printf("Deinitializing the block device... "); - fflush(stdout); - err = bd.deinit(); - printf("%s\n", (err ? "Fail :(" : "OK")); - if (err) { - error("error: %s (%d)\n", strerror(-err), err); - } -} - static FILE *f; volatile int counter = 0; -void read_acc() { - int16_t pDataXYZ[3] = {0}; - BSP_ACCELERO_AccGetXYZ(pDataXYZ); - if (abs(pDataXYZ[2]) > 900) { // board laying down - fprintf(f, "%d\n", LED_1_VALUE); - } else if (abs(pDataXYZ[1]) > 900) { // long side - fprintf(f, "%d\n", LED_2_VALUE); - } else if (abs(pDataXYZ[0]) > 900) { // short side - fprintf(f, "%d\n", LED_3_VALUE); - } else { // all other positions - fprintf(f, "%d\n", -1); - } - +void read_temp() { + float sensor_value = 0; + sensor_value = BSP_TSENSOR_ReadTemp(); + fprintf(f, "%f\n", sensor_value); fflush(f); fflush(stdout); + printf("Reading Temp...\n"); +} + +void print_data_from_file() { + fflush(stdout); + fflush(f); + fseek(f, 0, SEEK_SET); + float value; + while (!feof(f)) { + fscanf(f, "%f", &value); + printf("%f\n", value); + } + fflush(stdout); } -void read_data_from_file() { - int led1_counter = 0; - int led2_counter = 0; - int led3_counter = 0; - fflush(stdout); - fflush(f); - - // read file and count occurrences of position numbers - fseek(f, 0, SEEK_SET); - int number; - while (!feof(f)) { - fscanf(f, "%d", &number); - - switch (number){ - case LED_1_VALUE: - led1_counter += 1; - break; - case LED_2_VALUE: - led2_counter += 1; - break; - case LED_3_VALUE: - led3_counter += 1; - break; - default: - break; - } - } - - // check which position is the one present the most - if (led1_counter > led2_counter && led1_counter > led3_counter) { - led_1 = 1; - led_2 = 0; - led_3 = 0; - } else if (led2_counter > led1_counter && led2_counter > led3_counter ) { - led_1 = 0; - led_2 = 1; - led_3 = 0; - } else if (led3_counter > led1_counter && led3_counter > led2_counter) { - led_1 = 0; - led_2 = 0; - led_3 = 1; - } - - fflush(stdout); - int err = fclose(f); - printf("%s\n", (err < 0 ? "Fail :(" : "OK")); - if (err < 0) { - error("error: %s (%d)\n", strerror(err), -err); - } - err = fs.unmount(); - printf("%s\n", (err < 0 ? "Fail :(" : "OK")); - if (err < 0) { - error("error: %s (%d)\n", strerror(-err), err); - } - - if (led_1){ - printf("Lighting up corresponding LED 1!\n"); - } else if (led_2){ - printf("Lighting up corresponding LED 2!\n"); - } else if (led_3){ - printf("Lighting up corresponding LED 3!\n"); - } else { - printf("No valid position detected during the sampling time, no LED lighting up!\n"); - } - +// needed since we can't have a file open in an interrupt +void button_rising() { + queue.call(print_data_from_file); } -void toggle_led() { - queue.call(read_acc); - counter++; - if (counter == 100*10) { - check_led_status.detach(); - queue.call(read_data_from_file); - } - +// needed since we can't have a file open in an interrupt +void read_temp_call() { + queue.call(read_temp); } -// Entry point for the example +// Entry point int main() { t.start(callback(&queue, &EventQueue::dispatch_forever)); - BSP_ACCELERO_Init(); - + BSP_TSENSOR_Init(); + button.rise(&button_rising); printf("--- Mbed OS filesystem example ---\n"); - // Setup the erase event on button press, use the event queue - // to avoid running in interrupt context - irq.fall(mbed_event_queue()->event(erase)); - // Try to mount the filesystem printf("Mounting the filesystem... "); fflush(stdout); @@ -206,6 +105,7 @@ 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... "); @@ -225,5 +125,6 @@ } } - check_led_status.attach(&toggle_led, 0.01); + start_read_temp.attach(&read_temp_call, 60); + } \ No newline at end of file