Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
elmbed
Date:
Mon Dec 14 14:24:10 2015 +0000
Revision:
23:26f27c462976
Parent:
18:affef3a7db2a
Child:
26:40a0c775ff27
Radio sort of works but still has many bugs.

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