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-14
Revision:
8:e3aff00ef600
Parent:
6:c7220f7c1ae0
Child:
9:6fb498cf6156

File content as of revision 8:e3aff00ef600:

#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];

int number;
char dayStr[3];
char monthStr[3];
char yearStr[5];
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){
            number = atoi(string2);
            if(number > 0){
                pc.printf("reading <%d>\n",number);
            }
            else{
                pc.printf("invalid value\n");
            }
        }
        else{
            pc.printf("invalid command\n");
        }
    }
    

    
    else if(strcmp(string1,"DELETE")==0){
        int test=0;
        sscanf(string2,"%d",&test);
        
        if(test>0){
            number = atoi(string2);
            if(number > 0){
                pc.printf("deleting <%d>\n",number);
            }
            else{
                pc.printf("invalid value\n");
            }
        }
        else{
            pc.printf("invalid command\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){
           pc.printf("**********SETTING DATE**********\n");
           pc.printf("date string: %s\n", string2);
           
           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;         
            
           pc.printf("Day = %s\n", dayStr);
           pc.printf("Month = %s\n",monthStr);
           pc.printf("Year = %s\n",yearStr);
           pc.printf("**********DATE SET**********\n");
        }
        else{
            pc.printf("invalid value\n");
        }
    }
            
    else{
        pc.printf("invalid command\n");
    }
}