LAB1

Dependencies:   mbed

Fork of mbed_blinky by Mbed

Committer:
m0t0
Date:
Wed Jan 31 00:41:06 2018 +0000
Revision:
20:12c90bd3e6e0
Parent:
4:81cea7a352b0
LAB1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dan 0:7dec7e9ac085 1 #include "mbed.h"
m0t0 20:12c90bd3e6e0 2 #include "string"
m0t0 20:12c90bd3e6e0 3 #include "ctype.h"
m0t0 20:12c90bd3e6e0 4
m0t0 20:12c90bd3e6e0 5 Serial pc(USBTX, USBRX);
m0t0 20:12c90bd3e6e0 6 DigitalOut led1(LED1);
m0t0 20:12c90bd3e6e0 7 DigitalIn button(PC_13);
m0t0 20:12c90bd3e6e0 8
m0t0 20:12c90bd3e6e0 9
m0t0 20:12c90bd3e6e0 10 //function that recursively calls itself
m0t0 20:12c90bd3e6e0 11 void scan(){
m0t0 20:12c90bd3e6e0 12
m0t0 20:12c90bd3e6e0 13 //array of strings for checking input
m0t0 20:12c90bd3e6e0 14 char* str[4];
m0t0 20:12c90bd3e6e0 15 int j = 0;
m0t0 20:12c90bd3e6e0 16 str[0] = "led off";
m0t0 20:12c90bd3e6e0 17 str[1] = "led off";
m0t0 20:12c90bd3e6e0 18 str[2] = "button";
m0t0 20:12c90bd3e6e0 19 str[3] = "blink";
m0t0 20:12c90bd3e6e0 20 //init counters and arrays
m0t0 20:12c90bd3e6e0 21 int t = 0;
m0t0 20:12c90bd3e6e0 22 int i = 0;
m0t0 20:12c90bd3e6e0 23 int val=0;
m0t0 20:12c90bd3e6e0 24 int err = 1;
m0t0 20:12c90bd3e6e0 25 char s[31];
m0t0 20:12c90bd3e6e0 26 char nums[2];
m0t0 20:12c90bd3e6e0 27
m0t0 20:12c90bd3e6e0 28 //loop that grabs what is typed and puts it in a string
m0t0 20:12c90bd3e6e0 29 while (i<30) {
m0t0 20:12c90bd3e6e0 30 s[i]=pc.getc();
m0t0 20:12c90bd3e6e0 31 if(s[i]!=0x0d){
m0t0 20:12c90bd3e6e0 32 pc.putc(s[i]);
m0t0 20:12c90bd3e6e0 33 i++;
m0t0 20:12c90bd3e6e0 34 }
m0t0 20:12c90bd3e6e0 35 else{
m0t0 20:12c90bd3e6e0 36 break;
m0t0 20:12c90bd3e6e0 37 }
m0t0 20:12c90bd3e6e0 38 }
m0t0 20:12c90bd3e6e0 39 //adds null character to end of string and prints
m0t0 20:12c90bd3e6e0 40 s[i]='\0';
m0t0 20:12c90bd3e6e0 41 printf("%s", s);
m0t0 20:12c90bd3e6e0 42
m0t0 20:12c90bd3e6e0 43
m0t0 20:12c90bd3e6e0 44 //while loop to check what string is entered
m0t0 20:12c90bd3e6e0 45 while(j < 4){
m0t0 20:12c90bd3e6e0 46 val = strcmp(s, str[j]);
m0t0 20:12c90bd3e6e0 47
m0t0 20:12c90bd3e6e0 48
m0t0 20:12c90bd3e6e0 49 if(val == 0 && j == 0){
m0t0 20:12c90bd3e6e0 50 led1 = 1;
m0t0 20:12c90bd3e6e0 51 err = 0;
m0t0 20:12c90bd3e6e0 52 }
m0t0 20:12c90bd3e6e0 53 else if (val== 0 && j == 1){
m0t0 20:12c90bd3e6e0 54 led1 = 0;
m0t0 20:12c90bd3e6e0 55 err = 0;
m0t0 20:12c90bd3e6e0 56 }
m0t0 20:12c90bd3e6e0 57
m0t0 20:12c90bd3e6e0 58
m0t0 20:12c90bd3e6e0 59 else if(val == 0 && j == 2){
m0t0 20:12c90bd3e6e0 60 err = 0;
m0t0 20:12c90bd3e6e0 61 if (button.read()){
m0t0 20:12c90bd3e6e0 62 char p[] = "RELEASED\n";
m0t0 20:12c90bd3e6e0 63 printf("%s\n", p);
m0t0 20:12c90bd3e6e0 64 }
m0t0 20:12c90bd3e6e0 65 else {
m0t0 20:12c90bd3e6e0 66 char p1[] = "PRESSED\n";
m0t0 20:12c90bd3e6e0 67 printf("%s\n", p1);
m0t0 20:12c90bd3e6e0 68 }
m0t0 20:12c90bd3e6e0 69 }
m0t0 20:12c90bd3e6e0 70 //if blink is entered, this grabs the value typed
m0t0 20:12c90bd3e6e0 71 else if(val==0 && j==3){
m0t0 20:12c90bd3e6e0 72 err = 0;
m0t0 20:12c90bd3e6e0 73 int counter = 0;
m0t0 20:12c90bd3e6e0 74 int count = 0;
m0t0 20:12c90bd3e6e0 75 int numBlinks=0;
m0t0 20:12c90bd3e6e0 76 printf("num blinks:%d",numBlinks);
m0t0 20:12c90bd3e6e0 77 while(s[t] != '\0'){
m0t0 20:12c90bd3e6e0 78 if(isdigit(s[t])){
m0t0 20:12c90bd3e6e0 79 nums[count] = s[t];
m0t0 20:12c90bd3e6e0 80 count++;
m0t0 20:12c90bd3e6e0 81 t++;}
m0t0 20:12c90bd3e6e0 82
m0t0 20:12c90bd3e6e0 83 else t++;
m0t0 20:12c90bd3e6e0 84 }
m0t0 20:12c90bd3e6e0 85
m0t0 20:12c90bd3e6e0 86
m0t0 20:12c90bd3e6e0 87 numBlinks = atoi(nums);
m0t0 20:12c90bd3e6e0 88
m0t0 20:12c90bd3e6e0 89
m0t0 20:12c90bd3e6e0 90
m0t0 20:12c90bd3e6e0 91 //implementation of number of blinks
m0t0 20:12c90bd3e6e0 92 while(counter > numBlinks){
m0t0 20:12c90bd3e6e0 93 led1 = !led1;
m0t0 20:12c90bd3e6e0 94 wait(0.5);
m0t0 20:12c90bd3e6e0 95 counter++;
m0t0 20:12c90bd3e6e0 96 }
m0t0 20:12c90bd3e6e0 97 }
m0t0 20:12c90bd3e6e0 98
m0t0 20:12c90bd3e6e0 99 j++;
m0t0 20:12c90bd3e6e0 100
m0t0 20:12c90bd3e6e0 101
m0t0 20:12c90bd3e6e0 102
m0t0 20:12c90bd3e6e0 103 }
m0t0 20:12c90bd3e6e0 104
m0t0 20:12c90bd3e6e0 105 //recrussive call
m0t0 20:12c90bd3e6e0 106 scan();
m0t0 20:12c90bd3e6e0 107 }
m0t0 20:12c90bd3e6e0 108
m0t0 20:12c90bd3e6e0 109 //main function that just calls the recurssive funciton scan
dan 0:7dec7e9ac085 110 int main() {
m0t0 20:12c90bd3e6e0 111 pc.printf("cps%");
m0t0 20:12c90bd3e6e0 112 scan();
dan 0:7dec7e9ac085 113 }