This simple program enable LPC11U24 MBED board to act as a simple integer calculator. Just type the operation you want to execute from the serial terminal. Admitted operations: +, -, * /, ^ (only positive exponent). Multiple operations in raw are supported as well as negative integer.
Fork of Serial_HelloWorld_Mbed by
main.cpp@5:8bb67df511a3, 2014-09-22 (annotated)
- Committer:
- andrelongo85
- Date:
- Mon Sep 22 06:32:31 2014 +0000
- Revision:
- 5:8bb67df511a3
- Parent:
- 4:235e50314809
Comments update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:879aa9d0247b | 1 | #include "mbed.h" |
andrelongo85 | 1:247161a5c5d6 | 2 | |
andrelongo85 | 1:247161a5c5d6 | 3 | #define FALSE 0 |
andrelongo85 | 1:247161a5c5d6 | 4 | #define TRUE 1 |
andrelongo85 | 1:247161a5c5d6 | 5 | |
mbed_official | 0:879aa9d0247b | 6 | Serial pc(USBTX, USBRX); // tx, rx |
andrelongo85 | 1:247161a5c5d6 | 7 | |
andrelongo85 | 1:247161a5c5d6 | 8 | // function to convert a char buffer to decimal inetger |
andrelongo85 | 1:247161a5c5d6 | 9 | int char_to_int (const char *char_p, const int size) |
andrelongo85 | 1:247161a5c5d6 | 10 | { |
andrelongo85 | 1:247161a5c5d6 | 11 | int tmp = 0; |
andrelongo85 | 1:247161a5c5d6 | 12 | int pot_ten=1; |
andrelongo85 | 1:247161a5c5d6 | 13 | for ( int i= (size -1); i >= 0 ; i-- ) { |
andrelongo85 | 1:247161a5c5d6 | 14 | tmp = int( tmp + ( ( int(char_p[i]) - 48 ) * pot_ten )); |
andrelongo85 | 1:247161a5c5d6 | 15 | pot_ten*=10; |
andrelongo85 | 1:247161a5c5d6 | 16 | } |
andrelongo85 | 1:247161a5c5d6 | 17 | return tmp; |
andrelongo85 | 1:247161a5c5d6 | 18 | } |
andrelongo85 | 1:247161a5c5d6 | 19 | |
andrelongo85 | 1:247161a5c5d6 | 20 | |
andrelongo85 | 1:247161a5c5d6 | 21 | // check if the input is valid |
andrelongo85 | 1:247161a5c5d6 | 22 | bool si_check_valid(const char & input) |
andrelongo85 | 1:247161a5c5d6 | 23 | { |
andrelongo85 | 3:3bef1c4e42d4 | 24 | if ( ( ( int(input) > 57) || ( int(input) < 48) ) && (input != '+') && (input != '-') && (input != '*') && (input != '/') && (input != '^') && (input != '=') ) { |
andrelongo85 | 1:247161a5c5d6 | 25 | return FALSE; |
andrelongo85 | 1:247161a5c5d6 | 26 | } else { |
andrelongo85 | 1:247161a5c5d6 | 27 | return TRUE; |
andrelongo85 | 1:247161a5c5d6 | 28 | } |
andrelongo85 | 1:247161a5c5d6 | 29 | } |
andrelongo85 | 1:247161a5c5d6 | 30 | |
andrelongo85 | 1:247161a5c5d6 | 31 | //check if the input is a number |
andrelongo85 | 1:247161a5c5d6 | 32 | bool si_check_number(const char & input) |
andrelongo85 | 1:247161a5c5d6 | 33 | { |
andrelongo85 | 1:247161a5c5d6 | 34 | if ( (int(input) >= 48) && ( int(input) <= 57) ) { |
andrelongo85 | 1:247161a5c5d6 | 35 | return TRUE; |
andrelongo85 | 1:247161a5c5d6 | 36 | } else { |
andrelongo85 | 1:247161a5c5d6 | 37 | return FALSE; |
andrelongo85 | 1:247161a5c5d6 | 38 | } |
andrelongo85 | 1:247161a5c5d6 | 39 | } |
andrelongo85 | 1:247161a5c5d6 | 40 | |
andrelongo85 | 5:8bb67df511a3 | 41 | //global variables |
andrelongo85 | 3:3bef1c4e42d4 | 42 | char c; |
andrelongo85 | 3:3bef1c4e42d4 | 43 | char op = '='; |
andrelongo85 | 3:3bef1c4e42d4 | 44 | char operand [100]; |
andrelongo85 | 3:3bef1c4e42d4 | 45 | char buf[10]; |
andrelongo85 | 3:3bef1c4e42d4 | 46 | int result =0; |
andrelongo85 | 3:3bef1c4e42d4 | 47 | int result_copy=0; |
andrelongo85 | 3:3bef1c4e42d4 | 48 | int index = 0; |
andrelongo85 | 3:3bef1c4e42d4 | 49 | int ioperand =0; |
andrelongo85 | 3:3bef1c4e42d4 | 50 | bool first = TRUE; |
andrelongo85 | 3:3bef1c4e42d4 | 51 | bool sign_check = TRUE; |
andrelongo85 | 3:3bef1c4e42d4 | 52 | bool neg_operand = FALSE; |
andrelongo85 | 3:3bef1c4e42d4 | 53 | bool error=FALSE; |
andrelongo85 | 2:0baf535ed8f8 | 54 | |
andrelongo85 | 5:8bb67df511a3 | 55 | // serial interrupt callback function |
andrelongo85 | 3:3bef1c4e42d4 | 56 | void callback() |
andrelongo85 | 1:247161a5c5d6 | 57 | { |
mbed_official | 0:879aa9d0247b | 58 | while(1) { |
andrelongo85 | 2:0baf535ed8f8 | 59 | |
andrelongo85 | 1:247161a5c5d6 | 60 | c=pc.getc(); //get serial input |
andrelongo85 | 1:247161a5c5d6 | 61 | |
andrelongo85 | 1:247161a5c5d6 | 62 | if (!si_check_valid(c)) { // Invalid Input |
andrelongo85 | 1:247161a5c5d6 | 63 | pc.putc(c); |
andrelongo85 | 1:247161a5c5d6 | 64 | printf ("Invalid Input!\n"); |
andrelongo85 | 1:247161a5c5d6 | 65 | } else if (si_check_number(c)) { // Operand has been entered |
andrelongo85 | 1:247161a5c5d6 | 66 | pc.putc(c); |
andrelongo85 | 1:247161a5c5d6 | 67 | operand[index] = c; |
andrelongo85 | 1:247161a5c5d6 | 68 | index++; |
andrelongo85 | 2:0baf535ed8f8 | 69 | sign_check = FALSE; //sign check disactivated |
andrelongo85 | 1:247161a5c5d6 | 70 | } else { //Operator has been entered |
andrelongo85 | 2:0baf535ed8f8 | 71 | |
andrelongo85 | 2:0baf535ed8f8 | 72 | if(!sign_check) { |
andrelongo85 | 2:0baf535ed8f8 | 73 | pc.putc('\n'); |
andrelongo85 | 2:0baf535ed8f8 | 74 | pc.putc(c); |
andrelongo85 | 2:0baf535ed8f8 | 75 | pc.putc('\n'); |
andrelongo85 | 1:247161a5c5d6 | 76 | |
andrelongo85 | 2:0baf535ed8f8 | 77 | // sign adjustment |
andrelongo85 | 2:0baf535ed8f8 | 78 | if(neg_operand) { |
andrelongo85 | 2:0baf535ed8f8 | 79 | ioperand = -(char_to_int(operand,index)); |
andrelongo85 | 2:0baf535ed8f8 | 80 | } else { |
andrelongo85 | 2:0baf535ed8f8 | 81 | ioperand = char_to_int(operand,index); |
andrelongo85 | 2:0baf535ed8f8 | 82 | } |
andrelongo85 | 3:3bef1c4e42d4 | 83 | |
andrelongo85 | 3:3bef1c4e42d4 | 84 | |
andrelongo85 | 5:8bb67df511a3 | 85 | if (first) { //First operator |
andrelongo85 | 2:0baf535ed8f8 | 86 | result = ioperand; |
andrelongo85 | 2:0baf535ed8f8 | 87 | first = FALSE; |
andrelongo85 | 5:8bb67df511a3 | 88 | } else { //Do calculation |
andrelongo85 | 1:247161a5c5d6 | 89 | |
andrelongo85 | 2:0baf535ed8f8 | 90 | if(op == '+') { |
andrelongo85 | 2:0baf535ed8f8 | 91 | result = int (result + ioperand); |
andrelongo85 | 2:0baf535ed8f8 | 92 | } else if (op == '-') { |
andrelongo85 | 2:0baf535ed8f8 | 93 | result = int (result - ioperand); |
andrelongo85 | 2:0baf535ed8f8 | 94 | } else if (op == '/') { |
andrelongo85 | 2:0baf535ed8f8 | 95 | result = int (result / ioperand); |
andrelongo85 | 2:0baf535ed8f8 | 96 | } else if (op == '*') { |
andrelongo85 | 2:0baf535ed8f8 | 97 | result = int (result * ioperand); |
andrelongo85 | 3:3bef1c4e42d4 | 98 | } else if (op == '^') { |
andrelongo85 | 3:3bef1c4e42d4 | 99 | if(!neg_operand) { |
andrelongo85 | 3:3bef1c4e42d4 | 100 | result_copy = result; |
andrelongo85 | 3:3bef1c4e42d4 | 101 | for(int i = (abs(ioperand) -1); i>0; i--) { |
andrelongo85 | 3:3bef1c4e42d4 | 102 | result *=result_copy; |
andrelongo85 | 3:3bef1c4e42d4 | 103 | } |
andrelongo85 | 3:3bef1c4e42d4 | 104 | } else { |
andrelongo85 | 3:3bef1c4e42d4 | 105 | pc.printf("ERROR:Negative exponent"); |
andrelongo85 | 3:3bef1c4e42d4 | 106 | error = TRUE; |
andrelongo85 | 3:3bef1c4e42d4 | 107 | } |
andrelongo85 | 2:0baf535ed8f8 | 108 | } |
andrelongo85 | 2:0baf535ed8f8 | 109 | |
andrelongo85 | 2:0baf535ed8f8 | 110 | if (c == '=') { // Operator = has been entered--> Print result and reset calculator |
andrelongo85 | 2:0baf535ed8f8 | 111 | for (int j =9; j>=0; j--) { |
andrelongo85 | 2:0baf535ed8f8 | 112 | buf[j]='\0'; |
andrelongo85 | 2:0baf535ed8f8 | 113 | } |
andrelongo85 | 3:3bef1c4e42d4 | 114 | if(!error) { |
andrelongo85 | 3:3bef1c4e42d4 | 115 | sprintf(buf, "%d", result); |
andrelongo85 | 3:3bef1c4e42d4 | 116 | pc.printf("%s", buf); |
andrelongo85 | 3:3bef1c4e42d4 | 117 | } |
andrelongo85 | 2:0baf535ed8f8 | 118 | pc.printf("\n\n"); |
andrelongo85 | 4:235e50314809 | 119 | pc.printf("Please enter a new operation:\n"); |
andrelongo85 | 2:0baf535ed8f8 | 120 | first = TRUE; |
andrelongo85 | 3:3bef1c4e42d4 | 121 | error = FALSE; |
andrelongo85 | 2:0baf535ed8f8 | 122 | } |
andrelongo85 | 1:247161a5c5d6 | 123 | } |
andrelongo85 | 5:8bb67df511a3 | 124 | op = c; // assign the operator |
andrelongo85 | 2:0baf535ed8f8 | 125 | index=0; // reset the operand |
andrelongo85 | 2:0baf535ed8f8 | 126 | sign_check = TRUE; // sign check activated |
andrelongo85 | 3:3bef1c4e42d4 | 127 | neg_operand=FALSE; |
andrelongo85 | 2:0baf535ed8f8 | 128 | } else { |
andrelongo85 | 2:0baf535ed8f8 | 129 | pc.putc(c); |
andrelongo85 | 2:0baf535ed8f8 | 130 | if (c=='-') { |
andrelongo85 | 2:0baf535ed8f8 | 131 | neg_operand=TRUE; |
andrelongo85 | 2:0baf535ed8f8 | 132 | sign_check = FALSE; |
andrelongo85 | 2:0baf535ed8f8 | 133 | } else if (c=='+') { |
andrelongo85 | 2:0baf535ed8f8 | 134 | neg_operand=FALSE; |
andrelongo85 | 2:0baf535ed8f8 | 135 | sign_check = FALSE; |
andrelongo85 | 3:3bef1c4e42d4 | 136 | } else { |
andrelongo85 | 3:3bef1c4e42d4 | 137 | pc.printf("ERROR:Two consecutive Operators entered"); |
andrelongo85 | 3:3bef1c4e42d4 | 138 | error = TRUE; |
andrelongo85 | 1:247161a5c5d6 | 139 | } |
andrelongo85 | 1:247161a5c5d6 | 140 | } |
andrelongo85 | 1:247161a5c5d6 | 141 | } |
mbed_official | 0:879aa9d0247b | 142 | } |
andrelongo85 | 1:247161a5c5d6 | 143 | } |
andrelongo85 | 1:247161a5c5d6 | 144 | |
andrelongo85 | 5:8bb67df511a3 | 145 | // MAIN |
andrelongo85 | 3:3bef1c4e42d4 | 146 | int main() |
andrelongo85 | 3:3bef1c4e42d4 | 147 | { |
andrelongo85 | 3:3bef1c4e42d4 | 148 | pc.attach(&callback,Serial::RxIrq); |
andrelongo85 | 3:3bef1c4e42d4 | 149 | |
andrelongo85 | 3:3bef1c4e42d4 | 150 | pc.printf("LPC Integer Calculator!\n"); |
andrelongo85 | 4:235e50314809 | 151 | pc.printf("Please enter a new operation:\n"); |
andrelongo85 | 3:3bef1c4e42d4 | 152 | while (1) {} |
andrelongo85 | 3:3bef1c4e42d4 | 153 | } |