LAB1

Dependencies:   mbed

Fork of mbed_blinky by Mbed

Files at this revision

API Documentation at this revision

Comitter:
m0t0
Date:
Wed Jan 31 00:41:06 2018 +0000
Parent:
19:2157e25073b7
Commit message:
LAB1

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Dec 25 03:11:29 2017 +0000
+++ b/main.cpp	Wed Jan 31 00:41:06 2018 +0000
@@ -1,12 +1,113 @@
 #include "mbed.h"
-
-DigitalOut myled(LED1);
-
+    #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() {
-    while(1) {
-        myled = 1;
-        wait(0.2);
-        myled = 0;
-        wait(0.2);
-    }
+  pc.printf("cps%");
+  scan();
 }