LAB1

Dependencies:   mbed

Fork of mbed_blinky by Mbed

main.cpp

Committer:
m0t0
Date:
2018-01-31
Revision:
20:12c90bd3e6e0
Parent:
4:81cea7a352b0

File content as of revision 20:12c90bd3e6e0:

#include "mbed.h"
    #include "string"
    #include "ctype.h"
    
Serial pc(USBTX, USBRX);
DigitalOut led1(LED1);
DigitalIn button(PC_13);
    
    
//function that recursively calls itself
void scan(){
        
  //array of strings for checking input
  char* str[4];
  int j = 0;
  str[0] = "led off";
  str[1] = "led off";
  str[2] = "button";
  str[3] = "blink";
  //init counters and arrays
  int t = 0;
  int i = 0;
  int val=0;
  int err = 1;
  char s[31];
  char nums[2];
      
  //loop that grabs what is typed and puts it in a string
  while (i<30) {
    s[i]=pc.getc();
    if(s[i]!=0x0d){
      pc.putc(s[i]);
      i++;
    }
    else{
      break;    
    }
  }
  //adds null character to end of string and prints
  s[i]='\0';  
  printf("%s", s);
            
            
  //while loop to check what string is entered        
  while(j < 4){       
    val = strcmp(s, str[j]);  
              
              
    if(val == 0 && j == 0){
      led1 = 1;
      err = 0;
    }
    else if (val== 0 && j == 1){
      led1 = 0;
      err = 0;
    }
                    
                    
    else if(val == 0 && j == 2){
      err = 0;
      if (button.read()){
    char p[] = "RELEASED\n";
    printf("%s\n", p);
      }
      else {
    char p1[] = "PRESSED\n";
    printf("%s\n", p1);
      }
    }
    //if blink is entered, this grabs the value typed   
    else if(val==0 && j==3){
      err = 0;
      int counter = 0;
      int count = 0;
      int numBlinks=0;
      printf("num blinks:%d",numBlinks);
      while(s[t] != '\0'){
    if(isdigit(s[t])){
      nums[count] = s[t];
      count++;
      t++;}
                                
    else t++;
      }
                        
                        
      numBlinks = atoi(nums);
                                
                    
                    
      //implementation of number of blinks
      while(counter > numBlinks){
    led1 = !led1;
    wait(0.5);
    counter++;
      }      
    }        
                    
    j++; 
                    
                        
                    
  }
                
  //recrussive call
  scan();
}
                
//main function that just calls the recurssive funciton scan
int main() {
  pc.printf("cps%");
  scan();
}