Deals with user inputs

Dependents:   ELEC350_Project2

serial_terminal.cpp

Committer:
Swabey89
Date:
2018-12-28
Revision:
25:71b6c52169b0
Parent:
24:1cd91e660da8
Child:
26:d06a55487056

File content as of revision 25:71b6c52169b0:

#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()
{
        serial_tout.attach(serial_toutISR,TOUT_TIME);
        
        static time_t seconds;
        
        //change these sizes
        char cmnd[30];
        char arg[30];
        float val;        
    
        e_commands e_cmnd;
        e_commands e_arg;
        
        pc->printf("\nCommand buffer:%s\n\r",cmdBuffer);
        
        sscanf(cmdBuffer,"%s %s",cmnd, arg);
        int argnum = sscanf(arg, "%f", &val); //returns 0 if no number present
        
        e_cmnd = stringconv(cmnd);
        e_arg = stringconv(arg);
        
        if(logging)
        {
            printlock.lock();
            pc->printf("Command %s recieved with argument of %s, containing %d readable numbers\r\n\n",cmnd,arg,argnum);
            printlock.unlock();   
        }
        
        switch (e_cmnd)
        {
            
            case (READ) :
                if ((argnum == 0 && e_arg != ALL) || (argnum == 1 && val < 1)) {printlock.lock(); pc->printf("INVALID COMMAND\n\n\r"); printlock.unlock(); break;}
                else if (e_arg == ALL || val >= (BUFFERSIZE-Nspaces))
                {
                    SDqueue.call(SDread,-1); //read all                    
                }
                else 
                {
                    SDqueue.call(SDread,val);  
                }
                break;     
                       
            case (DELETE) : 
                if ((argnum == 0 && e_arg != ALL) || (argnum == 1 && val < 1)) {printlock.lock(); pc->printf("INVALID COMMAND\n\n\r"); printlock.unlock(); break;}
                if (e_arg == ALL || val > (BUFFERSIZE-Nspaces)) SDqueue.call(SDdelete,-1); //delete all
                else SDqueue.call(SDdelete,val);
                break;
                
            case (SETDATE) : 
                timeLock.lock();
                seconds = time(NULL);
                timeData = localtime(&seconds);
                //localtime_r(&seconds, timeData);
                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;
                timeLock.unlock();
                if (mktime(timeData) == -1)
                {
                    printlock.lock();
                    pc->printf("SETDATE failed\n\n\r");
                    printlock.unlock();   
                }
                else
                {
                    timeLock.lock();
                    set_time(mktime(timeData));
                    timeLock.unlock();
                    seconds = time(NULL);
                    printlock.lock();
                    pc->printf("Date updated to %s\n\n\r", ctime(&seconds));
                    printlock.unlock();
                }
                break;
                
            case (SETTIME) :
                //Get current time and update the tm structure
                timeLock.lock();
                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
                timeLock.unlock();
                if (mktime(timeData) == -1)
                {
                    printlock.lock();
                    pc->printf("SETTIME FAILED\n\n\r");
                    printlock.unlock();   
                }
                else
                {
                    timeLock.lock();
                    set_time(mktime(timeData));
                    seconds = time(NULL);
                    timeLock.unlock();
                    printlock.lock();
                    pc->printf("TIME UPDATED TO %s\n\n\r", ctime(&seconds));
                    printlock.unlock();
                }
                break;
                
            case (SETT) :
                //Set the sampling rate
                if (e_arg == ALL || val < 0.1 || val > 60) {printlock.lock(); pc->printf("OUT OF RANGE\n\n\r"); printlock.unlock(); break;}
                else {sample_rate = val; sample.attach(&sampleISR, sample_rate); printlock.lock(); pc->printf("Sample rate set to %5.2f\n\n\r", val); printlock.unlock();}                
                break;
                
            case (STATE) :
                if (e_arg == ON)
                {
                    sample.attach(&sampleISR, sample_rate);
                    printlock.lock();
                    pc->printf("Sampling ON\n\n\r");
                    printlock.unlock();
                    break;
                }
                else if (e_arg == OFF)
                {
                    sample.attach(NULL, sample_rate);
                    printlock.lock();
                    pc->printf("Sampling OFF\n\n\r");
                    printlock.unlock();
                    break; 
                }
                else
                {
                    printlock.lock();
                    pc->printf("Invalid command\n\n\r"); 
                    printlock.unlock();
                    break;
                }
                  
                
            case (LOGGING) :
               if (e_arg == ON)
                {
                    logging = true;
                    printlock.lock();
                    pc->printf("LOGGING ON\n\n\r");
                    printlock.unlock();
                    break;
                }
                else if (e_arg == OFF)
                {
                    logging = false;
                    printlock.lock();
                    pc->printf("LOGGING OFF\n\n\r");
                    printlock.unlock();
                    break; 
                }
                else
                {
                    printlock.lock();
                    pc->printf("INVALID COMMAND\n\n\r"); 
                    printlock.unlock();
                    break;
                }
                break;
                
            default :
                printlock.lock();
                pc->printf("INVALID COMMAND\n\n\r");
                printlock.unlock();
                break;            
        }    
        serial_tout.detach();
        memset(cmdBuffer,0,sizeof cmdBuffer);
}

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;
    if (in == "ON") return ON;
    if (in == "OFF") return OFF;
    else return UNKNOWN;
}