Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

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