Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Sat Jan 16 03:16:06 2016 +0000
Revision:
54:2878d62714d6
Parent:
53:c1c3e65b4542
Child:
55:b3cb69d69ce8
Low battery pulse--temp.  Freeform random bugfix.

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