Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Mon Jan 11 16:34:46 2016 +0000
Revision:
46:28c29ef61276
Parent:
39:b1f864b71318
Child:
49:626e84ce5e52
Freeform improvements, etc.

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