Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Wed Mar 02 03:21:37 2016 +0000
Revision:
73:ac3588017e32
Parent:
70:bd4b1e19a0c6
Child:
75:1b357bee1839
Ignore radio messages when charging.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elmbed 18:affef3a7db2a 1 #include <RFM69.h>
elmbed 18:affef3a7db2a 2 #include <SPI.h>
elmbed 23:26f27c462976 3 #include "types.h"
elmbed 23:26f27c462976 4 #include "TA.h"
elmbed 62:9b34dc1b265d 5 #include "DataStore.hh"
AntonLS 67:5650f461722a 6 #include "Radio.hh"
elmbed 18:affef3a7db2a 7
elmbed 44:4ad6133987ed 8 #define RADIO_STARTUP_ID 99
elmbed 18:affef3a7db2a 9 #define NETWORKID 101 //the same on all nodes that talk to each other
elmbed 18:affef3a7db2a 10
elmbed 18:affef3a7db2a 11 #define FREQUENCY RF69_915MHZ
elmbed 18:affef3a7db2a 12
elmbed 18:affef3a7db2a 13 //#define IS_RFM69HW //NOTE: uncomment this ONLY for RFM69HW or RFM69HCW
elmbed 18:affef3a7db2a 14 #define ENCRYPT_KEY "EncryptKey123456" // use same 16byte encryption key for all devices on net
elmbed 18:affef3a7db2a 15 #define LED 9 // Anardino miniWireless has LEDs on D9
elmbed 18:affef3a7db2a 16 #define SERIAL_BAUD 115200
elmbed 18:affef3a7db2a 17 #define VERSION "1.0"
elmbed 18:affef3a7db2a 18
elmbed 18:affef3a7db2a 19 #define MSGBUFSIZE 64 // message buffersize, but for this demo we only use:
elmbed 18:affef3a7db2a 20 // 1-byte NODEID + 4-bytes for time + 1-byte for temp in C + 2-bytes for vcc(mV)
elmbed 18:affef3a7db2a 21
elmbed 18:affef3a7db2a 22 //RFM69::RFM69(PinName PinName mosi, PinName miso, PinName sclk,slaveSelectPin, PinName int)
elmbed 23:26f27c462976 23 RFM69 radio(P0_24,P0_23,P0_25,P0_28,P0_7);
elmbed 18:affef3a7db2a 24
elmbed 23:26f27c462976 25 static bool promiscuousMode = true; // set 'true' to sniff all packets on the same network
elmbed 44:4ad6133987ed 26 extern unsigned long millis();
elmbed 44:4ad6133987ed 27
elmbed 62:9b34dc1b265d 28 extern bool is_master;
elmbed 18:affef3a7db2a 29
elmbed 44:4ad6133987ed 30 struct node_id_mapping
elmbed 44:4ad6133987ed 31 {
elmbed 44:4ad6133987ed 32 int mac;
elmbed 44:4ad6133987ed 33 int node_id;
elmbed 44:4ad6133987ed 34 };
elmbed 18:affef3a7db2a 35
elmbed 44:4ad6133987ed 36 struct extended_message
elmbed 44:4ad6133987ed 37 {
elmbed 44:4ad6133987ed 38 Message m;
elmbed 44:4ad6133987ed 39 int mac;
elmbed 44:4ad6133987ed 40 };
elmbed 23:26f27c462976 41
AntonLS 67:5650f461722a 42 struct prev_message // maybe TODO we could have an array of NUM_CONES of these.
AntonLS 67:5650f461722a 43 {
AntonLS 67:5650f461722a 44 Message m;
AntonLS 67:5650f461722a 45 uint8_t msgid;
AntonLS 67:5650f461722a 46 };
AntonLS 67:5650f461722a 47
AntonLS 65:7addf8506bdb 48 /*
elmbed 56:dccf9b22d594 49 int get_node_id()
elmbed 56:dccf9b22d594 50 {
elmbed 56:dccf9b22d594 51 return my_node_id == RADIO_STARTUP_ID ? 0 : my_node_id;
elmbed 56:dccf9b22d594 52 }
AntonLS 65:7addf8506bdb 53 */
elmbed 56:dccf9b22d594 54
AntonLS 67:5650f461722a 55 static node_id_mapping nodes[NUM_CONES] = {0};
elmbed 44:4ad6133987ed 56 static int node_idx = 0;
elmbed 44:4ad6133987ed 57 static int my_mac = 0;
elmbed 18:affef3a7db2a 58
elmbed 18:affef3a7db2a 59 void radio_init()
elmbed 18:affef3a7db2a 60 {
elmbed 62:9b34dc1b265d 61 radio.initialize(FREQUENCY, datastore_node_id(), NETWORKID);
elmbed 23:26f27c462976 62 radio.encrypt(0);
elmbed 23:26f27c462976 63 radio.promiscuous(promiscuousMode);
elmbed 23:26f27c462976 64 }
elmbed 23:26f27c462976 65
elmbed 56:dccf9b22d594 66 /* Gets the node ID which is associated with the
elmbed 56:dccf9b22d594 67 * given mac. If no ID has been given then zero
elmbed 56:dccf9b22d594 68 * is returned.
elmbed 56:dccf9b22d594 69 *
elmbed 56:dccf9b22d594 70 * @param mac - the mac address if the cone
elmbed 56:dccf9b22d594 71 * @returns ID if found else zero
elmbed 56:dccf9b22d594 72 */
elmbed 44:4ad6133987ed 73 static int get_node_id(int mac)
elmbed 44:4ad6133987ed 74 {
elmbed 44:4ad6133987ed 75 for (int i = 0; i < node_idx; ++i)
elmbed 44:4ad6133987ed 76 {
elmbed 44:4ad6133987ed 77 if (nodes[i].mac == mac)
elmbed 44:4ad6133987ed 78 {
elmbed 44:4ad6133987ed 79 return nodes[i].node_id;
elmbed 44:4ad6133987ed 80 }
elmbed 44:4ad6133987ed 81 }
elmbed 44:4ad6133987ed 82
elmbed 44:4ad6133987ed 83 return 0;
elmbed 44:4ad6133987ed 84 }
elmbed 28:8e74ddc4f70f 85
elmbed 56:dccf9b22d594 86 /* Process a radio startup message from the master.
elmbed 56:dccf9b22d594 87 *
elmbed 56:dccf9b22d594 88 * @param em - the message from the master
elmbed 56:dccf9b22d594 89 */
elmbed 44:4ad6133987ed 90 static void slave_process(extended_message *em)
elmbed 44:4ad6133987ed 91 {
elmbed 44:4ad6133987ed 92 if (em->m.command == 'R')
elmbed 44:4ad6133987ed 93 {
elmbed 44:4ad6133987ed 94 // We have been given a node id
elmbed 62:9b34dc1b265d 95 datastore_set_node_id(em->m.value);
elmbed 44:4ad6133987ed 96
elmbed 62:9b34dc1b265d 97 if( Dbg ) writeToPhone("NID: %d\r\n", datastore_node_id());
elmbed 44:4ad6133987ed 98
elmbed 44:4ad6133987ed 99 // Now reset the radio :)
AntonLS 67:5650f461722a 100 /// promiscuousMode = false;
elmbed 44:4ad6133987ed 101 radio_init();
elmbed 44:4ad6133987ed 102 }
elmbed 44:4ad6133987ed 103 }
elmbed 44:4ad6133987ed 104
elmbed 56:dccf9b22d594 105 /* Process a radio startup message from a slave.
elmbed 56:dccf9b22d594 106 *
elmbed 56:dccf9b22d594 107 * @param m - the message from the slave
elmbed 56:dccf9b22d594 108 */
elmbed 44:4ad6133987ed 109 static void master_process(Message *m)
elmbed 44:4ad6133987ed 110 {
elmbed 44:4ad6133987ed 111 byte payload[10] = {0};
elmbed 44:4ad6133987ed 112 int node_id = 0;
elmbed 44:4ad6133987ed 113
elmbed 44:4ad6133987ed 114 if (m->command == 'S')
elmbed 44:4ad6133987ed 115 {
elmbed 44:4ad6133987ed 116 // Message from a cone requesting an ID.
elmbed 44:4ad6133987ed 117 // The payload should be part of the MAC address
elmbed 44:4ad6133987ed 118 node_id = 0;
elmbed 44:4ad6133987ed 119
elmbed 44:4ad6133987ed 120 if (node_idx == 0)
elmbed 44:4ad6133987ed 121 {
elmbed 44:4ad6133987ed 122 node_id = 2;
elmbed 44:4ad6133987ed 123 }
elmbed 44:4ad6133987ed 124 else
elmbed 44:4ad6133987ed 125 {
elmbed 44:4ad6133987ed 126 node_id = nodes[node_idx-1].node_id + 1;
elmbed 44:4ad6133987ed 127 }
elmbed 44:4ad6133987ed 128
elmbed 44:4ad6133987ed 129 int node_found = get_node_id(m->value);
elmbed 44:4ad6133987ed 130
elmbed 44:4ad6133987ed 131 if (node_found == 0)
elmbed 44:4ad6133987ed 132 {
elmbed 44:4ad6133987ed 133 nodes[node_idx].node_id = node_id;
elmbed 44:4ad6133987ed 134 nodes[node_idx++].mac = m->value;
elmbed 44:4ad6133987ed 135 }
elmbed 44:4ad6133987ed 136 else
elmbed 44:4ad6133987ed 137 {
elmbed 44:4ad6133987ed 138 node_id = node_found;
elmbed 44:4ad6133987ed 139 }
elmbed 44:4ad6133987ed 140
AntonLS 67:5650f461722a 141 // Send the cone its ID
elmbed 44:4ad6133987ed 142 extended_message em;
elmbed 44:4ad6133987ed 143 em.m.cone = 99;
elmbed 44:4ad6133987ed 144 em.m.command = 'R';
elmbed 44:4ad6133987ed 145 em.m.value = node_id;
elmbed 44:4ad6133987ed 146 em.mac = m->value;
elmbed 44:4ad6133987ed 147
elmbed 44:4ad6133987ed 148 payload[0] = (byte)em.m.command;
elmbed 44:4ad6133987ed 149
elmbed 44:4ad6133987ed 150 payload[4] = (byte)em.m.value & 255;
elmbed 44:4ad6133987ed 151 payload[3] = (byte)(em.m.value >> 8);
elmbed 44:4ad6133987ed 152 payload[2] = (byte)(em.m.value >> 16);
elmbed 44:4ad6133987ed 153 payload[1] = (byte)(em.m.value >> 24);
elmbed 44:4ad6133987ed 154
elmbed 44:4ad6133987ed 155 payload[8] = (byte)em.mac & 255;
elmbed 44:4ad6133987ed 156 payload[7] = (byte)(em.mac >> 8);
elmbed 44:4ad6133987ed 157 payload[6] = (byte)(em.mac >> 16);
elmbed 44:4ad6133987ed 158 payload[5] = (byte)(em.mac >> 24);
AntonLS 67:5650f461722a 159
AntonLS 69:a3295b74209e 160 radio_send_raw( em.m.cone, payload, sizeof(payload), false ); //// NO ACK req.
AntonLS 67:5650f461722a 161 /// radio.send(em.m.cone, payload, sizeof(payload));
elmbed 44:4ad6133987ed 162
AntonLS 52:060fdec99780 163 if( Dbg ) writeToPhone("SND: %d %d 0x%x\r\n", em.m.cone, node_id, em.mac);
elmbed 44:4ad6133987ed 164 }
elmbed 44:4ad6133987ed 165 }
elmbed 28:8e74ddc4f70f 166
AntonLS 69:a3295b74209e 167 void radio_send_raw( uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK ) ////
AntonLS 69:a3295b74209e 168 {
AntonLS 69:a3295b74209e 169 radio.send( toAddress, buffer, bufferSize, requestACK );
AntonLS 69:a3295b74209e 170
AntonLS 69:a3295b74209e 171 // radio_ensure_rx_mode();
AntonLS 69:a3295b74209e 172 }
AntonLS 69:a3295b74209e 173
AntonLS 67:5650f461722a 174 void radio_send( Message *m, bool requestACK ) ////
elmbed 23:26f27c462976 175 {
elmbed 23:26f27c462976 176 static byte payload [6] = {0};
elmbed 23:26f27c462976 177
elmbed 23:26f27c462976 178 if (m == NULL)
elmbed 23:26f27c462976 179 {
elmbed 23:26f27c462976 180 return;
elmbed 23:26f27c462976 181 }
AntonLS 67:5650f461722a 182
AntonLS 52:060fdec99780 183 //if( Dbg ) writeToPhone("SM: %c to: %d frm: %d (%d)\r\n", m->command, m->cone, NODE_ID, m->value);
AntonLS 52:060fdec99780 184
elmbed 23:26f27c462976 185 payload[0] = (byte)m->command;
elmbed 44:4ad6133987ed 186
elmbed 23:26f27c462976 187 payload[4] = (byte)m->value & 255;
elmbed 23:26f27c462976 188 payload[3] = (byte)(m->value >> 8);
elmbed 23:26f27c462976 189 payload[2] = (byte)(m->value >> 16);
elmbed 23:26f27c462976 190 payload[1] = (byte)(m->value >> 24);
AntonLS 67:5650f461722a 191
AntonLS 67:5650f461722a 192 payload[5] = (byte)++TA::msg_id; //// Msg ID, was: = (byte)'%';
AntonLS 67:5650f461722a 193
AntonLS 69:a3295b74209e 194 radio_send_raw( m->cone, payload, sizeof(payload), requestACK );
AntonLS 67:5650f461722a 195 }
AntonLS 67:5650f461722a 196
AntonLS 67:5650f461722a 197 void stompage_check()
AntonLS 67:5650f461722a 198 {
AntonLS 67:5650f461722a 199 if( --radio.INCNT != 0 )
AntonLS 67:5650f461722a 200 {
AntonLS 67:5650f461722a 201 writeToPhone( "LOST MSGS: %d\r\n", radio.INCNT );
AntonLS 67:5650f461722a 202 radio.INCNT = 0;
AntonLS 67:5650f461722a 203 }
elmbed 18:affef3a7db2a 204 }
elmbed 18:affef3a7db2a 205
AntonLS 73:ac3588017e32 206 bool radio_receive_complete() //// Currently not used.
elmbed 23:26f27c462976 207 {
elmbed 23:26f27c462976 208 return radio.receiveDone();
elmbed 23:26f27c462976 209 }
elmbed 23:26f27c462976 210
elmbed 23:26f27c462976 211 bool radio_receive(Message *m)
elmbed 23:26f27c462976 212 {
AntonLS 69:a3295b74209e 213 bool retval = false;
AntonLS 69:a3295b74209e 214 bool ensure_rx_mode = false;
AntonLS 67:5650f461722a 215
AntonLS 67:5650f461722a 216 //// For msg id feature...
AntonLS 67:5650f461722a 217 static prev_message mPrev;
AntonLS 69:a3295b74209e 218 static long lastMsgAt = millis();
AntonLS 67:5650f461722a 219
elmbed 44:4ad6133987ed 220 Message lm;
elmbed 44:4ad6133987ed 221 extended_message em;
AntonLS 67:5650f461722a 222
AntonLS 67:5650f461722a 223 do
elmbed 23:26f27c462976 224 {
AntonLS 67:5650f461722a 225 if( m == NULL ) break;
AntonLS 67:5650f461722a 226
AntonLS 73:ac3588017e32 227 if( radio.receiveDone() )
AntonLS 67:5650f461722a 228 {
AntonLS 67:5650f461722a 229 if( radio.TARGETID == datastore_node_id() ) //// Check for stompage...
AntonLS 67:5650f461722a 230 {
AntonLS 67:5650f461722a 231 stompage_check();
AntonLS 67:5650f461722a 232 }
AntonLS 67:5650f461722a 233 if( !get_crc_ok() ) // CRC error will be rechecked/sent from caller.
AntonLS 67:5650f461722a 234 {
AntonLS 67:5650f461722a 235 retval = true;
AntonLS 67:5650f461722a 236 break;
AntonLS 67:5650f461722a 237 }
AntonLS 69:a3295b74209e 238 if( (radio.DATALEN < 6) && !radio.ACK_RECEIVED )
AntonLS 67:5650f461722a 239 {
AntonLS 69:a3295b74209e 240 ensure_rx_mode = true;
AntonLS 69:a3295b74209e 241
AntonLS 67:5650f461722a 242 RA_DEBUG( "Warn: Short packet\r\n" );
AntonLS 67:5650f461722a 243 RA_DEBUG( "Warn: Data len %d\r\n", radio.DATALEN );
AntonLS 67:5650f461722a 244 RA_DEBUG( "Warn: Cmd %c, Frm %d\r\n", radio.DATA[0], radio.SENDERID );
AntonLS 67:5650f461722a 245 break;
AntonLS 67:5650f461722a 246 }
AntonLS 67:5650f461722a 247
AntonLS 67:5650f461722a 248 lm.cone = radio.SENDERID;
AntonLS 67:5650f461722a 249 lm.command = radio.DATA[0];
AntonLS 67:5650f461722a 250
AntonLS 67:5650f461722a 251 lm.value = ((int)radio.DATA[1] << 24);
AntonLS 67:5650f461722a 252 lm.value |= ((int)radio.DATA[2] << 16);
AntonLS 67:5650f461722a 253 lm.value |= (((int)radio.DATA[3]) << 8);
AntonLS 67:5650f461722a 254 lm.value |= (radio.DATA[4]);
AntonLS 67:5650f461722a 255
AntonLS 67:5650f461722a 256 if (is_master && lm.command == 'S')
AntonLS 67:5650f461722a 257 {
AntonLS 67:5650f461722a 258 // Don't pass on radio startup messages
AntonLS 67:5650f461722a 259 master_process(&lm);
AntonLS 67:5650f461722a 260 break;
AntonLS 67:5650f461722a 261 }
AntonLS 67:5650f461722a 262 else if (!is_master && (NODE_ID == 99) && radio.TARGETID == RADIO_STARTUP_ID) //// NODE_ID check allows not using auto node IDs
AntonLS 67:5650f461722a 263 {
AntonLS 67:5650f461722a 264 em.mac = (int)radio.DATA[8] & 0xff;
AntonLS 67:5650f461722a 265 em.mac |= (int)radio.DATA[7] << 8;
AntonLS 67:5650f461722a 266 em.mac |= (int)radio.DATA[6] << 16;
AntonLS 67:5650f461722a 267 em.mac |= (int)radio.DATA[5] << 24;
AntonLS 70:bd4b1e19a0c6 268
AntonLS 67:5650f461722a 269 if( Dbg ) writeToPhone("RP: 0x%x\r\n", em.mac);
elmbed 44:4ad6133987ed 270
AntonLS 67:5650f461722a 271 if (em.mac != my_mac)
AntonLS 67:5650f461722a 272 {
AntonLS 67:5650f461722a 273 if( Dbg ) writeToPhone("DM 0x%x\r\n", em.mac);
AntonLS 67:5650f461722a 274 break; // This message was meant for someone else
AntonLS 67:5650f461722a 275 }
AntonLS 70:bd4b1e19a0c6 276
AntonLS 67:5650f461722a 277 memcpy(&em.m, &lm, sizeof(Message));
AntonLS 67:5650f461722a 278 slave_process(&em);
AntonLS 67:5650f461722a 279 }
AntonLS 67:5650f461722a 280 else if (radio.TARGETID == datastore_node_id())
AntonLS 67:5650f461722a 281 {
AntonLS 67:5650f461722a 282 memcpy(m, &lm, sizeof(Message));
AntonLS 67:5650f461722a 283
AntonLS 69:a3295b74209e 284 if( !radio.ACK_RECEIVED )
AntonLS 69:a3295b74209e 285 {
AntonLS 69:a3295b74209e 286 //// Check if sender missed an ACK (Got duplicate of last msg, so ignore dup.)
AntonLS 69:a3295b74209e 287 if( (millis() -lastMsgAt < 100) && //// Only consider as dup if w/in 100ms of prev.
AntonLS 69:a3295b74209e 288 (mPrev.m.cone == m->cone) &&
AntonLS 69:a3295b74209e 289 (mPrev.msgid == radio.DATA[5]) )
AntonLS 69:a3295b74209e 290 { //// ASS-U-ME [for now] last msg is from same cone.
AntonLS 69:a3295b74209e 291 RA_DEBUG( "Ignored dup cmd\r\n" );
AntonLS 69:a3295b74209e 292 radio_send_ack();
AntonLS 69:a3295b74209e 293 ensure_rx_mode = true;
AntonLS 69:a3295b74209e 294 break;
AntonLS 69:a3295b74209e 295 }
AntonLS 69:a3295b74209e 296
AntonLS 69:a3295b74209e 297 memcpy( &mPrev.m, m, sizeof( Message ) ); ////
AntonLS 69:a3295b74209e 298 mPrev.msgid = radio.DATA[5]; ////
AntonLS 69:a3295b74209e 299 lastMsgAt = millis(); ////
AntonLS 69:a3295b74209e 300
AntonLS 69:a3295b74209e 301 if( Dbg ) writeToPhone("GM: %d %c %d\r\n", radio.SENDERID, m->command, m->value);
AntonLS 67:5650f461722a 302 }
AntonLS 67:5650f461722a 303 retval = true;
AntonLS 67:5650f461722a 304 }
AntonLS 67:5650f461722a 305 }
AntonLS 67:5650f461722a 306
AntonLS 67:5650f461722a 307 } while( false );
AntonLS 67:5650f461722a 308
AntonLS 69:a3295b74209e 309 if( ensure_rx_mode )
AntonLS 69:a3295b74209e 310 {
AntonLS 69:a3295b74209e 311 radio_ensure_rx_mode(); // Will stomp/erase payload data.
AntonLS 69:a3295b74209e 312 }
AntonLS 69:a3295b74209e 313
AntonLS 67:5650f461722a 314 if( retval )
AntonLS 67:5650f461722a 315 {
AntonLS 67:5650f461722a 316 if( get_crc_ok() )
AntonLS 67:5650f461722a 317 RA_DEBUG( "M: %c %d r:%d\r\n", m->command, m->value, get_rssi() );
AntonLS 67:5650f461722a 318 if( get_fifo_ov() )
AntonLS 67:5650f461722a 319 RA_DEBUG( "Radio FIFO Overflow\r\n" );
elmbed 23:26f27c462976 320 }
AntonLS 67:5650f461722a 321
AntonLS 67:5650f461722a 322 return retval;
elmbed 23:26f27c462976 323 }
elmbed 23:26f27c462976 324
AntonLS 69:a3295b74209e 325 void radio_ensure_rx_mode()
AntonLS 69:a3295b74209e 326 {
AntonLS 69:a3295b74209e 327 if( RF69_MODE_RX != radio._mode )
AntonLS 69:a3295b74209e 328 {
AntonLS 69:a3295b74209e 329 // Either do this or change the setMode in RFM69.cpp at end of sendFrame() to setMode(RF69_MODE_RX);
AntonLS 70:bd4b1e19a0c6 330 while( radio.receiveDone() ); //// Prevent radio from going to standby or sleep. (Shouldn't return true here.)
AntonLS 69:a3295b74209e 331 }
AntonLS 69:a3295b74209e 332 }
AntonLS 69:a3295b74209e 333
AntonLS 67:5650f461722a 334 void radio_send_ack() ////
AntonLS 67:5650f461722a 335 {
AntonLS 67:5650f461722a 336 radio.sendACK( "K", 1 );
AntonLS 69:a3295b74209e 337
AntonLS 69:a3295b74209e 338 // radio_ensure_rx_mode();
AntonLS 67:5650f461722a 339 }
AntonLS 67:5650f461722a 340
AntonLS 67:5650f461722a 341 bool radio_ack_received( int cone )
elmbed 23:26f27c462976 342 {
AntonLS 70:bd4b1e19a0c6 343 ////bool retval = radio.ACKReceived(cone);
AntonLS 70:bd4b1e19a0c6 344 bool retval = radio.receiveDone();
AntonLS 70:bd4b1e19a0c6 345
AntonLS 70:bd4b1e19a0c6 346 if( !get_crc_ok() )
AntonLS 70:bd4b1e19a0c6 347 {
AntonLS 70:bd4b1e19a0c6 348 writeToPhone( "BAD-CRC on ACK wait\r\n" );
AntonLS 70:bd4b1e19a0c6 349 retval = false;
AntonLS 70:bd4b1e19a0c6 350 }
AntonLS 67:5650f461722a 351
AntonLS 69:a3295b74209e 352 if( retval )
AntonLS 67:5650f461722a 353 {
AntonLS 70:bd4b1e19a0c6 354 if( !((radio.SENDERID == cone || cone == RF69_BROADCAST_ADDR) && radio.ACK_RECEIVED) )////...
AntonLS 70:bd4b1e19a0c6 355 {
AntonLS 70:bd4b1e19a0c6 356 // Got something that wasn't expected ACK. (Paranoia--Haven't seen this happen yet.)
AntonLS 70:bd4b1e19a0c6 357 RA_DEBUG( "Got '%c', not ACK!\r\n", (char)radio.DATA[0] );
AntonLS 70:bd4b1e19a0c6 358 RA_DEBUG( "Wanted from %u, got from %u\r\n", cone, radio.SENDERID );
AntonLS 70:bd4b1e19a0c6 359 retval = false;
AntonLS 70:bd4b1e19a0c6 360 }
AntonLS 70:bd4b1e19a0c6 361
AntonLS 69:a3295b74209e 362 if( radio.TARGETID == datastore_node_id() )
AntonLS 69:a3295b74209e 363 {
AntonLS 69:a3295b74209e 364 stompage_check();
AntonLS 69:a3295b74209e 365 }
AntonLS 69:a3295b74209e 366
AntonLS 69:a3295b74209e 367 // Note if we ever want to retrieve data from ACK packet: This stomps the packet data.
AntonLS 69:a3295b74209e 368 // radio_ensure_rx_mode();
AntonLS 67:5650f461722a 369 }
AntonLS 67:5650f461722a 370
AntonLS 67:5650f461722a 371 return retval;
elmbed 23:26f27c462976 372 }
elmbed 23:26f27c462976 373
elmbed 56:dccf9b22d594 374 /* This is only needed for the slave cones, the master
elmbed 56:dccf9b22d594 375 * cone will just return.
elmbed 56:dccf9b22d594 376 *
elmbed 56:dccf9b22d594 377 * If the slave cone doesn't have a valid node ID send out
elmbed 56:dccf9b22d594 378 * the node ID request message to the master cone.
elmbed 56:dccf9b22d594 379 *
elmbed 56:dccf9b22d594 380 * @param mac - the mac address for this cone (currently the first two bytes)
elmbed 56:dccf9b22d594 381 */
elmbed 44:4ad6133987ed 382 void radio_loop(int mac)
elmbed 18:affef3a7db2a 383 {
elmbed 44:4ad6133987ed 384 static unsigned long last_send = 0L;
elmbed 44:4ad6133987ed 385 unsigned long current = millis();
elmbed 44:4ad6133987ed 386 Message m;
elmbed 44:4ad6133987ed 387
elmbed 44:4ad6133987ed 388 if (my_mac == 0)
elmbed 44:4ad6133987ed 389 {
AntonLS 67:5650f461722a 390 my_mac = mac;
elmbed 44:4ad6133987ed 391 }
elmbed 44:4ad6133987ed 392
elmbed 44:4ad6133987ed 393 if (is_master)
elmbed 44:4ad6133987ed 394 {
elmbed 44:4ad6133987ed 395 return; // Only needed for slave cones
elmbed 44:4ad6133987ed 396 }
AntonLS 67:5650f461722a 397
elmbed 62:9b34dc1b265d 398 if (datastore_node_id() != RADIO_STARTUP_ID && current - last_send > 1000L)
elmbed 44:4ad6133987ed 399 {
elmbed 62:9b34dc1b265d 400 if( Dbg ) writeToPhone("NID: %d\r\n", datastore_node_id());
elmbed 44:4ad6133987ed 401 last_send = current;
elmbed 44:4ad6133987ed 402 }
elmbed 44:4ad6133987ed 403
elmbed 44:4ad6133987ed 404 // Send out an i'm here message
elmbed 62:9b34dc1b265d 405 if (datastore_node_id() != RADIO_STARTUP_ID || current - last_send < 1000L)
elmbed 44:4ad6133987ed 406 {
elmbed 44:4ad6133987ed 407 return;
elmbed 44:4ad6133987ed 408 }
elmbed 44:4ad6133987ed 409
elmbed 44:4ad6133987ed 410 m.command = 'S';
elmbed 44:4ad6133987ed 411 m.cone = 1;
elmbed 44:4ad6133987ed 412 m.value = my_mac;
elmbed 44:4ad6133987ed 413
AntonLS 52:060fdec99780 414 if( Dbg ) writeToPhone("SNM\r\n");
elmbed 44:4ad6133987ed 415
elmbed 44:4ad6133987ed 416 last_send = current;
elmbed 44:4ad6133987ed 417
AntonLS 67:5650f461722a 418 radio_send( &m, false ); ////
AntonLS 67:5650f461722a 419 /// radio_send(&m);
AntonLS 67:5650f461722a 420 }
AntonLS 67:5650f461722a 421
AntonLS 67:5650f461722a 422
AntonLS 67:5650f461722a 423
AntonLS 67:5650f461722a 424 int16_t get_rssi()
AntonLS 67:5650f461722a 425 {
AntonLS 67:5650f461722a 426 return radio.RSSI;
elmbed 18:affef3a7db2a 427 }
elmbed 23:26f27c462976 428
AntonLS 67:5650f461722a 429 bool get_crc_ok()
AntonLS 67:5650f461722a 430 {
AntonLS 67:5650f461722a 431 return radio.CRCOK;
AntonLS 67:5650f461722a 432 }
AntonLS 67:5650f461722a 433
AntonLS 67:5650f461722a 434 bool get_fifo_ov()
AntonLS 67:5650f461722a 435 {
AntonLS 67:5650f461722a 436 return radio.FIFOV;
AntonLS 67:5650f461722a 437 }
AntonLS 67:5650f461722a 438
AntonLS 67:5650f461722a 439 // for debugging
AntonLS 67:5650f461722a 440 void read_all_regs()
AntonLS 67:5650f461722a 441 {
AntonLS 67:5650f461722a 442 radio.readAllRegs();
AntonLS 67:5650f461722a 443 }
AntonLS 67:5650f461722a 444