![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
fe
main.cpp@0:cd3f9689553a, 2019-12-12 (annotated)
- Committer:
- guidoglorioso
- Date:
- Thu Dec 12 20:04:08 2019 +0000
- Revision:
- 0:cd3f9689553a
f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
guidoglorioso | 0:cd3f9689553a | 1 | #include "mbed.h" |
guidoglorioso | 0:cd3f9689553a | 2 | #include "tsi_sensor.h" |
guidoglorioso | 0:cd3f9689553a | 3 | |
guidoglorioso | 0:cd3f9689553a | 4 | #define INICIO 0 |
guidoglorioso | 0:cd3f9689553a | 5 | #define RW 1 |
guidoglorioso | 0:cd3f9689553a | 6 | #define DISP 2 |
guidoglorioso | 0:cd3f9689553a | 7 | #define DISP_W 3 |
guidoglorioso | 0:cd3f9689553a | 8 | #define DISP_R 4 |
guidoglorioso | 0:cd3f9689553a | 9 | #define EST 5 |
guidoglorioso | 0:cd3f9689553a | 10 | #define VERIF 6 |
guidoglorioso | 0:cd3f9689553a | 11 | #define FIN 7 |
guidoglorioso | 0:cd3f9689553a | 12 | #define PWMD 8 |
guidoglorioso | 0:cd3f9689553a | 13 | #define PWMU 9 |
guidoglorioso | 0:cd3f9689553a | 14 | |
guidoglorioso | 0:cd3f9689553a | 15 | /* Cosas del TSI */ |
guidoglorioso | 0:cd3f9689553a | 16 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
guidoglorioso | 0:cd3f9689553a | 17 | #define ELEC0 9 |
guidoglorioso | 0:cd3f9689553a | 18 | #define ELEC1 10 |
guidoglorioso | 0:cd3f9689553a | 19 | #elif defined (TARGET_KL05Z) |
guidoglorioso | 0:cd3f9689553a | 20 | #define ELEC0 9 |
guidoglorioso | 0:cd3f9689553a | 21 | #define ELEC1 8 |
guidoglorioso | 0:cd3f9689553a | 22 | #else |
guidoglorioso | 0:cd3f9689553a | 23 | #error TARGET NOT DEFINED |
guidoglorioso | 0:cd3f9689553a | 24 | #endif |
guidoglorioso | 0:cd3f9689553a | 25 | |
guidoglorioso | 0:cd3f9689553a | 26 | //Definimos que el puerto serie se llama pc |
guidoglorioso | 0:cd3f9689553a | 27 | Serial pc(USBTX, USBRX); |
guidoglorioso | 0:cd3f9689553a | 28 | |
guidoglorioso | 0:cd3f9689553a | 29 | //Variable donde se guarda lo recibido |
guidoglorioso | 0:cd3f9689553a | 30 | unsigned char c = '\0'; |
guidoglorioso | 0:cd3f9689553a | 31 | |
guidoglorioso | 0:cd3f9689553a | 32 | unsigned char slider=0, pote=0, verif; |
guidoglorioso | 0:cd3f9689553a | 33 | |
guidoglorioso | 0:cd3f9689553a | 34 | //bit usado como flag para procesar datos |
guidoglorioso | 0:cd3f9689553a | 35 | bool newdata = false; //Se pone en true cuando hay nuevos datos |
guidoglorioso | 0:cd3f9689553a | 36 | bool ejec = false; //Se pone en true cuando hay una trama correcta |
guidoglorioso | 0:cd3f9689553a | 37 | unsigned char rec[7]= {0}; |
guidoglorioso | 0:cd3f9689553a | 38 | |
guidoglorioso | 0:cd3f9689553a | 39 | //Creamos los tres leds pra usar |
guidoglorioso | 0:cd3f9689553a | 40 | DigitalIn bt1(PTA4,PullUp); |
guidoglorioso | 0:cd3f9689553a | 41 | DigitalIn bt2(PTA5,PullUp); |
guidoglorioso | 0:cd3f9689553a | 42 | DigitalOut LR(LED1); |
guidoglorioso | 0:cd3f9689553a | 43 | DigitalOut LG(LED2); |
guidoglorioso | 0:cd3f9689553a | 44 | DigitalOut LB(LED3); |
guidoglorioso | 0:cd3f9689553a | 45 | AnalogIn analo(PTB0); |
guidoglorioso | 0:cd3f9689553a | 46 | AnalogOut pwm (PTE30); |
guidoglorioso | 0:cd3f9689553a | 47 | |
guidoglorioso | 0:cd3f9689553a | 48 | //Callback cuando se detecta una entrada |
guidoglorioso | 0:cd3f9689553a | 49 | void onCharReceived() |
guidoglorioso | 0:cd3f9689553a | 50 | { |
guidoglorioso | 0:cd3f9689553a | 51 | //Copiamos lo leido en c |
guidoglorioso | 0:cd3f9689553a | 52 | c = pc.getc(); |
guidoglorioso | 0:cd3f9689553a | 53 | newdata = true; |
guidoglorioso | 0:cd3f9689553a | 54 | } |
guidoglorioso | 0:cd3f9689553a | 55 | |
guidoglorioso | 0:cd3f9689553a | 56 | bool recibiendo(void) |
guidoglorioso | 0:cd3f9689553a | 57 | { |
guidoglorioso | 0:cd3f9689553a | 58 | static unsigned char est=INICIO; |
guidoglorioso | 0:cd3f9689553a | 59 | switch(est) { |
guidoglorioso | 0:cd3f9689553a | 60 | default: |
guidoglorioso | 0:cd3f9689553a | 61 | case INICIO: |
guidoglorioso | 0:cd3f9689553a | 62 | if (c=='$') { //Si $ lo guardo en el char 0 de rec |
guidoglorioso | 0:cd3f9689553a | 63 | rec[0]=c; |
guidoglorioso | 0:cd3f9689553a | 64 | est=RW; |
guidoglorioso | 0:cd3f9689553a | 65 | } |
guidoglorioso | 0:cd3f9689553a | 66 | //Hacemos NULL c para que no vuelva a ejecutar |
guidoglorioso | 0:cd3f9689553a | 67 | c = '\0'; |
guidoglorioso | 0:cd3f9689553a | 68 | break; |
guidoglorioso | 0:cd3f9689553a | 69 | |
guidoglorioso | 0:cd3f9689553a | 70 | case RW: |
guidoglorioso | 0:cd3f9689553a | 71 | if ((c=='W')||(c=='R')) { //Si W o R lo guardo en el char 1 de rec |
guidoglorioso | 0:cd3f9689553a | 72 | |
guidoglorioso | 0:cd3f9689553a | 73 | rec[1]=c; |
guidoglorioso | 0:cd3f9689553a | 74 | est=DISP; |
guidoglorioso | 0:cd3f9689553a | 75 | } else { |
guidoglorioso | 0:cd3f9689553a | 76 | if(c=='P') { |
guidoglorioso | 0:cd3f9689553a | 77 | rec[1]=c; |
guidoglorioso | 0:cd3f9689553a | 78 | est=PWMD; |
guidoglorioso | 0:cd3f9689553a | 79 | } else { |
guidoglorioso | 0:cd3f9689553a | 80 | if(c=='$') { |
guidoglorioso | 0:cd3f9689553a | 81 | rec[0]=c; |
guidoglorioso | 0:cd3f9689553a | 82 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 83 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 84 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 85 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 86 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 87 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 88 | est=RW; |
guidoglorioso | 0:cd3f9689553a | 89 | } else { |
guidoglorioso | 0:cd3f9689553a | 90 | rec[0]=0; |
guidoglorioso | 0:cd3f9689553a | 91 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 92 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 93 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 94 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 95 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 96 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 97 | est=INICIO; |
guidoglorioso | 0:cd3f9689553a | 98 | } |
guidoglorioso | 0:cd3f9689553a | 99 | } |
guidoglorioso | 0:cd3f9689553a | 100 | } |
guidoglorioso | 0:cd3f9689553a | 101 | //Hacemos NULL c para que no vuelva a ejecutar |
guidoglorioso | 0:cd3f9689553a | 102 | c = '\0'; |
guidoglorioso | 0:cd3f9689553a | 103 | break; |
guidoglorioso | 0:cd3f9689553a | 104 | |
guidoglorioso | 0:cd3f9689553a | 105 | case DISP: |
guidoglorioso | 0:cd3f9689553a | 106 | if ((rec[1]=='R')&&((c=='L')||(c=='A')||(c=='D'))) { //Si leo y recibo L, A o D lo guardo en el char 2 de rec |
guidoglorioso | 0:cd3f9689553a | 107 | rec[2]=c; |
guidoglorioso | 0:cd3f9689553a | 108 | est=DISP_R; |
guidoglorioso | 0:cd3f9689553a | 109 | } else { |
guidoglorioso | 0:cd3f9689553a | 110 | if ((rec[1]=='W')&&(c=='L')) { //Si escribo y recibo L lo guardo en el char 2 de rec |
guidoglorioso | 0:cd3f9689553a | 111 | rec[2]=c; |
guidoglorioso | 0:cd3f9689553a | 112 | est=DISP_W; |
guidoglorioso | 0:cd3f9689553a | 113 | } else { |
guidoglorioso | 0:cd3f9689553a | 114 | if(c=='P') { |
guidoglorioso | 0:cd3f9689553a | 115 | rec[2]=c; |
guidoglorioso | 0:cd3f9689553a | 116 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 117 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 118 | est=VERIF; |
guidoglorioso | 0:cd3f9689553a | 119 | } else { |
guidoglorioso | 0:cd3f9689553a | 120 | if(c=='$') { |
guidoglorioso | 0:cd3f9689553a | 121 | rec[0]=c; |
guidoglorioso | 0:cd3f9689553a | 122 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 123 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 124 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 125 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 126 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 127 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 128 | est=RW; |
guidoglorioso | 0:cd3f9689553a | 129 | } else { |
guidoglorioso | 0:cd3f9689553a | 130 | rec[0]=0; |
guidoglorioso | 0:cd3f9689553a | 131 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 132 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 133 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 134 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 135 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 136 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 137 | est=INICIO; |
guidoglorioso | 0:cd3f9689553a | 138 | } |
guidoglorioso | 0:cd3f9689553a | 139 | } |
guidoglorioso | 0:cd3f9689553a | 140 | } |
guidoglorioso | 0:cd3f9689553a | 141 | } |
guidoglorioso | 0:cd3f9689553a | 142 | //Hacemos NULL c para que no vuelva a ejecutar |
guidoglorioso | 0:cd3f9689553a | 143 | c = '\0'; |
guidoglorioso | 0:cd3f9689553a | 144 | break; |
guidoglorioso | 0:cd3f9689553a | 145 | |
guidoglorioso | 0:cd3f9689553a | 146 | case DISP_W: |
guidoglorioso | 0:cd3f9689553a | 147 | if ((c=='R')||(c=='G')||(c=='B')) { //Si escribo y recibo G, B o R lo guardo en el char 3 de rec |
guidoglorioso | 0:cd3f9689553a | 148 | rec[3]=c; |
guidoglorioso | 0:cd3f9689553a | 149 | est=EST; |
guidoglorioso | 0:cd3f9689553a | 150 | } else { |
guidoglorioso | 0:cd3f9689553a | 151 | if(c=='$') { |
guidoglorioso | 0:cd3f9689553a | 152 | rec[0]=c; |
guidoglorioso | 0:cd3f9689553a | 153 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 154 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 155 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 156 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 157 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 158 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 159 | est=RW; |
guidoglorioso | 0:cd3f9689553a | 160 | } else { |
guidoglorioso | 0:cd3f9689553a | 161 | rec[0]=0; |
guidoglorioso | 0:cd3f9689553a | 162 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 163 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 164 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 165 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 166 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 167 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 168 | est=INICIO; |
guidoglorioso | 0:cd3f9689553a | 169 | } |
guidoglorioso | 0:cd3f9689553a | 170 | } |
guidoglorioso | 0:cd3f9689553a | 171 | c=0; |
guidoglorioso | 0:cd3f9689553a | 172 | break; |
guidoglorioso | 0:cd3f9689553a | 173 | |
guidoglorioso | 0:cd3f9689553a | 174 | case DISP_R: |
guidoglorioso | 0:cd3f9689553a | 175 | if ((c=='1')||(c=='2')||(c=='3')) { //Si leo y recibo 1 o 2 lo guardo en el char 3 de rec |
guidoglorioso | 0:cd3f9689553a | 176 | rec[3]=c; |
guidoglorioso | 0:cd3f9689553a | 177 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 178 | est=VERIF; |
guidoglorioso | 0:cd3f9689553a | 179 | } else { |
guidoglorioso | 0:cd3f9689553a | 180 | if(c=='$') { |
guidoglorioso | 0:cd3f9689553a | 181 | rec[0]=c; |
guidoglorioso | 0:cd3f9689553a | 182 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 183 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 184 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 185 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 186 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 187 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 188 | est=RW; |
guidoglorioso | 0:cd3f9689553a | 189 | } else { |
guidoglorioso | 0:cd3f9689553a | 190 | rec[0]=0; |
guidoglorioso | 0:cd3f9689553a | 191 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 192 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 193 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 194 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 195 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 196 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 197 | est=INICIO; |
guidoglorioso | 0:cd3f9689553a | 198 | } |
guidoglorioso | 0:cd3f9689553a | 199 | } |
guidoglorioso | 0:cd3f9689553a | 200 | //Hacemos NULL c para que no vuelva a ejecutar |
guidoglorioso | 0:cd3f9689553a | 201 | c=0; |
guidoglorioso | 0:cd3f9689553a | 202 | break; |
guidoglorioso | 0:cd3f9689553a | 203 | |
guidoglorioso | 0:cd3f9689553a | 204 | case EST: |
guidoglorioso | 0:cd3f9689553a | 205 | if ((c=='1')||(c=='0')) { //Si escribo y recibo 1 o 0 lo guardo en el char 4 de rec |
guidoglorioso | 0:cd3f9689553a | 206 | rec[4]=c; |
guidoglorioso | 0:cd3f9689553a | 207 | est=VERIF; |
guidoglorioso | 0:cd3f9689553a | 208 | } else { |
guidoglorioso | 0:cd3f9689553a | 209 | if(c=='$') { |
guidoglorioso | 0:cd3f9689553a | 210 | rec[0]=c; |
guidoglorioso | 0:cd3f9689553a | 211 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 212 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 213 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 214 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 215 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 216 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 217 | est=RW; |
guidoglorioso | 0:cd3f9689553a | 218 | } else { |
guidoglorioso | 0:cd3f9689553a | 219 | rec[0]=0; |
guidoglorioso | 0:cd3f9689553a | 220 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 221 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 222 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 223 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 224 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 225 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 226 | est=INICIO; |
guidoglorioso | 0:cd3f9689553a | 227 | } |
guidoglorioso | 0:cd3f9689553a | 228 | } |
guidoglorioso | 0:cd3f9689553a | 229 | //Hacemos NULL c para que no vuelva a ejecutar |
guidoglorioso | 0:cd3f9689553a | 230 | c=0; |
guidoglorioso | 0:cd3f9689553a | 231 | break; |
guidoglorioso | 0:cd3f9689553a | 232 | |
guidoglorioso | 0:cd3f9689553a | 233 | case VERIF: |
guidoglorioso | 0:cd3f9689553a | 234 | if(c==verif) { |
guidoglorioso | 0:cd3f9689553a | 235 | rec[5]=c;//Guarda la verificacion de la trama en rec 5 |
guidoglorioso | 0:cd3f9689553a | 236 | est=FIN; |
guidoglorioso | 0:cd3f9689553a | 237 | } else { |
guidoglorioso | 0:cd3f9689553a | 238 | if(c=='$') { |
guidoglorioso | 0:cd3f9689553a | 239 | rec[0]=c; |
guidoglorioso | 0:cd3f9689553a | 240 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 241 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 242 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 243 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 244 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 245 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 246 | est=RW; |
guidoglorioso | 0:cd3f9689553a | 247 | } else { |
guidoglorioso | 0:cd3f9689553a | 248 | rec[0]=0; |
guidoglorioso | 0:cd3f9689553a | 249 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 250 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 251 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 252 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 253 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 254 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 255 | est=INICIO; |
guidoglorioso | 0:cd3f9689553a | 256 | } |
guidoglorioso | 0:cd3f9689553a | 257 | } |
guidoglorioso | 0:cd3f9689553a | 258 | c=0; |
guidoglorioso | 0:cd3f9689553a | 259 | break; |
guidoglorioso | 0:cd3f9689553a | 260 | |
guidoglorioso | 0:cd3f9689553a | 261 | case FIN: |
guidoglorioso | 0:cd3f9689553a | 262 | if(c=='!') { //Si recibo !0 lo guardo en el char 6 de rec |
guidoglorioso | 0:cd3f9689553a | 263 | rec[6]=c; |
guidoglorioso | 0:cd3f9689553a | 264 | est=INICIO; |
guidoglorioso | 0:cd3f9689553a | 265 | //Hacemos NULL c para que no vuelva a ejecutar |
guidoglorioso | 0:cd3f9689553a | 266 | c=0; |
guidoglorioso | 0:cd3f9689553a | 267 | return (true); |
guidoglorioso | 0:cd3f9689553a | 268 | } else { |
guidoglorioso | 0:cd3f9689553a | 269 | if((c=='$')||((rec[5]=='$')&&((c=='W')||(c=='R')))) { |
guidoglorioso | 0:cd3f9689553a | 270 | if(c=='$') { |
guidoglorioso | 0:cd3f9689553a | 271 | rec[0]='$'; |
guidoglorioso | 0:cd3f9689553a | 272 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 273 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 274 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 275 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 276 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 277 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 278 | est=RW; |
guidoglorioso | 0:cd3f9689553a | 279 | } else { |
guidoglorioso | 0:cd3f9689553a | 280 | rec[0]='$'; |
guidoglorioso | 0:cd3f9689553a | 281 | rec[1]=c; |
guidoglorioso | 0:cd3f9689553a | 282 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 283 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 284 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 285 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 286 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 287 | est=DISP; |
guidoglorioso | 0:cd3f9689553a | 288 | } |
guidoglorioso | 0:cd3f9689553a | 289 | } else { |
guidoglorioso | 0:cd3f9689553a | 290 | rec[0]=0; |
guidoglorioso | 0:cd3f9689553a | 291 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 292 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 293 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 294 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 295 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 296 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 297 | est=INICIO; |
guidoglorioso | 0:cd3f9689553a | 298 | } |
guidoglorioso | 0:cd3f9689553a | 299 | } |
guidoglorioso | 0:cd3f9689553a | 300 | //Hacemos NULL c para que no vuelva a ejecutar |
guidoglorioso | 0:cd3f9689553a | 301 | c=0; |
guidoglorioso | 0:cd3f9689553a | 302 | break; |
guidoglorioso | 0:cd3f9689553a | 303 | |
guidoglorioso | 0:cd3f9689553a | 304 | case PWMD: |
guidoglorioso | 0:cd3f9689553a | 305 | if ((c>='0')&&(c<='9')) { //Si recibo de 0 a 9 o de A a F lo guardo en el char 2 de rec |
guidoglorioso | 0:cd3f9689553a | 306 | rec[2]=c; |
guidoglorioso | 0:cd3f9689553a | 307 | est=PWMU; |
guidoglorioso | 0:cd3f9689553a | 308 | } else { |
guidoglorioso | 0:cd3f9689553a | 309 | if(c=='$') { |
guidoglorioso | 0:cd3f9689553a | 310 | rec[0]=c; |
guidoglorioso | 0:cd3f9689553a | 311 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 312 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 313 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 314 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 315 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 316 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 317 | est=RW; |
guidoglorioso | 0:cd3f9689553a | 318 | } else { |
guidoglorioso | 0:cd3f9689553a | 319 | rec[0]=0; |
guidoglorioso | 0:cd3f9689553a | 320 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 321 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 322 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 323 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 324 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 325 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 326 | est=INICIO; |
guidoglorioso | 0:cd3f9689553a | 327 | } |
guidoglorioso | 0:cd3f9689553a | 328 | } |
guidoglorioso | 0:cd3f9689553a | 329 | //Hacemos NULL c para que no vuelva a ejecutar |
guidoglorioso | 0:cd3f9689553a | 330 | c=0; |
guidoglorioso | 0:cd3f9689553a | 331 | break; |
guidoglorioso | 0:cd3f9689553a | 332 | case PWMU: |
guidoglorioso | 0:cd3f9689553a | 333 | if (((c>='0')&&(c<='9'))||((c>='A')&&(c<='F'))) { //Si recibo de 0 a 9 o de A a F lo guardo en el char 2 de rec |
guidoglorioso | 0:cd3f9689553a | 334 | rec[3]=c; |
guidoglorioso | 0:cd3f9689553a | 335 | est=VERIF; |
guidoglorioso | 0:cd3f9689553a | 336 | } else { |
guidoglorioso | 0:cd3f9689553a | 337 | if(c=='$') { |
guidoglorioso | 0:cd3f9689553a | 338 | rec[0]=c; |
guidoglorioso | 0:cd3f9689553a | 339 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 340 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 341 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 342 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 343 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 344 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 345 | est=RW; |
guidoglorioso | 0:cd3f9689553a | 346 | } else { |
guidoglorioso | 0:cd3f9689553a | 347 | rec[0]=0; |
guidoglorioso | 0:cd3f9689553a | 348 | rec[1]=0; |
guidoglorioso | 0:cd3f9689553a | 349 | rec[2]=0; |
guidoglorioso | 0:cd3f9689553a | 350 | rec[3]=0; |
guidoglorioso | 0:cd3f9689553a | 351 | rec[4]=0; |
guidoglorioso | 0:cd3f9689553a | 352 | rec[5]=0; |
guidoglorioso | 0:cd3f9689553a | 353 | rec[6]=0; |
guidoglorioso | 0:cd3f9689553a | 354 | est=INICIO; |
guidoglorioso | 0:cd3f9689553a | 355 | } |
guidoglorioso | 0:cd3f9689553a | 356 | } |
guidoglorioso | 0:cd3f9689553a | 357 | //Hacemos NULL c para que no vuelva a ejecutar |
guidoglorioso | 0:cd3f9689553a | 358 | c=0; |
guidoglorioso | 0:cd3f9689553a | 359 | break; |
guidoglorioso | 0:cd3f9689553a | 360 | } |
guidoglorioso | 0:cd3f9689553a | 361 | return (false); |
guidoglorioso | 0:cd3f9689553a | 362 | } |
guidoglorioso | 0:cd3f9689553a | 363 | |
guidoglorioso | 0:cd3f9689553a | 364 | void interpretacion(void) |
guidoglorioso | 0:cd3f9689553a | 365 | { |
guidoglorioso | 0:cd3f9689553a | 366 | static unsigned char aux, aux2, aux3; |
guidoglorioso | 0:cd3f9689553a | 367 | if(rec[1]=='W') { |
guidoglorioso | 0:cd3f9689553a | 368 | if(rec[3]=='R') { |
guidoglorioso | 0:cd3f9689553a | 369 | if(rec[4]=='1') |
guidoglorioso | 0:cd3f9689553a | 370 | LR=0; |
guidoglorioso | 0:cd3f9689553a | 371 | if(rec[4]=='0') |
guidoglorioso | 0:cd3f9689553a | 372 | LR=1; |
guidoglorioso | 0:cd3f9689553a | 373 | printf("\n$RL1%d!\r\n",!LR); |
guidoglorioso | 0:cd3f9689553a | 374 | } |
guidoglorioso | 0:cd3f9689553a | 375 | if(rec[3]=='B') { |
guidoglorioso | 0:cd3f9689553a | 376 | if(rec[4]=='1') |
guidoglorioso | 0:cd3f9689553a | 377 | LB=0; |
guidoglorioso | 0:cd3f9689553a | 378 | if(rec[4]=='0') |
guidoglorioso | 0:cd3f9689553a | 379 | LB=1; |
guidoglorioso | 0:cd3f9689553a | 380 | printf("\n$RL3%d!\r\n",!LB); |
guidoglorioso | 0:cd3f9689553a | 381 | } |
guidoglorioso | 0:cd3f9689553a | 382 | if(rec[3]=='G') { |
guidoglorioso | 0:cd3f9689553a | 383 | if(rec[4]=='1') |
guidoglorioso | 0:cd3f9689553a | 384 | LG=0; |
guidoglorioso | 0:cd3f9689553a | 385 | if(rec[4]=='0') |
guidoglorioso | 0:cd3f9689553a | 386 | LG=1; |
guidoglorioso | 0:cd3f9689553a | 387 | printf("\n$RL2%d!\r\n",!LG); |
guidoglorioso | 0:cd3f9689553a | 388 | } |
guidoglorioso | 0:cd3f9689553a | 389 | |
guidoglorioso | 0:cd3f9689553a | 390 | } |
guidoglorioso | 0:cd3f9689553a | 391 | if(rec[1]=='R') { |
guidoglorioso | 0:cd3f9689553a | 392 | if(rec[2]=='L') { |
guidoglorioso | 0:cd3f9689553a | 393 | if(rec[3]=='1') |
guidoglorioso | 0:cd3f9689553a | 394 | printf("\n$RL1%d!\r\n",!LR); |
guidoglorioso | 0:cd3f9689553a | 395 | if(rec[3]=='2') |
guidoglorioso | 0:cd3f9689553a | 396 | printf("\n$RL2%d!\r\n",!LG); |
guidoglorioso | 0:cd3f9689553a | 397 | if(rec[3]=='3') |
guidoglorioso | 0:cd3f9689553a | 398 | printf("\n$RL3%d!\r\n",!LB); |
guidoglorioso | 0:cd3f9689553a | 399 | } |
guidoglorioso | 0:cd3f9689553a | 400 | if(rec[2]=='A') { |
guidoglorioso | 0:cd3f9689553a | 401 | if(rec[3]=='1') |
guidoglorioso | 0:cd3f9689553a | 402 | printf("\n$RA1%02X!\r\n",slider); |
guidoglorioso | 0:cd3f9689553a | 403 | if(rec[3]=='2') |
guidoglorioso | 0:cd3f9689553a | 404 | printf("\n$RA2%02X!\r\n",pote); |
guidoglorioso | 0:cd3f9689553a | 405 | } |
guidoglorioso | 0:cd3f9689553a | 406 | if(rec[2]=='D') { |
guidoglorioso | 0:cd3f9689553a | 407 | if(rec[3]=='1') |
guidoglorioso | 0:cd3f9689553a | 408 | printf("\n$RD1%d!\r\n",!bt1); |
guidoglorioso | 0:cd3f9689553a | 409 | if(rec[3]=='2') |
guidoglorioso | 0:cd3f9689553a | 410 | printf("\n$RD2%d!\r\n",!bt2); |
guidoglorioso | 0:cd3f9689553a | 411 | } |
guidoglorioso | 0:cd3f9689553a | 412 | if(rec[2]=='P') { |
guidoglorioso | 0:cd3f9689553a | 413 | aux = pwm*100; |
guidoglorioso | 0:cd3f9689553a | 414 | printf("\n$P%02X!\r\n", aux); |
guidoglorioso | 0:cd3f9689553a | 415 | } |
guidoglorioso | 0:cd3f9689553a | 416 | } |
guidoglorioso | 0:cd3f9689553a | 417 | if (rec[1]=='P') { |
guidoglorioso | 0:cd3f9689553a | 418 | if (rec[3]>='A') |
guidoglorioso | 0:cd3f9689553a | 419 | aux3 = rec[3]-'A'+10; |
guidoglorioso | 0:cd3f9689553a | 420 | else |
guidoglorioso | 0:cd3f9689553a | 421 | aux3 = rec[3]-'0'; |
guidoglorioso | 0:cd3f9689553a | 422 | aux2 = (16*(rec[2]-'0'))+aux3; |
guidoglorioso | 0:cd3f9689553a | 423 | pwm = (float) aux2/100; |
guidoglorioso | 0:cd3f9689553a | 424 | printf("\n$P%02X!\r\n", aux2); |
guidoglorioso | 0:cd3f9689553a | 425 | } |
guidoglorioso | 0:cd3f9689553a | 426 | } |
guidoglorioso | 0:cd3f9689553a | 427 | |
guidoglorioso | 0:cd3f9689553a | 428 | int main() |
guidoglorioso | 0:cd3f9689553a | 429 | { |
guidoglorioso | 0:cd3f9689553a | 430 | //Apagamos los leds |
guidoglorioso | 0:cd3f9689553a | 431 | LG=1; |
guidoglorioso | 0:cd3f9689553a | 432 | LR=1; |
guidoglorioso | 0:cd3f9689553a | 433 | LB=1; |
guidoglorioso | 0:cd3f9689553a | 434 | |
guidoglorioso | 0:cd3f9689553a | 435 | pwm=1; |
guidoglorioso | 0:cd3f9689553a | 436 | |
guidoglorioso | 0:cd3f9689553a | 437 | TSIAnalogSlider tsi(ELEC0, ELEC1, 40); |
guidoglorioso | 0:cd3f9689553a | 438 | |
guidoglorioso | 0:cd3f9689553a | 439 | //Ejecutar onCharReceived por cada entrada por puerto |
guidoglorioso | 0:cd3f9689553a | 440 | pc.attach(&onCharReceived); |
guidoglorioso | 0:cd3f9689553a | 441 | |
guidoglorioso | 0:cd3f9689553a | 442 | while (true) { |
guidoglorioso | 0:cd3f9689553a | 443 | verif=rec[1]^rec[2]^rec[3]; |
guidoglorioso | 0:cd3f9689553a | 444 | slider=255*tsi.readPercentage(); |
guidoglorioso | 0:cd3f9689553a | 445 | pote=255*analo.read(); |
guidoglorioso | 0:cd3f9689553a | 446 | if(newdata) { |
guidoglorioso | 0:cd3f9689553a | 447 | ejec=recibiendo(); |
guidoglorioso | 0:cd3f9689553a | 448 | newdata = false; |
guidoglorioso | 0:cd3f9689553a | 449 | } |
guidoglorioso | 0:cd3f9689553a | 450 | if(ejec) { |
guidoglorioso | 0:cd3f9689553a | 451 | interpretacion(); |
guidoglorioso | 0:cd3f9689553a | 452 | ejec=0; |
guidoglorioso | 0:cd3f9689553a | 453 | } |
guidoglorioso | 0:cd3f9689553a | 454 | } |
guidoglorioso | 0:cd3f9689553a | 455 | } |