Alessio Buratti / Mbed OS AccelerationFile

Dependencies:   BSP_B-L475E-IOT01

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 
00004 #include "stm32l475e_iot01_accelero.h"
00005 
00006 #include <stdio.h>
00007 #include <errno.h>
00008 
00009     // Block devices
00010 #if COMPONENT_SPIF
00011 #include "SPIFBlockDevice.h"
00012 #endif
00013 
00014 #if COMPONENT_DATAFLASH
00015 #include "DataFlashBlockDevice.h"
00016 #endif
00017 
00018 #if COMPONENT_SD
00019 #include "SDBlockDevice.h"
00020 #endif
00021 
00022 #include "HeapBlockDevice.h"
00023 
00024     // File systems
00025 #include "LittleFileSystem.h"
00026 #include "FATFileSystem.h"
00027 
00028     // Physical block device, can be any device that supports the BlockDevice API
00029 /*SPIFBlockDevice bd(
00030  MBED_CONF_SPIF_DRIVER_SPI_MOSI,
00031  MBED_CONF_SPIF_DRIVER_SPI_MISO,
00032  MBED_CONF_SPIF_DRIVER_SPI_CLK,
00033  MBED_CONF_SPIF_DRIVER_SPI_CS);*/
00034 
00035 #define X_VALUE pDataXYZ[0]
00036 #define Y_VALUE pDataXYZ[1]
00037 #define Z_VALUE pDataXYZ[2]
00038 
00039 #define HORIZONTAL_INDEX 0
00040 #define LONG_VERTICAL_INDEX 1
00041 #define SHORT_VERTICAL_INDEX 2
00042 
00043 #define BLOCK_SIZE 512
00044 HeapBlockDevice bd(16384, BLOCK_SIZE);
00045 
00046     // File system declaration
00047 LittleFileSystem fs("fs");
00048 
00049 DigitalOut led1(LED1);
00050 DigitalOut led2(LED2);
00051 DigitalOut led3(LED3);
00052 
00053 Ticker tTicker;
00054 Thread t;
00055 
00056 static FILE *f;
00057 volatile int countedPositionsd = 0;
00058 EventQueue queue(32 * EVENTS_EVENT_SIZE);
00059 
00060 InterruptIn button(USER_BUTTON);
00061 
00062 int max3(int a, int b, int c) {
00063     if (a > b) {
00064         return a > c ? HORIZONTAL_INDEX : SHORT_VERTICAL_INDEX;
00065     }
00066     return b > c ? LONG_VERTICAL_INDEX : SHORT_VERTICAL_INDEX;
00067 }
00068 
00069 void turnOffLeds() {
00070     led1 = 0;
00071     led2 = 0;
00072     led3 = 0;
00073 }
00074 
00075 void toggleLed() {
00076     int16_t occurencies[3] = {0};
00077     fflush(stdout);
00078     fflush(f);
00079     
00080     fseek(f, 0, SEEK_SET);
00081     
00082     printf("Start reading\n");
00083     
00084     int number;
00085     while (!feof(f)) {
00086         fscanf(f, "%d", &number);
00087         if (number == HORIZONTAL_INDEX || number == LONG_VERTICAL_INDEX || number == SHORT_VERTICAL_INDEX) {
00088             occurencies[number] = occurencies[number] + 1;
00089         }
00090     }
00091     
00092     printf("%d %d %d\n", occurencies[HORIZONTAL_INDEX], occurencies[LONG_VERTICAL_INDEX], occurencies[SHORT_VERTICAL_INDEX]);
00093     
00094     int mostOccurentValue = max3(occurencies[0], occurencies[1], occurencies[2]);
00095     
00096     printf("Max %d\n", mostOccurentValue);
00097     
00098     turnOffLeds();
00099     if (mostOccurentValue == HORIZONTAL_INDEX) {
00100         led1 = 1;
00101     } else if (mostOccurentValue == LONG_VERTICAL_INDEX) {
00102         led2 = 1;
00103     } else if (mostOccurentValue == SHORT_VERTICAL_INDEX) {
00104         led3 = 1;
00105     }
00106     
00107     fflush(stdout);
00108     int err = fclose(f);
00109     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00110     if (err < 0) {
00111         error("error: %s (%d)\n", strerror(err), -err);
00112     }
00113     err = fs.unmount();
00114     printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
00115     if (err < 0) {
00116         error("error: %s (%d)\n", strerror(-err), err);
00117     }
00118 }
00119 
00120 bool isInRange100(int val) {
00121     return val < 100 && val > -100;
00122 }
00123 
00124 bool isInRange900_1000(int val) {
00125     return (val < 1050 && val > 950) || (val < -950 && val > -1050);
00126 }
00127 
00128 void savePosition() {
00129     int16_t pDataXYZ[3] = {0};
00130     BSP_ACCELERO_AccGetXYZ(pDataXYZ);
00131     if (isInRange100(X_VALUE) && isInRange100(Y_VALUE) && isInRange900_1000(Z_VALUE)) {
00132         fprintf(f, "%d\n", HORIZONTAL_INDEX);
00133     } else if (isInRange100(X_VALUE) && isInRange900_1000(Y_VALUE) && isInRange100(Z_VALUE)) {
00134         fprintf(f, "%d\n", LONG_VERTICAL_INDEX);
00135     } else if (isInRange900_1000(X_VALUE) && isInRange100(Y_VALUE) && isInRange100(Z_VALUE)) {
00136         fprintf(f, "%d\n", SHORT_VERTICAL_INDEX);
00137     }
00138     fflush(f);
00139     fflush(stdout);
00140 }
00141 
00142 
00143 void tickerBlock() {
00144     queue.call(savePosition);
00145     countedPositionsd++;
00146     if (countedPositionsd == 1000) {
00147         tTicker.detach();
00148         queue.call(toggleLed);
00149         countedPositionsd = 0;
00150     }
00151 }
00152 
00153 int main(){
00154     t.start(callback(&queue, &EventQueue::dispatch_forever));
00155     BSP_ACCELERO_Init();
00156     
00157         // Try to mount the filesystem
00158     printf("Mounting the filesystem... ");
00159     fflush(stdout);
00160     int err = fs.mount(&bd);
00161     printf("%s\n", (err ? "Fail :(" : "OK"));
00162     if (err) {
00163             // Reformat if we can't mount the filesystem
00164             // this should only happen on the first boot
00165         printf("No filesystem found, formatting... ");
00166         fflush(stdout);
00167         err = fs.reformat(&bd);
00168         printf("%s\n", (err ? "Fail :(" : "OK"));
00169         if (err) {
00170             error("error: %s (%d)\n", strerror(-err), err);
00171         }
00172     }
00173     
00174         // Open the numbers file
00175     printf("Opening \"/fs/numbers.txt\"... ");
00176     fflush(stdout);
00177     f = fopen("/fs/numbers.txt", "r+");
00178     printf("%s\n", (!f ? "Fail :(" : "OK"));
00179     if (!f) {
00180             // Create the numbers file if it doesn't exist
00181         printf("No file found, creating a new file... ");
00182         fflush(stdout);
00183         f = fopen("/fs/numbers.txt", "w+");
00184         printf("%s\n", (!f ? "Fail :(" : "OK"));
00185         if (!f) {
00186             error("error: %s (%d)\n", strerror(errno), -errno);
00187         }
00188     }
00189     
00190     tTicker.attach(&tickerBlock, 0.01);
00191 }