FRA221_2016 / Mbed 2 deprecated FRA221_P12code1

Dependencies:   TextLCD mbed

Fork of RFID-RC522 by Thomas Kirchner

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MFRC522.h"
00003 #include "TextLCD.h"
00004 #include "string.h"
00005 #define MF_RESET    D8
00006 Serial blue(PC_6, D9);
00007 DigitalOut aaa(D15);
00008 DigitalOut myled(LED1);
00009 AnalogIn x(A0);
00010 AnalogIn y(A1);
00011 AnalogIn z(A2);
00012 DigitalOut LedGreen(LED1);
00013 Serial pc(SERIAL_TX, SERIAL_RX);
00014 MFRC522    RfChip   (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);
00015 TextLCD lcd(D2, D3, D4, D5, D6, D7, TextLCD::LCD20x4); // rs, e, d4-d7
00016 
00017 int card_check = 0;
00018 int i = 0;
00019 int H = 0;
00020 float L = 50;
00021 int card_type = 0;
00022 int arrow = 0;
00023 int a,b,c = 0;
00024 char charID[4];
00025 char indoor[4] = {'O','F','F'};
00026 char ON[4] = {'O','N','\0'};
00027 char OFF[4] = {'O','F','F'};
00028 char HC[4] = {'\0'};
00029 char LC[4] = {'\0'};
00030 int timming = 0;
00031 
00032 void rfid_check()
00033 {
00034 
00035     RfChip.PCD_Init();
00036 
00037     while (card_type == 0) {
00038 
00039 
00040         if ( ! RfChip.PICC_IsNewCardPresent()) {
00041             wait_ms(500);
00042             continue;
00043         }
00044 
00045         if ( ! RfChip.PICC_ReadCardSerial()) {
00046             wait_ms(500);
00047             continue;
00048         }
00049 
00050         for (uint8_t i = RfChip.uid.size; i >= 1; i--) {
00051             sprintf(charID, "%X02", RfChip.uid.uidByte[i]);
00052         }
00053         pc.printf("\n-%s-\n",charID);
00054         if (strcmp(charID,"BF02") == 0) {
00055             lcd.locate(3,2);
00056             lcd.printf("Hello Master!!");
00057             card_type = 1;
00058             blue.printf("i");
00059         }
00060 
00061         else if (strcmp(charID,"5D02") == 0) {
00062             lcd.locate(2,2);
00063             lcd.printf("Hello Light Man!");
00064             card_type = 3;
00065             blue.printf("i");
00066         }
00067 
00068         else if (strcmp(charID,"9B02") == 0) {
00069             lcd.locate(2,2);
00070             lcd.printf("Hello Pump Man!!");
00071             card_type = 2;
00072             blue.printf("i");
00073             
00074         } else /*if (strcmp(charID,"2E02") == 0)*/ {
00075             card_type = 4;
00076         }
00077     }
00078 
00079 }
00080 
00081 void menu()
00082 {
00083     blue.printf("a");
00084     pc.printf("a sended\n");
00085     for (i = 0; i <= 3 ; i++) {
00086         HC[i] = blue.getc();
00087     }
00088     pc.printf("a got\n");
00089     blue.printf("b");
00090     pc.printf("b sended\n");
00091     for (i = 0; i <= 3 ; i++) {
00092         LC[i] = blue.getc();
00093     }
00094     pc.printf("b got\n");
00095     lcd.printf("  HUMIDITY  = %.*s\n",4,HC);
00096     lcd.printf("  LIGHT     = %.*s\n",4,LC);
00097     lcd.printf("  LOG OUT\n\n");
00098     pc.printf("LCD SETING DONE\n");
00099 }
00100 
00101 void bb()
00102 {
00103     while (true)  {
00104         if (x == 1) {
00105             a=1;
00106             while (x >= 0.7) {};
00107             break;
00108         }
00109         if (y == 1) {
00110             b=1;
00111             while (y >= 0.7) {};
00112             break;
00113         }
00114         if (z == 1) {
00115             c=1;
00116             while (z >= 0.7) {};
00117             break;
00118         }
00119     }
00120 }
00121 
00122 void menu_light()
00123 {
00124     lcd.cls();
00125     lcd.printf("     ~~LIGHT~~\n");
00126     lcd.printf("  INDOOR  = %.0f %%\n",L);
00127     lcd.printf("  OUTDOOR = %s\n",indoor);
00128     lcd.printf("  BACK\n");
00129 }
00130 
00131 int main()
00132 {
00133     while (true)     {
00134         for(i=0; i<4; i++) {
00135             charID[i]='\0';
00136         }
00137         timming = 0;
00138         card_type = 0;
00139         arrow = 1;
00140         lcd.cls();
00141         lcd.printf("\n        ^O^         \n\n");
00142         rfid_check();
00143         if (card_type == 4) {
00144             lcd.locate(5,2);
00145             lcd.printf("WHO ARE U ??");
00146             timming = 1;
00147             aaa = 1;
00148             wait_ms(2000);
00149             aaa = 0;
00150         }
00151         wait_ms(2000);
00152         while (timming == 0) {
00153             if (arrow == 1) {
00154                 lcd.cls();
00155                 menu();
00156                 lcd.printf("->");
00157                 bb();
00158                 if (c == 1) {
00159                     arrow = 2;
00160                     c = 0;
00161                 }
00162                 if (b == 1) {
00163                     if(card_type == 2 || card_type == 1) {
00164                         arrow = 4;
00165                     }
00166                     b = 0;
00167                 }
00168             }
00169             if (arrow == 2) {
00170                 lcd.cls();
00171                 menu();
00172                 lcd.locate(0,1);
00173                 lcd.printf("->");
00174                 bb();
00175                 if (a == 1) {
00176                     arrow = 1;
00177                     a = 0;
00178                 }
00179                 if (c == 1) {
00180                     arrow = 3;
00181                     c = 0;
00182                 }
00183                 if (b == 1) {
00184                     if(card_type == 3 || card_type == 1) {
00185                         arrow = 5;
00186                     }
00187                     b = 0;
00188                 }
00189             }
00190             if (arrow == 3) {
00191                 lcd.cls();
00192                 menu();
00193                 lcd.locate(0,2);
00194                 lcd.printf("->");
00195                 bb();
00196                 if (a == 1) {
00197                     arrow = 2;
00198                     a = 0;
00199                 }
00200                 if (b == 1) {
00201                     b = 0;
00202                     blue.printf("o");
00203                     timming = 1;
00204                 }
00205             }
00206             if (arrow == 4) {
00207                 lcd.cls();
00208                 lcd.printf("    ~~HUMIDITY~~\n");
00209                 if (H == 0) {
00210                     lcd.printf("        %s",OFF);
00211                 }
00212                 if (H == 1) {
00213                     lcd.printf("        %s",ON);
00214                 }
00215                 bb();
00216                 if (a == 1) {
00217                     H = 1;
00218                     blue.printf("7");
00219                     a = 0;
00220                 }
00221                 if (c == 1) {
00222                     H = 0;
00223                     blue.printf("4");
00224                     c = 0;
00225                 }
00226                 if (b == 1) {
00227                     arrow = 1;
00228                     b = 0;
00229                 }
00230             }
00231             if (arrow == 5) {
00232                 menu_light();
00233                 lcd.locate(0,1);
00234                 lcd.printf("->");
00235                 bb();
00236                 if (c == 1) {
00237                     arrow = 6;
00238                     c = 0;
00239                 }
00240                 if (b == 1) {
00241                     arrow = 51;
00242                     b = 0;
00243                 }
00244             }
00245             if (arrow == 6) {
00246                 menu_light();
00247                 lcd.locate(0,2);
00248                 lcd.printf("->");
00249                 bb();
00250                 if (a == 1) {
00251                     arrow = 5;
00252                     a = 0;
00253                 }
00254                 if (b == 1) {
00255                     arrow = 52;
00256                     b = 0;
00257                 }
00258                 if (c == 1) {
00259                     arrow = 7;
00260                     c = 0;
00261                 }
00262             }
00263             if (arrow == 7) {
00264                 menu_light();
00265                 lcd.locate(0,3);
00266                 lcd.printf("->");
00267                 bb();
00268                 if (a == 1) {
00269                     arrow = 6;
00270                     a = 0;
00271                 }
00272                 if (b == 1) {
00273                     arrow = 2;
00274                     b = 0;
00275                 }
00276             }
00277             if (arrow == 51) {
00278                 lcd.cls();
00279                 lcd.printf("  ~~INDOOR LIGHT~~\n");
00280                 lcd.printf("        %.0f %%\n\n\n",L);
00281                 bb();
00282                 if (a ==1 ) {
00283                     L = L + 5;
00284                     blue.printf("8");
00285                     a = 0;
00286                 }
00287                 if (c == 1 ) {
00288                     L = L - 5;
00289                     blue.printf("5");
00290                     c = 0;
00291                 }
00292                 if (b == 1 ) {
00293                     arrow = 5;
00294                     b = 0;
00295                 }
00296             }
00297             if (arrow == 52) {
00298                 lcd.cls();
00299                 lcd.printf("  ~~OUTDOOR LIGHT~~\n");
00300                 lcd.printf("       %s\n\n\n",indoor);
00301                 bb();
00302                 if (a ==1 ) {
00303                     strcpy(indoor,ON);
00304                     blue.printf("9");
00305                     a = 0;
00306                 }
00307                 if (c == 1 ) {
00308                     strcpy(indoor,OFF);
00309                     blue.printf("6");
00310                     c = 0;
00311                 }
00312                 if (b == 1 ) {
00313                     arrow = 6;
00314                     b = 0;
00315                 }
00316             }
00317         }
00318         blue.printf("o");
00319     }
00320 }