Nelson Santos / Mbed 2 deprecated Coursework_copy

Dependencies:   X_NUCLEO_IKS01A1 mbed-rtos mbed

Fork of HelloWorld_IKS01A1 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  ANG Group (Nelson Santos; Irina Baptista; Pierluigi Urru)
00005  * @version V0.0.1
00006  * @date    05-May-2016
00007  * @brief   Simple Example application for using the X_NUCLEO_IKS01A1
00008  *          MEMS Inertial & Environmental Sensor Nucleo expansion board.
00009  ******************************************************************************
00010 */
00011 
00012 #include "mbed.h"
00013 #include "x_nucleo_iks01a1.h"
00014 #include "expansionBoard.cpp"
00015 #include <cstring>
00016 #include <ctime>
00017 #include <stdexcept>
00018 
00019 ExpansionBoard e;
00020 struct tm t;
00021 //Thread *t;
00022 
00023 void getData(const void*){
00024     while(true){
00025         //Block on queuefor at most 5s if no data is available
00026         osEvent event = mail_box.get(5000);
00027         
00028         if(event.status == osEventTimeout) {
00029             printf("mail_box.get %02x, Timeout error", event.status);
00030             return;
00031         }
00032         else if (event.status == osEventMail) {
00033             // Successful, store log_data
00034             //TODO Store it somewhere
00035             log_data *log_d = (log_data*)event.value.p;
00036             mail_box.free(log_d);
00037         }
00038     }
00039 }
00040 
00041 int main() {
00042 
00043     char command[20];
00044     char arg[10];
00045 
00046     printf("\n\rWelcome! Type one of the following commands");
00047     printf("\n\r READ ALL\n\r READ <n>\n\r DELETE ALL\n\r DELETE <n>");
00048     printf("\n\r SETDATE dd mm yyyy\n\r SETTIME hh mm ss");
00049     printf("\n\r SETT <T>\n\r STATE <x>\n\r LOGGING <x>");
00050 
00051     scanf("%s", command);
00052 
00053     if (strcmp("READ", command)==0) {
00054         scanf("%s", arg);
00055         if(strcmp("ALL", arg)==0)
00056             //readAllSamples()
00057             printf("You ended up in READ ALL");
00058         else if (atoi(arg)!= 0)
00059             //readSample(atoi(arg));
00060             printf("You ended up in READ <n>");
00061         else printf("The argument is invalid");
00062     }
00063     else if (strcmp("DELETE", command)==0) {
00064         scanf("%s", arg);
00065         if (strcmp("ALL", arg)==0)
00066             //deleteAllSamplesOrSomething()
00067             printf("You ended up in READ ALL");
00068         else if (atoi(arg)!= 0)
00069             //deleteSample(atoi(arg));
00070             printf("You ended up in DELETE <n>");
00071         else printf("The argument is invalid");
00072     }
00073     else if (strcmp("SETDATE", command)==0) {
00074         char day[2], month[2], year[4];
00075 
00076         //Read day
00077         scanf("%s", day);
00078         if (atoi(day)!=0)
00079             t.tm_mday = atoi(day);
00080         // Read month
00081         scanf("%s", month);
00082         if (atoi(month)!=0)
00083             t.tm_mon = atoi(month)-1;
00084         //Read year
00085         scanf("%s", year);
00086         if (atoi(year)!=0)
00087             t.tm_year = atoi(year)-1900;
00088 
00089         // Check whether date is correct
00090         if (mktime(&t)>0) {
00091             printf("DATE UPDATED TO %d %d %d", t.tm_mday,
00092                    t.tm_mon+1, t.tm_year+1900);
00093             set_time(mktime(&t));
00094         }
00095         else printf("Time inserted is invalid");
00096     }
00097     else if (strcmp("SETTIME", command)==0) {
00098         char hour[2], min[2], sec[2];
00099 
00100         scanf("%s", hour);
00101         if (atoi(hour)!=0)
00102             t.tm_hour = atoi(hour);
00103         scanf("%s", min);
00104         if (atoi(min)!=0)
00105             t.tm_min = atoi(min);
00106         scanf("%s", sec);
00107         if (atoi(sec)!=0)
00108             t.tm_sec = atoi(sec);
00109 
00110         //If the user sets this before without DATE, it's gonna be garbage
00111 
00112         // Check whether time is correct
00113         if (mktime(&t)>0) {
00114             set_time(mktime(&t));
00115             printf("TIME UPDATED TO %d %d %d", t.tm_hour,
00116                    t.tm_min, t.tm_sec);
00117         }
00118         else printf("Time inserted is invalid");
00119     }
00120     else if (strcmp("SETT", command)==0) {
00121         scanf("%s", arg);
00122         if (atof(arg) >= 0.1 && atof(arg) <= 60.0 ) {
00123             e.T = atof(arg);
00124             printf("T UPDATED TO %.1f", e.T);
00125         }
00126         //else throw std::out_of_range ("T MUST BE WITHIN 0.1 AND 60.0");
00127     }
00128     else if (strcmp("STATE", command)==0) {
00129         scanf("%s", arg);
00130         if (strcmp("ON", arg)==0)
00131             //TODO For now it just reads data and prints it out, FIFO needed
00132             e.startSampling();
00133         else if (strcmp("OFF", arg)==0)
00134             e.stopSampling();
00135         else printf("The argument is invalid");
00136     }
00137     else if(strcmp("LOGGING", command)==0) {
00138         scanf("%s", arg);
00139         if (strcmp("ON", arg)==0){
00140             //startLogging();
00141             printf("LOGGING ON");
00142         }
00143         else if (strcmp("OFF", arg)==0){
00144             //stopLogging();
00145             printf("LOGGING OFF");
00146         }
00147         else printf("The argument is invalid");
00148     }
00149     else printf("There is no command matching. Please try again");
00150 
00151     // Clear the input to avoid it to being reused in the next cycle
00152     command[0] = arg[0] = 0;
00153 
00154     return 0;
00155 }