Deals with user inputs

Dependents:   ELEC350_Project2

serial_terminal.cpp

Committer:
Swabey89
Date:
2018-11-23
Revision:
17:9090420e5818
Parent:
16:036b11e97f7f
Child:
18:23c223755f5d

File content as of revision 17:9090420e5818:

#include "serial_terminal.hpp"
#include "serial_terminal.hpp"
#include "sample_hardware.hpp"
#include "mbed_events.h"
#include "SDCard.hpp"
#include "mbed.h"

#define SAMPLES_IN_MEMORY 120 //Temporary for testing. 


//High priority? to make commands not miss? see if this blocks (unsure if the scanf will signal scheduler)


 
void serialterm()
{
        //change these sizes
        char cmnd[30];
        char arg[30];
        float val;        
    
        e_commands e_cmnd;
        e_commands e_arg;
        
        sscanf(buffer,"%s %s",cmnd, arg);
        int argnum = sscanf(arg, "%f", &val); //returns 0 if no number present  //might need to do this seperately in each case 
        
        e_cmnd = stringconv(cmnd);
        e_arg = stringconv(arg);
        
        switch (e_cmnd)
        {
            case (READ) :
                if ((argnum == 0 && e_arg != ALL) || (argnum == 1 && val < 1)) {pc->printf("INVALID COMMAND\n"); break;}
                else if (e_arg == ALL || val > SAMPLES_IN_MEMORY) SDqueue.call(SDread,-1); //read all
                else SDqueue.call(SDread,val);  
                break;            
            case (DELETE) : 
                if ((argnum == 0 && e_arg != ALL) || (argnum == 1 && val < 1)) {pc->printf("INVALID COMMAND\n"); break;}
                if (e_arg == ALL || val > SAMPLES_IN_MEMORY) SDqueue.call(SDdelete,-1); //delete all
                else SDqueue.call(SDdelete,val);
                break;
            case (SETDATE) : 
                seconds = time(NULL);
                t = localtime(&seconds);
                set_time(mktime(t));
                sscanf(arg,"%2d%2d%4d",&(t->tm_mday),&(t->tm_mon),&(t->tm_year));            
                (t->tm_mon) = (t->tm_mon)-1;
                (t->tm_year) = (t->tm_year)-1900;
                if (mktime(t) == -1)
                {
                    pc->printf("Failed.\n");   
                }
                else
                {
                    set_time(mktime(t));
                    seconds = time(NULL);
                    pc->printf("DATE UPDATED TO %s\n", ctime(&seconds));
                }
                break;
            case (SETTIME) :
                //Get current time and update the tm structure
                seconds = time(NULL);
                t = localtime(&seconds);
                set_time(mktime(t));  
                //scan the input for hhmmss
                sscanf(arg,"%2d%2d%2d",&(t->tm_hour),&(t->tm_min),&(t->tm_sec));      
                //check if the time is valid, if not do not update time
                if (mktime(t) == -1)
                {
                    pc->printf("Failed.\n");   
                }
                else
                {
                    set_time(mktime(t));
                    seconds = time(NULL);
                    pc->printf("TIME UPDATED TO %s\n", ctime(&seconds));
                }
                break;
            case (SETT) :
                //Set the sampling rate
                if (e_arg == ALL || val < 0.1 || val >60) {pc->printf("OUT OF RANGE\n"); break;}
                else {sample_rate = val; sample.attach(&sampleISR, sample_rate); pc->printf("Sample rate %5.2f\n", val);}                
                break;
            case (STATE) :
                pc->printf("Set sampling state command received\n");
                puts(arg);
                break;
            case (LOGGING) :
                pc->printf("Set logging command received\n");
                pc->printf("%s\n",arg);
                break;
            default :
                pc->printf("INVALID COMMAND\n");
                break;            
        }     
}

e_commands stringconv(string in)
{
    if (in == "READ") return READ;
    if (in == "DELETE") return DELETE;
    if (in == "SETDATE") return SETDATE;
    if (in == "SETTIME") return SETTIME;
    if (in == "SETT") return SETT;
    if (in == "STATE") return STATE;
    if (in == "LOGGING") return LOGGING;
    if (in == "ALL") return ALL;
    //else return UNKNOWN;
}