libreria hc12

Dependents:   recepcion

Committer:
guidoglorioso
Date:
Sat Oct 05 01:27:55 2019 +0000
Revision:
0:ac54225f5fcd
programa trama proyecto

Who changed what in which revision?

UserRevisionLine numberNew contents of line
guidoglorioso 0:ac54225f5fcd 1 #include "mbed.h"
guidoglorioso 0:ac54225f5fcd 2 #include "Checksum.h"
guidoglorioso 0:ac54225f5fcd 3 #include "hc12.h"
guidoglorioso 0:ac54225f5fcd 4
guidoglorioso 0:ac54225f5fcd 5
guidoglorioso 0:ac54225f5fcd 6 hc12::hc12()
guidoglorioso 0:ac54225f5fcd 7 {
guidoglorioso 0:ac54225f5fcd 8 RawSerial MODULOHC(PTE0,PTE1);
guidoglorioso 0:ac54225f5fcd 9 MODULOHC.attach(this,&hc12::llegan_datos);
guidoglorioso 0:ac54225f5fcd 10 vector_envio[0]='<';
guidoglorioso 0:ac54225f5fcd 11 vector_envio[1]='V';
guidoglorioso 0:ac54225f5fcd 12 vector_envio[3]='A';
guidoglorioso 0:ac54225f5fcd 13 vector_envio[5]='F';
guidoglorioso 0:ac54225f5fcd 14 vector_envio[7]='C';
guidoglorioso 0:ac54225f5fcd 15 vector_envio[10]='>';
guidoglorioso 0:ac54225f5fcd 16 datos=CASO_1;
guidoglorioso 0:ac54225f5fcd 17
guidoglorioso 0:ac54225f5fcd 18 tramamal=0;
guidoglorioso 0:ac54225f5fcd 19 llegomal=0;
guidoglorioso 0:ac54225f5fcd 20 llegobien=0;
guidoglorioso 0:ac54225f5fcd 21 newdata = 0;
guidoglorioso 0:ac54225f5fcd 22 }
guidoglorioso 0:ac54225f5fcd 23
guidoglorioso 0:ac54225f5fcd 24 void hc12::update_parameters(char speed,char dist,char ejex,char ejey,char bateria)
guidoglorioso 0:ac54225f5fcd 25 {
guidoglorioso 0:ac54225f5fcd 26 vector_envio[2]=speed;
guidoglorioso 0:ac54225f5fcd 27 vector_envio[4]=dist; //en mm
guidoglorioso 0:ac54225f5fcd 28 vector_envio[6]=ejex;
guidoglorioso 0:ac54225f5fcd 29 vector_envio[7]=ejey;
guidoglorioso 0:ac54225f5fcd 30 vector_envio[9]=bat;//porcetaje
guidoglorioso 0:ac54225f5fcd 31 vector_envio[11]=checksum.get_xor(vector_envio, 11);
guidoglorioso 0:ac54225f5fcd 32 MODULOHC.printf("<S%dD%dE%d%dB%d%d>",speed,dist,ejex,ejey,bat,vector_envio[11]);
guidoglorioso 0:ac54225f5fcd 33 NoLlegaNada.attach(this,&hc12::noentrandatos,0.1);
guidoglorioso 0:ac54225f5fcd 34
guidoglorioso 0:ac54225f5fcd 35 }
guidoglorioso 0:ac54225f5fcd 36
guidoglorioso 0:ac54225f5fcd 37
guidoglorioso 0:ac54225f5fcd 38 void hc12::llegan_datos()
guidoglorioso 0:ac54225f5fcd 39 {
guidoglorioso 0:ac54225f5fcd 40 NoLlegaNada.detach();
guidoglorioso 0:ac54225f5fcd 41 c = MODULOHC.getc();
guidoglorioso 0:ac54225f5fcd 42 newdata = 1;
guidoglorioso 0:ac54225f5fcd 43 maquina_datos();
guidoglorioso 0:ac54225f5fcd 44 if(llegomal!=0) {
guidoglorioso 0:ac54225f5fcd 45 tramamal++; //PONER QUE SI SE HACE CONSTANTE QUE LAS TRAMAS LLEGUEN MAL AVISE
guidoglorioso 0:ac54225f5fcd 46 }
guidoglorioso 0:ac54225f5fcd 47 }
guidoglorioso 0:ac54225f5fcd 48
guidoglorioso 0:ac54225f5fcd 49 void hc12::maquina_datos()
guidoglorioso 0:ac54225f5fcd 50 {
guidoglorioso 0:ac54225f5fcd 51 switch (datos) {
guidoglorioso 0:ac54225f5fcd 52 case CASO_1:
guidoglorioso 0:ac54225f5fcd 53 if(newdata==1 && c=='<') {
guidoglorioso 0:ac54225f5fcd 54 newdata=0;
guidoglorioso 0:ac54225f5fcd 55 vector_datos[0]='<';
guidoglorioso 0:ac54225f5fcd 56 datos=CASO_S;
guidoglorioso 0:ac54225f5fcd 57 } else if(newdata==1 && c!='<') {//poner para que termine
guidoglorioso 0:ac54225f5fcd 58 llegomal++;
guidoglorioso 0:ac54225f5fcd 59 }
guidoglorioso 0:ac54225f5fcd 60 break;
guidoglorioso 0:ac54225f5fcd 61
guidoglorioso 0:ac54225f5fcd 62 case CASO_S:
guidoglorioso 0:ac54225f5fcd 63 if(newdata==1 && c=='V') {
guidoglorioso 0:ac54225f5fcd 64 vector_datos[1]='V';
guidoglorioso 0:ac54225f5fcd 65 datos=CASO_SA;
guidoglorioso 0:ac54225f5fcd 66 newdata=0;
guidoglorioso 0:ac54225f5fcd 67 } else if(newdata==1 && c!='V') {
guidoglorioso 0:ac54225f5fcd 68 llegomal++;
guidoglorioso 0:ac54225f5fcd 69 datos=CASO_1;
guidoglorioso 0:ac54225f5fcd 70 }
guidoglorioso 0:ac54225f5fcd 71 break;
guidoglorioso 0:ac54225f5fcd 72
guidoglorioso 0:ac54225f5fcd 73 case CASO_SA:
guidoglorioso 0:ac54225f5fcd 74 if(newdata==1) {
guidoglorioso 0:ac54225f5fcd 75 vector_datos[2]=c;
guidoglorioso 0:ac54225f5fcd 76 datos=CASO_D;
guidoglorioso 0:ac54225f5fcd 77 newdata=0;
guidoglorioso 0:ac54225f5fcd 78 }
guidoglorioso 0:ac54225f5fcd 79 break;
guidoglorioso 0:ac54225f5fcd 80
guidoglorioso 0:ac54225f5fcd 81 case CASO_D:
guidoglorioso 0:ac54225f5fcd 82 if(newdata==1 && c=='A') {
guidoglorioso 0:ac54225f5fcd 83 vector_datos[3]='A';
guidoglorioso 0:ac54225f5fcd 84 newdata=0;
guidoglorioso 0:ac54225f5fcd 85 datos=CASO_DA;
guidoglorioso 0:ac54225f5fcd 86 } else if(newdata==1 && c!='A') {
guidoglorioso 0:ac54225f5fcd 87 llegomal++;
guidoglorioso 0:ac54225f5fcd 88 datos=CASO_1;
guidoglorioso 0:ac54225f5fcd 89 }
guidoglorioso 0:ac54225f5fcd 90 break;
guidoglorioso 0:ac54225f5fcd 91
guidoglorioso 0:ac54225f5fcd 92 case CASO_DA:
guidoglorioso 0:ac54225f5fcd 93 if(newdata==1) {
guidoglorioso 0:ac54225f5fcd 94 vector_datos[4]=c;
guidoglorioso 0:ac54225f5fcd 95 newdata=0;
guidoglorioso 0:ac54225f5fcd 96 datos=CASO_E;
guidoglorioso 0:ac54225f5fcd 97 }
guidoglorioso 0:ac54225f5fcd 98 break;
guidoglorioso 0:ac54225f5fcd 99
guidoglorioso 0:ac54225f5fcd 100 case CASO_E:
guidoglorioso 0:ac54225f5fcd 101 if(newdata==1 && c=='F') {
guidoglorioso 0:ac54225f5fcd 102 vector_datos[5]='F';
guidoglorioso 0:ac54225f5fcd 103 newdata=0;
guidoglorioso 0:ac54225f5fcd 104 datos=CASO_EA;
guidoglorioso 0:ac54225f5fcd 105 } else if(newdata==1 && c!='F') {
guidoglorioso 0:ac54225f5fcd 106 llegomal++;
guidoglorioso 0:ac54225f5fcd 107 datos=CASO_1;
guidoglorioso 0:ac54225f5fcd 108 }
guidoglorioso 0:ac54225f5fcd 109 break;
guidoglorioso 0:ac54225f5fcd 110
guidoglorioso 0:ac54225f5fcd 111 case CASO_EA:
guidoglorioso 0:ac54225f5fcd 112 if(newdata==1) {
guidoglorioso 0:ac54225f5fcd 113 vector_datos[6]=c;
guidoglorioso 0:ac54225f5fcd 114 newdata=0;
guidoglorioso 0:ac54225f5fcd 115 datos=CASO_B;
guidoglorioso 0:ac54225f5fcd 116 }
guidoglorioso 0:ac54225f5fcd 117 break;
guidoglorioso 0:ac54225f5fcd 118
guidoglorioso 0:ac54225f5fcd 119 case CASO_B:
guidoglorioso 0:ac54225f5fcd 120 if(newdata==1 && c=='C') {
guidoglorioso 0:ac54225f5fcd 121 vector_datos[7]='C';
guidoglorioso 0:ac54225f5fcd 122 newdata=0;
guidoglorioso 0:ac54225f5fcd 123 datos=CASO_BA;
guidoglorioso 0:ac54225f5fcd 124 } else if(newdata==1 && c!='C') {
guidoglorioso 0:ac54225f5fcd 125 llegomal++;
guidoglorioso 0:ac54225f5fcd 126 datos=CASO_1;
guidoglorioso 0:ac54225f5fcd 127 }
guidoglorioso 0:ac54225f5fcd 128 break;
guidoglorioso 0:ac54225f5fcd 129
guidoglorioso 0:ac54225f5fcd 130 case CASO_BA:
guidoglorioso 0:ac54225f5fcd 131 if(newdata==1) {
guidoglorioso 0:ac54225f5fcd 132 vector_datos[8]=c;
guidoglorioso 0:ac54225f5fcd 133 newdata=0;
guidoglorioso 0:ac54225f5fcd 134 datos=CASO_CHECK;
guidoglorioso 0:ac54225f5fcd 135 }
guidoglorioso 0:ac54225f5fcd 136 break;
guidoglorioso 0:ac54225f5fcd 137
guidoglorioso 0:ac54225f5fcd 138 case CASO_CHECK:
guidoglorioso 0:ac54225f5fcd 139 check=checksum.get_xor(vector_datos, 10);//hasta que numero kevinnn
guidoglorioso 0:ac54225f5fcd 140 if(newdata==1) {
guidoglorioso 0:ac54225f5fcd 141 vector_datos[9]=c;
guidoglorioso 0:ac54225f5fcd 142 newdata=0;
guidoglorioso 0:ac54225f5fcd 143 if(check==vector_datos[9]) {
guidoglorioso 0:ac54225f5fcd 144 llegobien=1;
guidoglorioso 0:ac54225f5fcd 145 datos=CASO_FIN;
guidoglorioso 0:ac54225f5fcd 146 } else if{
guidoglorioso 0:ac54225f5fcd 147 datos=CASO_1;
guidoglorioso 0:ac54225f5fcd 148 }
guidoglorioso 0:ac54225f5fcd 149 }
guidoglorioso 0:ac54225f5fcd 150 break;
guidoglorioso 0:ac54225f5fcd 151
guidoglorioso 0:ac54225f5fcd 152 case CASO_FIN:
guidoglorioso 0:ac54225f5fcd 153 if(newdata==1 && c=='>') {
guidoglorioso 0:ac54225f5fcd 154 vector_datos[10]='>';
guidoglorioso 0:ac54225f5fcd 155 newdata=0;
guidoglorioso 0:ac54225f5fcd 156 if(llegobien==1) {
guidoglorioso 0:ac54225f5fcd 157 for(char i=0; i<=10; i++) {
guidoglorioso 0:ac54225f5fcd 158 pc.printf("%c\n",vector_datos[i]);
guidoglorioso 0:ac54225f5fcd 159 }
guidoglorioso 0:ac54225f5fcd 160 llegobien=0;
guidoglorioso 0:ac54225f5fcd 161 llegomal=0;
guidoglorioso 0:ac54225f5fcd 162 tramamal=0;
guidoglorioso 0:ac54225f5fcd 163 volante=vector_datos[2];
guidoglorioso 0:ac54225f5fcd 164 acelerador=vector_datos[4];
guidoglorioso 0:ac54225f5fcd 165 freno=vector_datos[6];
guidoglorioso 0:ac54225f5fcd 166 cambios=vector_datos[8];
guidoglorioso 0:ac54225f5fcd 167 }
guidoglorioso 0:ac54225f5fcd 168 } else if(newdata==1 && c!='>') {
guidoglorioso 0:ac54225f5fcd 169 llegomal++;
guidoglorioso 0:ac54225f5fcd 170 }
guidoglorioso 0:ac54225f5fcd 171 datos=CASO_1;
guidoglorioso 0:ac54225f5fcd 172 break;
guidoglorioso 0:ac54225f5fcd 173 }
guidoglorioso 0:ac54225f5fcd 174 }
guidoglorioso 0:ac54225f5fcd 175 int hc12::get_break_pedal()
guidoglorioso 0:ac54225f5fcd 176 {
guidoglorioso 0:ac54225f5fcd 177 return freno;
guidoglorioso 0:ac54225f5fcd 178 }
guidoglorioso 0:ac54225f5fcd 179
guidoglorioso 0:ac54225f5fcd 180 int hc12::get_speed_pedal()
guidoglorioso 0:ac54225f5fcd 181 {
guidoglorioso 0:ac54225f5fcd 182 return acelerador;
guidoglorioso 0:ac54225f5fcd 183 }
guidoglorioso 0:ac54225f5fcd 184
guidoglorioso 0:ac54225f5fcd 185 int hc12::get_changes()
guidoglorioso 0:ac54225f5fcd 186 {
guidoglorioso 0:ac54225f5fcd 187 return cambios;
guidoglorioso 0:ac54225f5fcd 188 }
guidoglorioso 0:ac54225f5fcd 189 int hc12::get_steering_wheel()
guidoglorioso 0:ac54225f5fcd 190 {
guidoglorioso 0:ac54225f5fcd 191 return volante;
guidoglorioso 0:ac54225f5fcd 192 }
guidoglorioso 0:ac54225f5fcd 193 void hc12::noentrandatos()
guidoglorioso 0:ac54225f5fcd 194 {
guidoglorioso 0:ac54225f5fcd 195 MODULOHC.printf("<S%dD%dE%d%dB%d%d>",speed,distancia,ejex,ejey,vector_envio[11]);
guidoglorioso 0:ac54225f5fcd 196 }