Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Serial_HelloWorld_Mbed by
main.cpp@4:235e50314809, 2014-09-22 (annotated)
- Committer:
- andrelongo85
- Date:
- Mon Sep 22 06:27:25 2014 +0000
- Revision:
- 4:235e50314809
- Parent:
- 3:3bef1c4e42d4
- Child:
- 5:8bb67df511a3
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 | //pc.printf("\n"); |
| andrelongo85 | 1:247161a5c5d6 | 17 | //print_num(tmp); |
| andrelongo85 | 1:247161a5c5d6 | 18 | //pc.printf("\n"); |
| andrelongo85 | 1:247161a5c5d6 | 19 | //print_num(pot_ten); |
| andrelongo85 | 1:247161a5c5d6 | 20 | } |
| andrelongo85 | 1:247161a5c5d6 | 21 | return tmp; |
| andrelongo85 | 1:247161a5c5d6 | 22 | } |
| andrelongo85 | 1:247161a5c5d6 | 23 | |
| andrelongo85 | 1:247161a5c5d6 | 24 | |
| andrelongo85 | 1:247161a5c5d6 | 25 | // check if the input is valid |
| andrelongo85 | 1:247161a5c5d6 | 26 | bool si_check_valid(const char & input) |
| andrelongo85 | 1:247161a5c5d6 | 27 | { |
| andrelongo85 | 3:3bef1c4e42d4 | 28 | if ( ( ( int(input) > 57) || ( int(input) < 48) ) && (input != '+') && (input != '-') && (input != '*') && (input != '/') && (input != '^') && (input != '=') ) { |
| andrelongo85 | 1:247161a5c5d6 | 29 | return FALSE; |
| andrelongo85 | 1:247161a5c5d6 | 30 | } else { |
| andrelongo85 | 1:247161a5c5d6 | 31 | return TRUE; |
| andrelongo85 | 1:247161a5c5d6 | 32 | } |
| andrelongo85 | 1:247161a5c5d6 | 33 | } |
| andrelongo85 | 1:247161a5c5d6 | 34 | |
| andrelongo85 | 1:247161a5c5d6 | 35 | //check if the input is a number |
| andrelongo85 | 1:247161a5c5d6 | 36 | bool si_check_number(const char & input) |
| andrelongo85 | 1:247161a5c5d6 | 37 | { |
| andrelongo85 | 1:247161a5c5d6 | 38 | if ( (int(input) >= 48) && ( int(input) <= 57) ) { |
| andrelongo85 | 1:247161a5c5d6 | 39 | return TRUE; |
| andrelongo85 | 1:247161a5c5d6 | 40 | } else { |
| andrelongo85 | 1:247161a5c5d6 | 41 | return FALSE; |
| andrelongo85 | 1:247161a5c5d6 | 42 | } |
| andrelongo85 | 1:247161a5c5d6 | 43 | } |
| andrelongo85 | 1:247161a5c5d6 | 44 | |
| andrelongo85 | 3:3bef1c4e42d4 | 45 | char c; |
| andrelongo85 | 3:3bef1c4e42d4 | 46 | char op = '='; |
| andrelongo85 | 3:3bef1c4e42d4 | 47 | char operand [100]; |
| andrelongo85 | 3:3bef1c4e42d4 | 48 | char buf[10]; |
| andrelongo85 | 3:3bef1c4e42d4 | 49 | int result =0; |
| andrelongo85 | 3:3bef1c4e42d4 | 50 | int result_copy=0; |
| andrelongo85 | 3:3bef1c4e42d4 | 51 | int index = 0; |
| andrelongo85 | 3:3bef1c4e42d4 | 52 | int ioperand =0; |
| andrelongo85 | 3:3bef1c4e42d4 | 53 | bool first = TRUE; |
| andrelongo85 | 3:3bef1c4e42d4 | 54 | bool sign_check = TRUE; |
| andrelongo85 | 3:3bef1c4e42d4 | 55 | bool neg_operand = FALSE; |
| andrelongo85 | 3:3bef1c4e42d4 | 56 | bool error=FALSE; |
| andrelongo85 | 2:0baf535ed8f8 | 57 | |
| andrelongo85 | 1:247161a5c5d6 | 58 | |
| andrelongo85 | 3:3bef1c4e42d4 | 59 | // serial interrup callback function |
| andrelongo85 | 3:3bef1c4e42d4 | 60 | void callback() |
| andrelongo85 | 1:247161a5c5d6 | 61 | { |
| mbed_official | 0:879aa9d0247b | 62 | while(1) { |
| andrelongo85 | 2:0baf535ed8f8 | 63 | |
| andrelongo85 | 1:247161a5c5d6 | 64 | c=pc.getc(); //get serial input |
| andrelongo85 | 1:247161a5c5d6 | 65 | |
| andrelongo85 | 1:247161a5c5d6 | 66 | if (!si_check_valid(c)) { // Invalid Input |
| andrelongo85 | 1:247161a5c5d6 | 67 | pc.putc(c); |
| andrelongo85 | 1:247161a5c5d6 | 68 | printf ("Invalid Input!\n"); |
| andrelongo85 | 1:247161a5c5d6 | 69 | } else if (si_check_number(c)) { // Operand has been entered |
| andrelongo85 | 1:247161a5c5d6 | 70 | pc.putc(c); |
| andrelongo85 | 1:247161a5c5d6 | 71 | operand[index] = c; |
| andrelongo85 | 1:247161a5c5d6 | 72 | index++; |
| andrelongo85 | 2:0baf535ed8f8 | 73 | sign_check = FALSE; //sign check disactivated |
| andrelongo85 | 1:247161a5c5d6 | 74 | } else { //Operator has been entered |
| andrelongo85 | 2:0baf535ed8f8 | 75 | |
| andrelongo85 | 2:0baf535ed8f8 | 76 | if(!sign_check) { |
| andrelongo85 | 2:0baf535ed8f8 | 77 | pc.putc('\n'); |
| andrelongo85 | 2:0baf535ed8f8 | 78 | pc.putc(c); |
| andrelongo85 | 2:0baf535ed8f8 | 79 | pc.putc('\n'); |
| andrelongo85 | 1:247161a5c5d6 | 80 | |
| andrelongo85 | 2:0baf535ed8f8 | 81 | // sign adjustment |
| andrelongo85 | 2:0baf535ed8f8 | 82 | if(neg_operand) { |
| andrelongo85 | 2:0baf535ed8f8 | 83 | ioperand = -(char_to_int(operand,index)); |
| andrelongo85 | 2:0baf535ed8f8 | 84 | } else { |
| andrelongo85 | 2:0baf535ed8f8 | 85 | ioperand = char_to_int(operand,index); |
| andrelongo85 | 2:0baf535ed8f8 | 86 | } |
| andrelongo85 | 3:3bef1c4e42d4 | 87 | |
| andrelongo85 | 3:3bef1c4e42d4 | 88 | |
| andrelongo85 | 2:0baf535ed8f8 | 89 | if (first) { //First Operator |
| andrelongo85 | 2:0baf535ed8f8 | 90 | result = ioperand; |
| andrelongo85 | 2:0baf535ed8f8 | 91 | first = FALSE; |
| andrelongo85 | 2:0baf535ed8f8 | 92 | } else { //Do Calculation |
| andrelongo85 | 1:247161a5c5d6 | 93 | |
| andrelongo85 | 2:0baf535ed8f8 | 94 | 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 | 2:0baf535ed8f8 | 98 | } else if (op == '/') { |
| andrelongo85 | 2:0baf535ed8f8 | 99 | result = int (result / ioperand); |
| andrelongo85 | 2:0baf535ed8f8 | 100 | } else if (op == '*') { |
| andrelongo85 | 2:0baf535ed8f8 | 101 | result = int (result * ioperand); |
| andrelongo85 | 3:3bef1c4e42d4 | 102 | } else if (op == '^') { |
| andrelongo85 | 3:3bef1c4e42d4 | 103 | if(!neg_operand) { |
| andrelongo85 | 3:3bef1c4e42d4 | 104 | result_copy = result; |
| andrelongo85 | 3:3bef1c4e42d4 | 105 | for(int i = (abs(ioperand) -1); i>0; i--) { |
| andrelongo85 | 3:3bef1c4e42d4 | 106 | result *=result_copy; |
| andrelongo85 | 3:3bef1c4e42d4 | 107 | } |
| andrelongo85 | 3:3bef1c4e42d4 | 108 | } else { |
| andrelongo85 | 3:3bef1c4e42d4 | 109 | pc.printf("ERROR:Negative exponent"); |
| andrelongo85 | 3:3bef1c4e42d4 | 110 | error = TRUE; |
| andrelongo85 | 3:3bef1c4e42d4 | 111 | } |
| andrelongo85 | 2:0baf535ed8f8 | 112 | } |
| andrelongo85 | 2:0baf535ed8f8 | 113 | |
| andrelongo85 | 2:0baf535ed8f8 | 114 | if (c == '=') { // Operator = has been entered--> Print result and reset calculator |
| andrelongo85 | 2:0baf535ed8f8 | 115 | for (int j =9; j>=0; j--) { |
| andrelongo85 | 2:0baf535ed8f8 | 116 | buf[j]='\0'; |
| andrelongo85 | 2:0baf535ed8f8 | 117 | } |
| andrelongo85 | 3:3bef1c4e42d4 | 118 | if(!error) { |
| andrelongo85 | 3:3bef1c4e42d4 | 119 | sprintf(buf, "%d", result); |
| andrelongo85 | 3:3bef1c4e42d4 | 120 | pc.printf("%s", buf); |
| andrelongo85 | 3:3bef1c4e42d4 | 121 | } |
| andrelongo85 | 2:0baf535ed8f8 | 122 | pc.printf("\n\n"); |
| andrelongo85 | 4:235e50314809 | 123 | pc.printf("Please enter a new operation:\n"); |
| andrelongo85 | 2:0baf535ed8f8 | 124 | first = TRUE; |
| andrelongo85 | 3:3bef1c4e42d4 | 125 | error = FALSE; |
| andrelongo85 | 2:0baf535ed8f8 | 126 | } |
| andrelongo85 | 1:247161a5c5d6 | 127 | } |
| andrelongo85 | 2:0baf535ed8f8 | 128 | op = c; // assign the Operator |
| andrelongo85 | 2:0baf535ed8f8 | 129 | index=0; // reset the operand |
| andrelongo85 | 2:0baf535ed8f8 | 130 | sign_check = TRUE; // sign check activated |
| andrelongo85 | 3:3bef1c4e42d4 | 131 | neg_operand=FALSE; |
| andrelongo85 | 2:0baf535ed8f8 | 132 | } else { |
| andrelongo85 | 2:0baf535ed8f8 | 133 | pc.putc(c); |
| andrelongo85 | 2:0baf535ed8f8 | 134 | if (c=='-') { |
| andrelongo85 | 2:0baf535ed8f8 | 135 | neg_operand=TRUE; |
| andrelongo85 | 2:0baf535ed8f8 | 136 | sign_check = FALSE; |
| andrelongo85 | 2:0baf535ed8f8 | 137 | } else if (c=='+') { |
| andrelongo85 | 2:0baf535ed8f8 | 138 | neg_operand=FALSE; |
| andrelongo85 | 2:0baf535ed8f8 | 139 | sign_check = FALSE; |
| andrelongo85 | 3:3bef1c4e42d4 | 140 | } else { |
| andrelongo85 | 3:3bef1c4e42d4 | 141 | pc.printf("ERROR:Two consecutive Operators entered"); |
| andrelongo85 | 3:3bef1c4e42d4 | 142 | error = TRUE; |
| andrelongo85 | 1:247161a5c5d6 | 143 | } |
| andrelongo85 | 1:247161a5c5d6 | 144 | } |
| andrelongo85 | 1:247161a5c5d6 | 145 | } |
| mbed_official | 0:879aa9d0247b | 146 | } |
| andrelongo85 | 1:247161a5c5d6 | 147 | } |
| andrelongo85 | 1:247161a5c5d6 | 148 | |
| andrelongo85 | 3:3bef1c4e42d4 | 149 | int main() |
| andrelongo85 | 3:3bef1c4e42d4 | 150 | { |
| andrelongo85 | 3:3bef1c4e42d4 | 151 | pc.attach(&callback,Serial::RxIrq); |
| andrelongo85 | 3:3bef1c4e42d4 | 152 | |
| andrelongo85 | 3:3bef1c4e42d4 | 153 | pc.printf("LPC Integer Calculator!\n"); |
| andrelongo85 | 4:235e50314809 | 154 | pc.printf("Please enter a new operation:\n"); |
| andrelongo85 | 3:3bef1c4e42d4 | 155 | while (1) {} |
| andrelongo85 | 3:3bef1c4e42d4 | 156 | } |
