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