Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Sat Jan 09 22:36:55 2016 +0000
Revision:
45:1eb335c00cb2
Parent:
43:6eeb44bee87e
Child:
46:28c29ef61276
Buf size consistency

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