Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
elmbed
Date:
Mon Dec 14 14:24:10 2015 +0000
Revision:
23:26f27c462976
Parent:
18:affef3a7db2a
Child:
25:99f1399d40b3
Radio sort of works but still has many bugs.

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
elmbed 23:26f27c462976 124 //DigitalOut TA::cap_enable(p1);
elmbed 23:26f27c462976 125
elmbed 17:d8b901d791fd 126 DigitalOut TA::buzzPin(p20);
elmbed 17:d8b901d791fd 127
elmbed 17:d8b901d791fd 128 #if 0
elmbed 17:d8b901d791fd 129 // inputs
elmbed 17:d8b901d791fd 130 DigitalIn TA::touch_1(A0);
elmbed 17:d8b901d791fd 131 DigitalIn TA::touch_2(A1);
elmbed 17:d8b901d791fd 132 DigitalIn TA::touch_3(A2);
elmbed 17:d8b901d791fd 133 #endif
elmbed 17:d8b901d791fd 134
elmbed 18:affef3a7db2a 135 //NeoStrip *neo;
elmbed 18:affef3a7db2a 136
elmbed 17:d8b901d791fd 137 TA::TA()
elmbed 17:d8b901d791fd 138 {
elmbed 23:26f27c462976 139 buzzPin = 0;
elmbed 17:d8b901d791fd 140 neopixel_init(&m_strip, dig_pin_num, leds_per_strip);
elmbed 23:26f27c462976 141 neopixel_clear(&m_strip);
elmbed 17:d8b901d791fd 142 }
elmbed 17:d8b901d791fd 143
elmbed 17:d8b901d791fd 144 void TA::post_color(uint32_t rgb)
elmbed 17:d8b901d791fd 145 {
elmbed 18:affef3a7db2a 146 if (rgb == 0)
elmbed 18:affef3a7db2a 147 {
elmbed 18:affef3a7db2a 148 neopixel_clear(&m_strip);
elmbed 18:affef3a7db2a 149 return;
elmbed 18:affef3a7db2a 150 }
elmbed 17:d8b901d791fd 151
elmbed 17:d8b901d791fd 152 for (int i = 0; i <= leds_per_strip; ++i)
elmbed 17:d8b901d791fd 153 {
elmbed 17:d8b901d791fd 154 neopixel_set_color_and_show(&m_strip, i, (rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
elmbed 18:affef3a7db2a 155 //neo->setPixel(i, rgb);
elmbed 17:d8b901d791fd 156 }
elmbed 18:affef3a7db2a 157
elmbed 18:affef3a7db2a 158 buzzPin = 1;
elmbed 17:d8b901d791fd 159 }
elmbed 17:d8b901d791fd 160
elmbed 17:d8b901d791fd 161 void TA::mask_color(uint32_t rgb)
elmbed 17:d8b901d791fd 162 {
elmbed 17:d8b901d791fd 163 // enable_1 = 0;
elmbed 17:d8b901d791fd 164 // enable_2 = 0;
elmbed 17:d8b901d791fd 165 // enable_3 = 0;
elmbed 17:d8b901d791fd 166 //neopixels.setRGBStrip1((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
elmbed 17:d8b901d791fd 167
elmbed 17:d8b901d791fd 168 //enable_1 = (mask & 0x01)?1:0;
elmbed 17:d8b901d791fd 169 //enable_2 = (mask & 0x02)?1:0;
elmbed 17:d8b901d791fd 170 //enable_3 = (mask & 0x04)?1:0;
elmbed 17:d8b901d791fd 171
elmbed 17:d8b901d791fd 172 for (int i = 0; i <= leds_per_strip; ++i)
elmbed 17:d8b901d791fd 173 {
elmbed 23:26f27c462976 174 neopixel_set_color_and_show(&m_strip, i, (rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
elmbed 17:d8b901d791fd 175 }
elmbed 17:d8b901d791fd 176 }
elmbed 17:d8b901d791fd 177
elmbed 17:d8b901d791fd 178 void TA::beep(uint16_t ms)
elmbed 17:d8b901d791fd 179 {
elmbed 17:d8b901d791fd 180 beeping = true;
elmbed 17:d8b901d791fd 181 beep_start = millis();
elmbed 17:d8b901d791fd 182 beep_duration = ms;
elmbed 17:d8b901d791fd 183 }
elmbed 17:d8b901d791fd 184
elmbed 17:d8b901d791fd 185 void TA::beep_off(void)
elmbed 17:d8b901d791fd 186 {
elmbed 17:d8b901d791fd 187 beeping = false;
elmbed 17:d8b901d791fd 188 }
elmbed 17:d8b901d791fd 189
elmbed 17:d8b901d791fd 190 void TA::powerup(uint8_t sound)
elmbed 17:d8b901d791fd 191 {
elmbed 17:d8b901d791fd 192 powerup_start = millis();
elmbed 17:d8b901d791fd 193 beeping = false;
elmbed 17:d8b901d791fd 194 pulsing = false;
elmbed 17:d8b901d791fd 195 powering_up1 = false;
elmbed 17:d8b901d791fd 196 powering_up2 = false;
elmbed 17:d8b901d791fd 197
elmbed 17:d8b901d791fd 198 if(sound == 1)
elmbed 17:d8b901d791fd 199 {
elmbed 17:d8b901d791fd 200 powering_up1 = true;
elmbed 17:d8b901d791fd 201 powerup_toggle = 300;
elmbed 17:d8b901d791fd 202 }
elmbed 17:d8b901d791fd 203
elmbed 17:d8b901d791fd 204 if(sound == 2)
elmbed 17:d8b901d791fd 205 {
elmbed 17:d8b901d791fd 206 powering_up2 = true;
elmbed 17:d8b901d791fd 207 powerup_toggle = 20;
elmbed 17:d8b901d791fd 208 }
elmbed 17:d8b901d791fd 209 }
elmbed 17:d8b901d791fd 210
elmbed 17:d8b901d791fd 211 void TA::pulse(uint16_t on_time, uint16_t period, uint16_t ms, uint32_t rgb)
elmbed 17:d8b901d791fd 212 {
elmbed 17:d8b901d791fd 213 current_color = rgb;
elmbed 17:d8b901d791fd 214 pulsing = true;
elmbed 17:d8b901d791fd 215 pulse_start = millis();
elmbed 17:d8b901d791fd 216 pulse_period = period;
elmbed 17:d8b901d791fd 217 pulse_on = on_time;
elmbed 17:d8b901d791fd 218 pulse_duration = ms;
elmbed 17:d8b901d791fd 219 }
elmbed 17:d8b901d791fd 220
elmbed 17:d8b901d791fd 221 void TA::pulse_off(void)
elmbed 17:d8b901d791fd 222 {
elmbed 17:d8b901d791fd 223 pulsing = false;
elmbed 17:d8b901d791fd 224 }
elmbed 17:d8b901d791fd 225
elmbed 17:d8b901d791fd 226 int TA::get_buffer_size(void)
elmbed 17:d8b901d791fd 227 {
elmbed 17:d8b901d791fd 228 return send_buffer.getSize();
elmbed 17:d8b901d791fd 229 }
elmbed 17:d8b901d791fd 230
elmbed 17:d8b901d791fd 231 bool TA::send(Message *m)
elmbed 17:d8b901d791fd 232 {
elmbed 17:d8b901d791fd 233 send_immediate(m);
elmbed 17:d8b901d791fd 234 return true;
elmbed 17:d8b901d791fd 235 }
elmbed 17:d8b901d791fd 236
elmbed 17:d8b901d791fd 237 void TA::send_immediate(Message *m)
elmbed 17:d8b901d791fd 238 {
elmbed 23:26f27c462976 239 radio_send(m);
elmbed 17:d8b901d791fd 240 }
elmbed 17:d8b901d791fd 241
elmbed 17:d8b901d791fd 242 bool TA::sendRaw(uint8_t *message, uint8_t len, uint8_t cone)
elmbed 17:d8b901d791fd 243 {
elmbed 17:d8b901d791fd 244
elmbed 17:d8b901d791fd 245 return true;
elmbed 17:d8b901d791fd 246 }
elmbed 17:d8b901d791fd 247
elmbed 17:d8b901d791fd 248 bool TA::recieve(Message *m)
elmbed 17:d8b901d791fd 249 {
elmbed 23:26f27c462976 250 return radio_receive(m);
elmbed 17:d8b901d791fd 251 }
elmbed 17:d8b901d791fd 252
elmbed 17:d8b901d791fd 253 bool TA::waitForAck(int cone)
elmbed 17:d8b901d791fd 254 {
elmbed 23:26f27c462976 255 long start = micros();
elmbed 23:26f27c462976 256 long timeout = ack_time;// + random(0,2000);
elmbed 23:26f27c462976 257 while (micros() - start < timeout){
elmbed 23:26f27c462976 258 if (radio_ack_received(cone)){
elmbed 23:26f27c462976 259 //Serial.println(millis() - now);
elmbed 23:26f27c462976 260 return true;
elmbed 23:26f27c462976 261 }
elmbed 23:26f27c462976 262 }
elmbed 23:26f27c462976 263 //Serial.println(millis() - start);
elmbed 23:26f27c462976 264 return false;
elmbed 17:d8b901d791fd 265 }
elmbed 17:d8b901d791fd 266
elmbed 17:d8b901d791fd 267 void TA::spin(void)
elmbed 17:d8b901d791fd 268 {
elmbed 23:26f27c462976 269 static byte payload [6];
elmbed 23:26f27c462976 270 static bool message_in_queue = false;
elmbed 23:26f27c462976 271 static bool waiting_for_ack = false;
elmbed 23:26f27c462976 272 static byte dest_cone = 0;
elmbed 23:26f27c462976 273 static uint16_t index = 0;
elmbed 23:26f27c462976 274 static uint16_t ack_tries = 0;
elmbed 23:26f27c462976 275 static uint8_t send_tries = 0;
elmbed 23:26f27c462976 276 static unsigned long ack_start = 0;
elmbed 23:26f27c462976 277 static unsigned long random_wait = 0;
elmbed 23:26f27c462976 278 static unsigned long random_wait_start = 0;
elmbed 23:26f27c462976 279 static unsigned long mem_monitor_start = 0;
elmbed 18:affef3a7db2a 280
elmbed 23:26f27c462976 281 if(tripped())
elmbed 23:26f27c462976 282 {
elmbed 23:26f27c462976 283 //cap_enable = 1;
elmbed 23:26f27c462976 284 //wait_ms(100);
elmbed 23:26f27c462976 285 //cap_enable = 0;
elmbed 23:26f27c462976 286 //activated_start = millis();
elmbed 23:26f27c462976 287 //Serial.println(F("toggled cap sense power"));
elmbed 18:affef3a7db2a 288 }
elmbed 23:26f27c462976 289 if(powering_up2)
elmbed 23:26f27c462976 290 {
elmbed 23:26f27c462976 291 unsigned long t = millis() - powerup_start;
elmbed 23:26f27c462976 292 if(t > powerup_toggle)
elmbed 23:26f27c462976 293 {
elmbed 23:26f27c462976 294 buzzPin = !buzzPin;
elmbed 23:26f27c462976 295 powerup_toggle *= 1.2;
elmbed 23:26f27c462976 296 }
elmbed 23:26f27c462976 297 if(t > 1250)
elmbed 18:affef3a7db2a 298 {
elmbed 23:26f27c462976 299 buzzPin = 0;
elmbed 23:26f27c462976 300 powering_up2 = false;
elmbed 23:26f27c462976 301 }
elmbed 23:26f27c462976 302 }
elmbed 23:26f27c462976 303 else if(powering_up1)
elmbed 23:26f27c462976 304 {
elmbed 23:26f27c462976 305 unsigned long t = millis() - powerup_start;
elmbed 23:26f27c462976 306 if(t > powerup_toggle)
elmbed 23:26f27c462976 307 {
elmbed 23:26f27c462976 308 buzzPin = !buzzPin;
elmbed 23:26f27c462976 309 powerup_toggle *= 0.95;
elmbed 23:26f27c462976 310 powerup_start = millis();
elmbed 18:affef3a7db2a 311 }
elmbed 23:26f27c462976 312 if(powerup_toggle < 10)
elmbed 18:affef3a7db2a 313 {
elmbed 23:26f27c462976 314 buzzPin = 0;
elmbed 23:26f27c462976 315 powering_up1 = false;
elmbed 23:26f27c462976 316 }
elmbed 23:26f27c462976 317 }
elmbed 23:26f27c462976 318 else
elmbed 23:26f27c462976 319 {
elmbed 23:26f27c462976 320 if(beeping && (millis()-beep_start > beep_duration))
elmbed 23:26f27c462976 321 beeping = false;
elmbed 23:26f27c462976 322 if(pulsing && (millis()-pulse_start > pulse_duration))
elmbed 23:26f27c462976 323 pulsing = false;
elmbed 23:26f27c462976 324 if(beeping)
elmbed 23:26f27c462976 325 buzzPin = 1;
elmbed 23:26f27c462976 326 else if( pulsing && (((millis()-pulse_start) % pulse_period) < pulse_on))
elmbed 23:26f27c462976 327 {
elmbed 23:26f27c462976 328 if(!(mask & SILENT))
elmbed 23:26f27c462976 329 buzzPin = 1;
elmbed 23:26f27c462976 330 mask_color(0);
elmbed 18:affef3a7db2a 331 }
elmbed 18:affef3a7db2a 332 else
elmbed 18:affef3a7db2a 333 {
elmbed 23:26f27c462976 334 if(pulsing)mask_color(current_color);
elmbed 23:26f27c462976 335 buzzPin = 0;
elmbed 18:affef3a7db2a 336 }
elmbed 23:26f27c462976 337 }
elmbed 23:26f27c462976 338
elmbed 23:26f27c462976 339 if(!waiting_for_ack && radio_receive_complete())
elmbed 23:26f27c462976 340 {
elmbed 23:26f27c462976 341 //if(radio.CRCPass())
elmbed 18:affef3a7db2a 342 {
elmbed 23:26f27c462976 343 receive_buffer.put(radio.SENDERID);
elmbed 23:26f27c462976 344
elmbed 23:26f27c462976 345 for(byte i = 0; i < radio.DATALEN; i++)
elmbed 23:26f27c462976 346 {
elmbed 23:26f27c462976 347 receive_buffer.put(radio.DATA[i]);
elmbed 23:26f27c462976 348 }
elmbed 18:affef3a7db2a 349 }
elmbed 23:26f27c462976 350
elmbed 23:26f27c462976 351 if(radio.ACKRequested())
elmbed 18:affef3a7db2a 352 {
elmbed 23:26f27c462976 353 wait_ms(1300);
elmbed 23:26f27c462976 354 radio.sendACK();
elmbed 23:26f27c462976 355 //Serial.println(F("Sent ACK"));
elmbed 23:26f27c462976 356 }
elmbed 23:26f27c462976 357 }
elmbed 23:26f27c462976 358
elmbed 23:26f27c462976 359 //if(index > 4999 || waiting_for_ack){
elmbed 23:26f27c462976 360 if(waiting_for_ack){
elmbed 23:26f27c462976 361 //Serial.println(F("Waiting for ack"));
elmbed 23:26f27c462976 362 bool success = radio.ACKReceived(dest_cone);
elmbed 23:26f27c462976 363 if(success || send_tries > 15){
elmbed 23:26f27c462976 364 //Serial.print(F("dequeued: "));
elmbed 23:26f27c462976 365 //Serial.println((uint16_t)payload[1]<<8 + payload[2]);
elmbed 23:26f27c462976 366 message_in_queue = false;
elmbed 23:26f27c462976 367 waiting_for_ack = false;
elmbed 23:26f27c462976 368 unsigned long t = micros() - ack_start;
elmbed 23:26f27c462976 369 //Serial.print(F("Received ACK, microseconds: "));
elmbed 23:26f27c462976 370 //Serial.println(t);
elmbed 23:26f27c462976 371 //ack_tries = 0;
elmbed 23:26f27c462976 372
elmbed 23:26f27c462976 373 message_in_queue = false;
elmbed 23:26f27c462976 374 send_tries = 0;
elmbed 23:26f27c462976 375 /*Serial.print(F("Sent "));
elmbed 23:26f27c462976 376 Serial.print((char)payload[0]);
elmbed 23:26f27c462976 377 Serial.print(F(", to cone "));
elmbed 23:26f27c462976 378 Serial.println(dest_cone);*/
elmbed 23:26f27c462976 379 }
elmbed 23:26f27c462976 380 else{
elmbed 23:26f27c462976 381 if(micros() - ack_start > 10000){
elmbed 23:26f27c462976 382 //Serial.println(F("no ACK"));
elmbed 23:26f27c462976 383 waiting_for_ack = false;
elmbed 23:26f27c462976 384 random_wait_start = micros();
elmbed 23:26f27c462976 385 random_wait = random(1500,3000);
elmbed 23:26f27c462976 386 /*if(send_tries > 15){
elmbed 23:26f27c462976 387 Serial.print(F("Failed to deliver message to cone "));
elmbed 23:26f27c462976 388 Serial.println(dest_cone);
elmbed 23:26f27c462976 389 message_in_queue = false;
elmbed 23:26f27c462976 390 send_tries = 0;
elmbed 23:26f27c462976 391 }*/
elmbed 23:26f27c462976 392 if(send_tries > 4) random_wait = random(3000,9000);
elmbed 23:26f27c462976 393 //Serial.print(F("Failed to deliver message, waiting "));
elmbed 23:26f27c462976 394 //Serial.println(random_wait);
elmbed 23:26f27c462976 395 //ack_tries = 0;
elmbed 18:affef3a7db2a 396
elmbed 23:26f27c462976 397 /*if(send_tries%50 == 0){
elmbed 23:26f27c462976 398 Serial.print(send_tries);
elmbed 23:26f27c462976 399 Serial.println(F(" tries"));
elmbed 23:26f27c462976 400 }*/
elmbed 23:26f27c462976 401 }
elmbed 18:affef3a7db2a 402 }
elmbed 23:26f27c462976 403 // ack_tries++;
elmbed 23:26f27c462976 404 }
elmbed 23:26f27c462976 405 else if(message_in_queue && micros() - random_wait > random_wait_start){// && index%64 == 0){
elmbed 23:26f27c462976 406 requestACK = true;
elmbed 23:26f27c462976 407 //Serial.println(F("sending"));
elmbed 23:26f27c462976 408 writeToPhone("Sending message to: %d\r\n", dest_cone);
elmbed 23:26f27c462976 409 radio.send(dest_cone, payload, 6, requestACK);
elmbed 23:26f27c462976 410 //Serial.println(F("sent"));
elmbed 23:26f27c462976 411 //Serial.print(F("Trying to send: "));
elmbed 23:26f27c462976 412 //uint16_t temp = (uint16_t)payload[1]<<8;
elmbed 23:26f27c462976 413 //temp += (uint8_t)payload[2];
elmbed 23:26f27c462976 414 //Serial.println((uint8_t)payload[0]);
elmbed 23:26f27c462976 415 send_tries++;
elmbed 23:26f27c462976 416 ack_start = micros();
elmbed 23:26f27c462976 417 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 418 else message_in_queue = false;
elmbed 23:26f27c462976 419 }
elmbed 23:26f27c462976 420 else {
elmbed 18:affef3a7db2a 421 /*if(send_buffer.getSize() > 0 && message_in_queue == false){
elmbed 18:affef3a7db2a 422 payload[0] = send_buffer.get();
elmbed 18:affef3a7db2a 423 //Serial.print(F("Got from queue: "));
elmbed 18:affef3a7db2a 424 //Serial.println((char)payload[0]);
elmbed 18:affef3a7db2a 425 message_in_queue = true;
elmbed 18:affef3a7db2a 426 }*/
elmbed 23:26f27c462976 427 while(send_buffer.getSize() > 4 && message_in_queue == false){
elmbed 18:affef3a7db2a 428 payload[0] = send_buffer.get();
elmbed 18:affef3a7db2a 429 payload[1] = send_buffer.get();
elmbed 18:affef3a7db2a 430 payload[2] = send_buffer.get();
elmbed 18:affef3a7db2a 431 payload[3] = send_buffer.get();
elmbed 18:affef3a7db2a 432 payload[4] = send_buffer.get();
elmbed 18:affef3a7db2a 433 dest_cone = send_buffer.get();
elmbed 18:affef3a7db2a 434 payload[5] = send_buffer.get();
elmbed 18:affef3a7db2a 435 /*Serial.println(F(""));
elmbed 18:affef3a7db2a 436 Serial.println(F("sending..."));
elmbed 18:affef3a7db2a 437 Serial.println(payload[0], BIN);
elmbed 18:affef3a7db2a 438 Serial.println(payload[1], BIN);
elmbed 18:affef3a7db2a 439 Serial.println(payload[2], BIN);
elmbed 18:affef3a7db2a 440 Serial.println(payload[3], BIN);
elmbed 18:affef3a7db2a 441 Serial.println(payload[4], BIN);
elmbed 18:affef3a7db2a 442 Serial.println(dest_cone, BIN);
elmbed 18:affef3a7db2a 443 Serial.println(payload[5], BIN);
elmbed 18:affef3a7db2a 444 Serial.println(F(""));*/
elmbed 23:26f27c462976 445 if((char)payload[5] == '%')message_in_queue = true;
elmbed 23:26f27c462976 446 else {
elmbed 23:26f27c462976 447 //Serial.println(F("bad message format"));
elmbed 23:26f27c462976 448 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 449 }
elmbed 18:affef3a7db2a 450 }
elmbed 18:affef3a7db2a 451 }
elmbed 17:d8b901d791fd 452 }
elmbed 17:d8b901d791fd 453
elmbed 17:d8b901d791fd 454 bool TA::activated(void)
elmbed 17:d8b901d791fd 455 {
elmbed 23:26f27c462976 456 return (buttons() & mask)?true:false;
elmbed 17:d8b901d791fd 457 }
elmbed 17:d8b901d791fd 458
elmbed 17:d8b901d791fd 459 bool TA::tripped(void)
elmbed 17:d8b901d791fd 460 {
elmbed 23:26f27c462976 461 // detect if something is staying on the cap sensor (i.e. the floor is causing the sensor to overload)
elmbed 23:26f27c462976 462 // in some cases the sensor output is choppy, so we must detect those situations also.
elmbed 23:26f27c462976 463 uint8_t current = 0;
elmbed 23:26f27c462976 464 static bool sensor_hysteresis = false;
elmbed 23:26f27c462976 465 static uint8_t last;
elmbed 23:26f27c462976 466 static unsigned long activated_start;
elmbed 23:26f27c462976 467 static unsigned long sensor_hyst_start;
elmbed 23:26f27c462976 468
elmbed 23:26f27c462976 469 current = buttons();
elmbed 23:26f27c462976 470 if(current && !last && (millis() - sensor_hyst_start > 250)){
elmbed 23:26f27c462976 471 activated_start = millis();
elmbed 23:26f27c462976 472 //Serial.println(current | 0b10000000, BIN);
elmbed 23:26f27c462976 473 }
elmbed 23:26f27c462976 474 if(!current && last) sensor_hyst_start = millis();
elmbed 23:26f27c462976 475 last = current;
elmbed 23:26f27c462976 476
elmbed 23:26f27c462976 477 return (current && (millis() - activated_start > 1000))?true:false;
elmbed 23:26f27c462976 478
elmbed 17:d8b901d791fd 479 }
elmbed 17:d8b901d791fd 480
elmbed 17:d8b901d791fd 481 uint8_t TA::buttons(void)
elmbed 17:d8b901d791fd 482 {
elmbed 23:26f27c462976 483 uint8_t buttons = 0;
elmbed 17:d8b901d791fd 484
elmbed 23:26f27c462976 485 //buttons |= touch_1?1:0;
elmbed 23:26f27c462976 486 //buttons |= touch_2?2:0;
elmbed 23:26f27c462976 487 //buttons |= touch_3?4:0;
elmbed 23:26f27c462976 488 return buttons;
elmbed 23:26f27c462976 489
elmbed 17:d8b901d791fd 490 }
elmbed 17:d8b901d791fd 491
elmbed 17:d8b901d791fd 492 void TA::setMask(uint8_t the_mask)
elmbed 17:d8b901d791fd 493 {
elmbed 17:d8b901d791fd 494 mask = the_mask;
elmbed 17:d8b901d791fd 495 }
elmbed 17:d8b901d791fd 496
elmbed 17:d8b901d791fd 497 void TA::initialize(uint8_t address)
elmbed 17:d8b901d791fd 498 {
elmbed 17:d8b901d791fd 499 }
elmbed 17:d8b901d791fd 500
elmbed 17:d8b901d791fd 501 /* Can't find a way of implementing this function.
elmbed 17:d8b901d791fd 502 *
elmbed 17:d8b901d791fd 503 * !SR
elmbed 17:d8b901d791fd 504 */
elmbed 17:d8b901d791fd 505 void TA::Ram_TableDisplay(void)
elmbed 17:d8b901d791fd 506 {
elmbed 17:d8b901d791fd 507
elmbed 17:d8b901d791fd 508 }
elmbed 17:d8b901d791fd 509
elmbed 17:d8b901d791fd 510 /* 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 511 *
elmbed 17:d8b901d791fd 512 * !SR
elmbed 17:d8b901d791fd 513 */
elmbed 17:d8b901d791fd 514 void TA::get_free_memory(void)
elmbed 17:d8b901d791fd 515 {
elmbed 17:d8b901d791fd 516 char stackVariable;
elmbed 17:d8b901d791fd 517 char *heap;
elmbed 17:d8b901d791fd 518 unsigned long result;
elmbed 17:d8b901d791fd 519 heap = (char*)malloc(4);
elmbed 17:d8b901d791fd 520 result = &stackVariable - heap;
elmbed 17:d8b901d791fd 521 free(heap);
elmbed 17:d8b901d791fd 522
elmbed 17:d8b901d791fd 523 //serial->printf("Free memory: %ul\n\n",result);
elmbed 17:d8b901d791fd 524 }
elmbed 17:d8b901d791fd 525
elmbed 17:d8b901d791fd 526 void TA::check_mem(void)
elmbed 17:d8b901d791fd 527 {
elmbed 17:d8b901d791fd 528 uint8_t * heapptr, * stackptr;
elmbed 17:d8b901d791fd 529 unsigned int stack, heap = 0;
elmbed 17:d8b901d791fd 530
elmbed 17:d8b901d791fd 531 stackptr = (uint8_t *)malloc(4); // use stackptr temporarily
elmbed 17:d8b901d791fd 532 heapptr = stackptr; // save value of heap pointer
elmbed 17:d8b901d791fd 533 free(stackptr); // free up the memory again (sets stackptr to 0)
elmbed 17:d8b901d791fd 534
elmbed 17:d8b901d791fd 535 stack = __current_sp() ;
elmbed 17:d8b901d791fd 536 heap = (uint16_t)heapptr;
elmbed 17:d8b901d791fd 537 uint16_t last_call = *(stackptr++);
elmbed 17:d8b901d791fd 538
elmbed 17:d8b901d791fd 539 //serial->printf("Stack: 0x");
elmbed 17:d8b901d791fd 540 //serial->printf("%x ",stack);
elmbed 17:d8b901d791fd 541 //serial->printf("Heap: 0x");
elmbed 17:d8b901d791fd 542 //serial->printf("%x\n",heap);
elmbed 17:d8b901d791fd 543 //serial->printf("Last call: 0x");
elmbed 17:d8b901d791fd 544 // serial->printf("%x\n",last_call);
elmbed 17:d8b901d791fd 545 get_free_memory();
elmbed 17:d8b901d791fd 546 }
elmbed 17:d8b901d791fd 547