Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
elmbed
Date:
Wed Dec 30 14:00:28 2015 +0000
Revision:
26:40a0c775ff27
Parent:
25:99f1399d40b3
Child:
29:ae208b014987
Child:
30:c60b0d52b067
Multiple radios tested

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