ECE 4180 Lab 04 - Jung Hun Kim

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "uLCD_4DGL.h"
00003 #include <iostream>
00004 #include <math.h>
00005 #include <cmath>
00006 #include <string>
00007 #include <cstring>
00008 #include <list>
00009 #include <vector>
00010 #include <mpr121.h>
00011 Serial pc(USBTX, USBRX);
00012 using namespace std;
00013 //
00014 uLCD_4DGL uLCD(p13, p14, p11);
00015 InterruptIn interrupt(p26);
00016 InterruptIn interrupt2(p25);
00017 I2C i2c(p9, p10);
00018 I2C i2c2(p28, p27);
00019 //
00020 Mpr121 numpad(&i2c, Mpr121::ADD_VSS);
00021 Mpr121 operpad(&i2c2, Mpr121::ADD_VSS);
00022 
00023 int fallInterrupt() 
00024 {
00025      int num = numpad.read(0x00);
00026      num +=numpad.read(0x01)<<8;
00027      int i=0;
00028      i = 0;
00029      for (i=0; i<12; i++) {
00030         if (num & (1<<i))
00031         break;
00032      }
00033      if(i == 0) {
00034         uLCD.text_width(2); //4X size text
00035         uLCD.text_height(2);
00036         uLCD.color(BLUE);
00037         uLCD.printf("%d", i);
00038      }
00039      else if (i == 1) {
00040         uLCD.text_width(2); //4X size text
00041         uLCD.text_height(2);
00042         uLCD.color(BLUE);
00043         uLCD.printf("%d", i);
00044      }
00045      else if (i == 2) {
00046         uLCD.text_width(2); //4X size text
00047         uLCD.text_height(2);
00048         uLCD.color(BLUE);
00049         uLCD.printf("%d", i);
00050          }
00051      else if (i == 3) {
00052         uLCD.text_width(2); //4X size text
00053         uLCD.text_height(2);
00054         uLCD.color(BLUE);
00055         uLCD.printf("%d", i);
00056          }
00057      else if (i == 4) {
00058         uLCD.text_width(2); //4X size text
00059         uLCD.text_height(2);
00060         uLCD.color(BLUE);
00061         uLCD.printf("%d", i);
00062          }
00063      else if (i == 5) {
00064         uLCD.text_width(2); //4X size text
00065         uLCD.text_height(2);
00066         uLCD.color(BLUE);
00067         uLCD.printf("%d", i);
00068          }
00069      else if (i == 6) {
00070         uLCD.text_width(2); //4X size text
00071         uLCD.text_height(2);
00072         uLCD.color(BLUE);
00073         uLCD.printf("%d", i);
00074          }
00075      else if (i == 7) {
00076         uLCD.text_width(2); //4X size text
00077         uLCD.text_height(2);
00078         uLCD.color(BLUE);
00079         uLCD.printf("%d", i);
00080          }
00081      else if (i == 8) {
00082         uLCD.text_width(2); //4X size text
00083         uLCD.text_height(2);
00084         uLCD.color(BLUE);
00085         uLCD.printf("%d", i);
00086          }
00087      else if (i == 9) {
00088         uLCD.text_width(2); //4X size text
00089         uLCD.text_height(2);
00090         uLCD.color(BLUE);
00091         uLCD.printf("%d", i);
00092          }
00093      else if (i == 10) {
00094         uLCD.text_width(2); //4X size text
00095         uLCD.text_height(2);
00096         uLCD.color(BLUE);
00097         uLCD.printf(".");
00098          }
00099      else if (i == 11) {
00100         uLCD.text_width(2); //4X size text
00101         uLCD.text_height(2);
00102         uLCD.color(BLUE);
00103         uLCD.printf("-");
00104          }
00105 //        pc.printf("%d", i);
00106     return i;
00107 }
00108 
00109 int fallInterrupt2() 
00110 {
00111      int num = operpad.read(0x00);
00112      num +=operpad.read(0x01)<<8;
00113      int i =0;
00114      i = 0;
00115      for (i=0; i<12; i++) {
00116         if (num & (1<<i))
00117         break;
00118      }
00119      if(i == 1) {
00120         uLCD.printf("+");
00121      }
00122      else if (i == 2) {
00123         uLCD.printf("-");
00124      }
00125      else if (i == 3) {
00126          uLCD.printf("*");
00127          }
00128      else if (i == 4) {
00129          uLCD.printf("/");
00130     }
00131 //    else if (i == 4) {
00132 //         uLCD.printf("/");
00133 //         }
00134      else if (i == 5) {
00135          uLCD.color(RED);
00136          uLCD.printf("\n=");
00137      }
00138      else if (i == 6) {
00139          uLCD.cls();
00140     }
00141     return i;
00142 }
00143 
00144 int main() {
00145     while (1) {
00146 //  C;
00147 //  interrupt2.fall(&fallInterrupt2);
00148 //  interrupt.fall(&fallInterrupt);
00149 //  interrupt.mode(PullUp);
00150 //  interrupt2.mode(PullUp);
00151   float temp = 0;
00152   float temp1 = 0;
00153   int temp2 = 0;
00154   float temp3 = 0;
00155   int temp4 = 0;
00156   float temp5 = 0;
00157   int temp6 = 0;
00158   int count = 1;
00159   int count2 = 1;
00160   int count3 = 1;
00161   int fans = 10;
00162   int fans2 = 10;
00163   int fans3 = 10;
00164   int negate = 0;
00165   while (1) {
00166          int oper1 = operpad.read(0x00);
00167          oper1 +=operpad.read(0x01)<<8;
00168          int num1 = numpad.read(0x00);
00169          num1 +=numpad.read(0x01)<<8;
00170 //         temp2 = fallInterrupt2();
00171 //         wait(0.1);
00172          if (oper1){
00173 //             wait(0.3);
00174             int ope = fallInterrupt2();
00175             wait(0.2);
00176             if (ope == 0) {
00177                 uLCD.cls();
00178                 uLCD.printf("Cleared");
00179                 wait(0.5);
00180                 uLCD.cls();
00181                 count = 1;
00182                 temp1 = 0;
00183                 temp2 = 0;
00184                 fans = 10;
00185             }
00186             else{
00187                 temp2 = ope;
00188                 break;
00189              }
00190          }
00191          else if (num1){
00192                 int ans = fallInterrupt();
00193                 wait(0.2);
00194                 if (ans == 10) {
00195                     count = count +1;
00196 //                    pc.printf("hi I am in ans==10");
00197 //                    pc.printf("hi count from ans ==10 is %d\n", count);
00198                     }
00199                 else if (ans == 11) {
00200                     negate = 1;
00201                     }
00202                 else {
00203                     if (count == 2) {
00204                         if (negate == 1){
00205 //                        pc.printf("ans = %d\n", ans);
00206 //                        pc.printf("count = %d\n", count);
00207 //                        pc.printf("temp1 before = %0.2f\n", temp1);
00208                             temp1 = temp1 *10;
00209                             temp1 = (temp1 - ans)/10;
00210                             temp1 = temp1;
00211 //                        pc.printf("temp1 after = %0.2f\n", temp1);
00212                              count = count +1;
00213                             fans = fans * 10;
00214                         }
00215                         else{
00216                             temp1 = temp1 *10;
00217                             temp1 = (temp1 + ans)/10;
00218 //                        pc.printf("temp1 after = %0.2f\n", temp1);
00219                             count = count +1;
00220                             fans = fans * 10;
00221                         }
00222                     }
00223 //                        wait(0.3);
00224                     else if (count > 2) {
00225                         if (negate == 1){
00226                             temp1 = temp1 * fans;
00227                             temp1 = temp1 - ans;
00228 //                        pc.printf("ans = %d\n", ans);
00229                             temp1 = temp1/fans;
00230                             temp1 = temp1;
00231 //                        pc.printf("temp1 after = %0.2f\n", temp1);
00232                             fans = fans * 10;
00233                         }
00234                         else{
00235                             temp1 = temp1 * fans;
00236                             temp1 = temp1 + ans;
00237 //                        pc.printf("ans = %d\n", ans);
00238                             temp1 = temp1/fans;
00239 //                        pc.printf("temp1 after = %0.2f\n", temp1);
00240                             fans = fans * 10;
00241                         }    
00242                     }
00243                     else if(count==1){
00244                         if (negate == 1){
00245                             temp1 = temp1 * 10;
00246                             temp1 = temp1 - ans;
00247                             temp1 = temp1;
00248                         pc.printf("temp1 = %d\n", temp1);
00249                         pc.printf("count = %d\n", count);
00250                         }
00251                         else {
00252                             temp1 = temp1 * 10;
00253                             temp1 = temp1 + ans;
00254                         }
00255                    }
00256 //                    wait(0.3);
00257                 }
00258         wait(0.2);
00259         }
00260     }
00261     while (1) {
00262          int oper2 = operpad.read(0x00);
00263          oper2 +=operpad.read(0x01)<<8;
00264          int num2 = numpad.read(0x00);
00265          num2 +=numpad.read(0x01)<<8;
00266 //         temp4 = fallInterrupt2();
00267 //         wait(0.1);
00268          if (oper2){
00269 //             wait(0.3);
00270             int ope = fallInterrupt2();
00271             wait(0.2);
00272             if (ope == 0) {
00273                 uLCD.cls();
00274                 uLCD.printf("Cleared");
00275                 wait(0.5);
00276                 uLCD.cls();
00277                 count2 = 1;
00278                 temp3 = 0;
00279                 temp4 = 0;
00280                 fans2 = 10;
00281             }
00282             else{
00283                 temp4 = ope;
00284                 break;
00285              }
00286          }
00287          else if (num2){
00288                 int ans = fallInterrupt();
00289                 wait(0.2);
00290                 if (ans == 10) {
00291                     count2 = count2 +1;
00292 //                    pc.printf("hi I am in ans==10");
00293 //                    pc.printf("hi count from ans ==10 is %d\n", count);
00294                     }
00295                 else {
00296                     if (count2 == 2) {
00297 //                        pc.printf("ans = %d\n", ans);
00298 //                        pc.printf("count = %d\n", count2);
00299 //                        pc.printf("temp1 before = %0.2f\n", temp3);
00300                         temp3 = temp3 *10;
00301                         temp3 = (temp3+ans)/10;
00302 //                        pc.printf("temp1 after = %0.2f\n", temp3);
00303                         count2 = count2 +1;
00304                         fans2 = fans2 * 10;
00305                         }
00306 //                        wait(0.3);
00307                     else if (count2 > 2) {
00308                         temp3 = temp3 * fans2;
00309                         temp3 = temp3 + ans;
00310 //                        pc.printf("ans = %d\n", ans);
00311                         temp3 = temp3/fans2;
00312 //                        pc.printf("temp1 after = %0.2f\n", temp1);
00313                         fans2 = fans2 * 10;
00314                         }
00315                     else if(count2==1){
00316                         temp3 = temp3 * 10;
00317                         temp3 = temp3 + ans;
00318 //                        pc.printf("temp1 = %d\n", temp1);
00319 //                        pc.printf("count = %d\n", count);
00320                         }
00321                     }
00322 //                    wait(0.3);
00323                 }
00324         wait(0.2);
00325        }
00326     while (1) {
00327         int oper3 = operpad.read(0x00);
00328          oper3 +=operpad.read(0x01)<<8;
00329          int num3 = numpad.read(0x00);
00330          num3 +=numpad.read(0x01)<<8;
00331 //         temp6 = fallInterrupt2();
00332 //         wait(0.1);
00333          if (oper3){
00334 //             wait(0.3);
00335             int ope = fallInterrupt2();
00336             wait(0.2);
00337             if (ope == 0) {
00338                 uLCD.cls();
00339                 uLCD.printf("Cleared");
00340                 wait(0.5);
00341                 uLCD.cls();
00342                 count3 = 1;
00343                 temp5 = 0;
00344                 temp6 = 0;
00345                 fans3 = 10;
00346             }
00347             else{
00348                 temp6 = ope;
00349                 break;
00350              }
00351          }
00352          else if (num3){
00353                 int ans = fallInterrupt();
00354                 wait(0.2);
00355                 if (ans == 10) {
00356                     count3 = count3 +1;
00357 //                    pc.printf("hi I am in ans==10");
00358 //                    pc.printf("hi count from ans ==10 is %d\n", count);
00359                     }
00360                 else {
00361                     if (count3 == 2) {
00362 //                        pc.printf("ans = %d\n", ans);
00363 //                        pc.printf("count = %d\n", count3);
00364 //                        pc.printf("temp1 before = %0.2f\n", temp5);
00365                         temp5 = temp5 *10;
00366                         temp5 = (temp5+ans)/10;
00367 //                        pc.printf("temp1 after = %0.2f\n", temp5);
00368                         count3 = count3 +1;
00369                         }
00370 //                        wait(0.3);
00371                     else if (count3 > 2) {
00372                         temp5 = temp5 * fans3;
00373                         temp5 = temp5 + ans;
00374 //                        pc.printf("ans = %d\n", ans);
00375                         temp5 = temp5/fans3;
00376 //                        pc.printf("temp1 after = %0.2f\n", temp1);
00377                         fans3 = fans3 * 10;
00378                         }
00379                     else if(count3==1){
00380                         temp5 = temp5 * 10;
00381                         temp5 = temp5 + ans;
00382 //                        pc.printf("temp1 = %d\n", temp1);
00383 //                        pc.printf("count = %d\n", count);
00384                         }
00385                     }
00386 //                    wait(0.3);
00387                 }
00388         wait(0.2);
00389        }
00390        if ((temp2 == 1) && (temp4 == 1)){
00391                 temp = temp1 + temp3 + temp5;
00392                 pc.printf("temp is %0.2f\n", temp);
00393            }
00394        else if ((temp2 == 1) && (temp4 == 2)){
00395                 temp = temp1 + temp3 - temp5;
00396            }
00397        else if ((temp2 == 1) && (temp4 == 3)){
00398                 temp = temp1 + (temp3 * temp5);
00399            }
00400        else if ((temp2 == 1) && (temp4 == 4)){
00401                 temp = temp1 + (temp3 / temp5);
00402            }
00403        else if ((temp2 == 2) && (temp4 == 1)){
00404                 temp = temp1 - temp3 + temp5;
00405         }
00406        else if ((temp2 == 2) && (temp4 == 2)){
00407                 temp = temp1 - temp3 - temp5;
00408         }
00409        else if ((temp2 == 2) && (temp4 == 3)){
00410                 temp = temp1 - (temp3 * temp5);
00411            }
00412        else if ((temp2 == 2) && (temp4 == 4)){
00413                 temp = temp1 - (temp3 / temp5);
00414            }
00415        else if ((temp2 == 3) && (temp4 == 1)){
00416                 temp = temp1 * temp3 + temp5;
00417         }
00418        else if ((temp2 == 3) && (temp4 == 2)){
00419                 temp = temp1 * temp3 - temp5;
00420         }
00421        else if ((temp2 == 3) && (temp4 == 3)){
00422                 temp = temp1 * temp3 * temp5;
00423         }
00424        else if ((temp2 == 3) && (temp4 == 4)){
00425                 temp = temp1 * temp3 / temp5;
00426         }
00427        else if ((temp2 == 4) && (temp4 == 1)){
00428                 temp = temp1 / temp3 + temp5;
00429         }
00430        else if ((temp2 == 4) && (temp4 == 2)){
00431                 temp = temp1 / temp3 - temp5;
00432         }
00433        else if ((temp2 == 4) && (temp4 == 3)){
00434                 temp = temp1 / temp3 * temp5;
00435         }
00436        else if ((temp2 == 4) && (temp4 == 4)){
00437                 temp = temp1 / temp3 / temp5;
00438         }
00439         else{
00440             uLCD.printf("Error");
00441             }
00442         
00443  uLCD.color(RED);
00444  uLCD.printf("%0.2f\n", temp);
00445 
00446   }
00447   }