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.

Dependencies:   mbed

Fork of Serial_HelloWorld_Mbed by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #define FALSE 0
00004 #define TRUE 1
00005 
00006 Serial pc(USBTX, USBRX); // tx, rx
00007 
00008 // function to convert a char buffer to decimal inetger
00009 int char_to_int (const char *char_p, const int size)
00010 {
00011     int tmp = 0;
00012     int pot_ten=1;
00013     for ( int i= (size -1); i >= 0 ; i-- ) {
00014         tmp = int( tmp + ( ( int(char_p[i]) - 48 ) * pot_ten ));
00015         pot_ten*=10;
00016     }
00017     return tmp;
00018 }
00019 
00020 
00021 // check if the input is valid
00022 bool si_check_valid(const char & input)
00023 {
00024     if ( ( ( int(input) > 57) || ( int(input) < 48) ) && (input != '+') && (input != '-') && (input != '*') && (input != '/') && (input != '^') && (input != '=') ) {
00025         return FALSE;
00026     } else {
00027         return TRUE;
00028     }
00029 }
00030 
00031 //check if the input is a number
00032 bool si_check_number(const char & input)
00033 {
00034     if ( (int(input) >= 48) && ( int(input) <= 57) ) {
00035         return TRUE;
00036     } else {
00037         return FALSE;
00038     }
00039 }
00040 
00041 //global variables
00042 char c;
00043 char op = '=';
00044 char operand [100];
00045 char buf[10];
00046 int result =0;
00047 int result_copy=0;
00048 int index = 0;
00049 int ioperand =0;
00050 bool first = TRUE;
00051 bool sign_check = TRUE;
00052 bool neg_operand = FALSE;
00053 bool error=FALSE;
00054 
00055 // serial interrupt callback function
00056 void callback()
00057 {
00058     while(1) {
00059 
00060         c=pc.getc();  //get serial input
00061 
00062         if (!si_check_valid(c)) { // Invalid Input
00063             pc.putc(c);
00064             printf ("Invalid Input!\n");
00065         } else if (si_check_number(c)) { // Operand has been entered
00066             pc.putc(c);
00067             operand[index] = c;
00068             index++;
00069             sign_check = FALSE;  //sign check disactivated
00070         } else { //Operator has been entered
00071 
00072             if(!sign_check) {
00073                 pc.putc('\n');
00074                 pc.putc(c);
00075                 pc.putc('\n');
00076 
00077                 // sign adjustment
00078                 if(neg_operand) {
00079                     ioperand = -(char_to_int(operand,index));
00080                 } else {
00081                     ioperand = char_to_int(operand,index);
00082                 }
00083 
00084 
00085                 if (first) {  //First operator
00086                     result = ioperand;
00087                     first = FALSE;
00088                 } else { //Do calculation
00089 
00090                     if(op == '+') {
00091                         result = int (result + ioperand);
00092                     } else if (op == '-') {
00093                         result = int (result - ioperand);
00094                     } else if (op  == '/') {
00095                         result = int (result / ioperand);
00096                     } else if (op == '*') {
00097                         result = int (result * ioperand);
00098                     } else if (op == '^') {
00099                         if(!neg_operand) {
00100                             result_copy = result;
00101                             for(int i = (abs(ioperand) -1); i>0; i--) {
00102                                 result *=result_copy;
00103                             }
00104                         } else {
00105                             pc.printf("ERROR:Negative exponent");
00106                             error = TRUE;
00107                         }
00108                     }
00109 
00110                     if (c == '=') { // Operator = has been entered--> Print result and reset calculator
00111                         for (int j =9; j>=0; j--) {
00112                             buf[j]='\0';
00113                         }
00114                         if(!error) {
00115                             sprintf(buf, "%d", result);
00116                             pc.printf("%s", buf);
00117                         }
00118                         pc.printf("\n\n");
00119                         pc.printf("Please enter a new operation:\n");
00120                         first = TRUE;
00121                         error = FALSE;
00122                     }
00123                 }
00124                 op = c;  // assign the operator
00125                 index=0; // reset the operand
00126                 sign_check = TRUE; // sign check activated
00127                 neg_operand=FALSE;
00128             } else {
00129                 pc.putc(c);
00130                 if (c=='-') {
00131                     neg_operand=TRUE;
00132                     sign_check = FALSE;
00133                 } else if (c=='+') {
00134                     neg_operand=FALSE;
00135                     sign_check = FALSE;
00136                 } else {
00137                     pc.printf("ERROR:Two consecutive Operators entered");
00138                     error = TRUE;
00139                 }
00140             }
00141         }
00142     }
00143 }
00144 
00145 // MAIN
00146 int main()
00147 {
00148     pc.attach(&callback,Serial::RxIrq);
00149 
00150     pc.printf("LPC Integer Calculator!\n");
00151     pc.printf("Please enter a new operation:\n");
00152     while (1) {}
00153 }