Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Mon Feb 22 09:48:42 2016 +0000
Revision:
70:bd4b1e19a0c6
Parent:
67:5650f461722a
Child:
72:84365ba93d6b
New Battery b cmd. z cmd tweaks. Radio lib Rx hang fix.

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