escaner RF - RTOS multitareas

Dependencies:   mbed Servo MFRC522_Updated HC-SR04 ssd1306_library

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "Servo.h"
00004 #include "HCSR04.h"
00005 #include "MFRC522.h"
00006 #include "ssd1306.h"
00007 
00008 MFRC522    RfChip   (D11, D12, D13, PTE0, D10);
00009 SSD1306 lcd (D7,D6);
00010 BusOut display7Seg(PTC10, PTC7, PTC6, PTC4, PTC0, PTB19, PTB18);
00011 DigitalOut display1(PTC11);
00012 DigitalOut display2(PTC13);
00013 DigitalOut display3(PTC16);
00014 Servo myservo(D2);
00015 
00016 Thread t;
00017 Thread t1;
00018 Thread t2;
00019 Thread t3;
00020 Thread t4;
00021 
00022 Serial pc(USBTX, USBRX);
00023 HCSR04 sonar(D3,D4);
00024 
00025 float menor;
00026 float da;
00027 float dis = 0.0;
00028 static const char tar[]={0xD7,0x4F,0x88,0x19};
00029 static const char llav[]={0x27,0x57,0xC1,0x7B};
00030 int q,a,al,b,w;
00031 
00032 
00033 int anodeComun[10] = {
00034     0b0000001, // 0
00035     0b1001111, // 1
00036     0b0010010, // 2
00037     0b0000110, // 3
00038     0b1001100, // 4
00039     0b0100100, // 5
00040     0b0100000, // 6
00041     0b0001111, // 7
00042     0b0000000, // 8
00043     0b0001100, // 9
00044 };
00045 
00046 void  servo()
00047 {
00048 
00049     while(true) {
00050         menor = sonar.getCm();
00051         for(int i=0; i<40; i++) {
00052             myservo = i/40.0;
00053             da  = sonar.getCm();
00054             w=i;
00055             if(menor > da) {
00056                 menor = da;
00057                 a=(i*180)/40;
00058             }
00059            Thread::wait(50);
00060         }
00061         menor=sonar.getCm();
00062         a=0;
00063         for(int i=40; i>0; i--) {
00064             myservo = i/40.0;
00065             da = sonar.getCm();
00066               w=i;
00067             if(menor > da) {
00068                 menor = da;
00069                 a=(i*180)/40;
00070             }
00071             Thread::wait(50);
00072         }
00073     }
00074 }
00075 void seg7()
00076 {
00077     while(1) {
00078         int d,u,n,c;
00079         n=int(menor);
00080 
00081         c=  n/100;
00082         d = (n - (c*100))/10;
00083         u = n % 10;
00084 
00085         display1=1;
00086         display2=0;
00087         display3=0;
00088         display7Seg=anodeComun[d];
00089         
00090         Thread::wait(3);
00091 
00092         display1=0;
00093         display2=1;
00094         display3=0;
00095         display7Seg=anodeComun[u];
00096 
00097         Thread::wait(3);
00098 
00099         display1=0;
00100         display2=0;
00101         display3=1;
00102         display7Seg=anodeComun[c];
00103 
00104         Thread::wait(3);
00105 
00106 
00107     }
00108 }
00109 void oled()
00110 {
00111     while(1) {
00112         lcd.init();
00113         lcd.speed (SSD1306::Fast);
00114         lcd.cls();
00115         lcd.locate (4,1);
00116         lcd.printf ("dis menor :%2.f ",menor);
00117         lcd.locate (5,1);
00118         lcd.printf ("angulo :%d ",a);
00119         lcd.locate (1,0);
00120         time_t seconds = time(NULL);
00121         lcd.printf("Fecha= %s", ctime(&seconds));
00122         lcd.redraw();
00123         
00124         Thread::wait(500);
00125     }
00126 }
00127 void rf()
00128 {
00129     while(1) {
00130         al=0;
00131         b=0;
00132         lcd.init();
00133         lcd.speed (SSD1306::Fast);
00134         if ( ! RfChip.PICC_IsNewCardPresent()) {
00135             
00136             Thread::wait(500);
00137             continue;
00138         }
00139         if ( ! RfChip.PICC_ReadCardSerial()) {
00140          
00141             Thread::wait(500);
00142             continue;
00143         }
00144         uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
00145         lcd.locate (6,3);
00146         lcd.printf(" %s ", RfChip.PICC_GetTypeName(piccType));
00147         lcd.redraw();
00148          for (uint8_t i = 0; i < RfChip.uid.size; i++) {
00149             
00150          if(uint8_t (tar[i])==RfChip.uid.uidByte[i]){
00151             al=al+1;
00152             }else{
00153                 al=0;
00154                 }
00155         }
00156          for (uint8_t i = 0; i < RfChip.uid.size; i++) {
00157             
00158          if(uint8_t (llav[i])==RfChip.uid.uidByte[i]){
00159             b=b+1;
00160             }else{
00161                 b=0;
00162                 }
00163         }
00164      
00165         //ThisThread::sleep_for(1000);
00166         Thread::wait(1000);
00167     }
00168 }
00169 void serial()
00170 {
00171     while(1) {
00172 
00173         time_t seconds = time(NULL);
00174 
00175         char buffer2[32];
00176         strftime(buffer2, 32, "%m/%d/%y", localtime(&seconds));
00177         pc.printf("%s", buffer2);
00178 
00179         char buffer1[32];
00180         strftime(buffer1, 32, "%H:%M:%S", localtime(&seconds));
00181         pc.printf(" , %s", buffer1);
00182         pc.printf(" , %1.f  ,  %.2f , ",float(w*4.5),da);
00183            if(al==4){
00184         pc.printf("tarjeta");  
00185             }else if(b==4){ 
00186                 pc.printf("llavero");
00187                 }else{
00188                    pc.printf("sin lectura"); 
00189                     }  
00190         pc.printf("\n\r");
00191         Thread::wait(40);
00192         
00193     }
00194 }
00195 int main()
00196 {
00197     myservo.calibrate(0.0015,180);
00198     set_time(1591673863);
00199     RfChip.PCD_Init();
00200     t.start((servo));
00201     t.set_priority (osPriorityNormal3);
00202     t1.start(seg7);
00203     t1.set_priority (osPriorityNormal);
00204     t2.start(oled);
00205     t2.set_priority (osPriorityNormal1);
00206     t3.start(serial);
00207     t2.set_priority (osPriorityNormal2);
00208     t4.start(rf);
00209     t.join();
00210 }