Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
elmbed
Date:
Sat Jan 16 17:24:38 2016 +0000
Revision:
58:fb2198ceb412
Parent:
49:626e84ce5e52
Child:
59:297133497153
Initial pattern mode

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