Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Thu Jan 14 05:27:44 2016 +0000
Revision:
52:060fdec99780
Parent:
49:626e84ce5e52
Child:
53:c1c3e65b4542
D for debug. Misc bugfixes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elmbed 17:d8b901d791fd 1 #include <TA.h>
elmbed 17:d8b901d791fd 2 #include <types.h>
elmbed 17:d8b901d791fd 3
elmbed 17:d8b901d791fd 4 #define RED p3
elmbed 17:d8b901d791fd 5 #define GREEN p5
elmbed 17:d8b901d791fd 6 #define BLUE p6
elmbed 17:d8b901d791fd 7
elmbed 17:d8b901d791fd 8 #define ENABLE_1 p4
elmbed 17:d8b901d791fd 9 #define ENABLE_2 p7
elmbed 17:d8b901d791fd 10 #define ENABLE_3 p8
elmbed 17:d8b901d791fd 11
elmbed 17:d8b901d791fd 12 #define DEBUG_BAUD 57600 //57600
elmbed 26:40a0c775ff27 13 #define NUM_CONES 3
elmbed 17:d8b901d791fd 14 #define STATIONS 20 // max length of a pattern
elmbed 17:d8b901d791fd 15 #define SEQUENCES 9 // number of patterns to store
elmbed 17:d8b901d791fd 16
elmbed 17:d8b901d791fd 17 #define TRILAT_CONE 99
elmbed 17:d8b901d791fd 18
elmbed 17:d8b901d791fd 19 #define DEBOUNCE_MS 100
AntonLS 46:28c29ef61276 20 #define LIGHTS TOUCHLIGHTS
AntonLS 46:28c29ef61276 21 #define FAKEOUT 0x08
AntonLS 46:28c29ef61276 22 #define FAIL_QUICK 0x10
AntonLS 46:28c29ef61276 23 #define SILENT 0x20
elmbed 17:d8b901d791fd 24 #define GRACE_PERIOD 3000
elmbed 17:d8b901d791fd 25
AntonLS 48:4c9551c826e9 26 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need //////////////////////DEBUG messages on the console;
elmbed 17:d8b901d791fd 27 * it will have an impact on code-size and power consumption. */
elmbed 17:d8b901d791fd 28
elmbed 17:d8b901d791fd 29 #define LOOPBACK_MODE 0 // Loopback mode
elmbed 17:d8b901d791fd 30
elmbed 17:d8b901d791fd 31 #if NEED_CONSOLE_OUTPUT
elmbed 17:d8b901d791fd 32 #define DEBUG(...) { printf(__VA_ARGS__); }
elmbed 17:d8b901d791fd 33 #else
elmbed 17:d8b901d791fd 34 #define DEBUG(...) /* nothing */
elmbed 17:d8b901d791fd 35 #endif /* #if NEED_CONSOLE_OUTPUT */
elmbed 17:d8b901d791fd 36
elmbed 18:affef3a7db2a 37 #if 1
elmbed 17:d8b901d791fd 38 extern int random(int numberone, int numbertwo);
elmbed 17:d8b901d791fd 39
elmbed 17:d8b901d791fd 40 unsigned long millis();
elmbed 17:d8b901d791fd 41 unsigned long micros();
elmbed 17:d8b901d791fd 42
elmbed 17:d8b901d791fd 43 TA ta;
elmbed 17:d8b901d791fd 44
elmbed 18:affef3a7db2a 45 static bool active_cones[NUM_CONES + 1]; // + 1 so we can be 1 based like the cone numbers are
elmbed 17:d8b901d791fd 46
elmbed 28:8e74ddc4f70f 47 const static int CONTROL_CONE = 1;
elmbed 28:8e74ddc4f70f 48
elmbed 23:26f27c462976 49 #ifdef MASTER
elmbed 18:affef3a7db2a 50 static Mode_t mode = PATTERN;
elmbed 18:affef3a7db2a 51 static patternState_t state_p = IDLE_P;
elmbed 18:affef3a7db2a 52 static inputState_t state_i = IDLE_I;
elmbed 23:26f27c462976 53 #else
elmbed 23:26f27c462976 54 State_t state = IDLE;
elmbed 23:26f27c462976 55 #endif
elmbed 17:d8b901d791fd 56
elmbed 18:affef3a7db2a 57 static Message m1 = { 'm',0,2 };
elmbed 17:d8b901d791fd 58
elmbed 18:affef3a7db2a 59 static Message *m = &m1;
elmbed 18:affef3a7db2a 60 static Message m2 = { 'm',0,2 };
elmbed 18:affef3a7db2a 61 static Message *m_in = &m2;
elmbed 17:d8b901d791fd 62
elmbed 18:affef3a7db2a 63 static uint8_t active_cone = 0;
elmbed 38:76e49d045a3b 64 static unsigned long timeout = 10000L;
elmbed 18:affef3a7db2a 65 static uint8_t mask = 0x07;
elmbed 18:affef3a7db2a 66 static uint8_t fakeout = 0;
elmbed 18:affef3a7db2a 67 static uint8_t fail_quick = 0;
elmbed 18:affef3a7db2a 68 static uint8_t index = 0;
elmbed 18:affef3a7db2a 69 static bool new_state = false;
elmbed 18:affef3a7db2a 70 static bool tag_start = false; // flag to indicate we should wait for the user to activate the first station before going through the sequence
elmbed 18:affef3a7db2a 71
elmbed 17:d8b901d791fd 72 static bool in_menu = false;
elmbed 17:d8b901d791fd 73 static bool warning = false;
elmbed 17:d8b901d791fd 74 static bool penalty = false;
elmbed 17:d8b901d791fd 75 static bool logging = false;
elmbed 17:d8b901d791fd 76
elmbed 17:d8b901d791fd 77 // course setup (probably should make some of these persistant settings)
elmbed 18:affef3a7db2a 78 static uint8_t active_sequence = 0;
elmbed 18:affef3a7db2a 79 static uint8_t station = 1;
elmbed 18:affef3a7db2a 80 static uint8_t cone = 0;
elmbed 18:affef3a7db2a 81 static uint16_t ltime = 0;
elmbed 17:d8b901d791fd 82
elmbed 17:d8b901d791fd 83 volatile bool triggered;
elmbed 17:d8b901d791fd 84 volatile bool pin;
elmbed 17:d8b901d791fd 85 volatile bool ping = false;
elmbed 18:affef3a7db2a 86
elmbed 17:d8b901d791fd 87 static volatile bool captured = false;
elmbed 17:d8b901d791fd 88 static volatile unsigned long ping_timer = 0;
elmbed 17:d8b901d791fd 89 static volatile unsigned long dist_timeout = 0;
elmbed 17:d8b901d791fd 90
elmbed 17:d8b901d791fd 91 // all sequence data
elmbed 17:d8b901d791fd 92 uint8_t cone_table[STATIONS * SEQUENCES];
elmbed 17:d8b901d791fd 93 uint8_t mask_table[STATIONS * SEQUENCES];
elmbed 17:d8b901d791fd 94 uint16_t time_table[STATIONS * SEQUENCES];
elmbed 17:d8b901d791fd 95
elmbed 17:d8b901d791fd 96 // pointer to active table
elmbed 17:d8b901d791fd 97 uint8_t *cones = (uint8_t*)cone_table;
elmbed 17:d8b901d791fd 98 uint8_t *masks = (uint8_t*)mask_table;
elmbed 17:d8b901d791fd 99 uint16_t *times = (uint16_t*)time_table;
elmbed 17:d8b901d791fd 100
elmbed 17:d8b901d791fd 101 static bool lonely = false;
elmbed 17:d8b901d791fd 102
elmbed 17:d8b901d791fd 103 // Function prototypes
elmbed 23:26f27c462976 104 #ifdef MASTER
elmbed 17:d8b901d791fd 105 void spin();
elmbed 17:d8b901d791fd 106 patternState_t stateFromCone(uint8_t cone);
elmbed 17:d8b901d791fd 107 void printSplit(unsigned long timer);
elmbed 17:d8b901d791fd 108 uint8_t getRandomCone(void);
elmbed 17:d8b901d791fd 109 void clearCones(void);
elmbed 17:d8b901d791fd 110 void powerupCones(uint8_t sound);
elmbed 17:d8b901d791fd 111 void failCones(void);
elmbed 17:d8b901d791fd 112 void resetSensors(void);
elmbed 17:d8b901d791fd 113 void successCones(void);
elmbed 17:d8b901d791fd 114 void find_cones(void);
elmbed 17:d8b901d791fd 115 void printMsAsSeconds(unsigned long number);
elmbed 17:d8b901d791fd 116 void spinButtons(void);
elmbed 17:d8b901d791fd 117 uint8_t checkButtons(void);
elmbed 17:d8b901d791fd 118 Event getInputEvent(void);
elmbed 17:d8b901d791fd 119 void getRadioInput(char *ibuffer, int size);
elmbed 17:d8b901d791fd 120 void interpret(char parameter, int value);
elmbed 23:26f27c462976 121 #endif
elmbed 23:26f27c462976 122
elmbed 23:26f27c462976 123 extern "C" void writeToPhone(char *format, ...);
elmbed 17:d8b901d791fd 124
elmbed 38:76e49d045a3b 125
AntonLS 39:b1f864b71318 126 static const int active_colour = 0x00FF00; /// 0x00FF;
AntonLS 39:b1f864b71318 127 static const int warn_colour = 0xFFA500; /// 0xF050;
AntonLS 52:060fdec99780 128 static const int fail_colour = 0xFF0000; /// 0x0000FF;
AntonLS 39:b1f864b71318 129 static const int touch_colour = 0x0000FF; /// 0x005050;
AntonLS 39:b1f864b71318 130 static const int success_colour = 0x00FF00; /// 0xFFFF;
AntonLS 39:b1f864b71318 131 static const int pen_colour = 0xFFFF00; /// 0x10FF;
AntonLS 39:b1f864b71318 132 static const int idle_colour = 0xFF0000; /// 0x00;
elmbed 38:76e49d045a3b 133
elmbed 17:d8b901d791fd 134 char local_input[50] = {0};
elmbed 17:d8b901d791fd 135
elmbed 17:d8b901d791fd 136 //DigitalOut red(RED);
elmbed 17:d8b901d791fd 137 //DigitalOut enable1(ENABLE_1);
elmbed 17:d8b901d791fd 138 //DigitalOut green(GREEN);
elmbed 17:d8b901d791fd 139 //DigitalOut blue(BLUE);
elmbed 17:d8b901d791fd 140 //DigitalOut enable2(ENABLE_2);
elmbed 17:d8b901d791fd 141 //DigitalOut enable3(ENABLE_3);
elmbed 17:d8b901d791fd 142
elmbed 23:26f27c462976 143 #ifdef MASTER
elmbed 23:26f27c462976 144 static void master_setup()
elmbed 17:d8b901d791fd 145 {
elmbed 28:8e74ddc4f70f 146 ta.initialize(NODE_ID);
elmbed 17:d8b901d791fd 147
elmbed 17:d8b901d791fd 148 uint16_t sent = 0;
elmbed 17:d8b901d791fd 149 uint16_t lost = 0;
elmbed 17:d8b901d791fd 150 unsigned long start = micros();
elmbed 17:d8b901d791fd 151 //while (millis() - now <= ACK_TIME){
elmbed 17:d8b901d791fd 152 m->command = 'p';
elmbed 17:d8b901d791fd 153 m->value = 0;
elmbed 17:d8b901d791fd 154 m->cone = 0;
elmbed 17:d8b901d791fd 155
elmbed 23:26f27c462976 156
elmbed 23:26f27c462976 157 //find_cones(); // this causes the beep to be interrupted
elmbed 23:26f27c462976 158 //ta.beep(100);
elmbed 17:d8b901d791fd 159 int i;
elmbed 23:26f27c462976 160
elmbed 28:8e74ddc4f70f 161 for(i = 1; i < NUM_CONES + 1; ++i)
elmbed 28:8e74ddc4f70f 162 {
elmbed 28:8e74ddc4f70f 163 active_cones[i] = false;
elmbed 28:8e74ddc4f70f 164 }
elmbed 17:d8b901d791fd 165
elmbed 28:8e74ddc4f70f 166 unsigned long time = 0;
elmbed 17:d8b901d791fd 167
elmbed 17:d8b901d791fd 168 // pull course sequences from non-volatile memory
elmbed 28:8e74ddc4f70f 169 for(uint8_t i = 0; i <STATIONS * SEQUENCES; ++i)
elmbed 28:8e74ddc4f70f 170 {
elmbed 17:d8b901d791fd 171 cone_table[i] = i+1;
elmbed 17:d8b901d791fd 172 mask_table[i] = 1;
elmbed 17:d8b901d791fd 173 time_table[i] = 1000;
elmbed 17:d8b901d791fd 174 }
elmbed 18:affef3a7db2a 175
elmbed 18:affef3a7db2a 176 ta.post_color(0xFF0000);
elmbed 40:dec5270e03e9 177
AntonLS 48:4c9551c826e9 178 srand( rndHW() );
elmbed 17:d8b901d791fd 179 }
elmbed 23:26f27c462976 180 #else
elmbed 23:26f27c462976 181 static void slave_setup()
elmbed 23:26f27c462976 182 {
elmbed 28:8e74ddc4f70f 183 ta.initialize(NODE_ID);
AntonLS 46:28c29ef61276 184 /// ta.beep(250);
elmbed 23:26f27c462976 185 }
elmbed 23:26f27c462976 186 #endif
elmbed 23:26f27c462976 187
elmbed 23:26f27c462976 188 void setup()
elmbed 23:26f27c462976 189 {
elmbed 23:26f27c462976 190 #ifdef MASTER
elmbed 23:26f27c462976 191 master_setup();
elmbed 23:26f27c462976 192 #else
elmbed 23:26f27c462976 193 slave_setup();
elmbed 23:26f27c462976 194 #endif
elmbed 23:26f27c462976 195 }
elmbed 23:26f27c462976 196
elmbed 23:26f27c462976 197 #ifdef SLAVE
elmbed 23:26f27c462976 198 static void slave_loop()
elmbed 23:26f27c462976 199 {
elmbed 23:26f27c462976 200 static unsigned long start = 0;
elmbed 23:26f27c462976 201 static unsigned long timeout;
elmbed 23:26f27c462976 202 static bool process_next_as_time = false;
elmbed 23:26f27c462976 203 static bool process_next_as_mask = false;
elmbed 23:26f27c462976 204 static unsigned long timer;
elmbed 23:26f27c462976 205 static State_t last_state = IDLE;
elmbed 28:8e74ddc4f70f 206 static int counter = 0;
elmbed 23:26f27c462976 207
AntonLS 46:28c29ef61276 208 ta.spin();
AntonLS 46:28c29ef61276 209
elmbed 23:26f27c462976 210 if(last_state != state)
elmbed 23:26f27c462976 211 {
elmbed 23:26f27c462976 212 if(state == ACTIVE_TARGET)
AntonLS 48:4c9551c826e9 213 DEBUG("State is ACTIVE_TARGET\r\n");
elmbed 23:26f27c462976 214 if(state == IDLE)
AntonLS 48:4c9551c826e9 215 DEBUG("State is IDLE\r\n");
elmbed 23:26f27c462976 216 if(state == FAIL)
AntonLS 48:4c9551c826e9 217 DEBUG("State is FAIL\r\n");
elmbed 23:26f27c462976 218 if(state == SUCCESS)
AntonLS 48:4c9551c826e9 219 DEBUG("State is SUCCESS\r\n");
elmbed 23:26f27c462976 220 }
elmbed 23:26f27c462976 221 last_state = state;
AntonLS 31:a6110950f385 222
elmbed 23:26f27c462976 223 char message = 'n';
elmbed 23:26f27c462976 224 timer = millis() - start;
elmbed 23:26f27c462976 225
elmbed 23:26f27c462976 226 if(ta.recieve(m_in))
elmbed 23:26f27c462976 227 {
elmbed 28:8e74ddc4f70f 228 //ta.beep(1000);
elmbed 28:8e74ddc4f70f 229
elmbed 23:26f27c462976 230 message = m_in->command;
elmbed 23:26f27c462976 231 switch(m_in->command)
elmbed 23:26f27c462976 232 {
elmbed 23:26f27c462976 233 case 't':
AntonLS 52:060fdec99780 234 timeout = m_in->value;
elmbed 23:26f27c462976 235 //serial.printf("timeout: ");
elmbed 23:26f27c462976 236 //serial.printf("%d\n",timeout);
elmbed 23:26f27c462976 237 break;
elmbed 23:26f27c462976 238 case 'm':
elmbed 23:26f27c462976 239 //serial.printf("config bits: ");
elmbed 23:26f27c462976 240 mask = m_in->value;
elmbed 23:26f27c462976 241 ta.setMask(mask);// & LIGHTS);
elmbed 23:26f27c462976 242 //serial.printf("%s\n",byte_to_binary(mask));
elmbed 23:26f27c462976 243 fakeout = mask & FAKEOUT;
elmbed 23:26f27c462976 244
elmbed 23:26f27c462976 245 //if(fakeout)
elmbed 23:26f27c462976 246 // serial.printf("fakeout!");
elmbed 23:26f27c462976 247
elmbed 23:26f27c462976 248 fail_quick = mask & FAIL_QUICK;
elmbed 23:26f27c462976 249 break;
elmbed 23:26f27c462976 250 default: break;
elmbed 23:26f27c462976 251 }
elmbed 23:26f27c462976 252 }
elmbed 23:26f27c462976 253
elmbed 23:26f27c462976 254 if(message == 'x')
elmbed 23:26f27c462976 255 {
AntonLS 46:28c29ef61276 256 ta.resetTouch();
elmbed 23:26f27c462976 257 }
elmbed 23:26f27c462976 258
elmbed 23:26f27c462976 259 if(message == 'f')
elmbed 23:26f27c462976 260 { // Fail
elmbed 23:26f27c462976 261 //serial.printf("Fail!\n");
AntonLS 52:060fdec99780 262 DEBUG("FAIL\r\n");
AntonLS 46:28c29ef61276 263 /// ta.post_color(fail_colour);
AntonLS 46:28c29ef61276 264 ta.pulse(25,200,3000,fail_colour);
elmbed 23:26f27c462976 265 state = FAIL;
elmbed 23:26f27c462976 266 }
elmbed 23:26f27c462976 267
elmbed 23:26f27c462976 268 if(message == 's')
elmbed 23:26f27c462976 269 { // Success
elmbed 23:26f27c462976 270 //serial.printf("Success!\n");
AntonLS 52:060fdec99780 271 ta.post_color(success_colour);
elmbed 23:26f27c462976 272 ta.beep(1500);
elmbed 23:26f27c462976 273 state = SUCCESS;
elmbed 23:26f27c462976 274 }
elmbed 23:26f27c462976 275
elmbed 23:26f27c462976 276 if(message == 'g')
elmbed 23:26f27c462976 277 { // This cone is the active target, GO!
elmbed 23:26f27c462976 278 start = millis();
elmbed 23:26f27c462976 279 warning = false;
elmbed 23:26f27c462976 280 penalty = false;
AntonLS 46:28c29ef61276 281 /// ta.post_color(active_colour);
AntonLS 46:28c29ef61276 282 ta.pulse(50,750,~0L,active_colour);
elmbed 23:26f27c462976 283 state = ACTIVE_TARGET;
elmbed 23:26f27c462976 284 //serial.printf("fakeout, fail_quick\n");
elmbed 23:26f27c462976 285 //serial.printf("%d",fakeout);
elmbed 23:26f27c462976 286 //serial.printf(", ");
elmbed 23:26f27c462976 287 //serial.printf("%d\n",fail_quick);
elmbed 23:26f27c462976 288 }
elmbed 23:26f27c462976 289
elmbed 23:26f27c462976 290 if(message == 'q')
elmbed 23:26f27c462976 291 { // Quit
AntonLS 52:060fdec99780 292 DEBUG("Clear!\r\n");
elmbed 23:26f27c462976 293 ta.pulse_off();
AntonLS 52:060fdec99780 294 // ta.mask_color(idle_colour);
elmbed 23:26f27c462976 295 state = IDLE;
elmbed 23:26f27c462976 296 }
elmbed 23:26f27c462976 297
elmbed 23:26f27c462976 298 if(message != 'n')
elmbed 23:26f27c462976 299 {
elmbed 23:26f27c462976 300 //serial.printf("%d",message);
elmbed 23:26f27c462976 301 //serial.printf(", ");
elmbed 23:26f27c462976 302 //serial.printf("%d\n",m_in->value);
elmbed 23:26f27c462976 303 }
elmbed 23:26f27c462976 304
elmbed 23:26f27c462976 305 if(message == 'u')
elmbed 23:26f27c462976 306 { // powerup noise
elmbed 23:26f27c462976 307 ta.powerup(m_in->value);
elmbed 23:26f27c462976 308 }
elmbed 23:26f27c462976 309
elmbed 23:26f27c462976 310 if(message == 'z')
elmbed 23:26f27c462976 311 {
elmbed 23:26f27c462976 312 m->value = m_in->value;
elmbed 23:26f27c462976 313 m->command = 'z';
elmbed 28:8e74ddc4f70f 314 m->cone = 1;
elmbed 23:26f27c462976 315 ta.send(m);
elmbed 23:26f27c462976 316 }
elmbed 23:26f27c462976 317
elmbed 23:26f27c462976 318 timer = millis() - start;
elmbed 23:26f27c462976 319
elmbed 23:26f27c462976 320 switch(state)
elmbed 23:26f27c462976 321 {
elmbed 23:26f27c462976 322 case ACTIVE_TARGET:
elmbed 23:26f27c462976 323 if(ta.activated() || ((timer > timeout) && (fakeout)))
elmbed 23:26f27c462976 324 {
elmbed 23:26f27c462976 325 m->command = 'd';
elmbed 23:26f27c462976 326 m->cone = CONTROL_CONE;
elmbed 23:26f27c462976 327 ta.send(m);
AntonLS 46:28c29ef61276 328 /// ta.post_color(success_colour);
elmbed 23:26f27c462976 329 ta.pulse_off();
elmbed 23:26f27c462976 330 state = IDLE;
elmbed 23:26f27c462976 331 //serial.printf("Done!\n");
elmbed 23:26f27c462976 332 }
elmbed 23:26f27c462976 333 else if(timer > timeout && !penalty)
elmbed 23:26f27c462976 334 {
AntonLS 52:060fdec99780 335 DEBUG("PEN\r\n");
elmbed 23:26f27c462976 336 //serial.printf("Penalty!\n");
elmbed 23:26f27c462976 337 penalty = true;
AntonLS 46:28c29ef61276 338 ta.pulse(350,600,~0L,pen_colour);
AntonLS 46:28c29ef61276 339 /// ta.post_color(pen_colour);
elmbed 23:26f27c462976 340 }
elmbed 23:26f27c462976 341 else if(timer > ((timeout*3)/4) && !warning)
elmbed 23:26f27c462976 342 {
AntonLS 52:060fdec99780 343 DEBUG("WARN\r\n");
elmbed 23:26f27c462976 344 //serial.printf("Warning!\n");
elmbed 23:26f27c462976 345 warning = true;
AntonLS 46:28c29ef61276 346 ta.pulse(350,750,~0L,warn_colour);
AntonLS 46:28c29ef61276 347 /// ta.post_color(warn_colour);
elmbed 23:26f27c462976 348 }
elmbed 23:26f27c462976 349 break;
elmbed 23:26f27c462976 350 case IDLE:
AntonLS 52:060fdec99780 351 bool act = ta.activated();
elmbed 38:76e49d045a3b 352 ta.post_color(act?touch_colour:idle_colour);
elmbed 23:26f27c462976 353 break;
elmbed 23:26f27c462976 354 default:
elmbed 23:26f27c462976 355 break;
elmbed 23:26f27c462976 356 }
elmbed 23:26f27c462976 357 }
elmbed 23:26f27c462976 358 #endif
elmbed 23:26f27c462976 359
elmbed 23:26f27c462976 360 #ifdef MASTER
elmbed 23:26f27c462976 361 static void master_loop()
elmbed 23:26f27c462976 362 {
elmbed 23:26f27c462976 363 static Mode_t last_mode = mode;
elmbed 28:8e74ddc4f70f 364 static int counter = 0;
elmbed 23:26f27c462976 365
elmbed 23:26f27c462976 366 if (last_mode != mode)
elmbed 23:26f27c462976 367 {
elmbed 23:26f27c462976 368 if (mode == FREEFORM)
AntonLS 52:060fdec99780 369 {
AntonLS 48:4c9551c826e9 370 srnd( rndHW() );
elmbed 38:76e49d045a3b 371 writeToPhone("RR.\r\n");
AntonLS 52:060fdec99780 372 }
elmbed 23:26f27c462976 373 if (mode == PATTERN)
elmbed 38:76e49d045a3b 374 writeToPhone("SP.\r\n");
elmbed 23:26f27c462976 375 }
elmbed 23:26f27c462976 376
elmbed 23:26f27c462976 377 last_mode = mode;
elmbed 23:26f27c462976 378
elmbed 23:26f27c462976 379 ta.spin();
elmbed 23:26f27c462976 380 //spinButtons();
elmbed 38:76e49d045a3b 381 //if((logging || micros() < dist_timeout) && ta.recieve(m_in))
elmbed 38:76e49d045a3b 382 // writeToPhone("CMD: %c",m_in->command);
elmbed 38:76e49d045a3b 383 // else
elmbed 23:26f27c462976 384 spin();
elmbed 23:26f27c462976 385 }
elmbed 23:26f27c462976 386 #endif
elmbed 17:d8b901d791fd 387
elmbed 17:d8b901d791fd 388 void loop()
elmbed 17:d8b901d791fd 389 {
elmbed 23:26f27c462976 390 #ifdef MASTER
elmbed 23:26f27c462976 391 master_loop();
elmbed 23:26f27c462976 392 #else
elmbed 23:26f27c462976 393 slave_loop();
elmbed 23:26f27c462976 394 #endif
elmbed 17:d8b901d791fd 395 }
elmbed 17:d8b901d791fd 396
elmbed 23:26f27c462976 397 static void getNext()
elmbed 17:d8b901d791fd 398 {
elmbed 23:26f27c462976 399 #ifdef MASTER
elmbed 17:d8b901d791fd 400 if(mode == FREEFORM)
elmbed 17:d8b901d791fd 401 {
elmbed 40:dec5270e03e9 402 active_cone = getRandomCone();
AntonLS 52:060fdec99780 403
AntonLS 52:060fdec99780 404 if( Dbg ) writeToPhone("GN_AC: %d\r\n", active_cone);
AntonLS 52:060fdec99780 405
elmbed 17:d8b901d791fd 406 mask = 0x07;
elmbed 17:d8b901d791fd 407 ta.setMask(mask & LIGHTS);
AntonLS 46:28c29ef61276 408 timeout = ~0;
elmbed 38:76e49d045a3b 409
elmbed 17:d8b901d791fd 410 return;
elmbed 17:d8b901d791fd 411 }
elmbed 17:d8b901d791fd 412
elmbed 17:d8b901d791fd 413 active_cone = cones[index];
elmbed 17:d8b901d791fd 414 mask = masks[index];
elmbed 17:d8b901d791fd 415 fakeout = mask & FAKEOUT;
elmbed 17:d8b901d791fd 416 fail_quick = mask & FAIL_QUICK;
elmbed 17:d8b901d791fd 417 timeout = times[index];
elmbed 17:d8b901d791fd 418 index++;
AntonLS 48:4c9551c826e9 419 writeToPhone( "Next cone is %u\r\n", active_cone ); // PROTOCOL
elmbed 23:26f27c462976 420 #endif
elmbed 17:d8b901d791fd 421 }
elmbed 18:affef3a7db2a 422 #if 1
elmbed 23:26f27c462976 423 static void spin()
elmbed 17:d8b901d791fd 424 {
AntonLS 39:b1f864b71318 425 #ifdef MASTER
elmbed 17:d8b901d791fd 426 static patternState_t last_state = START_P;
elmbed 17:d8b901d791fd 427 static uint8_t last_cone = 255;
elmbed 17:d8b901d791fd 428 static unsigned long start;
elmbed 17:d8b901d791fd 429 static unsigned long timer;
elmbed 17:d8b901d791fd 430 static bool first; // first should be true when we first enter a state (even if we just exited the same state)
elmbed 17:d8b901d791fd 431 char command = 'n';
elmbed 17:d8b901d791fd 432 unsigned long value = 0;
elmbed 17:d8b901d791fd 433
elmbed 17:d8b901d791fd 434 m_in->command = 0;
elmbed 17:d8b901d791fd 435 m_in->value = 0;
elmbed 17:d8b901d791fd 436 m_in->cone = 0;
elmbed 17:d8b901d791fd 437
elmbed 17:d8b901d791fd 438 if(ta.recieve(m_in))
elmbed 17:d8b901d791fd 439 {
AntonLS 52:060fdec99780 440 if( Dbg ) writeToPhone("SR: %d '%c'\r\n", m_in->cone, m_in->command);
elmbed 17:d8b901d791fd 441 command = m_in->command;
elmbed 17:d8b901d791fd 442 value = m_in->value;
elmbed 17:d8b901d791fd 443 }
elmbed 17:d8b901d791fd 444
elmbed 17:d8b901d791fd 445 first = false;
elmbed 17:d8b901d791fd 446
elmbed 17:d8b901d791fd 447 if(last_state != state_p || new_state)
elmbed 17:d8b901d791fd 448 {
elmbed 17:d8b901d791fd 449 if(state_p == START_P)
AntonLS 39:b1f864b71318 450 writeToPhone("State is START\r\n"); // PROTOCOL
elmbed 17:d8b901d791fd 451 if(state_p == WAITING_P)
AntonLS 39:b1f864b71318 452 writeToPhone("State is WAITING\r\n"); // PROTOCOL
elmbed 17:d8b901d791fd 453 if(state_p == ACTIVE_TARGET_P)
AntonLS 39:b1f864b71318 454 writeToPhone("State is ACTIVE_TARGET\r\n"); // PROTOCOL
elmbed 17:d8b901d791fd 455 if(state_p == IDLE_P)
AntonLS 39:b1f864b71318 456 writeToPhone("State is IDLE\r\n"); // PROTOCOL
elmbed 17:d8b901d791fd 457 if(state_p == FAIL_P)
AntonLS 39:b1f864b71318 458 writeToPhone("State is FAIL\r\n"); // PROTOCOL
elmbed 17:d8b901d791fd 459 if(state_p == SUCCESS_P)
AntonLS 39:b1f864b71318 460 writeToPhone("State is SUCCESS\r\n"); // PROTOCOL
elmbed 17:d8b901d791fd 461
elmbed 17:d8b901d791fd 462 first = true;
elmbed 17:d8b901d791fd 463 new_state = false;
elmbed 17:d8b901d791fd 464 }
elmbed 17:d8b901d791fd 465
elmbed 17:d8b901d791fd 466 last_state = state_p;
elmbed 17:d8b901d791fd 467 last_cone = active_cone;
elmbed 17:d8b901d791fd 468
elmbed 17:d8b901d791fd 469 timer = millis() - start;
elmbed 17:d8b901d791fd 470
elmbed 17:d8b901d791fd 471 switch(state_p)
elmbed 17:d8b901d791fd 472 {
elmbed 17:d8b901d791fd 473 case IDLE_P:
AntonLS 30:c60b0d52b067 474 ta.setMask( DEFTOUCHMASK );
elmbed 17:d8b901d791fd 475 if(!in_menu)
elmbed 38:76e49d045a3b 476 ta.post_color((ta.activated())?touch_colour:idle_colour);
elmbed 17:d8b901d791fd 477 break;
elmbed 17:d8b901d791fd 478
elmbed 17:d8b901d791fd 479 case START_P:
elmbed 38:76e49d045a3b 480 clearCones();
elmbed 17:d8b901d791fd 481 tag_start = false;
elmbed 38:76e49d045a3b 482 ta.post_color(idle_colour);
elmbed 17:d8b901d791fd 483 index = 0;
elmbed 17:d8b901d791fd 484 getNext();
elmbed 17:d8b901d791fd 485
elmbed 17:d8b901d791fd 486 if(timeout == 0)
elmbed 17:d8b901d791fd 487 { // timeout of 0 means wait indefinitely for the first cone to be activated, then start the rest of the sequence
elmbed 17:d8b901d791fd 488 timeout = ~0;
elmbed 17:d8b901d791fd 489 tag_start = true;
AntonLS 39:b1f864b71318 490 // DEBUG("Activate cone "); // May use this again in future.
AntonLS 39:b1f864b71318 491 // DEBUG("%d",active_cone);
AntonLS 39:b1f864b71318 492 // DEBUG(" to start.\n");
elmbed 38:76e49d045a3b 493 writeToPhone("ACS: %d\r\n", active_cone);
elmbed 17:d8b901d791fd 494 }
elmbed 17:d8b901d791fd 495
elmbed 17:d8b901d791fd 496 start = millis();
elmbed 17:d8b901d791fd 497 state_p = stateFromCone(active_cone);
elmbed 17:d8b901d791fd 498 if(active_cone == 0)
elmbed 17:d8b901d791fd 499 { // in this case stateFromCone returns SUCCESS_P, but really there was no course sequence
elmbed 17:d8b901d791fd 500 DEBUG("\n");
elmbed 17:d8b901d791fd 501 DEBUG("Empty course sequence!\n");
elmbed 38:76e49d045a3b 502 writeToPhone("ECS\r\n");
elmbed 17:d8b901d791fd 503 ta.post_color(0x0000FF);
elmbed 17:d8b901d791fd 504 state_p = IDLE_P;
elmbed 17:d8b901d791fd 505 break;
elmbed 17:d8b901d791fd 506 }
elmbed 17:d8b901d791fd 507 break;
elmbed 17:d8b901d791fd 508
elmbed 17:d8b901d791fd 509 case WAITING_P:
elmbed 17:d8b901d791fd 510 if(first)
elmbed 17:d8b901d791fd 511 {
elmbed 17:d8b901d791fd 512 m->command = 'm';
elmbed 17:d8b901d791fd 513 m->value = mask;
elmbed 17:d8b901d791fd 514 m->cone = active_cone;
elmbed 17:d8b901d791fd 515 ta.send(m);
AntonLS 52:060fdec99780 516
AntonLS 52:060fdec99780 517 wait_ms(50);
AntonLS 52:060fdec99780 518 /// wait_ms(300);
elmbed 17:d8b901d791fd 519 m->command = 't';
elmbed 17:d8b901d791fd 520 m->value = (uint32_t)timeout;
elmbed 17:d8b901d791fd 521 ta.send(m);
AntonLS 52:060fdec99780 522
AntonLS 52:060fdec99780 523 wait_ms(50);
AntonLS 52:060fdec99780 524 /// wait_ms(300);
elmbed 17:d8b901d791fd 525 m->command = 'g';
elmbed 17:d8b901d791fd 526 ta.send(m);
elmbed 17:d8b901d791fd 527
AntonLS 52:060fdec99780 528 if( Dbg ) writeToPhone("ACW: %d\r\n", active_cone);
elmbed 17:d8b901d791fd 529 }
elmbed 17:d8b901d791fd 530
elmbed 38:76e49d045a3b 531 ta.post_color(( ta.activated())?touch_colour:idle_colour);
elmbed 17:d8b901d791fd 532
elmbed 17:d8b901d791fd 533 DEBUG("Waiting: %d %d %c\n", m_in->cone, active_cone, command);
elmbed 17:d8b901d791fd 534
elmbed 17:d8b901d791fd 535 if((m_in->cone == active_cone && command == 'd' ) || ((timer >= timeout) && fakeout))
elmbed 17:d8b901d791fd 536 {
elmbed 17:d8b901d791fd 537 if((timer >= timeout) && !fakeout)
elmbed 17:d8b901d791fd 538 {
AntonLS 52:060fdec99780 539 writeToPhone("Timesplit penalty! "); // PROTOCOL
elmbed 17:d8b901d791fd 540 }
AntonLS 52:060fdec99780 541 printSplit(timer); // PROTOCOL
elmbed 17:d8b901d791fd 542 getNext();
elmbed 17:d8b901d791fd 543 start = millis();
elmbed 17:d8b901d791fd 544
elmbed 17:d8b901d791fd 545 state_p = stateFromCone(active_cone);
elmbed 17:d8b901d791fd 546
elmbed 17:d8b901d791fd 547 if(state_p == WAITING_P)
elmbed 17:d8b901d791fd 548 {
AntonLS 52:060fdec99780 549 if( Dbg ) writeToPhone("NSW\r\n");
elmbed 17:d8b901d791fd 550 new_state = true;
elmbed 17:d8b901d791fd 551 }
elmbed 17:d8b901d791fd 552 }
elmbed 17:d8b901d791fd 553 else if(~timeout != 0 && mode == PATTERN && timer >= (timeout + ((fail_quick)?0:GRACE_PERIOD)))
elmbed 17:d8b901d791fd 554 {
AntonLS 52:060fdec99780 555 if( Dbg ) writeToPhone("FW\r\n");
elmbed 17:d8b901d791fd 556 state_p = FAIL_P;
elmbed 17:d8b901d791fd 557 }
elmbed 17:d8b901d791fd 558 break;
elmbed 17:d8b901d791fd 559
elmbed 17:d8b901d791fd 560 case ACTIVE_TARGET_P:
elmbed 17:d8b901d791fd 561 if(first)
elmbed 17:d8b901d791fd 562 {
elmbed 17:d8b901d791fd 563 ta.setMask(mask);
elmbed 17:d8b901d791fd 564
elmbed 17:d8b901d791fd 565 warning = false;
elmbed 17:d8b901d791fd 566 penalty = false;
AntonLS 46:28c29ef61276 567 /// ta.post_color(active_colour);
AntonLS 46:28c29ef61276 568 ta.pulse(50,750,~0L,active_colour);
AntonLS 46:28c29ef61276 569 //if(!(mask & SILENT))ta.pulse(50,750,~0L,active_colour);
elmbed 17:d8b901d791fd 570 }
elmbed 17:d8b901d791fd 571 if(timer >= timeout)
elmbed 17:d8b901d791fd 572 {
elmbed 17:d8b901d791fd 573 if(!penalty)
elmbed 17:d8b901d791fd 574 {
AntonLS 46:28c29ef61276 575 /// ta.post_color(pen_colour);
AntonLS 46:28c29ef61276 576 ta.pulse(50,325,~0L,pen_colour);
elmbed 17:d8b901d791fd 577
elmbed 17:d8b901d791fd 578 penalty = true;
elmbed 17:d8b901d791fd 579 }
elmbed 38:76e49d045a3b 580 ta.post_color(pen_colour);
elmbed 17:d8b901d791fd 581 }
elmbed 17:d8b901d791fd 582 else if(timer > ((timeout*3)/4) && !warning)
elmbed 17:d8b901d791fd 583 {
elmbed 17:d8b901d791fd 584 warning = true;
AntonLS 46:28c29ef61276 585 /// ta.post_color(warn_colour);
AntonLS 46:28c29ef61276 586 ta.pulse(50,750,~0L,warn_colour);
elmbed 17:d8b901d791fd 587 }
elmbed 17:d8b901d791fd 588
elmbed 17:d8b901d791fd 589 if(ta.activated() || ((timer >= timeout) && fakeout))
elmbed 17:d8b901d791fd 590 {
elmbed 17:d8b901d791fd 591 if((timer >= timeout) && !fakeout)
AntonLS 52:060fdec99780 592 writeToPhone("Timesplit penalty! "); // PROTOCOL
AntonLS 52:060fdec99780 593 printSplit(timer); // PROTOCOL
AntonLS 52:060fdec99780 594
elmbed 38:76e49d045a3b 595 ta.post_color(success_colour);
elmbed 17:d8b901d791fd 596 getNext();
elmbed 17:d8b901d791fd 597 start = millis();
elmbed 17:d8b901d791fd 598 state_p = stateFromCone(active_cone);
elmbed 17:d8b901d791fd 599
elmbed 17:d8b901d791fd 600 if(state_p == ACTIVE_TARGET_P)
elmbed 17:d8b901d791fd 601 new_state = true;
elmbed 17:d8b901d791fd 602 ta.pulse_off();
elmbed 17:d8b901d791fd 603
elmbed 17:d8b901d791fd 604 }
elmbed 17:d8b901d791fd 605 else if(~timeout != 0 && mode == PATTERN && timer >= (timeout + ((fail_quick)?0:GRACE_PERIOD)))
elmbed 17:d8b901d791fd 606 {
elmbed 17:d8b901d791fd 607 state_p = FAIL_P;
elmbed 17:d8b901d791fd 608 ta.pulse_off();
elmbed 17:d8b901d791fd 609 }
elmbed 17:d8b901d791fd 610 //DEBUG.println(timeout + ((fail_quick)?0:GRACE_PERIOD));
elmbed 17:d8b901d791fd 611 //DEBUG.println(timeout);
elmbed 17:d8b901d791fd 612
elmbed 17:d8b901d791fd 613 break;
elmbed 17:d8b901d791fd 614
elmbed 17:d8b901d791fd 615 case FAIL_P:
elmbed 17:d8b901d791fd 616 if(first)
elmbed 17:d8b901d791fd 617 {
elmbed 17:d8b901d791fd 618 start = millis();
elmbed 17:d8b901d791fd 619 DEBUG("\n");
elmbed 17:d8b901d791fd 620 DEBUG("Timeout\n");
elmbed 17:d8b901d791fd 621 //ta.post_color();
AntonLS 46:28c29ef61276 622 ta.pulse(25,200,3000,fail_colour);
AntonLS 46:28c29ef61276 623 /// ta.post_color(fail_colour);
elmbed 17:d8b901d791fd 624
elmbed 17:d8b901d791fd 625 failCones();
elmbed 17:d8b901d791fd 626 //ta.fail();
elmbed 17:d8b901d791fd 627 }
elmbed 17:d8b901d791fd 628 else if(timer > 3000)
elmbed 17:d8b901d791fd 629 {
elmbed 17:d8b901d791fd 630 DEBUG("Clear!\n");
elmbed 17:d8b901d791fd 631 state_p = IDLE_P;
elmbed 17:d8b901d791fd 632 clearCones();
elmbed 17:d8b901d791fd 633 }
elmbed 17:d8b901d791fd 634 break;
elmbed 17:d8b901d791fd 635 case SUCCESS_P:
elmbed 17:d8b901d791fd 636 if(first)
elmbed 17:d8b901d791fd 637 {
elmbed 17:d8b901d791fd 638 start = millis();
elmbed 17:d8b901d791fd 639 DEBUG("\n");
elmbed 17:d8b901d791fd 640 DEBUG("Done!\n");
elmbed 38:76e49d045a3b 641 ta.post_color(success_colour);
elmbed 17:d8b901d791fd 642 ta.beep(1500);
elmbed 17:d8b901d791fd 643 successCones();
elmbed 17:d8b901d791fd 644 //ta.success();
elmbed 17:d8b901d791fd 645 }
elmbed 17:d8b901d791fd 646 else if(timer > 1500)
elmbed 17:d8b901d791fd 647 {
elmbed 17:d8b901d791fd 648 DEBUG("Clear!\n");
elmbed 17:d8b901d791fd 649 state_p = IDLE_P;
elmbed 17:d8b901d791fd 650 clearCones();
elmbed 17:d8b901d791fd 651 }
elmbed 17:d8b901d791fd 652 break;
elmbed 17:d8b901d791fd 653
elmbed 17:d8b901d791fd 654 default:
elmbed 38:76e49d045a3b 655 ta.post_color(idle_colour);
elmbed 17:d8b901d791fd 656 break;
elmbed 17:d8b901d791fd 657 }
elmbed 23:26f27c462976 658 #endif
elmbed 17:d8b901d791fd 659 }
elmbed 18:affef3a7db2a 660 #endif
elmbed 18:affef3a7db2a 661
elmbed 23:26f27c462976 662 #ifdef MASTER
elmbed 17:d8b901d791fd 663 void getRadioInput(char *ibuffer, int size)
elmbed 17:d8b901d791fd 664 {
elmbed 17:d8b901d791fd 665 static int i = 0;
elmbed 17:d8b901d791fd 666 int buffer_counter = 0;
elmbed 17:d8b901d791fd 667 static char parameter = '_';
elmbed 17:d8b901d791fd 668 static char buffer[MAX_LEN + 1];
elmbed 17:d8b901d791fd 669 int value = 0;
elmbed 17:d8b901d791fd 670 char *endp = NULL;
AntonLS 45:1eb335c00cb2 671
elmbed 17:d8b901d791fd 672 // listen for commands coming over bluetooth
elmbed 17:d8b901d791fd 673 while (buffer_counter < size)
elmbed 17:d8b901d791fd 674 {
elmbed 17:d8b901d791fd 675 char ch = ibuffer[buffer_counter++];
elmbed 17:d8b901d791fd 676
elmbed 17:d8b901d791fd 677 if((ch == '\r' || ch == ';' || ch == '\n') && parameter != '_')
elmbed 17:d8b901d791fd 678 {
elmbed 17:d8b901d791fd 679 if(i > 1)
elmbed 17:d8b901d791fd 680 {
AntonLS 41:dee3fd34e37a 681 buffer[i-1] = 0;
elmbed 17:d8b901d791fd 682 if(parameter == 'l')
elmbed 17:d8b901d791fd 683 value = strtoul(buffer, &endp, 2);
AntonLS 41:dee3fd34e37a 684 else if(parameter == 'B')
AntonLS 41:dee3fd34e37a 685 value = strtoul(buffer, &endp, 16);
AntonLS 41:dee3fd34e37a 686 else
AntonLS 41:dee3fd34e37a 687 value = atoi(buffer);
elmbed 17:d8b901d791fd 688 }
elmbed 18:affef3a7db2a 689
elmbed 17:d8b901d791fd 690 interpret(parameter, value);
elmbed 17:d8b901d791fd 691 DEBUG("After interp: '%c'\r\n", parameter);
elmbed 17:d8b901d791fd 692 parameter = '_';
elmbed 17:d8b901d791fd 693 buffer[0] = 0;
elmbed 17:d8b901d791fd 694 i=0;
elmbed 17:d8b901d791fd 695 }
elmbed 17:d8b901d791fd 696 else
elmbed 17:d8b901d791fd 697 {
elmbed 17:d8b901d791fd 698 if(i==0)
elmbed 17:d8b901d791fd 699 parameter = ch;
elmbed 17:d8b901d791fd 700 else
elmbed 17:d8b901d791fd 701 buffer[i-1] = ch;
elmbed 17:d8b901d791fd 702 i++;
elmbed 17:d8b901d791fd 703 }
elmbed 17:d8b901d791fd 704
elmbed 17:d8b901d791fd 705 if(ch == '_' || ch == '\r' || ch == ';' || ch == '\n')
elmbed 17:d8b901d791fd 706 {
elmbed 17:d8b901d791fd 707 parameter = '_';
elmbed 17:d8b901d791fd 708 buffer[0] = 0;
elmbed 17:d8b901d791fd 709 i=0;
elmbed 17:d8b901d791fd 710 }
elmbed 17:d8b901d791fd 711 }
elmbed 17:d8b901d791fd 712 }
elmbed 17:d8b901d791fd 713
elmbed 23:26f27c462976 714 static void interpret(char parameter, int value)
elmbed 17:d8b901d791fd 715 {
elmbed 18:affef3a7db2a 716 int remainder;
elmbed 18:affef3a7db2a 717 uint16_t split;
elmbed 18:affef3a7db2a 718 uint16_t t;
elmbed 18:affef3a7db2a 719 uint8_t c;
elmbed 18:affef3a7db2a 720 uint8_t l;
elmbed 18:affef3a7db2a 721 int last;
elmbed 18:affef3a7db2a 722 int middle;
elmbed 18:affef3a7db2a 723 uint8_t length;
elmbed 18:affef3a7db2a 724 uint8_t offset;
elmbed 18:affef3a7db2a 725 int i;
elmbed 18:affef3a7db2a 726 uint8_t v;
elmbed 18:affef3a7db2a 727 uint16_t val;
elmbed 17:d8b901d791fd 728
elmbed 17:d8b901d791fd 729 switch(parameter)
elmbed 17:d8b901d791fd 730 {
elmbed 17:d8b901d791fd 731 case 'f':
elmbed 17:d8b901d791fd 732 if(lonely)
elmbed 17:d8b901d791fd 733 {
elmbed 18:affef3a7db2a 734 writeToPhone("Sorry, no other cones detected, please try detecting cones first.\r\n");
elmbed 17:d8b901d791fd 735 break;
elmbed 17:d8b901d791fd 736 }
elmbed 18:affef3a7db2a 737
elmbed 18:affef3a7db2a 738 writeToPhone("Entered freeform\r\n");
elmbed 17:d8b901d791fd 739 mode = FREEFORM;
elmbed 17:d8b901d791fd 740 state_p = START_P;
elmbed 17:d8b901d791fd 741 clearCones();
elmbed 18:affef3a7db2a 742 //find_cones();
elmbed 17:d8b901d791fd 743 break;
elmbed 17:d8b901d791fd 744 case 'p':
elmbed 17:d8b901d791fd 745 if(value == 0)
elmbed 17:d8b901d791fd 746 {
elmbed 18:affef3a7db2a 747 writeToPhone("\r\n");
elmbed 18:affef3a7db2a 748 writeToPhone("Running pattern %d\r\n", active_sequence + 1);
elmbed 18:affef3a7db2a 749
elmbed 17:d8b901d791fd 750 mode = PATTERN;
elmbed 17:d8b901d791fd 751 state_p = START_P;
elmbed 17:d8b901d791fd 752 active_cone = 0;
elmbed 18:affef3a7db2a 753
elmbed 17:d8b901d791fd 754 clearCones();
elmbed 17:d8b901d791fd 755 }
elmbed 17:d8b901d791fd 756 else
elmbed 17:d8b901d791fd 757 {
elmbed 18:affef3a7db2a 758 writeToPhone("Selected pattern %d\r\n", value);
elmbed 18:affef3a7db2a 759
elmbed 17:d8b901d791fd 760 if(value <= SEQUENCES && value > 0)
elmbed 17:d8b901d791fd 761 {
elmbed 17:d8b901d791fd 762 active_sequence = value - 1;
elmbed 17:d8b901d791fd 763 cones = (uint8_t*)cone_table + (value-1)*STATIONS;
elmbed 17:d8b901d791fd 764 times = (uint16_t*)time_table + (value-1)*STATIONS;
elmbed 17:d8b901d791fd 765 masks = (uint8_t*)mask_table + (value-1)*STATIONS;
elmbed 17:d8b901d791fd 766 }
elmbed 17:d8b901d791fd 767 else
elmbed 17:d8b901d791fd 768 {
AntonLS 41:dee3fd34e37a 769 writeToPhone("This pattern is not available. Please select a value between 1 and %d\r\n", SEQUENCES);
elmbed 17:d8b901d791fd 770 }
elmbed 17:d8b901d791fd 771 }
elmbed 17:d8b901d791fd 772 break;
AntonLS 41:dee3fd34e37a 773 case 'P':
AntonLS 41:dee3fd34e37a 774 if(value != 0)
AntonLS 41:dee3fd34e37a 775 {
AntonLS 41:dee3fd34e37a 776 if(value <= SEQUENCES && value > 0)
AntonLS 41:dee3fd34e37a 777 {
AntonLS 41:dee3fd34e37a 778 active_sequence = value - 1;
AntonLS 41:dee3fd34e37a 779 cones = (uint8_t*)cone_table + (value-1)*STATIONS;
AntonLS 41:dee3fd34e37a 780 times = (uint16_t*)time_table + (value-1)*STATIONS;
AntonLS 41:dee3fd34e37a 781 masks = (uint8_t*)mask_table + (value-1)*STATIONS;
AntonLS 41:dee3fd34e37a 782
AntonLS 41:dee3fd34e37a 783 // Side effect: Also sets current station to 1:
AntonLS 41:dee3fd34e37a 784 station = 1;
AntonLS 41:dee3fd34e37a 785 }
AntonLS 41:dee3fd34e37a 786 else
AntonLS 41:dee3fd34e37a 787 {
AntonLS 41:dee3fd34e37a 788 writeToPhone("Only from 1 to %d\r\n", SEQUENCES);
AntonLS 41:dee3fd34e37a 789 }
AntonLS 41:dee3fd34e37a 790 }
AntonLS 41:dee3fd34e37a 791 else // P; or P0;
AntonLS 41:dee3fd34e37a 792 {
AntonLS 41:dee3fd34e37a 793 writeToPhone("Nothing done\r\n");
AntonLS 41:dee3fd34e37a 794 }
AntonLS 41:dee3fd34e37a 795 break;
elmbed 17:d8b901d791fd 796 case 's':
AntonLS 41:dee3fd34e37a 797 writeToPhone("Selected station %d\r\n", value);
AntonLS 41:dee3fd34e37a 798 case 'S':
elmbed 17:d8b901d791fd 799 station = value;
elmbed 17:d8b901d791fd 800 break;
elmbed 17:d8b901d791fd 801 case 'd':
elmbed 18:affef3a7db2a 802 if(value == 0){
elmbed 17:d8b901d791fd 803 logging = false;
elmbed 17:d8b901d791fd 804 m->value = 0;
elmbed 17:d8b901d791fd 805 dist_timeout = micros() + 2000000;
elmbed 17:d8b901d791fd 806 }
elmbed 18:affef3a7db2a 807 if(value == 1){
elmbed 17:d8b901d791fd 808 logging = true;
elmbed 17:d8b901d791fd 809 m->value = 1;
elmbed 17:d8b901d791fd 810 }
elmbed 17:d8b901d791fd 811 m->command = 'd';
elmbed 17:d8b901d791fd 812 m->cone = TRILAT_CONE;
elmbed 17:d8b901d791fd 813 ta.send(m);
elmbed 18:affef3a7db2a 814 //Serial.print("Sent d");
elmbed 18:affef3a7db2a 815 //Serial.println(value);
elmbed 17:d8b901d791fd 816 break;
elmbed 17:d8b901d791fd 817 case 'c':
AntonLS 41:dee3fd34e37a 818 writeToPhone("Station %d will be cone %d\r\n", station, value);
AntonLS 41:dee3fd34e37a 819 case 'C':
elmbed 17:d8b901d791fd 820 c = value;
elmbed 18:affef3a7db2a 821 if(station <= STATIONS && station > 0)cones[station-1] = c;
elmbed 17:d8b901d791fd 822 break;
elmbed 17:d8b901d791fd 823 case 't':
elmbed 17:d8b901d791fd 824 t = (uint16_t)value;
elmbed 17:d8b901d791fd 825 remainder = t%1000;
AntonLS 19:afcbb425b3cf 826 writeToPhone("Station %d split time is: %d.%03d seconds.\r\n", station, t/1000, remainder);
AntonLS 41:dee3fd34e37a 827 case 'T':
AntonLS 41:dee3fd34e37a 828 t = (uint16_t)value;
AntonLS 41:dee3fd34e37a 829 // remainder = t%1000;
AntonLS 19:afcbb425b3cf 830 /// if(remainder < 100)writeToPhone("0");
AntonLS 19:afcbb425b3cf 831 /// if(remainder < 10)writeToPhone("0");
AntonLS 19:afcbb425b3cf 832 /// writeToPhone("%d seconds.\r\n",remainder);
elmbed 18:affef3a7db2a 833 if(station <= STATIONS && station > 0)times[station-1] = t;
elmbed 17:d8b901d791fd 834 break;
elmbed 17:d8b901d791fd 835 case 'l':
AntonLS 41:dee3fd34e37a 836 // l = 0;
elmbed 17:d8b901d791fd 837 l = (uint8_t)value;
elmbed 17:d8b901d791fd 838 masks[station-1] = l;
AntonLS 19:afcbb425b3cf 839 writeToPhone( "Station %d config bits: ", station );
AntonLS 19:afcbb425b3cf 840 writeBitsToPhone( l );
AntonLS 19:afcbb425b3cf 841 writeToPhone( "\r\n" );
elmbed 17:d8b901d791fd 842 break;
AntonLS 41:dee3fd34e37a 843 case 'B': // After setting bits, echo back station data.
AntonLS 41:dee3fd34e37a 844 l = (uint8_t)value;
AntonLS 41:dee3fd34e37a 845 masks[station-1] = l;
AntonLS 45:1eb335c00cb2 846 writeToPhone( "%c%02u;C%02u;T%05u;B%02x\r\n", (1==station) ? 'P' : 'S',
AntonLS 41:dee3fd34e37a 847 (1==station) ? active_sequence +1 : station,
AntonLS 41:dee3fd34e37a 848 cones[station-1], times[station-1], l );
AntonLS 41:dee3fd34e37a 849 break;
elmbed 17:d8b901d791fd 850 case 'q':
elmbed 17:d8b901d791fd 851 state_p = IDLE_P;
elmbed 17:d8b901d791fd 852 new_state = true; // force state reporting, even if we're already in IDLE
AntonLS 46:28c29ef61276 853 ta.pulse_off(); /// //
AntonLS 46:28c29ef61276 854 clearCones(); /// //
elmbed 17:d8b901d791fd 855 break;
elmbed 17:d8b901d791fd 856 case 'r':
elmbed 18:affef3a7db2a 857 //Serial.println(F(""));
AntonLS 39:b1f864b71318 858 // writeToPhone("Current pattern is %d:\r\n", active_sequence+1);
AntonLS 39:b1f864b71318 859 writeToPhone("Pattern is %d:\r\n", active_sequence+1); // PROTOCOL optimization
elmbed 18:affef3a7db2a 860 for(int i=0; i<STATIONS; i++){
AntonLS 39:b1f864b71318 861 // writeToPhone("Station %d: cone %d, ", i+1, cones[i]);
AntonLS 45:1eb335c00cb2 862 writeToPhone("S%02u,C%02u", i+1, cones[i]); // PROTOCOL optimization
elmbed 17:d8b901d791fd 863 split = times[i];
AntonLS 39:b1f864b71318 864 // printMsAsSeconds(split); // Removed for PROTOCOL optimization
elmbed 18:affef3a7db2a 865 //Serial.print(F("s timeout, lights: "));
AntonLS 39:b1f864b71318 866 // writeToPhone("s timeout, config bits: ");
AntonLS 45:1eb335c00cb2 867 writeToPhone(",T%05u", split); // PROTOCOL optimization
elmbed 17:d8b901d791fd 868 l = masks[i];
elmbed 17:d8b901d791fd 869 /*
elmbed 18:affef3a7db2a 870 if(l<0b10000000)
elmbed 18:affef3a7db2a 871 writeToPhone("0");
elmbed 18:affef3a7db2a 872 if(l<0b1000000)
elmbed 18:affef3a7db2a 873 writeToPhone("0");
elmbed 18:affef3a7db2a 874 if(l<0b100000)
elmbed 18:affef3a7db2a 875 writeToPhone("0");
elmbed 18:affef3a7db2a 876 if(l<0b10000)
elmbed 18:affef3a7db2a 877 writeToPhone("0");
elmbed 18:affef3a7db2a 878 if(l<0b1000)
elmbed 18:affef3a7db2a 879 writeToPhone("0");
elmbed 18:affef3a7db2a 880 if(l<0b100)
elmbed 18:affef3a7db2a 881 writeToPhone("0");
elmbed 18:affef3a7db2a 882 if(l<0b10)
elmbed 18:affef3a7db2a 883 writeToPhone("0");
elmbed 18:affef3a7db2a 884 */
AntonLS 39:b1f864b71318 885 // writeBitsToPhone( l, 3 ); // Removed for PROTOCOL optimization
AntonLS 39:b1f864b71318 886 writeToPhone(",B%02x", l); // PROTOCOL optimization
AntonLS 19:afcbb425b3cf 887 writeToPhone( "\r\n" );
elmbed 18:affef3a7db2a 888 //Serial.println(l, BIN);
elmbed 17:d8b901d791fd 889 }
elmbed 17:d8b901d791fd 890 break;
elmbed 17:d8b901d791fd 891 case 'u':
elmbed 17:d8b901d791fd 892 // let any pending messages clear
elmbed 18:affef3a7db2a 893 while(ta.get_buffer_size()){
elmbed 17:d8b901d791fd 894 ta.spin();
elmbed 17:d8b901d791fd 895 }
elmbed 18:affef3a7db2a 896 if(value == 1){
elmbed 18:affef3a7db2a 897 writeToPhone("Course leader!\r\n");
elmbed 17:d8b901d791fd 898 powerupCones(value);
elmbed 17:d8b901d791fd 899 ta.powerup(value);
elmbed 17:d8b901d791fd 900 }
elmbed 18:affef3a7db2a 901 if(value == 2){
elmbed 18:affef3a7db2a 902 writeToPhone("Split leader!\r\n");
elmbed 17:d8b901d791fd 903 powerupCones(value);
elmbed 18:affef3a7db2a 904 //ta.powerup(value);
elmbed 17:d8b901d791fd 905 }
elmbed 18:affef3a7db2a 906 if(value > 10 && value < 5000){
elmbed 17:d8b901d791fd 907 ta.beep(value);
elmbed 17:d8b901d791fd 908 }
elmbed 17:d8b901d791fd 909 break;
elmbed 17:d8b901d791fd 910 case 'w':
elmbed 18:affef3a7db2a 911 if(value > 0 && value <= SEQUENCES){
elmbed 17:d8b901d791fd 912 length = STATIONS;
elmbed 17:d8b901d791fd 913 offset = (value - 1) * STATIONS;
elmbed 18:affef3a7db2a 914 writeToPhone("Saved sequence %d\r\n", value);
elmbed 17:d8b901d791fd 915 }
elmbed 18:affef3a7db2a 916 else{
elmbed 17:d8b901d791fd 917 length = STATIONS * SEQUENCES;
elmbed 17:d8b901d791fd 918 offset = 0;
elmbed 18:affef3a7db2a 919 writeToPhone("Saved all sequences.\r\n");
elmbed 17:d8b901d791fd 920 }
elmbed 18:affef3a7db2a 921 for(i=offset;i<length+offset;i++){
elmbed 18:affef3a7db2a 922 //eeprom_update_byte(addressConeTable + i, cone_table[i]);
elmbed 18:affef3a7db2a 923 //eeprom_update_byte(addressMaskTable + i, mask_table[i]);
elmbed 18:affef3a7db2a 924 //eeprom_update_word(addressTimeTable + i, time_table[i]);
elmbed 17:d8b901d791fd 925 }
elmbed 17:d8b901d791fd 926 break;
elmbed 17:d8b901d791fd 927 case 'x':
elmbed 17:d8b901d791fd 928 resetSensors();
AntonLS 46:28c29ef61276 929 ta.resetTouch();
elmbed 17:d8b901d791fd 930 break;
elmbed 17:d8b901d791fd 931
elmbed 17:d8b901d791fd 932 case 'z':
elmbed 17:d8b901d791fd 933 find_cones();
elmbed 18:affef3a7db2a 934 /*m->value = value;
elmbed 17:d8b901d791fd 935 m->command = 'z';
elmbed 17:d8b901d791fd 936 m->cone = 2;
elmbed 18:affef3a7db2a 937 Serial.println("sending...");
elmbed 17:d8b901d791fd 938 ta.send(m);
elmbed 18:affef3a7db2a 939 Serial.println("sent");*/
elmbed 17:d8b901d791fd 940 break;
elmbed 17:d8b901d791fd 941 }
elmbed 17:d8b901d791fd 942 }
elmbed 23:26f27c462976 943
elmbed 23:26f27c462976 944 static patternState_t stateFromCone(uint8_t cone)
elmbed 17:d8b901d791fd 945 {
elmbed 17:d8b901d791fd 946 if(cone == 0 || index == STATIONS)
elmbed 17:d8b901d791fd 947 return SUCCESS_P;
elmbed 17:d8b901d791fd 948 if(cone == 1)
elmbed 17:d8b901d791fd 949 return ACTIVE_TARGET_P;
elmbed 17:d8b901d791fd 950
elmbed 17:d8b901d791fd 951 return WAITING_P;
elmbed 17:d8b901d791fd 952 }
elmbed 17:d8b901d791fd 953
elmbed 23:26f27c462976 954 static void printSplit(unsigned long timer)
elmbed 17:d8b901d791fd 955 {
elmbed 17:d8b901d791fd 956 if(tag_start)
elmbed 17:d8b901d791fd 957 {
AntonLS 48:4c9551c826e9 958 writeToPhone( "Sequence started at cone %u (", active_cone ); // PROTOCOL
AntonLS 48:4c9551c826e9 959 printMsAsSeconds(timer); // PROTOCOL
AntonLS 48:4c9551c826e9 960 writeToPhone( " seconds).\r\n" ); // PROTOCOL
elmbed 17:d8b901d791fd 961 tag_start = false;
elmbed 17:d8b901d791fd 962 }
elmbed 17:d8b901d791fd 963 else
elmbed 17:d8b901d791fd 964 {
AntonLS 48:4c9551c826e9 965 writeToPhone( "Target cone is: %u, ", active_cone ); // PROTOCOL
AntonLS 48:4c9551c826e9 966 printMsAsSeconds(timer); // PROTOCOL
AntonLS 48:4c9551c826e9 967 writeToPhone( " split\r\n" ); // PROTOCOL
elmbed 17:d8b901d791fd 968 }
elmbed 17:d8b901d791fd 969 }
elmbed 17:d8b901d791fd 970
elmbed 23:26f27c462976 971 static uint8_t getRandomCone(void)
elmbed 17:d8b901d791fd 972 {
elmbed 40:dec5270e03e9 973 static uint8_t last_cone = 0;
AntonLS 52:060fdec99780 974 static uint8_t ac[NUM_CONES] = {1};
AntonLS 52:060fdec99780 975
elmbed 17:d8b901d791fd 976 uint8_t cone;
AntonLS 48:4c9551c826e9 977
AntonLS 48:4c9551c826e9 978 uint8_t avail = 1;
AntonLS 48:4c9551c826e9 979 for( uint8_t i=2; i<=NUM_CONES; i++ )
AntonLS 48:4c9551c826e9 980 if( active_cones[i] && (i != last_cone) ) ac[avail++] = i;
AntonLS 48:4c9551c826e9 981
AntonLS 48:4c9551c826e9 982 cone = ((avail >1) ? ac[rnd() % avail] : 1);
AntonLS 48:4c9551c826e9 983
AntonLS 48:4c9551c826e9 984 writeToPhone( "Target cone is %u\r\n", cone ); // PROTOCOL
elmbed 40:dec5270e03e9 985
elmbed 40:dec5270e03e9 986 last_cone = cone;
AntonLS 48:4c9551c826e9 987
AntonLS 48:4c9551c826e9 988 return cone;
elmbed 17:d8b901d791fd 989 }
elmbed 17:d8b901d791fd 990
elmbed 23:26f27c462976 991 static void clearCones(void)
elmbed 17:d8b901d791fd 992 {
elmbed 38:76e49d045a3b 993 #if 1
elmbed 17:d8b901d791fd 994 uint8_t i;
elmbed 17:d8b901d791fd 995 m->command = 'q';
elmbed 17:d8b901d791fd 996
elmbed 17:d8b901d791fd 997 for(i=2;i<NUM_CONES+1;i++)
elmbed 17:d8b901d791fd 998 {
elmbed 17:d8b901d791fd 999 m->cone = i;
elmbed 17:d8b901d791fd 1000 if(active_cones[i])
elmbed 17:d8b901d791fd 1001 ta.send(m);
AntonLS 48:4c9551c826e9 1002 /// active_cones[i] = false;
elmbed 17:d8b901d791fd 1003 //ta.send("q", i);
elmbed 17:d8b901d791fd 1004 }
elmbed 17:d8b901d791fd 1005
elmbed 17:d8b901d791fd 1006 DEBUG("sent clear\r\n");
elmbed 23:26f27c462976 1007 #endif
elmbed 17:d8b901d791fd 1008 }
elmbed 17:d8b901d791fd 1009
elmbed 23:26f27c462976 1010 static void powerupCones(uint8_t sound)
elmbed 17:d8b901d791fd 1011 {
elmbed 23:26f27c462976 1012 #if 1
elmbed 17:d8b901d791fd 1013 uint8_t i;
elmbed 17:d8b901d791fd 1014 m->command = 'u';
elmbed 17:d8b901d791fd 1015 m->value = sound;
elmbed 17:d8b901d791fd 1016
elmbed 17:d8b901d791fd 1017 if(sound == 2)
elmbed 17:d8b901d791fd 1018 {
elmbed 28:8e74ddc4f70f 1019 if(active_cone == NODE_ID)
elmbed 17:d8b901d791fd 1020 {
elmbed 17:d8b901d791fd 1021 ta.powerup(sound);
elmbed 17:d8b901d791fd 1022 }
elmbed 17:d8b901d791fd 1023 else
elmbed 17:d8b901d791fd 1024 {
elmbed 17:d8b901d791fd 1025 m->cone = active_cone;
elmbed 17:d8b901d791fd 1026 ta.send(m);
elmbed 17:d8b901d791fd 1027 }
elmbed 17:d8b901d791fd 1028 }
elmbed 17:d8b901d791fd 1029 else
elmbed 17:d8b901d791fd 1030 {
elmbed 17:d8b901d791fd 1031 for(i=2;i<NUM_CONES+1;i++)
elmbed 17:d8b901d791fd 1032 {
elmbed 17:d8b901d791fd 1033 m->cone = i;
elmbed 17:d8b901d791fd 1034 if(active_cones[i])
elmbed 17:d8b901d791fd 1035 ta.send(m);
elmbed 17:d8b901d791fd 1036 //ta.send("f", i);
elmbed 17:d8b901d791fd 1037 }
elmbed 17:d8b901d791fd 1038 }
elmbed 23:26f27c462976 1039 #endif
elmbed 17:d8b901d791fd 1040 //DEBUG("sent powerup");
elmbed 17:d8b901d791fd 1041 }
elmbed 17:d8b901d791fd 1042
elmbed 23:26f27c462976 1043 static void failCones(void)
elmbed 17:d8b901d791fd 1044 {
elmbed 38:76e49d045a3b 1045 #if 1
elmbed 17:d8b901d791fd 1046 uint8_t i;
elmbed 17:d8b901d791fd 1047 m->command = 'f';
elmbed 17:d8b901d791fd 1048
elmbed 17:d8b901d791fd 1049 for(i=2;i<NUM_CONES+1;i++)
elmbed 17:d8b901d791fd 1050 {
elmbed 17:d8b901d791fd 1051 m->cone = i;
elmbed 17:d8b901d791fd 1052
elmbed 17:d8b901d791fd 1053 if(active_cones[i])
elmbed 17:d8b901d791fd 1054 ta.send(m);
elmbed 17:d8b901d791fd 1055 //ta.send("f", i);
elmbed 17:d8b901d791fd 1056 }
elmbed 17:d8b901d791fd 1057
elmbed 17:d8b901d791fd 1058 while(ta.get_buffer_size())
elmbed 17:d8b901d791fd 1059 {
elmbed 17:d8b901d791fd 1060 ta.spin();
elmbed 17:d8b901d791fd 1061 }
elmbed 17:d8b901d791fd 1062
elmbed 17:d8b901d791fd 1063 //DEBUG("sent fail\n");
elmbed 23:26f27c462976 1064 #endif
elmbed 17:d8b901d791fd 1065 }
elmbed 17:d8b901d791fd 1066
elmbed 23:26f27c462976 1067 static void resetSensors(void)
elmbed 17:d8b901d791fd 1068 {
elmbed 38:76e49d045a3b 1069 #if 1
elmbed 17:d8b901d791fd 1070 uint8_t i;
elmbed 17:d8b901d791fd 1071 m->command = 'x';
elmbed 17:d8b901d791fd 1072
elmbed 17:d8b901d791fd 1073 for(i=2;i<NUM_CONES+1;i++)
elmbed 17:d8b901d791fd 1074 {
elmbed 17:d8b901d791fd 1075 m->cone = i;
elmbed 17:d8b901d791fd 1076 if(active_cones[i])
elmbed 17:d8b901d791fd 1077 ta.send(m);
elmbed 17:d8b901d791fd 1078 //ta.send("f", i);
elmbed 17:d8b901d791fd 1079 }
elmbed 17:d8b901d791fd 1080
elmbed 17:d8b901d791fd 1081 DEBUG("sent sensor reset\n");
elmbed 23:26f27c462976 1082 #endif
elmbed 17:d8b901d791fd 1083 }
elmbed 17:d8b901d791fd 1084
elmbed 23:26f27c462976 1085 static void successCones(void)
elmbed 17:d8b901d791fd 1086 {
elmbed 38:76e49d045a3b 1087 #if 1
elmbed 17:d8b901d791fd 1088 uint8_t i;
elmbed 17:d8b901d791fd 1089 m->command = 's';
elmbed 17:d8b901d791fd 1090
elmbed 17:d8b901d791fd 1091 for(i=2;i<NUM_CONES+1;i++)
elmbed 17:d8b901d791fd 1092 {
elmbed 17:d8b901d791fd 1093 m->cone = i;
elmbed 17:d8b901d791fd 1094 if(active_cones[i])
elmbed 17:d8b901d791fd 1095 ta.send(m);
elmbed 17:d8b901d791fd 1096 //ta.send("s", i);
elmbed 17:d8b901d791fd 1097 }
elmbed 17:d8b901d791fd 1098
elmbed 17:d8b901d791fd 1099 while(ta.get_buffer_size())
elmbed 17:d8b901d791fd 1100 {
elmbed 17:d8b901d791fd 1101 ta.spin();
elmbed 17:d8b901d791fd 1102 }
elmbed 17:d8b901d791fd 1103
elmbed 17:d8b901d791fd 1104 //DEBUG("sent success\n");
elmbed 23:26f27c462976 1105 #endif
elmbed 17:d8b901d791fd 1106 }
elmbed 17:d8b901d791fd 1107
elmbed 23:26f27c462976 1108 static void find_cones(void)
elmbed 17:d8b901d791fd 1109 {
elmbed 17:d8b901d791fd 1110 while(ta.get_buffer_size())
elmbed 23:26f27c462976 1111 {
elmbed 17:d8b901d791fd 1112 ta.spin(); // wait for all messages to leave queue
elmbed 28:8e74ddc4f70f 1113 }
elmbed 17:d8b901d791fd 1114
elmbed 23:26f27c462976 1115 ta.beep_off();
elmbed 17:d8b901d791fd 1116 uint8_t i;
elmbed 17:d8b901d791fd 1117 m->command = 'z';
elmbed 28:8e74ddc4f70f 1118
elmbed 17:d8b901d791fd 1119 lonely = true;
elmbed 17:d8b901d791fd 1120
AntonLS 52:060fdec99780 1121 if( Dbg ) writeToPhone("FC\r\n");
elmbed 29:ae208b014987 1122
elmbed 29:ae208b014987 1123 int retry_count = 0;
AntonLS 32:64e5d7340d82 1124
elmbed 26:40a0c775ff27 1125 for(i = 2; i < NUM_CONES + 1; ++i)
elmbed 17:d8b901d791fd 1126 {
elmbed 29:ae208b014987 1127 retry_count = 0;
elmbed 29:ae208b014987 1128 go_again:
elmbed 17:d8b901d791fd 1129 active_cones[i] = false;
elmbed 17:d8b901d791fd 1130 m->cone = i;
elmbed 17:d8b901d791fd 1131 ta.send_immediate(m);
elmbed 17:d8b901d791fd 1132
elmbed 17:d8b901d791fd 1133 unsigned long st = millis();
elmbed 29:ae208b014987 1134 unsigned long current = 0L;
elmbed 29:ae208b014987 1135 unsigned long delta = 0L;
elmbed 17:d8b901d791fd 1136
AntonLS 52:060fdec99780 1137 if( Dbg ) writeToPhone("S: %lu F: %d\r\n", st, i);
elmbed 23:26f27c462976 1138
elmbed 17:d8b901d791fd 1139 while(1)
elmbed 17:d8b901d791fd 1140 {
elmbed 23:26f27c462976 1141 current = millis();
elmbed 23:26f27c462976 1142 delta = current - st;
elmbed 17:d8b901d791fd 1143
elmbed 35:c1405da88d3a 1144 if(delta > 300L)
elmbed 23:26f27c462976 1145 {
AntonLS 52:060fdec99780 1146 if( Dbg ) writeToPhone("FT: %d %lu\r\n", i, current);
elmbed 29:ae208b014987 1147
elmbed 29:ae208b014987 1148 if (++retry_count < 3)
elmbed 29:ae208b014987 1149 {
elmbed 29:ae208b014987 1150 goto go_again;
elmbed 29:ae208b014987 1151 }
elmbed 29:ae208b014987 1152 else
elmbed 29:ae208b014987 1153 {
elmbed 29:ae208b014987 1154 break;
elmbed 29:ae208b014987 1155 }
elmbed 28:8e74ddc4f70f 1156 }
elmbed 17:d8b901d791fd 1157 if(ta.recieve(m_in))
elmbed 17:d8b901d791fd 1158 {
elmbed 17:d8b901d791fd 1159 lonely = false;
elmbed 17:d8b901d791fd 1160 active_cones[m_in->cone] = true;
elmbed 17:d8b901d791fd 1161
AntonLS 52:060fdec99780 1162 if( Dbg ) writeToPhone("FS: %d\r\n", m_in->cone);
elmbed 17:d8b901d791fd 1163 break;
elmbed 17:d8b901d791fd 1164 }
elmbed 17:d8b901d791fd 1165 }
elmbed 29:ae208b014987 1166
elmbed 29:ae208b014987 1167 wait_us(50);
elmbed 17:d8b901d791fd 1168 }
AntonLS 52:060fdec99780 1169
AntonLS 52:060fdec99780 1170 writeToPhone("Available cones are: (1");
elmbed 17:d8b901d791fd 1171
elmbed 26:40a0c775ff27 1172 for(i = 2; i < NUM_CONES + 1; ++i)
elmbed 17:d8b901d791fd 1173 {
elmbed 17:d8b901d791fd 1174 if(active_cones[i])
elmbed 17:d8b901d791fd 1175 {
elmbed 26:40a0c775ff27 1176 writeToPhone(", %d", i);
elmbed 17:d8b901d791fd 1177 }
elmbed 17:d8b901d791fd 1178 }
elmbed 26:40a0c775ff27 1179
elmbed 26:40a0c775ff27 1180 writeToPhone(")\r\n");
elmbed 17:d8b901d791fd 1181 }
elmbed 17:d8b901d791fd 1182
elmbed 23:26f27c462976 1183 static void printMsAsSeconds(unsigned long number)
elmbed 17:d8b901d791fd 1184 {
elmbed 17:d8b901d791fd 1185 uint16_t remainder;
elmbed 17:d8b901d791fd 1186
elmbed 17:d8b901d791fd 1187 remainder = number%1000;
AntonLS 19:afcbb425b3cf 1188
AntonLS 52:060fdec99780 1189 writeToPhone( "%d.%03d", number/1000, remainder ); /// Needs to stay without CRLF.
elmbed 17:d8b901d791fd 1190 }
elmbed 17:d8b901d791fd 1191
elmbed 23:26f27c462976 1192 static void spinButtons(void)
elmbed 17:d8b901d791fd 1193 {
elmbed 17:d8b901d791fd 1194 static inputState_t last_state = IDLE_I;
elmbed 17:d8b901d791fd 1195 static bool first_i; // first should be true when we first enter a state (even if we just exited the same state)
elmbed 17:d8b901d791fd 1196 static Event event;
elmbed 17:d8b901d791fd 1197 static uint8_t sequence = 0;
elmbed 17:d8b901d791fd 1198 static unsigned long start = 0;
elmbed 17:d8b901d791fd 1199 uint8_t section = 0;
elmbed 17:d8b901d791fd 1200
elmbed 17:d8b901d791fd 1201 //uint8_t buttons;
elmbed 17:d8b901d791fd 1202
elmbed 17:d8b901d791fd 1203 //timer = millis() - start;
elmbed 17:d8b901d791fd 1204 if(millis() > 500)
elmbed 17:d8b901d791fd 1205 event = getInputEvent();
elmbed 17:d8b901d791fd 1206
elmbed 17:d8b901d791fd 1207 if(event.type == Event::press)
elmbed 17:d8b901d791fd 1208 {
elmbed 17:d8b901d791fd 1209 in_menu = true;
elmbed 17:d8b901d791fd 1210 state_i = MENU_I;
elmbed 17:d8b901d791fd 1211 }
elmbed 17:d8b901d791fd 1212
elmbed 17:d8b901d791fd 1213 first_i = false;
elmbed 17:d8b901d791fd 1214
elmbed 17:d8b901d791fd 1215 if(last_state != state_i)
elmbed 17:d8b901d791fd 1216 {
elmbed 17:d8b901d791fd 1217 //if(state_i == IDLE_I)////////////DEBUG.println(F("");
elmbed 17:d8b901d791fd 1218 //if(state_i == RUNNING_I)//////////////DEBUG.println(F("State is WAITING");
elmbed 17:d8b901d791fd 1219 // need to print menu timeout?
elmbed 17:d8b901d791fd 1220 if(state_i == MENU_I)
elmbed 17:d8b901d791fd 1221 ////DEBUG("Menu\n");
elmbed 17:d8b901d791fd 1222 if(state_i == PATTERN_SELECT_I)
elmbed 17:d8b901d791fd 1223 ////DEBUG("Choosing pattern");
elmbed 17:d8b901d791fd 1224 first_i = true;
elmbed 17:d8b901d791fd 1225 }
elmbed 17:d8b901d791fd 1226
elmbed 17:d8b901d791fd 1227 last_state = state_i;
elmbed 17:d8b901d791fd 1228
elmbed 17:d8b901d791fd 1229 if(state_i == RUNNING_I)
elmbed 17:d8b901d791fd 1230 in_menu = false;
elmbed 17:d8b901d791fd 1231
elmbed 17:d8b901d791fd 1232 switch(state_i)
elmbed 17:d8b901d791fd 1233 {
elmbed 17:d8b901d791fd 1234 case IDLE_I:
elmbed 17:d8b901d791fd 1235 // display something distinctive
elmbed 17:d8b901d791fd 1236 if(event.type == Event::tap)
elmbed 17:d8b901d791fd 1237 {
elmbed 17:d8b901d791fd 1238 state_i = RUNNING_I;
elmbed 17:d8b901d791fd 1239 //send 'p' or 'f' command as appropriate
elmbed 17:d8b901d791fd 1240 if(mode == FREEFORM)
elmbed 17:d8b901d791fd 1241 interpret('f', 0);
elmbed 17:d8b901d791fd 1242 else
elmbed 17:d8b901d791fd 1243 interpret('p', 0);
elmbed 17:d8b901d791fd 1244 }
elmbed 35:c1405da88d3a 1245
elmbed 17:d8b901d791fd 1246 break;
elmbed 17:d8b901d791fd 1247 case RUNNING_I:
elmbed 17:d8b901d791fd 1248 if(event.type == Event::finish)
elmbed 17:d8b901d791fd 1249 state_i = IDLE_I;
elmbed 17:d8b901d791fd 1250 break;
elmbed 17:d8b901d791fd 1251 case MENU_I:
elmbed 17:d8b901d791fd 1252 if(first_i)
elmbed 17:d8b901d791fd 1253 interpret('q', 0);
elmbed 17:d8b901d791fd 1254 // every 3 seconds we cycle through the menu
elmbed 17:d8b901d791fd 1255 // one option per second
elmbed 17:d8b901d791fd 1256 // light changes color and stays on for 500ms
elmbed 17:d8b901d791fd 1257 section = ((millis() - start)%3000)/500;
elmbed 17:d8b901d791fd 1258
elmbed 17:d8b901d791fd 1259 if(section == 0)
elmbed 17:d8b901d791fd 1260 ta.post_color(0xFF);
elmbed 17:d8b901d791fd 1261 if(section == 1)
elmbed 17:d8b901d791fd 1262 ta.post_color(0);
elmbed 17:d8b901d791fd 1263 if(section == 2)
elmbed 17:d8b901d791fd 1264 ta.post_color(0xFF00);
elmbed 17:d8b901d791fd 1265 if(section == 3)
elmbed 17:d8b901d791fd 1266 ta.post_color(0);
elmbed 17:d8b901d791fd 1267 if(section == 4)
elmbed 17:d8b901d791fd 1268 ta.post_color(0xFF0000);
elmbed 17:d8b901d791fd 1269 if(section == 5)
elmbed 17:d8b901d791fd 1270 ta.post_color(0);
elmbed 17:d8b901d791fd 1271 if(event.type == Event::tap)
elmbed 17:d8b901d791fd 1272 {
elmbed 17:d8b901d791fd 1273 // set state here based on current light color
elmbed 17:d8b901d791fd 1274 if(section < 2)
elmbed 17:d8b901d791fd 1275 {
elmbed 17:d8b901d791fd 1276 mode = FREEFORM;
elmbed 17:d8b901d791fd 1277 ta.post_color(0xFF);
elmbed 17:d8b901d791fd 1278 ////////DEBUG.println(F("Freeform mode.");
elmbed 17:d8b901d791fd 1279 state_i = IDLE_I;
elmbed 17:d8b901d791fd 1280 break;
elmbed 17:d8b901d791fd 1281 }
elmbed 17:d8b901d791fd 1282
elmbed 17:d8b901d791fd 1283 if(section < 4)
elmbed 17:d8b901d791fd 1284 {
elmbed 17:d8b901d791fd 1285 mode = PATTERN;
elmbed 17:d8b901d791fd 1286 ta.post_color(0xFF00);
elmbed 17:d8b901d791fd 1287 ////////DEBUG.println(F("Pattern mode.");
elmbed 17:d8b901d791fd 1288 state_i = IDLE_I;
elmbed 17:d8b901d791fd 1289 break;
elmbed 17:d8b901d791fd 1290 }
elmbed 17:d8b901d791fd 1291
elmbed 17:d8b901d791fd 1292 if(section < 6)
elmbed 17:d8b901d791fd 1293 {
elmbed 17:d8b901d791fd 1294 state_i = PATTERN_SELECT_I;
elmbed 17:d8b901d791fd 1295 ta.post_color(0xFF0000);
elmbed 17:d8b901d791fd 1296 break;
elmbed 17:d8b901d791fd 1297 }
elmbed 17:d8b901d791fd 1298 }
elmbed 17:d8b901d791fd 1299 break;
elmbed 17:d8b901d791fd 1300 case PATTERN_SELECT_I:
elmbed 17:d8b901d791fd 1301 if(first_i)
elmbed 17:d8b901d791fd 1302 {
elmbed 17:d8b901d791fd 1303 sequence = active_sequence;
elmbed 17:d8b901d791fd 1304 mode = PATTERN;
elmbed 17:d8b901d791fd 1305 //start = millis();
elmbed 17:d8b901d791fd 1306 }
elmbed 17:d8b901d791fd 1307 if(event.type == Event::tap)
elmbed 17:d8b901d791fd 1308 {
elmbed 17:d8b901d791fd 1309 ta.beep(50);
elmbed 17:d8b901d791fd 1310 if(event.value == 1 && sequence > 0)
elmbed 17:d8b901d791fd 1311 {
elmbed 17:d8b901d791fd 1312 sequence--;
elmbed 17:d8b901d791fd 1313 }
elmbed 17:d8b901d791fd 1314
elmbed 17:d8b901d791fd 1315 if(event.value == 2 && sequence < SEQUENCES - 1)
elmbed 17:d8b901d791fd 1316 {
elmbed 17:d8b901d791fd 1317 sequence++;
elmbed 17:d8b901d791fd 1318 }
elmbed 17:d8b901d791fd 1319
elmbed 17:d8b901d791fd 1320 if(event.value == 3)
elmbed 17:d8b901d791fd 1321 {
elmbed 17:d8b901d791fd 1322 interpret('p', sequence + 1);
elmbed 17:d8b901d791fd 1323 state_i = IDLE_I;
elmbed 17:d8b901d791fd 1324 }
elmbed 17:d8b901d791fd 1325
elmbed 17:d8b901d791fd 1326 if(event.value != 3)
elmbed 17:d8b901d791fd 1327 {
elmbed 17:d8b901d791fd 1328 ////DEBUG("Menu says: sequence ");
elmbed 17:d8b901d791fd 1329 ////DEBUG("%d\n",sequence + 1);
elmbed 17:d8b901d791fd 1330 }
elmbed 17:d8b901d791fd 1331 break;
elmbed 17:d8b901d791fd 1332 case TEACH_I:
elmbed 17:d8b901d791fd 1333 // not implemented yet
elmbed 17:d8b901d791fd 1334 break;
elmbed 17:d8b901d791fd 1335 default: break;
elmbed 17:d8b901d791fd 1336 }
elmbed 17:d8b901d791fd 1337 }
elmbed 17:d8b901d791fd 1338 }
elmbed 17:d8b901d791fd 1339
elmbed 23:26f27c462976 1340 static uint8_t checkButtons(void)
elmbed 17:d8b901d791fd 1341 {
elmbed 17:d8b901d791fd 1342 static unsigned long last_time = 0;
elmbed 17:d8b901d791fd 1343 //static uint8_t last = 0;
elmbed 17:d8b901d791fd 1344 static uint8_t buttons = 0;
elmbed 17:d8b901d791fd 1345
elmbed 17:d8b901d791fd 1346 // listen for commands from the buttons
elmbed 17:d8b901d791fd 1347 unsigned long time = millis();
elmbed 17:d8b901d791fd 1348 // only check every DEBOUNCE_MS to avoid jitter
elmbed 17:d8b901d791fd 1349 if(time - last_time > DEBOUNCE_MS)
elmbed 17:d8b901d791fd 1350 {
elmbed 17:d8b901d791fd 1351 last_time = time;
AntonLS 49:626e84ce5e52 1352 buttons = ta.buttons();
elmbed 17:d8b901d791fd 1353 }
elmbed 17:d8b901d791fd 1354
elmbed 17:d8b901d791fd 1355 return buttons;
elmbed 17:d8b901d791fd 1356 }
elmbed 17:d8b901d791fd 1357
elmbed 23:26f27c462976 1358 static Event getInputEvent(void)
elmbed 17:d8b901d791fd 1359 {
elmbed 17:d8b901d791fd 1360 static uint8_t last_buttons = 0;
elmbed 17:d8b901d791fd 1361 static unsigned long time1 = 0;
elmbed 17:d8b901d791fd 1362 static unsigned long time2 = 0;
elmbed 17:d8b901d791fd 1363 static unsigned long time3 = 0;
elmbed 17:d8b901d791fd 1364 unsigned long duration = 0;
elmbed 17:d8b901d791fd 1365 uint8_t buttons = 0;
elmbed 17:d8b901d791fd 1366 Event event;
elmbed 17:d8b901d791fd 1367 event.type = Event::none;
elmbed 17:d8b901d791fd 1368 event.value = 0;
elmbed 17:d8b901d791fd 1369
elmbed 17:d8b901d791fd 1370 buttons = checkButtons();
elmbed 17:d8b901d791fd 1371 uint8_t rising = buttons & ~last_buttons;
elmbed 17:d8b901d791fd 1372 uint8_t falling = ~buttons & last_buttons;
elmbed 17:d8b901d791fd 1373
elmbed 17:d8b901d791fd 1374 /*if(rising){
elmbed 17:d8b901d791fd 1375 //DEBUG.print("Rising ");
elmbed 17:d8b901d791fd 1376 //DEBUG.println(rising);
elmbed 17:d8b901d791fd 1377 }
elmbed 17:d8b901d791fd 1378 if(falling){
elmbed 17:d8b901d791fd 1379 //DEBUG.print("Falling ");
elmbed 17:d8b901d791fd 1380 //DEBUG.println(falling);
elmbed 17:d8b901d791fd 1381 }*/
elmbed 17:d8b901d791fd 1382 if(rising & 0x01)
elmbed 17:d8b901d791fd 1383 time1 = millis();
elmbed 17:d8b901d791fd 1384 if(rising & 0x02)
elmbed 17:d8b901d791fd 1385 time2 = millis();
elmbed 17:d8b901d791fd 1386 if(rising & 0x04)
elmbed 17:d8b901d791fd 1387 time3 = millis();
elmbed 17:d8b901d791fd 1388
elmbed 17:d8b901d791fd 1389 // simultaneous falling edges will cause lower values to be ignored
elmbed 17:d8b901d791fd 1390 if(falling & 0x01)
elmbed 17:d8b901d791fd 1391 {
elmbed 17:d8b901d791fd 1392 duration = millis() - time1;
elmbed 17:d8b901d791fd 1393 event.value = 1;
elmbed 17:d8b901d791fd 1394 }
elmbed 17:d8b901d791fd 1395
elmbed 17:d8b901d791fd 1396 if(falling & 0x02)
elmbed 17:d8b901d791fd 1397 {
elmbed 17:d8b901d791fd 1398 duration = millis() - time2;
elmbed 17:d8b901d791fd 1399 event.value = 2;
elmbed 17:d8b901d791fd 1400 }
elmbed 17:d8b901d791fd 1401
elmbed 17:d8b901d791fd 1402 if(falling & 0x04)
elmbed 17:d8b901d791fd 1403 {
elmbed 17:d8b901d791fd 1404 duration = millis() - time3;
elmbed 17:d8b901d791fd 1405 event.value = 3;
elmbed 17:d8b901d791fd 1406 }
elmbed 17:d8b901d791fd 1407
elmbed 17:d8b901d791fd 1408 if(duration > 0)
elmbed 17:d8b901d791fd 1409 event.type = Event::tap;
elmbed 17:d8b901d791fd 1410 if(duration > 2000)
elmbed 17:d8b901d791fd 1411 event.type = Event::press;
elmbed 17:d8b901d791fd 1412
elmbed 17:d8b901d791fd 1413 // give feedback that we've waited lng enough for a press
elmbed 17:d8b901d791fd 1414 unsigned long t = millis() - time1;
elmbed 17:d8b901d791fd 1415
elmbed 17:d8b901d791fd 1416 if(2020 > t && t > 2000 && (buttons & 0x01))
elmbed 17:d8b901d791fd 1417 ta.beep(20);
elmbed 17:d8b901d791fd 1418
elmbed 17:d8b901d791fd 1419 t = millis() - time2;
elmbed 17:d8b901d791fd 1420
elmbed 17:d8b901d791fd 1421 if(2020 > t && t > 2000 && (buttons & 0x02))
elmbed 17:d8b901d791fd 1422 ta.beep(20);
elmbed 17:d8b901d791fd 1423
elmbed 17:d8b901d791fd 1424 t = millis() - time3;
elmbed 17:d8b901d791fd 1425
elmbed 17:d8b901d791fd 1426 if(2020 > t && t > 2000 && (buttons & 0x04))
elmbed 17:d8b901d791fd 1427 ta.beep(20);
elmbed 17:d8b901d791fd 1428
elmbed 17:d8b901d791fd 1429 if(event.type != Event::none)
elmbed 17:d8b901d791fd 1430 {
elmbed 17:d8b901d791fd 1431 //DEBUG("Event: ");
elmbed 17:d8b901d791fd 1432 if(event.type == Event::tap)
elmbed 17:d8b901d791fd 1433 ////DEBUG("tap, ");
elmbed 17:d8b901d791fd 1434 if(event.type == Event::press)
elmbed 17:d8b901d791fd 1435 ////DEBUG("press, ");
elmbed 17:d8b901d791fd 1436
elmbed 17:d8b901d791fd 1437 uint8_t val = event.value;
elmbed 17:d8b901d791fd 1438 //DEBUG("%d\n",val);
elmbed 17:d8b901d791fd 1439 //////DEBUG.println(duration);
elmbed 17:d8b901d791fd 1440 }
elmbed 17:d8b901d791fd 1441
elmbed 17:d8b901d791fd 1442 last_buttons = buttons;
elmbed 17:d8b901d791fd 1443 return event;
elmbed 17:d8b901d791fd 1444 }
elmbed 18:affef3a7db2a 1445 #endif
elmbed 18:affef3a7db2a 1446
elmbed 18:affef3a7db2a 1447 #endif