Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Thu Feb 11 19:28:01 2016 +0000
Revision:
68:0c96bb3d73a7
Parent:
67:5650f461722a
Child:
69:a3295b74209e
Reduce radio-to-sleep possibility.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elmbed 17:d8b901d791fd 1 #include "TA.h"
AntonLS 67:5650f461722a 2 #include "Radio.hh"
elmbed 18:affef3a7db2a 3
elmbed 17:d8b901d791fd 4 #include <nrf51.h>
elmbed 17:d8b901d791fd 5 #include <mbed.h>
elmbed 17:d8b901d791fd 6 #include <device.h>
elmbed 17:d8b901d791fd 7 #include <vector>
elmbed 23:26f27c462976 8 #include <RFM69.h>
elmbed 17:d8b901d791fd 9
elmbed 17:d8b901d791fd 10 /* !SR
elmbed 17:d8b901d791fd 11 * Can't find any boards which use the nrf51822 and have analog output enabled. Currently
elmbed 17:d8b901d791fd 12 * all analog stuff has been commented out.
elmbed 17:d8b901d791fd 13 */
elmbed 17:d8b901d791fd 14
elmbed 17:d8b901d791fd 15 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need //////////////////////DEBUG messages on the console;
elmbed 17:d8b901d791fd 16 * it will have an impact on code-size and power consumption. */
elmbed 17:d8b901d791fd 17
elmbed 17:d8b901d791fd 18 #define LOOPBACK_MODE 0 // Loopback mode
elmbed 17:d8b901d791fd 19
elmbed 17:d8b901d791fd 20 #if NEED_CONSOLE_OUTPUT
elmbed 17:d8b901d791fd 21 #define DEBUG(...) { printf(__VA_ARGS__); }
elmbed 17:d8b901d791fd 22 #else
elmbed 17:d8b901d791fd 23 #define DEBUG(...) /* nothing */
elmbed 17:d8b901d791fd 24 #endif /* #if NEED_CONSOLE_OUTPUT */
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
AntonLS 67:5650f461722a 31 uint8_t TA::msg_id = 0; // Message ID which replaces '%' in radio send packet for duplicate message detection.
elmbed 23:26f27c462976 32
elmbed 17:d8b901d791fd 33 ByteBuffer TA::send_buffer;
elmbed 17:d8b901d791fd 34 ByteBuffer TA::receive_buffer;
elmbed 17:d8b901d791fd 35
AntonLS 67:5650f461722a 36 //// These are currently no longer used...
elmbed 17:d8b901d791fd 37 uint8_t TA::node_id;// 1 //network ID used for this unit
elmbed 17:d8b901d791fd 38 uint8_t TA::network_id;// 99 //network ID used for this network
elmbed 17:d8b901d791fd 39 uint8_t TA::gateway_id;// 1 //the ID of the network controller
AntonLS 67:5650f461722a 40 uint8_t TA::ack_time; // 50 // # of ms to wait for an ack
elmbed 17:d8b901d791fd 41
elmbed 17:d8b901d791fd 42 //encryption is OPTIONAL
elmbed 17:d8b901d791fd 43 //to enable encryption you will need to:
elmbed 17:d8b901d791fd 44 // - provide a 16-byte encryption KEY (same on all nodes that talk encrypted)
elmbed 17:d8b901d791fd 45 // - to call .Encrypt(KEY) to start encrypting
elmbed 17:d8b901d791fd 46 // - to stop encrypting call .Encrypt(NULL)
elmbed 17:d8b901d791fd 47 uint8_t TA::KEY[] = "ABCDABCDABCDABCD";
elmbed 17:d8b901d791fd 48 uint16_t TA::interPacketDelay;// = 1000; //wait this many ms between sending packets
elmbed 17:d8b901d791fd 49
elmbed 17:d8b901d791fd 50 // Need an instance of the Radio Module
elmbed 17:d8b901d791fd 51 //byte TA::sendSize;//=0;
elmbed 17:d8b901d791fd 52 //char TA::payload[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
elmbed 17:d8b901d791fd 53 //bool TA::requestACK;//=true;
elmbed 17:d8b901d791fd 54
elmbed 17:d8b901d791fd 55 //neopixels_spi TA::neopixels;
elmbed 17:d8b901d791fd 56
AntonLS 67:5650f461722a 57 bool gotACK = false; //// If we got an ACK between retrying attempts.
elmbed 17:d8b901d791fd 58
elmbed 17:d8b901d791fd 59 unsigned long millis();
elmbed 17:d8b901d791fd 60
elmbed 17:d8b901d791fd 61 unsigned long micros();
elmbed 17:d8b901d791fd 62
elmbed 23:26f27c462976 63 extern RFM69 radio;
elmbed 17:d8b901d791fd 64
elmbed 17:d8b901d791fd 65 /** Macro for min()
elmbed 17:d8b901d791fd 66 *
elmbed 17:d8b901d791fd 67 * @param any
elmbed 17:d8b901d791fd 68 */
elmbed 17:d8b901d791fd 69 #define min(a,b) ((a)<(b)?(a):(b))
elmbed 17:d8b901d791fd 70 /** Macro for max()
elmbed 17:d8b901d791fd 71 *
elmbed 17:d8b901d791fd 72 * @param any
elmbed 17:d8b901d791fd 73 */
elmbed 17:d8b901d791fd 74 #define max(a,b) ((a)>(b)?(a):(b))
elmbed 17:d8b901d791fd 75
elmbed 17:d8b901d791fd 76 /** generates a random number between two numbers
elmbed 17:d8b901d791fd 77 *
elmbed 17:d8b901d791fd 78 * @param numberone minimum value for random number
elmbed 17:d8b901d791fd 79 * @param numbertwo maximum value for random number
elmbed 17:d8b901d791fd 80 */
elmbed 17:d8b901d791fd 81 int random(int numberone, int numbertwo) {
elmbed 17:d8b901d791fd 82 int random = 0;
elmbed 17:d8b901d791fd 83 if ((numberone < 0) && (numbertwo < 0)) {
elmbed 17:d8b901d791fd 84 numberone = numberone * -1;
elmbed 17:d8b901d791fd 85 numbertwo = numbertwo * -1;
elmbed 17:d8b901d791fd 86 random = -1 * (rand()%(numberone + numbertwo));
elmbed 17:d8b901d791fd 87 }
elmbed 17:d8b901d791fd 88 if ((numbertwo < 0) && (numberone >= 0)) {
elmbed 17:d8b901d791fd 89 numbertwo = numbertwo * -1;
elmbed 17:d8b901d791fd 90 random = (rand()%(numberone + numbertwo)) - numbertwo;
elmbed 17:d8b901d791fd 91 }
elmbed 17:d8b901d791fd 92 if ((numberone < 0) && (numbertwo >= 0)) {
elmbed 17:d8b901d791fd 93 numberone = numberone * -1;
elmbed 17:d8b901d791fd 94 random = (rand()%(numberone + numbertwo)) - numberone;
elmbed 17:d8b901d791fd 95 } else {
elmbed 17:d8b901d791fd 96 random = (rand()%(numberone + numbertwo)) - min(numberone, numbertwo);
elmbed 17:d8b901d791fd 97 }
elmbed 17:d8b901d791fd 98 return random;
elmbed 17:d8b901d791fd 99 }
elmbed 17:d8b901d791fd 100
elmbed 17:d8b901d791fd 101 // ########## End code taken from audrino library ##########
elmbed 17:d8b901d791fd 102
elmbed 17:d8b901d791fd 103 uint8_t TA::mask;
elmbed 17:d8b901d791fd 104
elmbed 17:d8b901d791fd 105 // outputs
elmbed 17:d8b901d791fd 106 //uint8_t TA::buzzPin;
elmbed 17:d8b901d791fd 107 uint8_t TA::red;
elmbed 17:d8b901d791fd 108 uint8_t TA::green;
elmbed 17:d8b901d791fd 109 uint8_t TA::blue;
elmbed 17:d8b901d791fd 110
elmbed 17:d8b901d791fd 111 neopixel_strip_t m_strip;
elmbed 17:d8b901d791fd 112
elmbed 17:d8b901d791fd 113 uint8_t dig_pin_num = 16;
elmbed 17:d8b901d791fd 114 uint8_t leds_per_strip = 18;
elmbed 17:d8b901d791fd 115 uint8_t led_to_enable = 1;
elmbed 17:d8b901d791fd 116
elmbed 17:d8b901d791fd 117 #if 0
elmbed 17:d8b901d791fd 118 DigitalOut TA::enable_1(p4);
elmbed 17:d8b901d791fd 119 DigitalOut TA::enable_2(p7);
elmbed 17:d8b901d791fd 120 DigitalOut TA::enable_3(p8);
elmbed 23:26f27c462976 121 #endif
elmbed 17:d8b901d791fd 122
AntonLS 30:c60b0d52b067 123 DigitalOut TA::cap_enable( p1, 0 );
AntonLS 30:c60b0d52b067 124 /// DigitalIn touch_top(p1);
AntonLS 30:c60b0d52b067 125 /// DigitalIn touch(p12);
elmbed 23:26f27c462976 126
AntonLS 30:c60b0d52b067 127 #define BUZZ_ON 0
AntonLS 30:c60b0d52b067 128 #define BUZZ_OFF 1
elmbed 17:d8b901d791fd 129
AntonLS 30:c60b0d52b067 130 DigitalOut TA::buzzPin( p20, BUZZ_OFF );
AntonLS 30:c60b0d52b067 131
AntonLS 54:2878d62714d6 132 bool TA::batteryLow = false;
AntonLS 49:626e84ce5e52 133 uint8_t TA::buttonsRising = 0;
AntonLS 39:b1f864b71318 134
AntonLS 30:c60b0d52b067 135 #if 1
elmbed 17:d8b901d791fd 136 // inputs
AntonLS 50:efe0f3017848 137 DigitalIn TA::touch_1( p12,PullDown ); // Test button (p6 [was] RSVD for lower button--p6 is last analog-in though.)
AntonLS 50:efe0f3017848 138 EdgeDigIn TA::touch_2( p0, PullNone ); // Top touch sensor.
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 51:fabe54861f1b 228 pulse_on = on_time; // (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
AntonLS 67:5650f461722a 242 bool TA::send( Message *m ) //// Restored existing architecture to use buffer.
elmbed 17:d8b901d791fd 243 {
AntonLS 67:5650f461722a 244 int s = (int)send_buffer.getCapacity() - (int)send_buffer.getSize();
AntonLS 67:5650f461722a 245 //DEBUG( "buffer space: %d\r\n", s );
AntonLS 67:5650f461722a 246 if( s < 7 )
AntonLS 67:5650f461722a 247 {
AntonLS 67:5650f461722a 248 DEBUG( "Warning, radio buffer overflow!" );
AntonLS 67:5650f461722a 249 return false;
AntonLS 67:5650f461722a 250 }
AntonLS 67:5650f461722a 251 //DEBUG( "queued" );
AntonLS 67:5650f461722a 252 send_buffer.put( (byte)m->command );
AntonLS 67:5650f461722a 253 send_buffer.putLong( (long)m->value );
AntonLS 67:5650f461722a 254 send_buffer.put( (byte)m->cone );
AntonLS 67:5650f461722a 255 send_buffer.put( '%' );
AntonLS 67:5650f461722a 256 /*
AntonLS 67:5650f461722a 257 DEBUG( "Sent: \r\n" );
AntonLS 67:5650f461722a 258 DEBUG( "%d\r\n", m->cone );
AntonLS 67:5650f461722a 259 DEBUG( "%c\r\n", (char)m->command );
AntonLS 67:5650f461722a 260 DEBUG( "%d\r\n", m->value );
AntonLS 67:5650f461722a 261 DEBUG( "%%\r\n" );
AntonLS 67:5650f461722a 262 DEBUG( "\r\n" );
AntonLS 67:5650f461722a 263
AntonLS 67:5650f461722a 264 DEBUG( "Raw send:\r\n" );
AntonLS 67:5650f461722a 265 DBITS( (byte)m->cone, 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 266 DBITS( (byte)m->command, 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 267 DBITS( (byte)(m->value>>24), 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 268 DBITS( (byte)(m->value>>16), 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 269 DBITS( (byte)(m->value>>8), 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 270 DBITS( (byte)m->value, 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 271 DBITS( '%', 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 272 */
AntonLS 67:5650f461722a 273 /*
AntonLS 67:5650f461722a 274 DBITS( receive_buffer.peek(0), 8 ); DEBUG( "\r\n" ); //<-cone?
AntonLS 67:5650f461722a 275 DBITS( send_buffer.peek(1), 8 ); DEBUG( "\r\n" ); //<-command
AntonLS 67:5650f461722a 276 DBITS( send_buffer.peek(2), 8 ); DEBUG( "\r\n" ); // value byte4
AntonLS 67:5650f461722a 277 DBITS( send_buffer.peek(3), 8 ); DEBUG( "\r\n" ); // value byte2
AntonLS 67:5650f461722a 278 DBITS( send_buffer.peek(4), 8 ); DEBUG( "\r\n" ); // value byte1
AntonLS 67:5650f461722a 279 DBITS( send_buffer.peek(5), 8 ); DEBUG( "\r\n" ); // value byte0 (lsb)
AntonLS 67:5650f461722a 280 DBITS( send_buffer.peek(6), 8 ); DEBUG( "\r\n" ); //<-%
AntonLS 67:5650f461722a 281 */
AntonLS 67:5650f461722a 282 //DEBUG( "\r\n" );
AntonLS 67:5650f461722a 283 /*
AntonLS 67:5650f461722a 284 int i = 0;
AntonLS 67:5650f461722a 285 DEBUG( "\r\n" );
AntonLS 67:5650f461722a 286 DEBUG( "Sent\r\n" );
AntonLS 67:5650f461722a 287 for( i=0; i<8; i++ ){ DBITS( send_buffer.peek(i), 8 ); DEBUG( "\r\n" ); }
AntonLS 67:5650f461722a 288 DEBUG( "\r\n" );
AntonLS 67:5650f461722a 289 */
AntonLS 67:5650f461722a 290
AntonLS 67:5650f461722a 291 return true;
elmbed 17:d8b901d791fd 292 }
elmbed 17:d8b901d791fd 293
AntonLS 67:5650f461722a 294 void TA::send_immediate(Message *m) //// Does not req. ACK.
elmbed 17:d8b901d791fd 295 {
AntonLS 67:5650f461722a 296 radio_send( m, false );
elmbed 17:d8b901d791fd 297 }
elmbed 17:d8b901d791fd 298
AntonLS 67:5650f461722a 299 bool TA::sendRaw(uint8_t *message, uint8_t len, uint8_t cone) //// Currently not used.
elmbed 17:d8b901d791fd 300 {
elmbed 38:76e49d045a3b 301 radio.send(cone, message, len);
elmbed 17:d8b901d791fd 302 return true;
elmbed 17:d8b901d791fd 303 }
elmbed 17:d8b901d791fd 304
AntonLS 67:5650f461722a 305 // Restored existing architecture to use buffer.
elmbed 17:d8b901d791fd 306 bool TA::recieve(Message *m)
elmbed 17:d8b901d791fd 307 {
AntonLS 67:5650f461722a 308 /*
AntonLS 67:5650f461722a 309 if( receive_buffer.getSize() > 1 )
AntonLS 67:5650f461722a 310 {
AntonLS 67:5650f461722a 311 DEBUG( "buffer size is: %d\r\n", receive_buffer.getSize() );
AntonLS 67:5650f461722a 312 }
AntonLS 67:5650f461722a 313 */
AntonLS 67:5650f461722a 314 if( receive_buffer.getSize() < 7 ) return false;
AntonLS 67:5650f461722a 315 int i = 0;
AntonLS 67:5650f461722a 316 // DEBUG( "\r\n" );
AntonLS 67:5650f461722a 317 // DEBUG( "Received\r\n" );
AntonLS 67:5650f461722a 318 // for( i=0; i<8; i++ ) DBITS( receive_buffer.peek(i), 8 );
AntonLS 67:5650f461722a 319 // DEBUG( "\r\n" );
AntonLS 67:5650f461722a 320
AntonLS 67:5650f461722a 321 /*
AntonLS 67:5650f461722a 322 DEBUG( "%d\r\n", receive_buffer.peek(0) );
AntonLS 67:5650f461722a 323 DEBUG( "%d\r\n", receive_buffer.peek(1) );
AntonLS 67:5650f461722a 324 DEBUG( "%d\r\n", receive_buffer.peek(2) );
AntonLS 67:5650f461722a 325 DEBUG( "%d\r\n", receive_buffer.peek(3) );
AntonLS 67:5650f461722a 326 DEBUG( "%d\r\n", receive_buffer.peek(4) );
AntonLS 67:5650f461722a 327 DEBUG( "%d\r\n", receive_buffer.peek(5) );
AntonLS 67:5650f461722a 328 */
AntonLS 67:5650f461722a 329
AntonLS 67:5650f461722a 330 //DEBUG( "%d\r\n", receive_buffer.getSize() );
AntonLS 67:5650f461722a 331 while( (receive_buffer.getSize() > 7) && ((char)receive_buffer.peek(6) != '%') ) receive_buffer.get();
AntonLS 67:5650f461722a 332 //DEBUG( "%d\r\n", receive_buffer.getSize() );
AntonLS 67:5650f461722a 333
AntonLS 67:5650f461722a 334 if( receive_buffer.getSize() > 6 )// && receive_buffer.peek(6) == '%')
AntonLS 67:5650f461722a 335 {
AntonLS 67:5650f461722a 336 //DEBUG( "\r\n" );
AntonLS 67:5650f461722a 337 /*
AntonLS 67:5650f461722a 338 DEBUG( "Received:\r\n" );
AntonLS 67:5650f461722a 339 DBITS( receive_buffer.peek(0), 8 ); DEBUG( "\r\n" ); //<-cone?
AntonLS 67:5650f461722a 340 DBITS( receive_buffer.peek(1), 8 ); DEBUG( "\r\n" ); //<-command
AntonLS 67:5650f461722a 341 DBITS( receive_buffer.peek(2), 8 ); DEBUG( "\r\n" ); // value byte4
AntonLS 67:5650f461722a 342 DBITS( receive_buffer.peek(3), 8 ); DEBUG( "\r\n" ); // value byte2
AntonLS 67:5650f461722a 343 DBITS( receive_buffer.peek(4), 8 ); DEBUG( "\r\n" ); // value byte1
AntonLS 67:5650f461722a 344 DBITS( receive_buffer.peek(5), 8 ); DEBUG( "\r\n" ); // value byte0 (lsb)
AntonLS 67:5650f461722a 345 DBITS( receive_buffer.peek(6), 8 ); DEBUG( "\r\n" ); //<-%
AntonLS 67:5650f461722a 346 */
AntonLS 67:5650f461722a 347 m->cone = receive_buffer.get();
AntonLS 67:5650f461722a 348 m->command = receive_buffer.get();
AntonLS 67:5650f461722a 349 m->value = (uint32_t)receive_buffer.get();
AntonLS 67:5650f461722a 350 m->value = m->value << 8;
AntonLS 67:5650f461722a 351 m->value += (uint32_t)receive_buffer.get();
AntonLS 67:5650f461722a 352 m->value = m->value << 8;
AntonLS 67:5650f461722a 353 m->value += (uint32_t)receive_buffer.get();
AntonLS 67:5650f461722a 354 m->value = m->value << 8;
AntonLS 67:5650f461722a 355 m->value += (uint32_t)receive_buffer.get();
AntonLS 67:5650f461722a 356
AntonLS 67:5650f461722a 357 /*
AntonLS 67:5650f461722a 358 DEBUG( "Raw receive:\r\n" );
AntonLS 67:5650f461722a 359 DBITS( (byte)m->cone, 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 360 DBITS( (byte)m->command, 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 361 DBITS( (byte)(m->value>>24), 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 362 DBITS( (byte)(m->value>>16), 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 363 DBITS( (byte)(m->value>>8), 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 364 DBITS( (byte)m->value, 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 365 DBITS( '%', 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 366 */
AntonLS 67:5650f461722a 367
AntonLS 67:5650f461722a 368 /*
AntonLS 67:5650f461722a 369 DEBUG( "\r\n" );
AntonLS 67:5650f461722a 370 DEBUG( "Received:\r\n" );
AntonLS 67:5650f461722a 371 DEBUG( "%d\r\n", m->cone );
AntonLS 67:5650f461722a 372 DEBUG( "%c\r\n", (char)m->command );
AntonLS 67:5650f461722a 373 DEBUG( "%d\r\n", m->value );
AntonLS 67:5650f461722a 374 DEBUG( "\r\n" );
AntonLS 67:5650f461722a 375 */
AntonLS 67:5650f461722a 376 return true;
AntonLS 67:5650f461722a 377
AntonLS 67:5650f461722a 378 } else
AntonLS 67:5650f461722a 379 {
AntonLS 67:5650f461722a 380 DEBUG( "Bad Packet, size is: %d\r\n", receive_buffer.getSize() );
AntonLS 67:5650f461722a 381 while( receive_buffer.getSize() ) DEBUG( "%c", (char)receive_buffer.get() );
AntonLS 67:5650f461722a 382 DEBUG( "\r\n" );
AntonLS 67:5650f461722a 383 return false;
AntonLS 67:5650f461722a 384 }
elmbed 17:d8b901d791fd 385 }
elmbed 17:d8b901d791fd 386
AntonLS 67:5650f461722a 387 bool TA::waitForAck(int cone) //// Currently not used.
elmbed 17:d8b901d791fd 388 {
elmbed 23:26f27c462976 389 long start = micros();
elmbed 23:26f27c462976 390 long timeout = ack_time;// + random(0,2000);
elmbed 23:26f27c462976 391 while (micros() - start < timeout){
AntonLS 67:5650f461722a 392 if( radio_ack_received( cone ) ){
elmbed 23:26f27c462976 393 //Serial.println(millis() - now);
elmbed 23:26f27c462976 394 return true;
elmbed 23:26f27c462976 395 }
elmbed 23:26f27c462976 396 }
elmbed 23:26f27c462976 397 //Serial.println(millis() - start);
elmbed 23:26f27c462976 398 return false;
elmbed 17:d8b901d791fd 399 }
elmbed 17:d8b901d791fd 400
AntonLS 46:28c29ef61276 401 void TA::resetTouch()
AntonLS 31:a6110950f385 402 {
AntonLS 31:a6110950f385 403 cap_enable = 1;
AntonLS 31:a6110950f385 404 wait_ms(100);
AntonLS 31:a6110950f385 405 cap_enable = 0;
AntonLS 31:a6110950f385 406 }
AntonLS 31:a6110950f385 407
AntonLS 67:5650f461722a 408 #define MAXSENDTRIES 15
AntonLS 67:5650f461722a 409
elmbed 17:d8b901d791fd 410 void TA::spin(void)
elmbed 17:d8b901d791fd 411 {
elmbed 23:26f27c462976 412 static byte payload [6];
elmbed 23:26f27c462976 413 static bool message_in_queue = false;
elmbed 23:26f27c462976 414 static bool waiting_for_ack = false;
elmbed 23:26f27c462976 415 static byte dest_cone = 0;
elmbed 23:26f27c462976 416 static uint16_t index = 0;
elmbed 23:26f27c462976 417 static uint16_t ack_tries = 0;
elmbed 23:26f27c462976 418 static uint8_t send_tries = 0;
elmbed 23:26f27c462976 419 static unsigned long ack_start = 0;
elmbed 23:26f27c462976 420 static unsigned long random_wait = 0;
elmbed 23:26f27c462976 421 static unsigned long random_wait_start = 0;
elmbed 23:26f27c462976 422 static unsigned long mem_monitor_start = 0;
elmbed 18:affef3a7db2a 423
AntonLS 67:5650f461722a 424 static Message m; ////
AntonLS 67:5650f461722a 425
elmbed 26:40a0c775ff27 426 static unsigned long last_touch = 0;
AntonLS 32:64e5d7340d82 427
elmbed 26:40a0c775ff27 428 if (last_touch == 0 || millis()-last_touch > 3000)
elmbed 26:40a0c775ff27 429 {
AntonLS 50:efe0f3017848 430 //writeToPhone("Touch value: %d\r\n", touch_2);
elmbed 26:40a0c775ff27 431 last_touch = millis();
AntonLS 30:c60b0d52b067 432 }
elmbed 26:40a0c775ff27 433
AntonLS 46:28c29ef61276 434 if(tripped())
AntonLS 46:28c29ef61276 435 {
AntonLS 46:28c29ef61276 436 resetTouch();
AntonLS 46:28c29ef61276 437 writeToPhone("TCP\r\n");
AntonLS 46:28c29ef61276 438 //Serial.println(F("toggled cap sense power"));
AntonLS 46:28c29ef61276 439 }
AntonLS 31:a6110950f385 440
elmbed 23:26f27c462976 441 if(powering_up2)
elmbed 23:26f27c462976 442 {
elmbed 23:26f27c462976 443 unsigned long t = millis() - powerup_start;
elmbed 23:26f27c462976 444 if(t > powerup_toggle)
elmbed 23:26f27c462976 445 {
AntonLS 30:c60b0d52b067 446 buzzPin = !buzzPin;
elmbed 23:26f27c462976 447 powerup_toggle *= 1.2;
elmbed 23:26f27c462976 448 }
elmbed 23:26f27c462976 449 if(t > 1250)
elmbed 18:affef3a7db2a 450 {
AntonLS 30:c60b0d52b067 451 buzzPin = BUZZ_OFF;
elmbed 23:26f27c462976 452 powering_up2 = false;
elmbed 23:26f27c462976 453 }
elmbed 23:26f27c462976 454 }
elmbed 23:26f27c462976 455 else if(powering_up1)
elmbed 23:26f27c462976 456 {
elmbed 23:26f27c462976 457 unsigned long t = millis() - powerup_start;
elmbed 23:26f27c462976 458 if(t > powerup_toggle)
elmbed 23:26f27c462976 459 {
AntonLS 30:c60b0d52b067 460 buzzPin = !buzzPin;
elmbed 23:26f27c462976 461 powerup_toggle *= 0.95;
elmbed 23:26f27c462976 462 powerup_start = millis();
elmbed 18:affef3a7db2a 463 }
elmbed 23:26f27c462976 464 if(powerup_toggle < 10)
elmbed 18:affef3a7db2a 465 {
AntonLS 30:c60b0d52b067 466 buzzPin = BUZZ_OFF;
elmbed 23:26f27c462976 467 powering_up1 = false;
elmbed 23:26f27c462976 468 }
elmbed 23:26f27c462976 469 }
elmbed 23:26f27c462976 470 else
elmbed 23:26f27c462976 471 {
elmbed 23:26f27c462976 472 if(beeping && (millis()-beep_start > beep_duration))
elmbed 23:26f27c462976 473 beeping = false;
elmbed 23:26f27c462976 474 if(pulsing && (millis()-pulse_start > pulse_duration))
elmbed 23:26f27c462976 475 pulsing = false;
AntonLS 30:c60b0d52b067 476 if(beeping)
AntonLS 30:c60b0d52b067 477 buzzPin = BUZZ_ON;
elmbed 23:26f27c462976 478 else if( pulsing && (((millis()-pulse_start) % pulse_period) < pulse_on))
elmbed 23:26f27c462976 479 {
AntonLS 30:c60b0d52b067 480 if(!(mask & SILENT))
AntonLS 30:c60b0d52b067 481 buzzPin = BUZZ_ON;
elmbed 23:26f27c462976 482 mask_color(0);
elmbed 18:affef3a7db2a 483 }
elmbed 18:affef3a7db2a 484 else
elmbed 18:affef3a7db2a 485 {
AntonLS 30:c60b0d52b067 486 if(pulsing)mask_color(current_color);
AntonLS 30:c60b0d52b067 487 buzzPin = BUZZ_OFF;
elmbed 18:affef3a7db2a 488 }
elmbed 23:26f27c462976 489 }
elmbed 23:26f27c462976 490
AntonLS 67:5650f461722a 491 gotACK = false; ////
AntonLS 67:5650f461722a 492 if( !waiting_for_ack && radio_receive( &m ) ) ////
elmbed 23:26f27c462976 493 {
AntonLS 67:5650f461722a 494 if( get_crc_ok() )
elmbed 18:affef3a7db2a 495 {
AntonLS 67:5650f461722a 496 if( radio.ACK_RECEIVED ) gotACK = true; ////
AntonLS 67:5650f461722a 497 else
AntonLS 67:5650f461722a 498 {
AntonLS 67:5650f461722a 499 receive_buffer.put( radio.SENDERID );
AntonLS 67:5650f461722a 500
AntonLS 67:5650f461722a 501 for( byte i=0; i < radio.DATALEN-1; i++ )
AntonLS 67:5650f461722a 502 {
AntonLS 67:5650f461722a 503 receive_buffer.put( radio.DATA[i] );
AntonLS 67:5650f461722a 504 }
AntonLS 67:5650f461722a 505 receive_buffer.put( '%' );
AntonLS 67:5650f461722a 506 /*
AntonLS 67:5650f461722a 507 DEBUG( "\r\n" );
AntonLS 67:5650f461722a 508 DEBUG( "Recieved: \r\n" );
AntonLS 67:5650f461722a 509 for( byte i=0; i < radio.DATALEN; i++ )
AntonLS 67:5650f461722a 510 {
AntonLS 67:5650f461722a 511 DBITS( radio.DATA[i], 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 512 }
AntonLS 67:5650f461722a 513 DEBUG( "\r\n" );
AntonLS 67:5650f461722a 514 */
AntonLS 67:5650f461722a 515 if( radio.ACKRequested() )
AntonLS 67:5650f461722a 516 {
AntonLS 67:5650f461722a 517 //// wait_us( 1300 );
AntonLS 67:5650f461722a 518 radio_send_ack();
AntonLS 67:5650f461722a 519 //DEBUG( "Sent ACK\r\n" );
AntonLS 67:5650f461722a 520 }
AntonLS 67:5650f461722a 521 }
AntonLS 67:5650f461722a 522
AntonLS 67:5650f461722a 523 } else writeToPhone( "BAD-CRC\r\n" );
elmbed 23:26f27c462976 524 }
elmbed 23:26f27c462976 525
elmbed 23:26f27c462976 526 //if(index > 4999 || waiting_for_ack){
AntonLS 67:5650f461722a 527 if( waiting_for_ack )
AntonLS 67:5650f461722a 528 {
AntonLS 67:5650f461722a 529 //DEBUG( "Waiting for ack\r\n" );
AntonLS 67:5650f461722a 530 bool success = radio_ack_received( dest_cone );
AntonLS 67:5650f461722a 531 if( success || (send_tries > MAXSENDTRIES) )
AntonLS 67:5650f461722a 532 {
AntonLS 67:5650f461722a 533 //DEBUG( "dequeued: %u\r\n", (uint16_t)payload[1]<<8 + payload[2] );
elmbed 23:26f27c462976 534 message_in_queue = false;
AntonLS 67:5650f461722a 535 waiting_for_ack = false;
elmbed 23:26f27c462976 536 unsigned long t = micros() - ack_start;
AntonLS 67:5650f461722a 537 //DEBUG( "Received ACK, microseconds: %u\r\n", t );
elmbed 23:26f27c462976 538 //ack_tries = 0;
AntonLS 67:5650f461722a 539 if( !success )
AntonLS 67:5650f461722a 540 {
AntonLS 67:5650f461722a 541 //// DEBUG( "Failed to deliver message to cone %d\r\n", dest_cone );
AntonLS 67:5650f461722a 542 RA_DEBUG( "Fail msg to cone %d\r\n", dest_cone ); ////
AntonLS 67:5650f461722a 543
AntonLS 67:5650f461722a 544 } else
AntonLS 67:5650f461722a 545 {
AntonLS 67:5650f461722a 546 if( send_tries > 1 )
AntonLS 67:5650f461722a 547 {
AntonLS 67:5650f461722a 548 /*
AntonLS 67:5650f461722a 549 DEBUG( "Sent message to cone %d, tries = %d\r\n", dest_cone, send_tries );
AntonLS 67:5650f461722a 550 */
AntonLS 67:5650f461722a 551 RA_DEBUG( "Rcvd ACK cone %d try %d, %uus\r\n", dest_cone, send_tries, t );
AntonLS 67:5650f461722a 552
AntonLS 67:5650f461722a 553 } else
AntonLS 67:5650f461722a 554 {
AntonLS 67:5650f461722a 555 RA_DEBUG( "Rcvd ACK, %uus\r\n", t ); ////
AntonLS 67:5650f461722a 556 }
AntonLS 67:5650f461722a 557 }
AntonLS 67:5650f461722a 558
elmbed 23:26f27c462976 559 message_in_queue = false;
AntonLS 67:5650f461722a 560 send_tries = 0;
AntonLS 67:5650f461722a 561 /*
AntonLS 67:5650f461722a 562 DEBUG( "Sent %c, to cone %d\r\n", (char)payload[0], dest_cone );
AntonLS 67:5650f461722a 563 */
AntonLS 67:5650f461722a 564
AntonLS 67:5650f461722a 565 } else // Still trying to get an ACK...
AntonLS 67:5650f461722a 566 {
AntonLS 67:5650f461722a 567 if( micros() - ack_start > 15000 /* 10000 */ ) ////
AntonLS 67:5650f461722a 568 {
AntonLS 67:5650f461722a 569 RA_DEBUG( "No ACK after 15ms\r\n" );
AntonLS 67:5650f461722a 570 waiting_for_ack = false;
AntonLS 67:5650f461722a 571 random_wait_start = micros();
AntonLS 67:5650f461722a 572 random_wait = random( 1500, 3000 );
AntonLS 67:5650f461722a 573 /*
AntonLS 67:5650f461722a 574 if( send_tries > MAXSENDTRIES )
AntonLS 67:5650f461722a 575 {
AntonLS 67:5650f461722a 576 DEBUG( "Failed to deliver message to cone %d\r\n", dest_cone );
AntonLS 67:5650f461722a 577 message_in_queue = false;
AntonLS 67:5650f461722a 578 send_tries = 0;
AntonLS 67:5650f461722a 579 }
AntonLS 67:5650f461722a 580 */
AntonLS 67:5650f461722a 581 if( send_tries > 4 ) random_wait = random( 3000, 9000 );
AntonLS 67:5650f461722a 582 //DEBUG( "Failed to deliver message, waiting %d\r\n", random_wait );
AntonLS 67:5650f461722a 583 //ack_tries = 0;
AntonLS 67:5650f461722a 584
AntonLS 67:5650f461722a 585 /*
AntonLS 67:5650f461722a 586 if( send_tries%50 == 0 )
AntonLS 67:5650f461722a 587 {
AntonLS 67:5650f461722a 588 DEBUG( "%d tries\r\n", send_tries );
AntonLS 67:5650f461722a 589 }
AntonLS 67:5650f461722a 590 */
AntonLS 67:5650f461722a 591 }
elmbed 23:26f27c462976 592 }
elmbed 23:26f27c462976 593 // ack_tries++;
AntonLS 67:5650f461722a 594
AntonLS 67:5650f461722a 595 } else if( message_in_queue && (micros() - random_wait > random_wait_start) )// && index%64 == 0)
AntonLS 67:5650f461722a 596 { //// Continued from ack timeout in a previous spin()...
AntonLS 67:5650f461722a 597 if( gotACK ) ////
AntonLS 67:5650f461722a 598 {
AntonLS 67:5650f461722a 599 message_in_queue = false;
AntonLS 67:5650f461722a 600 send_tries = 0;
AntonLS 67:5650f461722a 601 RA_DEBUG( "Rcvd B ACK %uus\r\n", micros() -ack_start ); ////
AntonLS 67:5650f461722a 602
AntonLS 67:5650f461722a 603 } else
AntonLS 67:5650f461722a 604 {
AntonLS 67:5650f461722a 605 requestACK = true;
AntonLS 67:5650f461722a 606 //DEBUG( "sending\r\n" );
AntonLS 67:5650f461722a 607 /// writeToPhone("Sending message to: %d\r\n", dest_cone);
AntonLS 67:5650f461722a 608 radio.send( dest_cone, payload, 6, requestACK );
AntonLS 67:5650f461722a 609 //DEBUG( "sent\r\n" );
AntonLS 67:5650f461722a 610 //DEBUG( "Trying to send: %d\r\n", (uint8_t)payload[0] );
AntonLS 67:5650f461722a 611 //uint16_t temp = (uint16_t)payload[1]<<8;
AntonLS 67:5650f461722a 612 //temp += (uint8_t)payload[2];
AntonLS 67:5650f461722a 613 send_tries++;
AntonLS 67:5650f461722a 614 ack_start = micros();
AntonLS 67:5650f461722a 615 if( !radio_receive_complete() /* !radio_ack_received( dest_cone ) */ ) // the 'if' is here to prevent the radio from going to sleep and missing the ACK
AntonLS 67:5650f461722a 616 {
AntonLS 67:5650f461722a 617 waiting_for_ack = true; //// Check for ack on next spin()
AntonLS 67:5650f461722a 618
AntonLS 68:0c96bb3d73a7 619 } else // Shouldn't happen...
AntonLS 67:5650f461722a 620 {
AntonLS 67:5650f461722a 621 if( radio.ACK_RECEIVED ) message_in_queue = false;
AntonLS 67:5650f461722a 622 RA_DEBUG( "Rcvd Immed %uus\r\n", micros() -ack_start ); ////
AntonLS 67:5650f461722a 623 }
AntonLS 67:5650f461722a 624 }
AntonLS 67:5650f461722a 625
AntonLS 67:5650f461722a 626 } else
AntonLS 67:5650f461722a 627 {
AntonLS 67:5650f461722a 628 /*
AntonLS 67:5650f461722a 629 if( (send_buffer.getSize() > 0) && !message_in_queue )
AntonLS 67:5650f461722a 630 {
AntonLS 67:5650f461722a 631 payload[0] = send_buffer.get();
AntonLS 67:5650f461722a 632 //DEBUG( "Got from queue: %c\r\n", (char)payload[0] );
AntonLS 67:5650f461722a 633 message_in_queue = true;
AntonLS 67:5650f461722a 634 }
AntonLS 67:5650f461722a 635 */
AntonLS 67:5650f461722a 636 while( (send_buffer.getSize() > 4) && !message_in_queue )
AntonLS 67:5650f461722a 637 {
AntonLS 67:5650f461722a 638 payload[0] = send_buffer.get();
AntonLS 67:5650f461722a 639 payload[1] = send_buffer.get();
AntonLS 67:5650f461722a 640 payload[2] = send_buffer.get();
AntonLS 67:5650f461722a 641 payload[3] = send_buffer.get();
AntonLS 67:5650f461722a 642 payload[4] = send_buffer.get();
AntonLS 67:5650f461722a 643 dest_cone = send_buffer.get();
AntonLS 67:5650f461722a 644 payload[5] = send_buffer.get();
AntonLS 67:5650f461722a 645 /*
AntonLS 67:5650f461722a 646 DEBUG( "\r\n" );
AntonLS 67:5650f461722a 647 DEBUG( "sending...\r\n" );
AntonLS 67:5650f461722a 648 DBITS( payload[0], 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 649 DBITS( payload[1], 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 650 DBITS( payload[2], 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 651 DBITS( payload[3], 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 652 DBITS( payload[4], 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 653 DBITS( dest_cone, 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 654 DBITS( payload[5], 8 ); DEBUG( "\r\n" );
AntonLS 67:5650f461722a 655 DEBUG( "\r\n" );
AntonLS 67:5650f461722a 656 */
AntonLS 67:5650f461722a 657 if( (char)payload[5] == '%' )
AntonLS 67:5650f461722a 658 {
AntonLS 67:5650f461722a 659 message_in_queue = true;
AntonLS 67:5650f461722a 660 payload[5] = ++msg_id; //// Message ID used for duplicate message check.
AntonLS 67:5650f461722a 661
AntonLS 67:5650f461722a 662 } else
AntonLS 67:5650f461722a 663 {
AntonLS 67:5650f461722a 664 //DEBUG( "bad message format\r\n" );
AntonLS 67:5650f461722a 665 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
AntonLS 67:5650f461722a 666 }
AntonLS 67:5650f461722a 667 }
elmbed 18:affef3a7db2a 668 }
elmbed 17:d8b901d791fd 669 }
elmbed 17:d8b901d791fd 670
elmbed 17:d8b901d791fd 671 bool TA::activated(void)
elmbed 17:d8b901d791fd 672 {
AntonLS 67:5650f461722a 673 /* Default to enabling activation on disabled--Except if mask has DIS_ON_DARK set */
AntonLS 67:5650f461722a 674 uint8_t maskOrDark = ((!(mask & TOUCHLIGHTS) != !!(mask & DIS_ON_DARK)) ? TOUCHLIGHTS : mask);
AntonLS 67:5650f461722a 675
AntonLS 67:5650f461722a 676 bool retval = (((buttons() | buttonsRising) & maskOrDark) ? true : false);
AntonLS 49:626e84ce5e52 677
AntonLS 49:626e84ce5e52 678 TA::buttonsRising = 0;
AntonLS 49:626e84ce5e52 679
AntonLS 49:626e84ce5e52 680 return retval;
elmbed 17:d8b901d791fd 681 }
elmbed 17:d8b901d791fd 682
elmbed 17:d8b901d791fd 683 bool TA::tripped(void)
elmbed 17:d8b901d791fd 684 {
elmbed 23:26f27c462976 685 // detect if something is staying on the cap sensor (i.e. the floor is causing the sensor to overload)
elmbed 23:26f27c462976 686 // in some cases the sensor output is choppy, so we must detect those situations also.
elmbed 23:26f27c462976 687 uint8_t current = 0;
elmbed 23:26f27c462976 688 static bool sensor_hysteresis = false;
elmbed 23:26f27c462976 689 static uint8_t last;
elmbed 23:26f27c462976 690 static unsigned long activated_start;
elmbed 23:26f27c462976 691 static unsigned long sensor_hyst_start;
elmbed 23:26f27c462976 692
elmbed 23:26f27c462976 693 current = buttons();
elmbed 23:26f27c462976 694 if(current && !last && (millis() - sensor_hyst_start > 250)){
elmbed 23:26f27c462976 695 activated_start = millis();
elmbed 23:26f27c462976 696 //Serial.println(current | 0b10000000, BIN);
elmbed 23:26f27c462976 697 }
elmbed 23:26f27c462976 698 if(!current && last) sensor_hyst_start = millis();
elmbed 23:26f27c462976 699 last = current;
elmbed 23:26f27c462976 700
elmbed 38:76e49d045a3b 701 return (current && (millis() - activated_start > 4000))?true:false;
elmbed 23:26f27c462976 702
elmbed 17:d8b901d791fd 703 }
elmbed 17:d8b901d791fd 704
elmbed 17:d8b901d791fd 705 uint8_t TA::buttons(void)
elmbed 17:d8b901d791fd 706 {
elmbed 23:26f27c462976 707 uint8_t buttons = 0;
elmbed 17:d8b901d791fd 708
AntonLS 30:c60b0d52b067 709 buttons |= touch_1?1:0;
AntonLS 30:c60b0d52b067 710 buttons |= touch_2?2:0;
AntonLS 30:c60b0d52b067 711 buttons |= touch_3?0:4;
AntonLS 30:c60b0d52b067 712 return buttons; /// touch;
elmbed 23:26f27c462976 713
elmbed 17:d8b901d791fd 714 }
elmbed 17:d8b901d791fd 715
elmbed 17:d8b901d791fd 716 void TA::setMask(uint8_t the_mask)
elmbed 17:d8b901d791fd 717 {
elmbed 17:d8b901d791fd 718 mask = the_mask;
elmbed 17:d8b901d791fd 719 }
elmbed 17:d8b901d791fd 720
elmbed 17:d8b901d791fd 721 void TA::initialize(uint8_t address)
elmbed 17:d8b901d791fd 722 {
AntonLS 67:5650f461722a 723 send_buffer.init(96); //48//64//128 is too big, seems to corrupt the data, WTF????
AntonLS 67:5650f461722a 724 receive_buffer.init(96);
AntonLS 67:5650f461722a 725
AntonLS 67:5650f461722a 726 resetTouch();
AntonLS 67:5650f461722a 727
AntonLS 67:5650f461722a 728 ack_time = ACK_TIME; //// Currently not used.
AntonLS 67:5650f461722a 729
AntonLS 67:5650f461722a 730 radio_init();
AntonLS 67:5650f461722a 731
AntonLS 67:5650f461722a 732 beep(50);
elmbed 17:d8b901d791fd 733 }
elmbed 17:d8b901d791fd 734
elmbed 17:d8b901d791fd 735 /* Can't find a way of implementing this function.
elmbed 17:d8b901d791fd 736 *
elmbed 17:d8b901d791fd 737 * !SR
elmbed 17:d8b901d791fd 738 */
elmbed 17:d8b901d791fd 739 void TA::Ram_TableDisplay(void)
elmbed 17:d8b901d791fd 740 {
elmbed 17:d8b901d791fd 741
elmbed 17:d8b901d791fd 742 }
elmbed 17:d8b901d791fd 743
elmbed 17:d8b901d791fd 744 /* 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 745 *
elmbed 17:d8b901d791fd 746 * !SR
elmbed 17:d8b901d791fd 747 */
elmbed 17:d8b901d791fd 748 void TA::get_free_memory(void)
elmbed 17:d8b901d791fd 749 {
elmbed 17:d8b901d791fd 750 char stackVariable;
elmbed 17:d8b901d791fd 751 char *heap;
elmbed 17:d8b901d791fd 752 unsigned long result;
elmbed 17:d8b901d791fd 753 heap = (char*)malloc(4);
elmbed 17:d8b901d791fd 754 result = &stackVariable - heap;
elmbed 17:d8b901d791fd 755 free(heap);
elmbed 17:d8b901d791fd 756
elmbed 17:d8b901d791fd 757 //serial->printf("Free memory: %ul\n\n",result);
elmbed 17:d8b901d791fd 758 }
elmbed 17:d8b901d791fd 759
elmbed 17:d8b901d791fd 760 void TA::check_mem(void)
elmbed 17:d8b901d791fd 761 {
elmbed 17:d8b901d791fd 762 uint8_t * heapptr, * stackptr;
elmbed 17:d8b901d791fd 763 unsigned int stack, heap = 0;
elmbed 17:d8b901d791fd 764
elmbed 17:d8b901d791fd 765 stackptr = (uint8_t *)malloc(4); // use stackptr temporarily
elmbed 17:d8b901d791fd 766 heapptr = stackptr; // save value of heap pointer
elmbed 17:d8b901d791fd 767 free(stackptr); // free up the memory again (sets stackptr to 0)
elmbed 17:d8b901d791fd 768
elmbed 17:d8b901d791fd 769 stack = __current_sp() ;
elmbed 17:d8b901d791fd 770 heap = (uint16_t)heapptr;
elmbed 17:d8b901d791fd 771 uint16_t last_call = *(stackptr++);
elmbed 17:d8b901d791fd 772
elmbed 17:d8b901d791fd 773 //serial->printf("Stack: 0x");
elmbed 17:d8b901d791fd 774 //serial->printf("%x ",stack);
elmbed 17:d8b901d791fd 775 //serial->printf("Heap: 0x");
elmbed 17:d8b901d791fd 776 //serial->printf("%x\n",heap);
elmbed 17:d8b901d791fd 777 //serial->printf("Last call: 0x");
elmbed 17:d8b901d791fd 778 // serial->printf("%x\n",last_call);
elmbed 17:d8b901d791fd 779 get_free_memory();
elmbed 17:d8b901d791fd 780 }
elmbed 17:d8b901d791fd 781