trabalho final

Dependencies:   X_NUCLEO_IKS01A1-f255a2c75ecb mbed-rtos mbed

main.cpp

Committer:
nlsantos
Date:
2016-05-15
Revision:
8:28b7b60bfc70
Parent:
4:597291e88109

File content as of revision 8:28b7b60bfc70:

/**
 ******************************************************************************
 * @file    main.cpp
 * @author  ANG Group (Nelson Santos; Irina Baptista; Pierluigi Urru)
 * @version V0.0.3
 * @brief   Simple Example application for using the X_NUCLEO_IKS01A1
 *          MEMS Inertial & Environmental Sensor Nucleo expansion board.
 ******************************************************************************
*/

#include "mbed.h"
#include "x_nucleo_iks01a1.h"
#include "rtos.h"
#include <cstring>
#include <stdexcept>
#include <vector>
#include "sensor.h"
#include "userMethods.h"

struct tm t = {t.tm_hour = 0};

std::vector<log_data> v;
int n = 0;
UserMethods u(v, n);
ExpansionBoard e(v, n);

Thread *logThread;
Thread *getThread;
//Thread *readAllThread;
Thread *readThread;

int main() {
      
    printf("Sampling started...\n");
    
    printf("\n\rWelcome! Type one of the following commands");
    printf("\n\r READ ALL\n\r READ <n>\n\r DELETE ALL\n\r DELETE <n>");
    printf("\n\r SETDATE dd mm yyyy\n\r SETTIME hh mm ss");
    printf("\n\r SETT <T>\n\r STATE <x>\n\r LOGGING <x>\n\r EXIT\n\n\r");
    
    //Initialise time
    set_time(0);   
    Ticker ticker;
    ticker.attach(e.sampleData, e.T);
    //getThread = new Thread(e.getData);
    
    char command[20];
    char arg[10];
        
    while(1) {
        scanf("%s", command);
        
        if (strcmp("READ", command)==0) {
            scanf("%s", arg);
            printf("\n");
            if(strcmp("ALL", arg)==0)
                Thread readAllThread(u.readAllData, NULL, osPriorityBelowNormal, DEFAULT_STACK_SIZE);
            else if (atoi(arg)!= 0) {
                n = atoi(arg); //A bit hacky, but it works
                Thread readNThread(u.readNData,NULL,osPriorityBelowNormal, DEFAULT_STACK_SIZE);
                }XCZVCZx
            
            else printf("The argument is invalid\n\r");
        }
        
        else if (strcmp("DELETE", command)==0) {
            scanf("%s", arg);
            printf("\n");
            if (strcmp("ALL", arg)==0) {
                int n = v.size();
                v.clear();
                printf("DELETED %d RECORDS", n);
            }
            else if (atoi(arg)!= 0) {
                int n = v.size();
                v.erase(v.begin(), v.end());
                if(n < atoi(arg))
                    printf("DELETED %d RECORDS", n);
                else printf("DELETED %d RECORDS", atoi(arg));
            }
            
            else printf("The argument is invalid\n\r");
        }
        
        else if (strcmp("SETDATE", command)==0) {
            char day[2], month[2], year[4];
            //Read day
            scanf("%s", day);
            if (atoi(day)!=0)
                atoi(day)<31 ? t.tm_mday=atoi(day) : printf("Invalid day\n");
            // Read month
            scanf("%s", month);
            if (atoi(month)!=0)
                atoi(month)<=12 ? t.tm_mon=atoi(month)-1 : printf("Invalid month\n");
            //Read year
            scanf("%s", year);
            printf("\n");
            if (atoi(year)!=0)
                atoi(year)>=70 ? t.tm_year=atoi(year)-1900 : printf("Invalid year\n");
            
            // Check whether date is correct
            if (mktime(&t)>0) {
                printf("DATE UPDATED TO %d %d %d", t.tm_mday,
                       t.tm_mon+1, t.tm_year+1900);
                set_time(mktime(&t));
            } else printf("Date inserted is invalid\n\r");
        }
        
        else if (strcmp("SETTIME", command)==0) {
            char hour[2], min[2], sec[2];
            
            scanf("%s", hour);
            if (atoi(hour)!=0)
                t.tm_hour = atoi(hour);
            scanf("%s", min);
            if (atoi(min)!=0)
                t.tm_min = atoi(min);
            scanf("%s", sec);
            if (atoi(sec)!=0)
                t.tm_sec = atoi(sec);
            printf("\n");

            //If the user sets this before without DATE, it's gonna be garbage
            
            // Check whether time is correct
            if (mktime(&t)>0) {
                set_time(mktime(&t));
                printf("TIME UPDATED TO %d %d %d", t.tm_hour, t.tm_min, t.tm_sec);
            } else printf("Time inserted is invalid\n\r");
        }
        
        else if (strcmp("SETT", command)==0) {
            scanf("%s", arg);
            printf("\n");
            if (atof(arg) >= 0.1 && atof(arg) <= 60.0 ) {
                e.T = atof(arg);
                printf("T UPDATED TO %.1f", e.T);
            }
            //else throw std::out_of_range ("T MUST BE WITHIN 0.1 AND 60.0");
        
        } else if (strcmp("STATE", command)==0) {
            scanf("%s", arg);
            printf("\n");            
            if (strcmp("ON", arg)==0) {
                ticker.attach(e.sampleData, e.T);
                getThread = new Thread(e.getData);
                printf("SAMPLING ON");
            }
            else if (strcmp("OFF", arg)==0) {
                ticker.detach();
                getThread->terminate();
                printf("SAMPLIG OFF");
            }
                
            else printf("The argument is invalid\n\r");
        }
        //TODO Create logging thread and log
        else if(strcmp("LOGGING", command)==0) {
            scanf("%s", arg);
            printf("\n");
            if (strcmp("ON", arg)==0)
                logThread = new Thread(u.logging);
            else if (strcmp("OFF", arg)==0)
                logThread->terminate();
            else printf("The argument is invalid\n\r");
        }
        
        else if (strcmp("Q", command) == 0) {
            ticker.detach();    //DEBUG
        }
        
        else if(strcmp("EXIT", command) == 0) { break; }
        
        else printf("\nThere is no command matching. Try again");

        // Clear the input to avoid it to being reused in the next cycle
        command[0] = arg[0] = 0;
        printf("\n\r");
    }
    
    printf("\nYou're outside the while loop\nReturning...");
    
  
    
    return 0;
}