Adithi Minnasandran / Mbed 2 deprecated calc

Dependencies:   mbed

Committer:
adithi_arun
Date:
Fri Feb 18 14:35:23 2022 +0000
Revision:
1:b9118b4c402e
Parent:
0:f02bd2638bdd
calculator main cpp code with comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adithi_arun 1:b9118b4c402e 1 #include "mbed.h" //includes library mbed.h
adithi_arun 1:b9118b4c402e 2 extern "C" int add_asm(int a, int b); //declaration of function used for addition. Extern keyword makes it visible to the entire program.
adithi_arun 1:b9118b4c402e 3 extern "C" int sub_asm(int a, int b); //declaration of function used for subtraction.
adithi_arun 1:b9118b4c402e 4 extern "C" int mul_asm(int a, int b); //declaration of function used for multiplication.
adithi_arun 1:b9118b4c402e 5 extern "C" int div_asm(int a, int b); //declaration of function used for division.
adithi_arun 1:b9118b4c402e 6 DigitalIn columns[3]= {PB_6,PB_7,PD_0}; //Initializes input pin columns.
adithi_arun 1:b9118b4c402e 7 DigitalOut row[4]= {PA_5,PA_1,PA_2,PA_3}; //Initializes output pin rows.
adithi_arun 1:b9118b4c402e 8 int numpad[4][3] = {
adithi_arun 1:b9118b4c402e 9 {1,2,3},
adithi_arun 1:b9118b4c402e 10 {4,5,6},
adithi_arun 1:b9118b4c402e 11 {7,8,9},
adithi_arun 1:b9118b4c402e 12 {-1,0,-2},
adithi_arun 1:b9118b4c402e 13 }; //2 D array representing keypad inputs. Integer array is created to make calculation easier. -1 represents '*' and -2 represents '#'
adithi_arun 1:b9118b4c402e 14 //main function:
adithi_arun 1:b9118b4c402e 15 int main()
adithi_arun 1:b9118b4c402e 16 {
adithi_arun 1:b9118b4c402e 17 int detected = 0; //holds the intermediate values of each operand. Once an operand is fully entered(ie # is pressed), it is stored in the array op[2].
adithi_arun 1:b9118b4c402e 18 int op[2] = {0,0}; //stores each of the operands once '#' is pressed.
adithi_arun 1:b9118b4c402e 19 int inc = 0; //counts the number of operands entered. If inc = 2, code looks for operation to be performed.
adithi_arun 1:b9118b4c402e 20 int read; //holds keypad inputs while the code is reading operands.
adithi_arun 1:b9118b4c402e 21 int operation = -1; //variable that holds keypad code for the operation (1 – add, 2 – subtract, 3 – multiply, 4 – divide)
adithi_arun 1:b9118b4c402e 22 int y = 0; //changes values so that the program does not repeatedly execute 'if statements' on lines 29 and 33.
adithi_arun 1:b9118b4c402e 23
adithi_arun 1:b9118b4c402e 24 columns[0].mode(PullUp); //Pull up resistor on column 0
adithi_arun 1:b9118b4c402e 25 columns[1].mode(PullUp); //Pull up resistor on column 1
adithi_arun 1:b9118b4c402e 26 columns[2].mode(PullUp); //Pull up resistor on column 2
adithi_arun 1:b9118b4c402e 27 //while(1) makes the microcontroller repeatedly execute the code inside it
adithi_arun 1:b9118b4c402e 28 while(1) {
adithi_arun 1:b9118b4c402e 29 if (inc == 0 && y == 0) { //if inc = 0 (0 operands have been entered) and y = 0 ('Enter the first number:' has not been printed on terminal)
adithi_arun 1:b9118b4c402e 30 y = 1; //y is incremented to 1 so that 'Enter the first number:' is not printed again
adithi_arun 1:b9118b4c402e 31 printf("Enter the first number:\n"); //print statement prompting the user to enter the 1st operand
adithi_arun 1:b9118b4c402e 32 }
adithi_arun 1:b9118b4c402e 33 if (inc == 1 && y == 1) { //if inc = 1 (1 operand has been entered) and y = 1 ('Enter the second number:' has not been printed on terminal)
adithi_arun 1:b9118b4c402e 34 y = 2; //y is incremented to 2 so that this if statement is not repeated.
adithi_arun 1:b9118b4c402e 35 printf("Enter the second number:\n"); //prompts user to enter the 2nd operand
adithi_arun 1:b9118b4c402e 36 }
adithi_arun 1:b9118b4c402e 37 for(int i=0; i<4; i++) { //loops through the rows (adds 1 to i until i reaches 4, which is when the loop terminates)
adithi_arun 1:b9118b4c402e 38 row[0]=1; //Initializes first row
adithi_arun 1:b9118b4c402e 39 row[1]=1; //Initializes Second row
adithi_arun 1:b9118b4c402e 40 row[2]=1; //Initializes third row
adithi_arun 1:b9118b4c402e 41 row[3]=1; //Initializes fourth row
adithi_arun 1:b9118b4c402e 42 row[i]=0; //Holds the last value of i
adithi_arun 1:b9118b4c402e 43 wait(0.01); //delay of 0.01 seconds
adithi_arun 1:b9118b4c402e 44 for(int j=0; j<3; j++) { //loops through columns (adds 1 to j until j reaches 3)
adithi_arun 1:b9118b4c402e 45 if(columns[j] == 0 && numpad[i][j] == -1) { //if statement for column[j]=0 and numpad[i][j]=-1 ('*' is pressed)
adithi_arun 1:b9118b4c402e 46 inc = 0; //pressing * on the keypad resets the calculator and makes it go back to line 29 prompting user to enter the 1st number.
adithi_arun 1:b9118b4c402e 47 y = 0; //inc = 0 and y = 0 are the conditions needed to enter the if statement on line 29
adithi_arun 1:b9118b4c402e 48 }
adithi_arun 1:b9118b4c402e 49 if(columns[j]==0 && y!=3) {//if statement for column[j] = 0 and y!=3 (if y != 3, it means 2 operands have not yet been entered)
adithi_arun 1:b9118b4c402e 50 read=numpad[i][j]; //read holds integer at row i and column j from 2x2 array based on keypad press
adithi_arun 1:b9118b4c402e 51 if (read == -2) { //if the key pressed is -2, it means an operand has been entered
adithi_arun 1:b9118b4c402e 52 op[inc] = detected; //operand is stored in array op[2] at location inc
adithi_arun 1:b9118b4c402e 53 printf("%d\n",op[inc]); //prints the operand
adithi_arun 1:b9118b4c402e 54 inc++; //increments location of operand array
adithi_arun 1:b9118b4c402e 55 detected = 0; //intermediate value of operand is reset to 0
adithi_arun 1:b9118b4c402e 56 }
adithi_arun 1:b9118b4c402e 57 if (read >= 0) { //if the keypad input is not * or #
adithi_arun 1:b9118b4c402e 58 detected = detected*10 + read; //intermediate value of operand before it is stored in array op[2]
adithi_arun 1:b9118b4c402e 59 }
adithi_arun 1:b9118b4c402e 60 wait(0.005); //delay of 0.005s
adithi_arun 1:b9118b4c402e 61 while(columns[j]==0); //acts as a wait
adithi_arun 1:b9118b4c402e 62 }
adithi_arun 1:b9118b4c402e 63 if(columns[j] == 0 && y == 3) { //if y = 3 (calculator has received 2 operands) and a key is pressed, the following lines are ezecuted
adithi_arun 1:b9118b4c402e 64 operation = numpad[i][j]; //'operation' variable holds operator value read by keypad
adithi_arun 1:b9118b4c402e 65 if(operation>4 || operation<=0) { //if key pressed is not 1,2,3, or 4
adithi_arun 1:b9118b4c402e 66 printf("invalid char\n"); //prints 'invalid character'
adithi_arun 1:b9118b4c402e 67 y = 3; //makes y = 3 so that if statement on line 63 can be re-entered to accept a valid operator
adithi_arun 1:b9118b4c402e 68 } else if(operation==1) { //if operator is 1, addition is performed
adithi_arun 1:b9118b4c402e 69 printf("%d + %d = %d\n", op[0], op[1], add_asm(op[0],op[1])); //prints addition result
adithi_arun 1:b9118b4c402e 70 y = 0; //when y is reset to 0, if statement from line 29 can be executed again ('Enter the first number' is output on terminal)
adithi_arun 1:b9118b4c402e 71 } else if(operation == 2) { //if operator is 2, subtraction is performed
adithi_arun 1:b9118b4c402e 72 printf("%d - %d = %d\n", op[0], op[1], sub_asm(op[0],op[1])); //prints subtraction result
adithi_arun 1:b9118b4c402e 73 y = 0; //when y is reset to 0, if statement from line 29 can be executed again ('Enter the first number' is output on terminal)
adithi_arun 1:b9118b4c402e 74 } else if(operation == 3) { //if operator is 3, multiplication is performed
adithi_arun 1:b9118b4c402e 75 printf("%d * %d = %d\n", op[0], op[1], mul_asm(op[0],op[1])); //prints multiplication result
adithi_arun 1:b9118b4c402e 76 y = 0; //when y is reset to 0, if statement from line 29 can be executed again ('Enter the first number' is output on terminal)
adithi_arun 1:b9118b4c402e 77 } else { //if operator is 4, division is performed
adithi_arun 1:b9118b4c402e 78 printf("%d / %d = %d\n", op[0], op[1],div_asm(op[0],op[1])); //prints division result
adithi_arun 1:b9118b4c402e 79 y = 0; //when y is reset to 0, if statement from line 29 can be executed again ('Enter the first number' is output on terminal)
adithi_arun 1:b9118b4c402e 80 }
adithi_arun 1:b9118b4c402e 81 wait(0.005); //waits 0.005s
adithi_arun 1:b9118b4c402e 82 while(columns[j]==0); //acts as a wait
adithi_arun 1:b9118b4c402e 83 }
adithi_arun 1:b9118b4c402e 84 }
adithi_arun 1:b9118b4c402e 85 }
adithi_arun 1:b9118b4c402e 86 if(inc == 2) { //if 2 operands have been stored into array op[2]
adithi_arun 1:b9118b4c402e 87 detected = 0; //variable that stores intermediate values of the operand is reset to 0
adithi_arun 1:b9118b4c402e 88 printf("The numbers are %d and %d\n",op[0],op[1]); //prints the 2 operands.
adithi_arun 1:b9118b4c402e 89 printf("Press '*' to reset or Select 1:Addition 2:Subtraction 3:Multiplication 4:Division\n:"); //prints this statement on terminal
adithi_arun 1:b9118b4c402e 90 y = 3; //changes value of y to 3 so that program can enter if statement on line 63 so that operator is accepted and result is output
adithi_arun 1:b9118b4c402e 91 inc = 0; //inc is reset to 0
adithi_arun 1:b9118b4c402e 92 }
adithi_arun 1:b9118b4c402e 93 }
adithi_arun 1:b9118b4c402e 94 }