Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
elmbed
Date:
Wed Dec 30 14:00:28 2015 +0000
Revision:
26:40a0c775ff27
Parent:
23:26f27c462976
Child:
28:8e74ddc4f70f
Multiple radios tested

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 18:affef3a7db2a 6 #define NETWORKID 101 //the same on all nodes that talk to each other
elmbed 18:affef3a7db2a 7
elmbed 18:affef3a7db2a 8 #define FREQUENCY RF69_915MHZ
elmbed 18:affef3a7db2a 9
elmbed 18:affef3a7db2a 10 //#define IS_RFM69HW //NOTE: uncomment this ONLY for RFM69HW or RFM69HCW
elmbed 18:affef3a7db2a 11 #define ENCRYPT_KEY "EncryptKey123456" // use same 16byte encryption key for all devices on net
elmbed 18:affef3a7db2a 12 #define ACK_TIME 50 // max msec for ACK wait
elmbed 18:affef3a7db2a 13 #define LED 9 // Anardino miniWireless has LEDs on D9
elmbed 18:affef3a7db2a 14 #define SERIAL_BAUD 115200
elmbed 18:affef3a7db2a 15 #define VERSION "1.0"
elmbed 18:affef3a7db2a 16
elmbed 18:affef3a7db2a 17 #define MSGBUFSIZE 64 // message buffersize, but for this demo we only use:
elmbed 18:affef3a7db2a 18 // 1-byte NODEID + 4-bytes for time + 1-byte for temp in C + 2-bytes for vcc(mV)
elmbed 18:affef3a7db2a 19
elmbed 18:affef3a7db2a 20 //RFM69::RFM69(PinName PinName mosi, PinName miso, PinName sclk,slaveSelectPin, PinName int)
elmbed 23:26f27c462976 21 RFM69 radio(P0_24,P0_23,P0_25,P0_28,P0_7);
elmbed 18:affef3a7db2a 22
elmbed 23:26f27c462976 23 static bool promiscuousMode = true; // set 'true' to sniff all packets on the same network
elmbed 18:affef3a7db2a 24 static bool requestACK = false;
elmbed 18:affef3a7db2a 25
elmbed 23:26f27c462976 26 static char phone_buffer[50] = {0};
elmbed 23:26f27c462976 27 static char radio_buffer[50] = {0};
elmbed 18:affef3a7db2a 28
elmbed 23:26f27c462976 29 extern "C" void writeToPhone(char *format, ...);
elmbed 23:26f27c462976 30
elmbed 18:affef3a7db2a 31
elmbed 18:affef3a7db2a 32 void radio_init()
elmbed 18:affef3a7db2a 33 {
elmbed 23:26f27c462976 34 radio.initialize(FREQUENCY, NODE_ID, NETWORKID);
elmbed 23:26f27c462976 35 radio.encrypt(0);
elmbed 23:26f27c462976 36 radio.promiscuous(promiscuousMode);
elmbed 23:26f27c462976 37 }
elmbed 23:26f27c462976 38
elmbed 23:26f27c462976 39 void radio_send(Message *m)
elmbed 23:26f27c462976 40 {
elmbed 23:26f27c462976 41 static byte payload [6] = {0};
elmbed 23:26f27c462976 42
elmbed 23:26f27c462976 43 if (m == NULL)
elmbed 23:26f27c462976 44 {
elmbed 23:26f27c462976 45 return;
elmbed 23:26f27c462976 46 }
elmbed 23:26f27c462976 47
elmbed 26:40a0c775ff27 48 //writeToPhone("Sending message: %c to: %d\r\n", m->command, m->cone);
elmbed 23:26f27c462976 49
elmbed 23:26f27c462976 50 payload[0] = (byte)m->command;
elmbed 23:26f27c462976 51 payload[4] = (byte)m->value & 255;
elmbed 23:26f27c462976 52 payload[3] = (byte)(m->value >> 8);
elmbed 23:26f27c462976 53 payload[2] = (byte)(m->value >> 16);
elmbed 23:26f27c462976 54 payload[1] = (byte)(m->value >> 24);
elmbed 23:26f27c462976 55 payload[5] = (byte)'%';
elmbed 18:affef3a7db2a 56
elmbed 23:26f27c462976 57 #ifdef MASTER
elmbed 23:26f27c462976 58 radio.send(2, payload, sizeof(payload));
elmbed 23:26f27c462976 59 #else
elmbed 23:26f27c462976 60 radio.send(1, payload, sizeof(payload));
elmbed 23:26f27c462976 61 #endif
elmbed 18:affef3a7db2a 62 }
elmbed 18:affef3a7db2a 63
elmbed 23:26f27c462976 64 bool radio_receive_complete()
elmbed 23:26f27c462976 65 {
elmbed 23:26f27c462976 66 return radio.receiveDone();
elmbed 23:26f27c462976 67 }
elmbed 23:26f27c462976 68
elmbed 23:26f27c462976 69 bool radio_receive(Message *m)
elmbed 23:26f27c462976 70 {
elmbed 23:26f27c462976 71 if (m == NULL)
elmbed 23:26f27c462976 72 {
elmbed 23:26f27c462976 73 return false;
elmbed 23:26f27c462976 74 }
elmbed 23:26f27c462976 75
elmbed 23:26f27c462976 76 if (radio.receiveDone())
elmbed 23:26f27c462976 77 {
elmbed 26:40a0c775ff27 78 //writeToPhone("Received: %d bytes", radio.DATALEN);
elmbed 23:26f27c462976 79 if (radio.DATALEN < 6)
elmbed 23:26f27c462976 80 {
elmbed 23:26f27c462976 81 return false;
elmbed 23:26f27c462976 82 }
elmbed 23:26f27c462976 83
elmbed 26:40a0c775ff27 84 //writeToPhone("Got message from: %d\r\n", radio.SENDERID);
elmbed 23:26f27c462976 85
elmbed 23:26f27c462976 86 m->cone = radio.SENDERID;
elmbed 23:26f27c462976 87 m->command = radio.DATA[0];
elmbed 23:26f27c462976 88 m->value = radio.DATA[1];
elmbed 23:26f27c462976 89
elmbed 23:26f27c462976 90 return true;
elmbed 23:26f27c462976 91 }
elmbed 23:26f27c462976 92
elmbed 23:26f27c462976 93 return false;
elmbed 23:26f27c462976 94 }
elmbed 23:26f27c462976 95
elmbed 23:26f27c462976 96 bool radio_ack_received(int cone)
elmbed 23:26f27c462976 97 {
elmbed 23:26f27c462976 98 return radio.ACKReceived(cone);
elmbed 23:26f27c462976 99 }
elmbed 23:26f27c462976 100
elmbed 18:affef3a7db2a 101 void radio_loop()
elmbed 18:affef3a7db2a 102 {
elmbed 18:affef3a7db2a 103 static int counter = 0;
elmbed 18:affef3a7db2a 104
elmbed 18:affef3a7db2a 105 if (radio.receiveDone())
elmbed 18:affef3a7db2a 106 {
elmbed 23:26f27c462976 107 snprintf(phone_buffer, sizeof(phone_buffer), "%d.\r\n[%s]\r\n", radio.SENDERID, radio.DATA);
elmbed 18:affef3a7db2a 108 writeToPhone(phone_buffer);
elmbed 18:affef3a7db2a 109
elmbed 18:affef3a7db2a 110 if (radio.ACKRequested())
elmbed 18:affef3a7db2a 111 {
elmbed 18:affef3a7db2a 112 radio.sendACK();
elmbed 18:affef3a7db2a 113 }
elmbed 18:affef3a7db2a 114 }
elmbed 18:affef3a7db2a 115 }
elmbed 23:26f27c462976 116