test to establish how we can share the project on MBED

Dependencies:   BME280 BMP280 ELEC350-Coursework-2017 TextLCD

Fork of ELEC350-CWTEMPLATE-2017 by University of Plymouth - Stages 1, 2 and 3

serial_protocol/serial_protocol.cpp

Committer:
bdarling
Date:
2017-12-15
Revision:
10:47e6166f0797
Parent:
9:6fb498cf6156
Child:
11:573cf2feced3

File content as of revision 10:47e6166f0797:

#include "serial_protocol.hpp"
#include "sample_hardware.hpp"
#include <string>
#include <ctype.h>
Serial pc(SERIAL_TX, SERIAL_RX);


char string1[128];
char string2[128];

char dayStr[3];
char monthStr[3];
char yearStr[5];
char hourStr[3];
char minuteStr[3];
char secondStr[3];
int day;
int month;
int year;



void getData(){
    pc.scanf("%s %s", string1, string2);
    //pc.printf("%s %s\n",string1, string2); \\for debugging 
}

void readSerial(){
    if((strcmp(string1,"READ")==0) && (strcmp(string2,"ALL")==0)){
        pc.printf("reading all\n");
    }
    
    else if((strcmp(string1,"DELETE")==0) && (strcmp(string2,"ALL")==0)){
        pc.printf("deleting all\n");
    }
    
    else if(strcmp(string1,"READ")==0){
        int test=0;
        sscanf(string2,"%d",&test);
                
        if(test>0){
            pc.printf("reading <%d>\n",test);
        }
        else{
            pc.printf("invalid value\n");
        }                
    }
    
    else if(strcmp(string1,"SETT")==0){
        float number=0;
        sscanf(string2,"%f",&number);
        if(number>=0.1f && number<=60.0f){
            pc.printf("sampling period set to %f\n",number);
        }
        else{
            pc.printf("invalid value\n");
        }                
    }
    
    else if(strcmp(string1,"DELETE")==0){
        int test=0;
        sscanf(string2,"%d",&test);
        
        if(test>0){
            pc.printf("deleting <%d>\n",test);
        }
        else{
                pc.printf("invalid value\n");
        }
    }
    
    else if(strcmp(string1,"SETDATE")==0) 
    {
        int validDigit=0;        
        int i;
        for(i=0;i<8;i++){ //check first 8 elements of array are digits
            if(isdigit(string2[i])){
                validDigit++;
            }
        }
        if(string2[8] == NULL){ //check element 8 of array is NULL, where the string terminates
            validDigit++;
        }               
            
        if(validDigit==9){ 
           
           dayStr[0] = string2[0];
           dayStr[1] = string2[1];
           dayStr[2] = NULL;
           monthStr[0] = string2[2];
           monthStr[1] = string2[3];
           monthStr[2] = NULL;
           yearStr[0] = string2[4];
           yearStr[1] = string2[5];
           yearStr[2] = string2[6];
           yearStr[3] = string2[7];
           yearStr[4] = NULL;
           //note if date is required in int form, use atoi() to convert from string to int    
            
           pc.printf("DATE UPDATED TO <%s><%s><%s>\n",dayStr,monthStr,yearStr);

        }
        else{
            pc.printf("invalid value\n");
        }
    }
    
        else if(strcmp(string1,"SETTIME")==0) 
    {
        int validDigit=0;        
        int i;
        for(i=0;i<6;i++){ //check first 8 elements of array are digits
            if(isdigit(string2[i])){
                validDigit++;
            }
        }
        if(string2[6] == NULL){ //check element 8 of array is NULL, where the string terminates
            validDigit++;
        }               
            
        if(validDigit==7){
           
           hourStr[0] = string2[0];
           hourStr[1] = string2[1];
           hourStr[2] = NULL;
           minuteStr[0] = string2[2];
           minuteStr[1] = string2[3];
           minuteStr[2] = NULL;
           secondStr[0] = string2[4];
           secondStr[1] = string2[5];
           secondStr[2] = NULL;    
            
           pc.printf("TIME UPDATE TO <%s><%s><%s>",hourStr,minuteStr,secondStr);
        }
        else{
            pc.printf("invalid value\n");
        }
    }
            
    else{
        pc.printf("invalid command\n");
    }
}