Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Tue Jan 19 03:15:25 2016 +0000
Revision:
65:7addf8506bdb
Parent:
64:8886b5e4ab57
Child:
67:5650f461722a
Commented-out get_node_id().

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"
elmbed 18:affef3a7db2a 6
elmbed 44:4ad6133987ed 7 #define RADIO_STARTUP_ID 99
elmbed 18:affef3a7db2a 8 #define NETWORKID 101 //the same on all nodes that talk to each other
elmbed 18:affef3a7db2a 9
elmbed 18:affef3a7db2a 10 #define FREQUENCY RF69_915MHZ
elmbed 18:affef3a7db2a 11
elmbed 18:affef3a7db2a 12 //#define IS_RFM69HW //NOTE: uncomment this ONLY for RFM69HW or RFM69HCW
elmbed 18:affef3a7db2a 13 #define ENCRYPT_KEY "EncryptKey123456" // use same 16byte encryption key for all devices on net
elmbed 18:affef3a7db2a 14 #define ACK_TIME 50 // max msec for ACK wait
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 "C" void writeToPhone(char *format, ...);
elmbed 44:4ad6133987ed 27 extern unsigned long millis();
elmbed 44:4ad6133987ed 28
elmbed 62:9b34dc1b265d 29 extern bool is_master;
elmbed 18:affef3a7db2a 30
elmbed 44:4ad6133987ed 31 struct node_id_mapping
elmbed 44:4ad6133987ed 32 {
elmbed 44:4ad6133987ed 33 int mac;
elmbed 44:4ad6133987ed 34 int node_id;
elmbed 44:4ad6133987ed 35 };
elmbed 18:affef3a7db2a 36
elmbed 44:4ad6133987ed 37 struct extended_message
elmbed 44:4ad6133987ed 38 {
elmbed 44:4ad6133987ed 39 Message m;
elmbed 44:4ad6133987ed 40 int mac;
elmbed 44:4ad6133987ed 41 };
elmbed 23:26f27c462976 42
AntonLS 65:7addf8506bdb 43 /*
elmbed 56:dccf9b22d594 44 int get_node_id()
elmbed 56:dccf9b22d594 45 {
elmbed 56:dccf9b22d594 46 return my_node_id == RADIO_STARTUP_ID ? 0 : my_node_id;
elmbed 56:dccf9b22d594 47 }
AntonLS 65:7addf8506bdb 48 */
elmbed 56:dccf9b22d594 49
elmbed 44:4ad6133987ed 50 static node_id_mapping nodes[10] = {0};
elmbed 44:4ad6133987ed 51 static int node_idx = 0;
elmbed 44:4ad6133987ed 52 static int my_mac = 0;
elmbed 18:affef3a7db2a 53
elmbed 18:affef3a7db2a 54 void radio_init()
elmbed 18:affef3a7db2a 55 {
elmbed 62:9b34dc1b265d 56 radio.initialize(FREQUENCY, datastore_node_id(), NETWORKID);
elmbed 23:26f27c462976 57 radio.encrypt(0);
elmbed 23:26f27c462976 58 radio.promiscuous(promiscuousMode);
elmbed 23:26f27c462976 59 }
elmbed 23:26f27c462976 60
elmbed 56:dccf9b22d594 61 /* Gets the node ID which is associated with the
elmbed 56:dccf9b22d594 62 * given mac. If no ID has been given then zero
elmbed 56:dccf9b22d594 63 * is returned.
elmbed 56:dccf9b22d594 64 *
elmbed 56:dccf9b22d594 65 * @param mac - the mac address if the cone
elmbed 56:dccf9b22d594 66 * @returns ID if found else zero
elmbed 56:dccf9b22d594 67 */
elmbed 44:4ad6133987ed 68 static int get_node_id(int mac)
elmbed 44:4ad6133987ed 69 {
elmbed 44:4ad6133987ed 70 for (int i = 0; i < node_idx; ++i)
elmbed 44:4ad6133987ed 71 {
elmbed 44:4ad6133987ed 72 if (nodes[i].mac == mac)
elmbed 44:4ad6133987ed 73 {
elmbed 44:4ad6133987ed 74 return nodes[i].node_id;
elmbed 44:4ad6133987ed 75 }
elmbed 44:4ad6133987ed 76 }
elmbed 44:4ad6133987ed 77
elmbed 44:4ad6133987ed 78 return 0;
elmbed 44:4ad6133987ed 79 }
elmbed 28:8e74ddc4f70f 80
elmbed 56:dccf9b22d594 81 /* Process a radio startup message from the master.
elmbed 56:dccf9b22d594 82 *
elmbed 56:dccf9b22d594 83 * @param em - the message from the master
elmbed 56:dccf9b22d594 84 */
elmbed 44:4ad6133987ed 85 static void slave_process(extended_message *em)
elmbed 44:4ad6133987ed 86 {
elmbed 44:4ad6133987ed 87 if (em->m.command == 'R')
elmbed 44:4ad6133987ed 88 {
elmbed 44:4ad6133987ed 89 // We have been given a node id
elmbed 62:9b34dc1b265d 90 datastore_set_node_id(em->m.value);
elmbed 44:4ad6133987ed 91
elmbed 62:9b34dc1b265d 92 if( Dbg ) writeToPhone("NID: %d\r\n", datastore_node_id());
elmbed 44:4ad6133987ed 93
elmbed 44:4ad6133987ed 94 // Now reset the radio :)
elmbed 44:4ad6133987ed 95 radio_init();
elmbed 44:4ad6133987ed 96 }
elmbed 44:4ad6133987ed 97 }
elmbed 44:4ad6133987ed 98
elmbed 56:dccf9b22d594 99 /* Process a radio startup message from a slave.
elmbed 56:dccf9b22d594 100 *
elmbed 56:dccf9b22d594 101 * @param m - the message from the slave
elmbed 56:dccf9b22d594 102 */
elmbed 44:4ad6133987ed 103 static void master_process(Message *m)
elmbed 44:4ad6133987ed 104 {
elmbed 44:4ad6133987ed 105 byte payload[10] = {0};
elmbed 44:4ad6133987ed 106 int node_id = 0;
elmbed 44:4ad6133987ed 107
elmbed 44:4ad6133987ed 108 if (m->command == 'S')
elmbed 44:4ad6133987ed 109 {
elmbed 44:4ad6133987ed 110 // Message from a cone requesting an ID.
elmbed 44:4ad6133987ed 111 // The payload should be part of the MAC address
elmbed 44:4ad6133987ed 112 node_id = 0;
elmbed 44:4ad6133987ed 113
elmbed 44:4ad6133987ed 114 if (node_idx == 0)
elmbed 44:4ad6133987ed 115 {
elmbed 44:4ad6133987ed 116 node_id = 2;
elmbed 44:4ad6133987ed 117 }
elmbed 44:4ad6133987ed 118 else
elmbed 44:4ad6133987ed 119 {
elmbed 44:4ad6133987ed 120 node_id = nodes[node_idx-1].node_id + 1;
elmbed 44:4ad6133987ed 121 }
elmbed 44:4ad6133987ed 122
elmbed 44:4ad6133987ed 123 int node_found = get_node_id(m->value);
elmbed 44:4ad6133987ed 124
elmbed 44:4ad6133987ed 125 if (node_found == 0)
elmbed 44:4ad6133987ed 126 {
elmbed 44:4ad6133987ed 127 nodes[node_idx].node_id = node_id;
elmbed 44:4ad6133987ed 128 nodes[node_idx++].mac = m->value;
elmbed 44:4ad6133987ed 129 }
elmbed 44:4ad6133987ed 130 else
elmbed 44:4ad6133987ed 131 {
elmbed 44:4ad6133987ed 132 node_id = node_found;
elmbed 44:4ad6133987ed 133 }
elmbed 44:4ad6133987ed 134
elmbed 44:4ad6133987ed 135 // Send the cone it's ID
elmbed 44:4ad6133987ed 136 extended_message em;
elmbed 44:4ad6133987ed 137 em.m.cone = 99;
elmbed 44:4ad6133987ed 138 em.m.command = 'R';
elmbed 44:4ad6133987ed 139 em.m.value = node_id;
elmbed 44:4ad6133987ed 140 em.mac = m->value;
elmbed 44:4ad6133987ed 141
elmbed 44:4ad6133987ed 142 payload[0] = (byte)em.m.command;
elmbed 44:4ad6133987ed 143
elmbed 44:4ad6133987ed 144 payload[4] = (byte)em.m.value & 255;
elmbed 44:4ad6133987ed 145 payload[3] = (byte)(em.m.value >> 8);
elmbed 44:4ad6133987ed 146 payload[2] = (byte)(em.m.value >> 16);
elmbed 44:4ad6133987ed 147 payload[1] = (byte)(em.m.value >> 24);
elmbed 44:4ad6133987ed 148
elmbed 44:4ad6133987ed 149 payload[8] = (byte)em.mac & 255;
elmbed 44:4ad6133987ed 150 payload[7] = (byte)(em.mac >> 8);
elmbed 44:4ad6133987ed 151 payload[6] = (byte)(em.mac >> 16);
elmbed 44:4ad6133987ed 152 payload[5] = (byte)(em.mac >> 24);
elmbed 44:4ad6133987ed 153
elmbed 44:4ad6133987ed 154 radio.send(em.m.cone, payload, sizeof(payload));
elmbed 44:4ad6133987ed 155
AntonLS 52:060fdec99780 156 if( Dbg ) writeToPhone("SND: %d %d 0x%x\r\n", em.m.cone, node_id, em.mac);
elmbed 44:4ad6133987ed 157 }
elmbed 44:4ad6133987ed 158 }
elmbed 28:8e74ddc4f70f 159
elmbed 23:26f27c462976 160 void radio_send(Message *m)
elmbed 23:26f27c462976 161 {
elmbed 23:26f27c462976 162 static byte payload [6] = {0};
elmbed 23:26f27c462976 163
elmbed 23:26f27c462976 164 if (m == NULL)
elmbed 23:26f27c462976 165 {
elmbed 23:26f27c462976 166 return;
elmbed 23:26f27c462976 167 }
elmbed 23:26f27c462976 168
AntonLS 52:060fdec99780 169 //if( Dbg ) writeToPhone("SM: %c to: %d frm: %d (%d)\r\n", m->command, m->cone, NODE_ID, m->value);
AntonLS 52:060fdec99780 170
elmbed 23:26f27c462976 171 payload[0] = (byte)m->command;
elmbed 44:4ad6133987ed 172
elmbed 23:26f27c462976 173 payload[4] = (byte)m->value & 255;
elmbed 23:26f27c462976 174 payload[3] = (byte)(m->value >> 8);
elmbed 23:26f27c462976 175 payload[2] = (byte)(m->value >> 16);
elmbed 23:26f27c462976 176 payload[1] = (byte)(m->value >> 24);
elmbed 44:4ad6133987ed 177
elmbed 23:26f27c462976 178 payload[5] = (byte)'%';
elmbed 28:8e74ddc4f70f 179
elmbed 28:8e74ddc4f70f 180 radio.send(m->cone, payload, sizeof(payload));
elmbed 18:affef3a7db2a 181 }
elmbed 18:affef3a7db2a 182
elmbed 23:26f27c462976 183 bool radio_receive_complete()
elmbed 23:26f27c462976 184 {
elmbed 23:26f27c462976 185 return radio.receiveDone();
elmbed 23:26f27c462976 186 }
elmbed 23:26f27c462976 187
elmbed 23:26f27c462976 188 bool radio_receive(Message *m)
elmbed 23:26f27c462976 189 {
elmbed 44:4ad6133987ed 190 Message lm;
elmbed 44:4ad6133987ed 191 extended_message em;
elmbed 44:4ad6133987ed 192
elmbed 23:26f27c462976 193 if (m == NULL)
elmbed 23:26f27c462976 194 {
elmbed 23:26f27c462976 195 return false;
elmbed 23:26f27c462976 196 }
elmbed 23:26f27c462976 197
elmbed 23:26f27c462976 198 if (radio.receiveDone())
elmbed 44:4ad6133987ed 199 {
elmbed 23:26f27c462976 200 if (radio.DATALEN < 6)
elmbed 23:26f27c462976 201 {
elmbed 23:26f27c462976 202 return false;
elmbed 23:26f27c462976 203 }
elmbed 23:26f27c462976 204
elmbed 44:4ad6133987ed 205 lm.cone = radio.SENDERID;
elmbed 44:4ad6133987ed 206 lm.command = radio.DATA[0];
elmbed 44:4ad6133987ed 207
elmbed 44:4ad6133987ed 208 lm.value = ((int)radio.DATA[1] << 24);
elmbed 44:4ad6133987ed 209 lm.value |= ((int)radio.DATA[2] << 16);
elmbed 44:4ad6133987ed 210 lm.value |= (((int)radio.DATA[3]) << 8);
elmbed 44:4ad6133987ed 211 lm.value |= (radio.DATA[4]);
elmbed 44:4ad6133987ed 212
elmbed 44:4ad6133987ed 213 if (is_master && lm.command == 'S')
elmbed 44:4ad6133987ed 214 {
elmbed 44:4ad6133987ed 215 // Don't pass on radio startup messages
elmbed 44:4ad6133987ed 216 master_process(&lm);
elmbed 44:4ad6133987ed 217 return false;
elmbed 44:4ad6133987ed 218 }
elmbed 44:4ad6133987ed 219 else if (!is_master && radio.TARGETID == RADIO_STARTUP_ID)
elmbed 29:ae208b014987 220 {
elmbed 44:4ad6133987ed 221
elmbed 44:4ad6133987ed 222 em.mac = (int)radio.DATA[8] & 0xff;
elmbed 44:4ad6133987ed 223 em.mac |= (int)radio.DATA[7] << 8;
elmbed 44:4ad6133987ed 224 em.mac |= (int)radio.DATA[6] << 16;
elmbed 44:4ad6133987ed 225 em.mac |= (int)radio.DATA[5] << 24;
elmbed 44:4ad6133987ed 226
AntonLS 52:060fdec99780 227 if( Dbg ) writeToPhone("RP: 0x%x\r\n", em.mac);
elmbed 44:4ad6133987ed 228
elmbed 44:4ad6133987ed 229 if (em.mac != my_mac)
elmbed 44:4ad6133987ed 230 {
AntonLS 52:060fdec99780 231 if( Dbg ) writeToPhone("DM 0x%x\r\n", em.mac);
elmbed 44:4ad6133987ed 232 return false; // This message was meant for someone else
elmbed 44:4ad6133987ed 233 }
elmbed 44:4ad6133987ed 234
elmbed 44:4ad6133987ed 235 memcpy(&em.m, &lm, sizeof(Message));
elmbed 44:4ad6133987ed 236 slave_process(&em);
elmbed 44:4ad6133987ed 237
elmbed 44:4ad6133987ed 238 }
elmbed 62:9b34dc1b265d 239 else if (radio.TARGETID == datastore_node_id())
elmbed 44:4ad6133987ed 240 {
elmbed 44:4ad6133987ed 241 memcpy(m, &lm, sizeof(Message));
elmbed 38:76e49d045a3b 242
AntonLS 52:060fdec99780 243 if( Dbg ) writeToPhone("GM: %d %c %d\r\n", radio.SENDERID, m->command, m->value);
AntonLS 32:64e5d7340d82 244
elmbed 29:ae208b014987 245 return true;
elmbed 29:ae208b014987 246 }
elmbed 23:26f27c462976 247 }
elmbed 23:26f27c462976 248
elmbed 23:26f27c462976 249 return false;
elmbed 23:26f27c462976 250 }
elmbed 23:26f27c462976 251
elmbed 23:26f27c462976 252 bool radio_ack_received(int cone)
elmbed 23:26f27c462976 253 {
elmbed 23:26f27c462976 254 return radio.ACKReceived(cone);
elmbed 23:26f27c462976 255 }
elmbed 23:26f27c462976 256
elmbed 56:dccf9b22d594 257 /* This is only needed for the slave cones, the master
elmbed 56:dccf9b22d594 258 * cone will just return.
elmbed 56:dccf9b22d594 259 *
elmbed 56:dccf9b22d594 260 * If the slave cone doesn't have a valid node ID send out
elmbed 56:dccf9b22d594 261 * the node ID request message to the master cone.
elmbed 56:dccf9b22d594 262 *
elmbed 56:dccf9b22d594 263 * @param mac - the mac address for this cone (currently the first two bytes)
elmbed 56:dccf9b22d594 264 */
elmbed 44:4ad6133987ed 265 void radio_loop(int mac)
elmbed 18:affef3a7db2a 266 {
elmbed 44:4ad6133987ed 267 static unsigned long last_send = 0L;
elmbed 44:4ad6133987ed 268 unsigned long current = millis();
elmbed 44:4ad6133987ed 269 Message m;
elmbed 44:4ad6133987ed 270
elmbed 44:4ad6133987ed 271 if (my_mac == 0)
elmbed 44:4ad6133987ed 272 {
elmbed 44:4ad6133987ed 273 my_mac = mac;
elmbed 44:4ad6133987ed 274 }
elmbed 44:4ad6133987ed 275
elmbed 44:4ad6133987ed 276 if (is_master)
elmbed 44:4ad6133987ed 277 {
elmbed 44:4ad6133987ed 278 return; // Only needed for slave cones
elmbed 44:4ad6133987ed 279 }
elmbed 44:4ad6133987ed 280
elmbed 62:9b34dc1b265d 281 if (datastore_node_id() != RADIO_STARTUP_ID && current - last_send > 1000L)
elmbed 44:4ad6133987ed 282 {
elmbed 62:9b34dc1b265d 283 if( Dbg ) writeToPhone("NID: %d\r\n", datastore_node_id());
elmbed 44:4ad6133987ed 284 last_send = current;
elmbed 44:4ad6133987ed 285 }
elmbed 44:4ad6133987ed 286
elmbed 44:4ad6133987ed 287 // Send out an i'm here message
elmbed 62:9b34dc1b265d 288 if (datastore_node_id() != RADIO_STARTUP_ID || current - last_send < 1000L)
elmbed 44:4ad6133987ed 289 {
elmbed 44:4ad6133987ed 290 return;
elmbed 44:4ad6133987ed 291 }
elmbed 44:4ad6133987ed 292
elmbed 44:4ad6133987ed 293 m.command = 'S';
elmbed 44:4ad6133987ed 294 m.cone = 1;
elmbed 44:4ad6133987ed 295 m.value = my_mac;
elmbed 44:4ad6133987ed 296
AntonLS 52:060fdec99780 297 if( Dbg ) writeToPhone("SNM\r\n");
elmbed 44:4ad6133987ed 298
elmbed 44:4ad6133987ed 299 last_send = current;
elmbed 44:4ad6133987ed 300
elmbed 44:4ad6133987ed 301 radio_send(&m);
elmbed 18:affef3a7db2a 302 }
elmbed 23:26f27c462976 303