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