Deals with user inputs

Dependents:   ELEC350_Project2

serial_terminal.cpp

Committer:
Swabey89
Date:
2018-12-17
Revision:
20:17a69bd39cbd
Parent:
19:5c45052abc84
Child:
21:896ad4241247

File content as of revision 20:17a69bd39cbd:

#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
 
void serialterm()
{
        static time_t seconds;
        
        //change these sizes
        char cmnd[30];
        char arg[30];
        float val;        
    
        e_commands e_cmnd;
        e_commands e_arg;
        
        sscanf(cmdBuffer,"%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);
                timeData = localtime(&seconds);
                set_time(mktime(timeData));
                sscanf(arg,"%2d%2d%4d",&(timeData->tm_mday),&(timeData->tm_mon),&(timeData->tm_year));            
                (timeData->tm_mon) = (timeData->tm_mon)-1;
                (timeData->tm_year) = (timeData->tm_year)-1900;
                if (mktime(timeData) == -1)
                {
                    pc->printf("SETDATE failed.\n");   
                }
                else
                {
                    set_time(mktime(timeData));
                    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);
                timeData = localtime(&seconds);
                set_time(mktime(timeData));  
                //scan the input for hhmmss
                sscanf(arg,"%2d%2d%2d",&(timeData->tm_hour),&(timeData->tm_min),&(timeData->tm_sec));      
                //check if the time is valid, if not do not update time
                if (mktime(timeData) == -1)
                {
                    pc->printf("Failed.\n");   
                }
                else
                {
                    set_time(mktime(timeData));
                    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 set to %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;
}