TP1_EJER3

Dependencies:   mbed tsi_sensor

Committer:
Charin
Date:
Wed May 22 13:07:54 2019 +0000
Revision:
0:256d00799c21
Child:
1:5b92fb2cd769
falta tiempo y display

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Charin 0:256d00799c21 1 #include "mbed.h"
Charin 0:256d00799c21 2 #include "tsi_sensor.h"
Charin 0:256d00799c21 3
Charin 0:256d00799c21 4 /* This defines will be replaced by PinNames soon */
Charin 0:256d00799c21 5 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
Charin 0:256d00799c21 6 #define ELEC0 9
Charin 0:256d00799c21 7 #define ELEC1 10
Charin 0:256d00799c21 8 #elif defined (TARGET_KL05Z)
Charin 0:256d00799c21 9 #define ELEC0 9
Charin 0:256d00799c21 10 #define ELEC1 8
Charin 0:256d00799c21 11 #else
Charin 0:256d00799c21 12 #error TARGET NOT DEFINED
Charin 0:256d00799c21 13 #endif
Charin 0:256d00799c21 14 Ticker tiempo;
Charin 0:256d00799c21 15
Charin 0:256d00799c21 16 enum maquina_estado_general {INICIO, CABLE1, CABLE2, CABLE3, CABLE4, WIN, LOOSER};
Charin 0:256d00799c21 17 maquina_estado_general maquina_general;
Charin 0:256d00799c21 18
Charin 0:256d00799c21 19 PwmOut display(PTA2);
Charin 0:256d00799c21 20 TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
Charin 0:256d00799c21 21 AnalogIn noise (PTC1);
Charin 0:256d00799c21 22 DigitalIn cable_0 (PTB0);
Charin 0:256d00799c21 23 DigitalIn cable_1 (PTB1);
Charin 0:256d00799c21 24 DigitalIn cable_2 (PTB2);
Charin 0:256d00799c21 25 DigitalIn cable_3 (PTC12);
Charin 0:256d00799c21 26 DigitalOut verde (LED_GREEN);
Charin 0:256d00799c21 27 DigitalOut rojo (LED_RED);
Charin 0:256d00799c21 28
Charin 0:256d00799c21 29 int vector_secuencia [4];
Charin 0:256d00799c21 30 int vector_orden_cables [4] = {-1, -1, -1, -1};
Charin 0:256d00799c21 31 int ccero = 0;
Charin 0:256d00799c21 32 int cuno = 0;
Charin 0:256d00799c21 33 int cdos = 0;
Charin 0:256d00799c21 34 int ctres = 0;
Charin 0:256d00799c21 35 int sumador = 0;
Charin 0:256d00799c21 36
Charin 0:256d00799c21 37 void secuencia();
Charin 0:256d00799c21 38 void maquina_general_sec ();
Charin 0:256d00799c21 39 //void maquina_display ();
Charin 0:256d00799c21 40 void maquina_orden_cables ();
Charin 0:256d00799c21 41 void suma ();
Charin 0:256d00799c21 42 int cablesDesconectados(int n);
Charin 0:256d00799c21 43
Charin 0:256d00799c21 44 int main(void)
Charin 0:256d00799c21 45 {
Charin 0:256d00799c21 46
Charin 0:256d00799c21 47 cable_0.mode (PullUp);
Charin 0:256d00799c21 48 cable_1.mode (PullUp);
Charin 0:256d00799c21 49 cable_2.mode (PullUp);
Charin 0:256d00799c21 50 cable_3.mode (PullUp);
Charin 0:256d00799c21 51
Charin 0:256d00799c21 52
Charin 0:256d00799c21 53 srand (int(noise * 10000));
Charin 0:256d00799c21 54
Charin 0:256d00799c21 55 secuencia ();
Charin 0:256d00799c21 56 verde = 1;
Charin 0:256d00799c21 57 rojo = 1;
Charin 0:256d00799c21 58
Charin 0:256d00799c21 59 while (true) {
Charin 0:256d00799c21 60 maquina_general_sec ();
Charin 0:256d00799c21 61 //maquina_display ();
Charin 0:256d00799c21 62 maquina_orden_cables ();
Charin 0:256d00799c21 63 printf("%d %d %d %d", vector_orden_cables[0], vector_orden_cables[1], vector_orden_cables[2], vector_orden_cables[3]);
Charin 0:256d00799c21 64 }
Charin 0:256d00799c21 65 }
Charin 0:256d00799c21 66
Charin 0:256d00799c21 67 void suma ()
Charin 0:256d00799c21 68 {
Charin 0:256d00799c21 69 sumador++;
Charin 0:256d00799c21 70 }
Charin 0:256d00799c21 71
Charin 0:256d00799c21 72 void secuencia ()
Charin 0:256d00799c21 73 {
Charin 0:256d00799c21 74 vector_secuencia [0] = rand () %4;
Charin 0:256d00799c21 75 for (int i = 1; i < 4; i++) {
Charin 0:256d00799c21 76 int randomm = rand () %4;
Charin 0:256d00799c21 77 if (vector_secuencia [i - 1] != randomm)
Charin 0:256d00799c21 78 vector_secuencia [i] = randomm;
Charin 0:256d00799c21 79 else
Charin 0:256d00799c21 80 i--;
Charin 0:256d00799c21 81 }
Charin 0:256d00799c21 82
Charin 0:256d00799c21 83 }
Charin 0:256d00799c21 84
Charin 0:256d00799c21 85
Charin 0:256d00799c21 86 void maquina_general_sec ()
Charin 0:256d00799c21 87 {
Charin 0:256d00799c21 88 switch (maquina_general) {
Charin 0:256d00799c21 89
Charin 0:256d00799c21 90 default:
Charin 0:256d00799c21 91 maquina_general = INICIO;
Charin 0:256d00799c21 92 break;
Charin 0:256d00799c21 93
Charin 0:256d00799c21 94 case INICIO:
Charin 0:256d00799c21 95 if (tsi.readPercentage() > 0.5){
Charin 0:256d00799c21 96 maquina_general = CABLE1;
Charin 0:256d00799c21 97 verde = 0;
Charin 0:256d00799c21 98 }
Charin 0:256d00799c21 99 else
Charin 0:256d00799c21 100 maquina_general = INICIO;
Charin 0:256d00799c21 101 break;
Charin 0:256d00799c21 102
Charin 0:256d00799c21 103 case CABLE1:
Charin 0:256d00799c21 104 verde = 1;
Charin 0:256d00799c21 105 if (vector_secuencia [0] == vector_orden_cables [0] && vector_orden_cables [0] != -1) {
Charin 0:256d00799c21 106 maquina_general = CABLE2;
Charin 0:256d00799c21 107 }
Charin 0:256d00799c21 108 else {
Charin 0:256d00799c21 109 maquina_general = CABLE1;
Charin 0:256d00799c21 110 }
Charin 0:256d00799c21 111 if (vector_secuencia [0] != vector_orden_cables [0] && vector_orden_cables [0] != -1) {
Charin 0:256d00799c21 112 maquina_general = LOOSER;
Charin 0:256d00799c21 113 }
Charin 0:256d00799c21 114
Charin 0:256d00799c21 115 break;
Charin 0:256d00799c21 116
Charin 0:256d00799c21 117 case CABLE2:
Charin 0:256d00799c21 118 rojo = 0;
Charin 0:256d00799c21 119 if (vector_secuencia [1] == vector_orden_cables [1] && vector_orden_cables [1] != -1) {
Charin 0:256d00799c21 120 maquina_general = CABLE3;
Charin 0:256d00799c21 121 tiempo.attach (&suma, 1);
Charin 0:256d00799c21 122 }
Charin 0:256d00799c21 123 else {
Charin 0:256d00799c21 124 maquina_general = CABLE2;
Charin 0:256d00799c21 125 }
Charin 0:256d00799c21 126 if (vector_secuencia [1] != vector_orden_cables [1] && vector_orden_cables [1] != -1) {
Charin 0:256d00799c21 127 maquina_general = LOOSER;
Charin 0:256d00799c21 128 }
Charin 0:256d00799c21 129
Charin 0:256d00799c21 130 break;
Charin 0:256d00799c21 131
Charin 0:256d00799c21 132 case CABLE3:
Charin 0:256d00799c21 133 rojo = 1;
Charin 0:256d00799c21 134 if (vector_secuencia [2] == vector_orden_cables [2] && vector_orden_cables [2] != -1) {
Charin 0:256d00799c21 135 maquina_general = CABLE4;
Charin 0:256d00799c21 136 tiempo.attach (&suma, 1);
Charin 0:256d00799c21 137 }
Charin 0:256d00799c21 138 else {
Charin 0:256d00799c21 139 maquina_general = CABLE3;
Charin 0:256d00799c21 140 }
Charin 0:256d00799c21 141 if (vector_secuencia [2] != vector_orden_cables [2] && vector_orden_cables [2] != -1) {
Charin 0:256d00799c21 142 maquina_general = LOOSER;
Charin 0:256d00799c21 143 }
Charin 0:256d00799c21 144 break;
Charin 0:256d00799c21 145
Charin 0:256d00799c21 146 case CABLE4:
Charin 0:256d00799c21 147 rojo = 0;
Charin 0:256d00799c21 148 if (vector_secuencia [3] == vector_orden_cables [3] && vector_orden_cables [3] != -1) {
Charin 0:256d00799c21 149 maquina_general = WIN;
Charin 0:256d00799c21 150 }
Charin 0:256d00799c21 151 else {
Charin 0:256d00799c21 152 maquina_general = CABLE4;
Charin 0:256d00799c21 153 }
Charin 0:256d00799c21 154 if (vector_secuencia [3] != vector_orden_cables [3] && vector_orden_cables [3] != -1) {
Charin 0:256d00799c21 155 maquina_general = LOOSER;
Charin 0:256d00799c21 156 }
Charin 0:256d00799c21 157 break;
Charin 0:256d00799c21 158
Charin 0:256d00799c21 159 case WIN:
Charin 0:256d00799c21 160 verde = 0;
Charin 0:256d00799c21 161 break;
Charin 0:256d00799c21 162 case LOOSER:
Charin 0:256d00799c21 163 verde = 0;
Charin 0:256d00799c21 164 rojo = 0;
Charin 0:256d00799c21 165 break;
Charin 0:256d00799c21 166
Charin 0:256d00799c21 167 }
Charin 0:256d00799c21 168 }
Charin 0:256d00799c21 169
Charin 0:256d00799c21 170 void maquina_orden_cables ()
Charin 0:256d00799c21 171 {
Charin 0:256d00799c21 172 static int a = 0;
Charin 0:256d00799c21 173
Charin 0:256d00799c21 174 if (cable_0 != 0 && cablesDesconectados(0) == 0) {
Charin 0:256d00799c21 175 ccero = 0;
Charin 0:256d00799c21 176 vector_orden_cables [a] = ccero;
Charin 0:256d00799c21 177 a++;
Charin 0:256d00799c21 178 printf ("vector orden cables [%d] = 0", &a);
Charin 0:256d00799c21 179
Charin 0:256d00799c21 180 }
Charin 0:256d00799c21 181 if (cable_1 != 0 && cablesDesconectados(1) == 0) {
Charin 0:256d00799c21 182 cuno = 1;
Charin 0:256d00799c21 183 vector_orden_cables [a] = cuno;
Charin 0:256d00799c21 184 a++;
Charin 0:256d00799c21 185 printf ("vector orden cables [%d] = 1", &a);
Charin 0:256d00799c21 186
Charin 0:256d00799c21 187 }
Charin 0:256d00799c21 188 if (cable_2 != 0 && cablesDesconectados(2) == 0) {
Charin 0:256d00799c21 189 cdos = 2;
Charin 0:256d00799c21 190 vector_orden_cables [a] = cdos;
Charin 0:256d00799c21 191 a++;
Charin 0:256d00799c21 192 printf ("vector orden cables [%d] = 2", &a);
Charin 0:256d00799c21 193 }
Charin 0:256d00799c21 194 if (cable_3 != 0 && cablesDesconectados(3) != 0) {
Charin 0:256d00799c21 195 ctres = 3;
Charin 0:256d00799c21 196 vector_orden_cables [a] = ctres;
Charin 0:256d00799c21 197 a++;
Charin 0:256d00799c21 198 printf ("vector orden cables [%d] = 3", &a);
Charin 0:256d00799c21 199 }
Charin 0:256d00799c21 200 }
Charin 0:256d00799c21 201
Charin 0:256d00799c21 202 int cablesDesconectados(int n)
Charin 0:256d00799c21 203 {
Charin 0:256d00799c21 204
Charin 0:256d00799c21 205 for(int j = 0; j < 4; j++) {
Charin 0:256d00799c21 206 if(vector_orden_cables[j] == n)
Charin 0:256d00799c21 207 return 1;
Charin 0:256d00799c21 208 }
Charin 0:256d00799c21 209 return 0;
Charin 0:256d00799c21 210 }