Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Tue Jan 19 03:12:17 2016 +0000
Revision:
64:8886b5e4ab57
Parent:
62:9b34dc1b265d
Parent:
56:dccf9b22d594
Child:
65:7addf8506bdb
Merged in orphaned Radio.cpp comments.

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