Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Sat Jan 09 01:32:54 2016 +0000
Revision:
39:b1f864b71318
Parent:
38:76e49d045a3b
Child:
46:28c29ef61276
Protocol fixes and r cmd optimizations. Colors tweaked. Typo fixed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elmbed 17:d8b901d791fd 1 #include "TA.h"
elmbed 18:affef3a7db2a 2
elmbed 17:d8b901d791fd 3 #include <nrf51.h>
elmbed 17:d8b901d791fd 4 #include <mbed.h>
elmbed 17:d8b901d791fd 5 #include <device.h>
elmbed 17:d8b901d791fd 6 #include <vector>
elmbed 23:26f27c462976 7 #include <RFM69.h>
elmbed 17:d8b901d791fd 8
elmbed 17:d8b901d791fd 9 /* !SR
elmbed 17:d8b901d791fd 10 * Can't find any boards which use the nrf51822 and have analog output enabled. Currently
elmbed 17:d8b901d791fd 11 * all analog stuff has been commented out.
elmbed 17:d8b901d791fd 12 */
elmbed 17:d8b901d791fd 13
elmbed 17:d8b901d791fd 14 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need //////////////////////DEBUG messages on the console;
elmbed 17:d8b901d791fd 15 * it will have an impact on code-size and power consumption. */
elmbed 17:d8b901d791fd 16
elmbed 17:d8b901d791fd 17 #define LOOPBACK_MODE 0 // Loopback mode
elmbed 17:d8b901d791fd 18
elmbed 17:d8b901d791fd 19 #if NEED_CONSOLE_OUTPUT
elmbed 17:d8b901d791fd 20 #define DEBUG(...) { printf(__VA_ARGS__); }
elmbed 17:d8b901d791fd 21 #else
elmbed 17:d8b901d791fd 22 #define DEBUG(...) /* nothing */
elmbed 17:d8b901d791fd 23 #endif /* #if NEED_CONSOLE_OUTPUT */
elmbed 23:26f27c462976 24 extern "C" void writeToPhone(char *format, ...);
elmbed 17:d8b901d791fd 25
elmbed 17:d8b901d791fd 26
elmbed 17:d8b901d791fd 27 extern unsigned long millis();
elmbed 17:d8b901d791fd 28 extern unsigned long micros();
elmbed 17:d8b901d791fd 29 extern int random(int numberone, int numbertwo);
elmbed 17:d8b901d791fd 30
elmbed 23:26f27c462976 31 extern void radio_send(Message *m);
elmbed 23:26f27c462976 32 extern bool radio_receive(Message *m);
elmbed 23:26f27c462976 33 extern bool radio_ack_received(int cone);
elmbed 23:26f27c462976 34 extern bool radio_receive_complete();
elmbed 23:26f27c462976 35
elmbed 17:d8b901d791fd 36 ByteBuffer TA::send_buffer;
elmbed 17:d8b901d791fd 37 ByteBuffer TA::receive_buffer;
elmbed 17:d8b901d791fd 38
elmbed 17:d8b901d791fd 39 uint8_t TA::node_id;// 1 //network ID used for this unit
elmbed 17:d8b901d791fd 40 uint8_t TA::network_id;// 99 //network ID used for this network
elmbed 17:d8b901d791fd 41 uint8_t TA::gateway_id;// 1 //the ID of the network controller
elmbed 17:d8b901d791fd 42 uint8_t TA::ack_time;// 50 // # of ms to wait for an ack
elmbed 17:d8b901d791fd 43
elmbed 17:d8b901d791fd 44 //encryption is OPTIONAL
elmbed 17:d8b901d791fd 45 //to enable encryption you will need to:
elmbed 17:d8b901d791fd 46 // - provide a 16-byte encryption KEY (same on all nodes that talk encrypted)
elmbed 17:d8b901d791fd 47 // - to call .Encrypt(KEY) to start encrypting
elmbed 17:d8b901d791fd 48 // - to stop encrypting call .Encrypt(NULL)
elmbed 17:d8b901d791fd 49 uint8_t TA::KEY[] = "ABCDABCDABCDABCD";
elmbed 17:d8b901d791fd 50 uint16_t TA::interPacketDelay;// = 1000; //wait this many ms between sending packets
elmbed 17:d8b901d791fd 51
elmbed 17:d8b901d791fd 52 // Need an instance of the Radio Module
elmbed 17:d8b901d791fd 53 //byte TA::sendSize;//=0;
elmbed 17:d8b901d791fd 54 //char TA::payload[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
elmbed 17:d8b901d791fd 55 //bool TA::requestACK;//=true;
elmbed 17:d8b901d791fd 56
elmbed 17:d8b901d791fd 57 //neopixels_spi TA::neopixels;
elmbed 17:d8b901d791fd 58
elmbed 17:d8b901d791fd 59
elmbed 17:d8b901d791fd 60 unsigned long millis();
elmbed 17:d8b901d791fd 61
elmbed 17:d8b901d791fd 62 unsigned long micros();
elmbed 17:d8b901d791fd 63
elmbed 23:26f27c462976 64 extern RFM69 radio;
elmbed 17:d8b901d791fd 65
elmbed 17:d8b901d791fd 66 /** Macro for min()
elmbed 17:d8b901d791fd 67 *
elmbed 17:d8b901d791fd 68 * @param any
elmbed 17:d8b901d791fd 69 */
elmbed 17:d8b901d791fd 70 #define min(a,b) ((a)<(b)?(a):(b))
elmbed 17:d8b901d791fd 71 /** Macro for max()
elmbed 17:d8b901d791fd 72 *
elmbed 17:d8b901d791fd 73 * @param any
elmbed 17:d8b901d791fd 74 */
elmbed 17:d8b901d791fd 75 #define max(a,b) ((a)>(b)?(a):(b))
elmbed 17:d8b901d791fd 76
elmbed 17:d8b901d791fd 77 /** generates a random number between two numbers
elmbed 17:d8b901d791fd 78 *
elmbed 17:d8b901d791fd 79 * @param numberone minimum value for random number
elmbed 17:d8b901d791fd 80 * @param numbertwo maximum value for random number
elmbed 17:d8b901d791fd 81 */
elmbed 17:d8b901d791fd 82 int random(int numberone, int numbertwo) {
elmbed 17:d8b901d791fd 83 int random = 0;
elmbed 17:d8b901d791fd 84 if ((numberone < 0) && (numbertwo < 0)) {
elmbed 17:d8b901d791fd 85 numberone = numberone * -1;
elmbed 17:d8b901d791fd 86 numbertwo = numbertwo * -1;
elmbed 17:d8b901d791fd 87 random = -1 * (rand()%(numberone + numbertwo));
elmbed 17:d8b901d791fd 88 }
elmbed 17:d8b901d791fd 89 if ((numbertwo < 0) && (numberone >= 0)) {
elmbed 17:d8b901d791fd 90 numbertwo = numbertwo * -1;
elmbed 17:d8b901d791fd 91 random = (rand()%(numberone + numbertwo)) - numbertwo;
elmbed 17:d8b901d791fd 92 }
elmbed 17:d8b901d791fd 93 if ((numberone < 0) && (numbertwo >= 0)) {
elmbed 17:d8b901d791fd 94 numberone = numberone * -1;
elmbed 17:d8b901d791fd 95 random = (rand()%(numberone + numbertwo)) - numberone;
elmbed 17:d8b901d791fd 96 } else {
elmbed 17:d8b901d791fd 97 random = (rand()%(numberone + numbertwo)) - min(numberone, numbertwo);
elmbed 17:d8b901d791fd 98 }
elmbed 17:d8b901d791fd 99 return random;
elmbed 17:d8b901d791fd 100 }
elmbed 17:d8b901d791fd 101
elmbed 17:d8b901d791fd 102 // ########## End code taken from audrino library ##########
elmbed 17:d8b901d791fd 103
elmbed 17:d8b901d791fd 104 uint8_t TA::mask;
elmbed 17:d8b901d791fd 105
elmbed 17:d8b901d791fd 106 // outputs
elmbed 17:d8b901d791fd 107 //uint8_t TA::buzzPin;
elmbed 17:d8b901d791fd 108 uint8_t TA::red;
elmbed 17:d8b901d791fd 109 uint8_t TA::green;
elmbed 17:d8b901d791fd 110 uint8_t TA::blue;
elmbed 17:d8b901d791fd 111
elmbed 17:d8b901d791fd 112 neopixel_strip_t m_strip;
elmbed 17:d8b901d791fd 113
elmbed 17:d8b901d791fd 114 uint8_t dig_pin_num = 16;
elmbed 17:d8b901d791fd 115 uint8_t leds_per_strip = 18;
elmbed 17:d8b901d791fd 116 uint8_t led_to_enable = 1;
elmbed 17:d8b901d791fd 117
elmbed 17:d8b901d791fd 118 #if 0
elmbed 17:d8b901d791fd 119 DigitalOut TA::enable_1(p4);
elmbed 17:d8b901d791fd 120 DigitalOut TA::enable_2(p7);
elmbed 17:d8b901d791fd 121 DigitalOut TA::enable_3(p8);
elmbed 23:26f27c462976 122 #endif
elmbed 17:d8b901d791fd 123
AntonLS 30:c60b0d52b067 124 DigitalOut TA::cap_enable( p1, 0 );
AntonLS 30:c60b0d52b067 125 /// DigitalIn touch_top(p1);
AntonLS 30:c60b0d52b067 126 /// DigitalIn touch(p12);
elmbed 23:26f27c462976 127
AntonLS 30:c60b0d52b067 128 #define BUZZ_ON 0
AntonLS 30:c60b0d52b067 129 #define BUZZ_OFF 1
elmbed 17:d8b901d791fd 130
AntonLS 30:c60b0d52b067 131 DigitalOut TA::buzzPin( p20, BUZZ_OFF );
AntonLS 30:c60b0d52b067 132
AntonLS 39:b1f864b71318 133 uint8_t TA::buttonsRising = 0; // Appease compiler
AntonLS 39:b1f864b71318 134
AntonLS 30:c60b0d52b067 135 #if 1
elmbed 17:d8b901d791fd 136 // inputs
AntonLS 31:a6110950f385 137 EdgeDigIn TA::touch_1( p0, PullNone ); // Top touch sensor.
AntonLS 30:c60b0d52b067 138 DigitalIn TA::touch_2( p12,PullDown ); // Test button (p6 [was] RSVD for lower button--p6 is last analog-in though.)
AntonLS 30:c60b0d52b067 139 DigitalIn TA::touch_3( p3, PullNone ); // /Power button (even though not a touch button.)
elmbed 17:d8b901d791fd 140 #endif
elmbed 17:d8b901d791fd 141
elmbed 18:affef3a7db2a 142 //NeoStrip *neo;
elmbed 18:affef3a7db2a 143
elmbed 17:d8b901d791fd 144 TA::TA()
elmbed 17:d8b901d791fd 145 {
AntonLS 30:c60b0d52b067 146 setMask( DEFTOUCHMASK );
AntonLS 30:c60b0d52b067 147 buzzPin = BUZZ_OFF;
elmbed 17:d8b901d791fd 148 neopixel_init(&m_strip, dig_pin_num, leds_per_strip);
elmbed 23:26f27c462976 149 neopixel_clear(&m_strip);
elmbed 17:d8b901d791fd 150 }
elmbed 17:d8b901d791fd 151
elmbed 17:d8b901d791fd 152 void TA::post_color(uint32_t rgb)
elmbed 17:d8b901d791fd 153 {
elmbed 18:affef3a7db2a 154 if (rgb == 0)
elmbed 18:affef3a7db2a 155 {
elmbed 18:affef3a7db2a 156 neopixel_clear(&m_strip);
elmbed 18:affef3a7db2a 157 return;
elmbed 18:affef3a7db2a 158 }
elmbed 17:d8b901d791fd 159
elmbed 17:d8b901d791fd 160 for (int i = 0; i <= leds_per_strip; ++i)
elmbed 17:d8b901d791fd 161 {
elmbed 17:d8b901d791fd 162 neopixel_set_color_and_show(&m_strip, i, (rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
elmbed 18:affef3a7db2a 163 //neo->setPixel(i, rgb);
elmbed 17:d8b901d791fd 164 }
elmbed 18:affef3a7db2a 165
elmbed 18:affef3a7db2a 166 buzzPin = 1;
elmbed 17:d8b901d791fd 167 }
elmbed 17:d8b901d791fd 168
elmbed 17:d8b901d791fd 169 void TA::mask_color(uint32_t rgb)
elmbed 17:d8b901d791fd 170 {
elmbed 17:d8b901d791fd 171 // enable_1 = 0;
elmbed 17:d8b901d791fd 172 // enable_2 = 0;
elmbed 17:d8b901d791fd 173 // enable_3 = 0;
elmbed 17:d8b901d791fd 174 //neopixels.setRGBStrip1((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
elmbed 17:d8b901d791fd 175
elmbed 17:d8b901d791fd 176 //enable_1 = (mask & 0x01)?1:0;
elmbed 17:d8b901d791fd 177 //enable_2 = (mask & 0x02)?1:0;
elmbed 17:d8b901d791fd 178 //enable_3 = (mask & 0x04)?1:0;
elmbed 17:d8b901d791fd 179
elmbed 17:d8b901d791fd 180 for (int i = 0; i <= leds_per_strip; ++i)
elmbed 17:d8b901d791fd 181 {
elmbed 23:26f27c462976 182 neopixel_set_color_and_show(&m_strip, i, (rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
elmbed 17:d8b901d791fd 183 }
elmbed 17:d8b901d791fd 184 }
elmbed 17:d8b901d791fd 185
elmbed 17:d8b901d791fd 186 void TA::beep(uint16_t ms)
elmbed 17:d8b901d791fd 187 {
elmbed 17:d8b901d791fd 188 beeping = true;
elmbed 17:d8b901d791fd 189 beep_start = millis();
elmbed 17:d8b901d791fd 190 beep_duration = ms;
elmbed 17:d8b901d791fd 191 }
elmbed 17:d8b901d791fd 192
elmbed 17:d8b901d791fd 193 void TA::beep_off(void)
elmbed 17:d8b901d791fd 194 {
elmbed 17:d8b901d791fd 195 beeping = false;
elmbed 17:d8b901d791fd 196 }
elmbed 17:d8b901d791fd 197
elmbed 17:d8b901d791fd 198 void TA::powerup(uint8_t sound)
elmbed 17:d8b901d791fd 199 {
elmbed 17:d8b901d791fd 200 powerup_start = millis();
elmbed 17:d8b901d791fd 201 beeping = false;
elmbed 17:d8b901d791fd 202 pulsing = false;
elmbed 17:d8b901d791fd 203 powering_up1 = false;
elmbed 17:d8b901d791fd 204 powering_up2 = false;
elmbed 17:d8b901d791fd 205
elmbed 17:d8b901d791fd 206 if(sound == 1)
elmbed 17:d8b901d791fd 207 {
elmbed 17:d8b901d791fd 208 powering_up1 = true;
elmbed 17:d8b901d791fd 209 powerup_toggle = 300;
elmbed 17:d8b901d791fd 210 }
elmbed 17:d8b901d791fd 211
elmbed 17:d8b901d791fd 212 if(sound == 2)
elmbed 17:d8b901d791fd 213 {
elmbed 17:d8b901d791fd 214 powering_up2 = true;
elmbed 17:d8b901d791fd 215 powerup_toggle = 20;
elmbed 17:d8b901d791fd 216 }
elmbed 17:d8b901d791fd 217 }
elmbed 17:d8b901d791fd 218
elmbed 17:d8b901d791fd 219 void TA::pulse(uint16_t on_time, uint16_t period, uint16_t ms, uint32_t rgb)
elmbed 17:d8b901d791fd 220 {
elmbed 17:d8b901d791fd 221 current_color = rgb;
elmbed 17:d8b901d791fd 222 pulsing = true;
elmbed 17:d8b901d791fd 223 pulse_start = millis();
elmbed 17:d8b901d791fd 224 pulse_period = period;
elmbed 17:d8b901d791fd 225 pulse_on = on_time;
elmbed 17:d8b901d791fd 226 pulse_duration = ms;
elmbed 17:d8b901d791fd 227 }
elmbed 17:d8b901d791fd 228
elmbed 17:d8b901d791fd 229 void TA::pulse_off(void)
elmbed 17:d8b901d791fd 230 {
elmbed 17:d8b901d791fd 231 pulsing = false;
elmbed 17:d8b901d791fd 232 }
elmbed 17:d8b901d791fd 233
elmbed 17:d8b901d791fd 234 int TA::get_buffer_size(void)
elmbed 17:d8b901d791fd 235 {
elmbed 17:d8b901d791fd 236 return send_buffer.getSize();
elmbed 17:d8b901d791fd 237 }
elmbed 17:d8b901d791fd 238
elmbed 17:d8b901d791fd 239 bool TA::send(Message *m)
elmbed 17:d8b901d791fd 240 {
elmbed 17:d8b901d791fd 241 send_immediate(m);
elmbed 17:d8b901d791fd 242 return true;
elmbed 17:d8b901d791fd 243 }
elmbed 17:d8b901d791fd 244
elmbed 17:d8b901d791fd 245 void TA::send_immediate(Message *m)
elmbed 17:d8b901d791fd 246 {
elmbed 23:26f27c462976 247 radio_send(m);
elmbed 17:d8b901d791fd 248 }
elmbed 17:d8b901d791fd 249
elmbed 17:d8b901d791fd 250 bool TA::sendRaw(uint8_t *message, uint8_t len, uint8_t cone)
elmbed 17:d8b901d791fd 251 {
elmbed 38:76e49d045a3b 252 radio.send(cone, message, len);
elmbed 17:d8b901d791fd 253 return true;
elmbed 17:d8b901d791fd 254 }
elmbed 17:d8b901d791fd 255
elmbed 17:d8b901d791fd 256 bool TA::recieve(Message *m)
elmbed 17:d8b901d791fd 257 {
elmbed 23:26f27c462976 258 return radio_receive(m);
elmbed 17:d8b901d791fd 259 }
elmbed 17:d8b901d791fd 260
elmbed 17:d8b901d791fd 261 bool TA::waitForAck(int cone)
elmbed 17:d8b901d791fd 262 {
elmbed 23:26f27c462976 263 long start = micros();
elmbed 23:26f27c462976 264 long timeout = ack_time;// + random(0,2000);
elmbed 23:26f27c462976 265 while (micros() - start < timeout){
elmbed 23:26f27c462976 266 if (radio_ack_received(cone)){
elmbed 23:26f27c462976 267 //Serial.println(millis() - now);
elmbed 23:26f27c462976 268 return true;
elmbed 23:26f27c462976 269 }
elmbed 23:26f27c462976 270 }
elmbed 23:26f27c462976 271 //Serial.println(millis() - start);
elmbed 23:26f27c462976 272 return false;
elmbed 17:d8b901d791fd 273 }
elmbed 17:d8b901d791fd 274
AntonLS 31:a6110950f385 275 void TA::resetTouchIfStuck()
AntonLS 31:a6110950f385 276 {
AntonLS 31:a6110950f385 277 if(tripped())
AntonLS 31:a6110950f385 278 {
AntonLS 31:a6110950f385 279 cap_enable = 1;
AntonLS 31:a6110950f385 280 wait_ms(100);
AntonLS 31:a6110950f385 281 cap_enable = 0;
elmbed 38:76e49d045a3b 282 writeToPhone("TCP\r\n");
AntonLS 31:a6110950f385 283 //Serial.println(F("toggled cap sense power"));
AntonLS 31:a6110950f385 284 }
AntonLS 31:a6110950f385 285 }
AntonLS 31:a6110950f385 286
elmbed 17:d8b901d791fd 287 void TA::spin(void)
elmbed 17:d8b901d791fd 288 {
elmbed 23:26f27c462976 289 static byte payload [6];
elmbed 23:26f27c462976 290 static bool message_in_queue = false;
elmbed 23:26f27c462976 291 static bool waiting_for_ack = false;
elmbed 23:26f27c462976 292 static byte dest_cone = 0;
elmbed 23:26f27c462976 293 static uint16_t index = 0;
elmbed 23:26f27c462976 294 static uint16_t ack_tries = 0;
elmbed 23:26f27c462976 295 static uint8_t send_tries = 0;
elmbed 23:26f27c462976 296 static unsigned long ack_start = 0;
elmbed 23:26f27c462976 297 static unsigned long random_wait = 0;
elmbed 23:26f27c462976 298 static unsigned long random_wait_start = 0;
elmbed 23:26f27c462976 299 static unsigned long mem_monitor_start = 0;
elmbed 18:affef3a7db2a 300
elmbed 26:40a0c775ff27 301 static unsigned long last_touch = 0;
AntonLS 32:64e5d7340d82 302
elmbed 26:40a0c775ff27 303 if (last_touch == 0 || millis()-last_touch > 3000)
elmbed 26:40a0c775ff27 304 {
AntonLS 32:64e5d7340d82 305 //writeToPhone("Touch value: %d\r\n", touch_1);
elmbed 26:40a0c775ff27 306 last_touch = millis();
AntonLS 30:c60b0d52b067 307 }
elmbed 26:40a0c775ff27 308
AntonLS 31:a6110950f385 309 resetTouchIfStuck();
AntonLS 31:a6110950f385 310
elmbed 23:26f27c462976 311 if(powering_up2)
elmbed 23:26f27c462976 312 {
elmbed 23:26f27c462976 313 unsigned long t = millis() - powerup_start;
elmbed 23:26f27c462976 314 if(t > powerup_toggle)
elmbed 23:26f27c462976 315 {
AntonLS 30:c60b0d52b067 316 buzzPin = !buzzPin;
elmbed 23:26f27c462976 317 powerup_toggle *= 1.2;
elmbed 23:26f27c462976 318 }
elmbed 23:26f27c462976 319 if(t > 1250)
elmbed 18:affef3a7db2a 320 {
AntonLS 30:c60b0d52b067 321 buzzPin = BUZZ_OFF;
elmbed 23:26f27c462976 322 powering_up2 = false;
elmbed 23:26f27c462976 323 }
elmbed 23:26f27c462976 324 }
elmbed 23:26f27c462976 325 else if(powering_up1)
elmbed 23:26f27c462976 326 {
elmbed 23:26f27c462976 327 unsigned long t = millis() - powerup_start;
elmbed 23:26f27c462976 328 if(t > powerup_toggle)
elmbed 23:26f27c462976 329 {
AntonLS 30:c60b0d52b067 330 buzzPin = !buzzPin;
elmbed 23:26f27c462976 331 powerup_toggle *= 0.95;
elmbed 23:26f27c462976 332 powerup_start = millis();
elmbed 18:affef3a7db2a 333 }
elmbed 23:26f27c462976 334 if(powerup_toggle < 10)
elmbed 18:affef3a7db2a 335 {
AntonLS 30:c60b0d52b067 336 buzzPin = BUZZ_OFF;
elmbed 23:26f27c462976 337 powering_up1 = false;
elmbed 23:26f27c462976 338 }
elmbed 23:26f27c462976 339 }
elmbed 23:26f27c462976 340 else
elmbed 23:26f27c462976 341 {
elmbed 23:26f27c462976 342 if(beeping && (millis()-beep_start > beep_duration))
elmbed 23:26f27c462976 343 beeping = false;
elmbed 23:26f27c462976 344 if(pulsing && (millis()-pulse_start > pulse_duration))
elmbed 23:26f27c462976 345 pulsing = false;
AntonLS 30:c60b0d52b067 346 if(beeping)
AntonLS 30:c60b0d52b067 347 buzzPin = BUZZ_ON;
elmbed 23:26f27c462976 348 else if( pulsing && (((millis()-pulse_start) % pulse_period) < pulse_on))
elmbed 23:26f27c462976 349 {
AntonLS 30:c60b0d52b067 350 if(!(mask & SILENT))
AntonLS 30:c60b0d52b067 351 buzzPin = BUZZ_ON;
elmbed 23:26f27c462976 352 mask_color(0);
elmbed 18:affef3a7db2a 353 }
elmbed 18:affef3a7db2a 354 else
elmbed 18:affef3a7db2a 355 {
AntonLS 30:c60b0d52b067 356 if(pulsing)mask_color(current_color);
AntonLS 30:c60b0d52b067 357 buzzPin = BUZZ_OFF;
elmbed 18:affef3a7db2a 358 }
elmbed 23:26f27c462976 359 }
elmbed 23:26f27c462976 360
elmbed 38:76e49d045a3b 361 /*
elmbed 23:26f27c462976 362 if(!waiting_for_ack && radio_receive_complete())
elmbed 23:26f27c462976 363 {
elmbed 23:26f27c462976 364 //if(radio.CRCPass())
elmbed 18:affef3a7db2a 365 {
elmbed 23:26f27c462976 366 receive_buffer.put(radio.SENDERID);
elmbed 23:26f27c462976 367
elmbed 23:26f27c462976 368 for(byte i = 0; i < radio.DATALEN; i++)
elmbed 23:26f27c462976 369 {
elmbed 23:26f27c462976 370 receive_buffer.put(radio.DATA[i]);
elmbed 23:26f27c462976 371 }
elmbed 18:affef3a7db2a 372 }
elmbed 23:26f27c462976 373
elmbed 23:26f27c462976 374 if(radio.ACKRequested())
elmbed 18:affef3a7db2a 375 {
elmbed 23:26f27c462976 376 wait_ms(1300);
elmbed 23:26f27c462976 377 radio.sendACK();
elmbed 23:26f27c462976 378 //Serial.println(F("Sent ACK"));
elmbed 23:26f27c462976 379 }
elmbed 23:26f27c462976 380 }
elmbed 38:76e49d045a3b 381 */
elmbed 23:26f27c462976 382
elmbed 23:26f27c462976 383 //if(index > 4999 || waiting_for_ack){
elmbed 23:26f27c462976 384 if(waiting_for_ack){
elmbed 23:26f27c462976 385 //Serial.println(F("Waiting for ack"));
elmbed 23:26f27c462976 386 bool success = radio.ACKReceived(dest_cone);
elmbed 23:26f27c462976 387 if(success || send_tries > 15){
elmbed 23:26f27c462976 388 //Serial.print(F("dequeued: "));
elmbed 23:26f27c462976 389 //Serial.println((uint16_t)payload[1]<<8 + payload[2]);
elmbed 23:26f27c462976 390 message_in_queue = false;
elmbed 23:26f27c462976 391 waiting_for_ack = false;
elmbed 23:26f27c462976 392 unsigned long t = micros() - ack_start;
elmbed 23:26f27c462976 393 //Serial.print(F("Received ACK, microseconds: "));
elmbed 23:26f27c462976 394 //Serial.println(t);
elmbed 23:26f27c462976 395 //ack_tries = 0;
elmbed 23:26f27c462976 396
elmbed 23:26f27c462976 397 message_in_queue = false;
elmbed 23:26f27c462976 398 send_tries = 0;
elmbed 23:26f27c462976 399 /*Serial.print(F("Sent "));
elmbed 23:26f27c462976 400 Serial.print((char)payload[0]);
elmbed 23:26f27c462976 401 Serial.print(F(", to cone "));
elmbed 23:26f27c462976 402 Serial.println(dest_cone);*/
elmbed 23:26f27c462976 403 }
elmbed 23:26f27c462976 404 else{
elmbed 23:26f27c462976 405 if(micros() - ack_start > 10000){
elmbed 23:26f27c462976 406 //Serial.println(F("no ACK"));
elmbed 23:26f27c462976 407 waiting_for_ack = false;
elmbed 23:26f27c462976 408 random_wait_start = micros();
elmbed 23:26f27c462976 409 random_wait = random(1500,3000);
elmbed 23:26f27c462976 410 /*if(send_tries > 15){
elmbed 23:26f27c462976 411 Serial.print(F("Failed to deliver message to cone "));
elmbed 23:26f27c462976 412 Serial.println(dest_cone);
elmbed 23:26f27c462976 413 message_in_queue = false;
elmbed 23:26f27c462976 414 send_tries = 0;
elmbed 23:26f27c462976 415 }*/
elmbed 23:26f27c462976 416 if(send_tries > 4) random_wait = random(3000,9000);
elmbed 23:26f27c462976 417 //Serial.print(F("Failed to deliver message, waiting "));
elmbed 23:26f27c462976 418 //Serial.println(random_wait);
elmbed 23:26f27c462976 419 //ack_tries = 0;
elmbed 18:affef3a7db2a 420
elmbed 23:26f27c462976 421 /*if(send_tries%50 == 0){
elmbed 23:26f27c462976 422 Serial.print(send_tries);
elmbed 23:26f27c462976 423 Serial.println(F(" tries"));
elmbed 23:26f27c462976 424 }*/
elmbed 23:26f27c462976 425 }
elmbed 18:affef3a7db2a 426 }
elmbed 23:26f27c462976 427 // ack_tries++;
elmbed 23:26f27c462976 428 }
elmbed 23:26f27c462976 429 else if(message_in_queue && micros() - random_wait > random_wait_start){// && index%64 == 0){
elmbed 23:26f27c462976 430 requestACK = true;
elmbed 23:26f27c462976 431 //Serial.println(F("sending"));
AntonLS 30:c60b0d52b067 432 /// writeToPhone("Sending message to: %d\r\n", dest_cone);
elmbed 23:26f27c462976 433 radio.send(dest_cone, payload, 6, requestACK);
elmbed 23:26f27c462976 434 //Serial.println(F("sent"));
elmbed 23:26f27c462976 435 //Serial.print(F("Trying to send: "));
elmbed 23:26f27c462976 436 //uint16_t temp = (uint16_t)payload[1]<<8;
elmbed 23:26f27c462976 437 //temp += (uint8_t)payload[2];
elmbed 23:26f27c462976 438 //Serial.println((uint8_t)payload[0]);
elmbed 23:26f27c462976 439 send_tries++;
elmbed 23:26f27c462976 440 ack_start = micros();
elmbed 23:26f27c462976 441 if(!radio.ACKReceived(dest_cone)) waiting_for_ack = true; // the 'if' is here to prevent the radio from going to sleep and missing the ACK
elmbed 23:26f27c462976 442 else message_in_queue = false;
elmbed 23:26f27c462976 443 }
elmbed 23:26f27c462976 444 else {
elmbed 18:affef3a7db2a 445 /*if(send_buffer.getSize() > 0 && message_in_queue == false){
elmbed 18:affef3a7db2a 446 payload[0] = send_buffer.get();
elmbed 18:affef3a7db2a 447 //Serial.print(F("Got from queue: "));
elmbed 18:affef3a7db2a 448 //Serial.println((char)payload[0]);
elmbed 18:affef3a7db2a 449 message_in_queue = true;
elmbed 18:affef3a7db2a 450 }*/
elmbed 23:26f27c462976 451 while(send_buffer.getSize() > 4 && message_in_queue == false){
elmbed 18:affef3a7db2a 452 payload[0] = send_buffer.get();
elmbed 18:affef3a7db2a 453 payload[1] = send_buffer.get();
elmbed 18:affef3a7db2a 454 payload[2] = send_buffer.get();
elmbed 18:affef3a7db2a 455 payload[3] = send_buffer.get();
elmbed 18:affef3a7db2a 456 payload[4] = send_buffer.get();
elmbed 18:affef3a7db2a 457 dest_cone = send_buffer.get();
elmbed 18:affef3a7db2a 458 payload[5] = send_buffer.get();
elmbed 18:affef3a7db2a 459 /*Serial.println(F(""));
elmbed 18:affef3a7db2a 460 Serial.println(F("sending..."));
elmbed 18:affef3a7db2a 461 Serial.println(payload[0], BIN);
elmbed 18:affef3a7db2a 462 Serial.println(payload[1], BIN);
elmbed 18:affef3a7db2a 463 Serial.println(payload[2], BIN);
elmbed 18:affef3a7db2a 464 Serial.println(payload[3], BIN);
elmbed 18:affef3a7db2a 465 Serial.println(payload[4], BIN);
elmbed 18:affef3a7db2a 466 Serial.println(dest_cone, BIN);
elmbed 18:affef3a7db2a 467 Serial.println(payload[5], BIN);
elmbed 18:affef3a7db2a 468 Serial.println(F(""));*/
elmbed 23:26f27c462976 469 if((char)payload[5] == '%')message_in_queue = true;
elmbed 23:26f27c462976 470 else {
elmbed 23:26f27c462976 471 //Serial.println(F("bad message format"));
elmbed 23:26f27c462976 472 while(send_buffer.getSize() > 0 && send_buffer.get() != '%'); // if we didn't land on the end of a message, peel stuff off until we are
elmbed 18:affef3a7db2a 473 }
elmbed 18:affef3a7db2a 474 }
elmbed 18:affef3a7db2a 475 }
elmbed 17:d8b901d791fd 476 }
elmbed 17:d8b901d791fd 477
elmbed 17:d8b901d791fd 478 bool TA::activated(void)
elmbed 17:d8b901d791fd 479 {
elmbed 23:26f27c462976 480 return (buttons() & mask)?true:false;
elmbed 17:d8b901d791fd 481 }
elmbed 17:d8b901d791fd 482
elmbed 17:d8b901d791fd 483 bool TA::tripped(void)
elmbed 17:d8b901d791fd 484 {
elmbed 23:26f27c462976 485 // detect if something is staying on the cap sensor (i.e. the floor is causing the sensor to overload)
elmbed 23:26f27c462976 486 // in some cases the sensor output is choppy, so we must detect those situations also.
elmbed 23:26f27c462976 487 uint8_t current = 0;
elmbed 23:26f27c462976 488 static bool sensor_hysteresis = false;
elmbed 23:26f27c462976 489 static uint8_t last;
elmbed 23:26f27c462976 490 static unsigned long activated_start;
elmbed 23:26f27c462976 491 static unsigned long sensor_hyst_start;
elmbed 23:26f27c462976 492
elmbed 23:26f27c462976 493 current = buttons();
elmbed 23:26f27c462976 494 if(current && !last && (millis() - sensor_hyst_start > 250)){
elmbed 23:26f27c462976 495 activated_start = millis();
elmbed 23:26f27c462976 496 //Serial.println(current | 0b10000000, BIN);
elmbed 23:26f27c462976 497 }
elmbed 23:26f27c462976 498 if(!current && last) sensor_hyst_start = millis();
elmbed 23:26f27c462976 499 last = current;
elmbed 23:26f27c462976 500
elmbed 38:76e49d045a3b 501 return (current && (millis() - activated_start > 4000))?true:false;
elmbed 23:26f27c462976 502
elmbed 17:d8b901d791fd 503 }
elmbed 17:d8b901d791fd 504
elmbed 17:d8b901d791fd 505 uint8_t TA::buttons(void)
elmbed 17:d8b901d791fd 506 {
elmbed 23:26f27c462976 507 uint8_t buttons = 0;
elmbed 17:d8b901d791fd 508
AntonLS 30:c60b0d52b067 509 buttons |= touch_1?1:0;
AntonLS 30:c60b0d52b067 510 buttons |= touch_2?2:0;
AntonLS 30:c60b0d52b067 511 buttons |= touch_3?0:4;
AntonLS 30:c60b0d52b067 512 return buttons; /// touch;
elmbed 23:26f27c462976 513
elmbed 17:d8b901d791fd 514 }
elmbed 17:d8b901d791fd 515
elmbed 17:d8b901d791fd 516 void TA::setMask(uint8_t the_mask)
elmbed 17:d8b901d791fd 517 {
elmbed 17:d8b901d791fd 518 mask = the_mask;
elmbed 17:d8b901d791fd 519 }
elmbed 17:d8b901d791fd 520
elmbed 17:d8b901d791fd 521 void TA::initialize(uint8_t address)
elmbed 17:d8b901d791fd 522 {
elmbed 17:d8b901d791fd 523 }
elmbed 17:d8b901d791fd 524
elmbed 17:d8b901d791fd 525 /* Can't find a way of implementing this function.
elmbed 17:d8b901d791fd 526 *
elmbed 17:d8b901d791fd 527 * !SR
elmbed 17:d8b901d791fd 528 */
elmbed 17:d8b901d791fd 529 void TA::Ram_TableDisplay(void)
elmbed 17:d8b901d791fd 530 {
elmbed 17:d8b901d791fd 531
elmbed 17:d8b901d791fd 532 }
elmbed 17:d8b901d791fd 533
elmbed 17:d8b901d791fd 534 /* Not sure if this work. Taken from: https://developer.mbed.org/questions/6994/How-to-print-Free-RAM-available-RAM-or-u/
elmbed 17:d8b901d791fd 535 *
elmbed 17:d8b901d791fd 536 * !SR
elmbed 17:d8b901d791fd 537 */
elmbed 17:d8b901d791fd 538 void TA::get_free_memory(void)
elmbed 17:d8b901d791fd 539 {
elmbed 17:d8b901d791fd 540 char stackVariable;
elmbed 17:d8b901d791fd 541 char *heap;
elmbed 17:d8b901d791fd 542 unsigned long result;
elmbed 17:d8b901d791fd 543 heap = (char*)malloc(4);
elmbed 17:d8b901d791fd 544 result = &stackVariable - heap;
elmbed 17:d8b901d791fd 545 free(heap);
elmbed 17:d8b901d791fd 546
elmbed 17:d8b901d791fd 547 //serial->printf("Free memory: %ul\n\n",result);
elmbed 17:d8b901d791fd 548 }
elmbed 17:d8b901d791fd 549
elmbed 17:d8b901d791fd 550 void TA::check_mem(void)
elmbed 17:d8b901d791fd 551 {
elmbed 17:d8b901d791fd 552 uint8_t * heapptr, * stackptr;
elmbed 17:d8b901d791fd 553 unsigned int stack, heap = 0;
elmbed 17:d8b901d791fd 554
elmbed 17:d8b901d791fd 555 stackptr = (uint8_t *)malloc(4); // use stackptr temporarily
elmbed 17:d8b901d791fd 556 heapptr = stackptr; // save value of heap pointer
elmbed 17:d8b901d791fd 557 free(stackptr); // free up the memory again (sets stackptr to 0)
elmbed 17:d8b901d791fd 558
elmbed 17:d8b901d791fd 559 stack = __current_sp() ;
elmbed 17:d8b901d791fd 560 heap = (uint16_t)heapptr;
elmbed 17:d8b901d791fd 561 uint16_t last_call = *(stackptr++);
elmbed 17:d8b901d791fd 562
elmbed 17:d8b901d791fd 563 //serial->printf("Stack: 0x");
elmbed 17:d8b901d791fd 564 //serial->printf("%x ",stack);
elmbed 17:d8b901d791fd 565 //serial->printf("Heap: 0x");
elmbed 17:d8b901d791fd 566 //serial->printf("%x\n",heap);
elmbed 17:d8b901d791fd 567 //serial->printf("Last call: 0x");
elmbed 17:d8b901d791fd 568 // serial->printf("%x\n",last_call);
elmbed 17:d8b901d791fd 569 get_free_memory();
elmbed 17:d8b901d791fd 570 }
elmbed 17:d8b901d791fd 571