Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Revision:
23:26f27c462976
Parent:
18:affef3a7db2a
Child:
26:40a0c775ff27
diff -r afcbb425b3cf -r 26f27c462976 Radio.cpp
--- a/Radio.cpp	Tue Dec 01 16:03:15 2015 +0000
+++ b/Radio.cpp	Mon Dec 14 14:24:10 2015 +0000
@@ -1,7 +1,13 @@
 #include <RFM69.h>
 #include <SPI.h>
+#include "types.h"
+#include "TA.h"
 
-#define GATEWAY_ID    1     // this is ME, TGateway
+#ifdef MASTER
+#define NODE_ID 1
+#else
+#define NODE_ID 2     
+#endif
 #define NETWORKID     101   //the same on all nodes that talk to each other
 
 #define FREQUENCY     RF69_915MHZ
@@ -17,35 +23,93 @@
                         // 1-byte NODEID + 4-bytes for time + 1-byte for temp in C + 2-bytes for vcc(mV)
 
 //RFM69::RFM69(PinName  PinName mosi, PinName miso, PinName sclk,slaveSelectPin, PinName int)
-//RFM69 radio(P0_24,P0_23,P0_25,P0_28,P0_7);
+RFM69 radio(P0_24,P0_23,P0_25,P0_28,P0_7);
 
-static bool promiscuousMode = false; // set 'true' to sniff all packets on the same network
+static bool promiscuousMode = true; // set 'true' to sniff all packets on the same network
 static bool requestACK = false;
 
-static char phone_buffer[150] = {0};
-static char radio_buffer[150] = {0};
+static char phone_buffer[50] = {0};
+static char radio_buffer[50] = {0};
 
-extern void writeToPhone(char *data);
+extern "C" void writeToPhone(char *format, ...);
+
 
 void radio_init()
 {
-  //tmr.start();
-  
-  //delay_ms(1000);
+  radio.initialize(FREQUENCY, NODE_ID, NETWORKID);
+  radio.encrypt(0);
+  radio.promiscuous(promiscuousMode);
+}
+
+void radio_send(Message *m)
+{
+    static byte payload [6] = {0};
+ 
+    if (m == NULL)
+    {
+        return;    
+    }
+    
+    writeToPhone("Sending message: %c to: %d\r\n", m->command, m->cone);
+    
+    payload[0] = (byte)m->command;
+    payload[4] = (byte)m->value & 255;
+    payload[3] = (byte)(m->value >> 8);
+    payload[2] = (byte)(m->value >> 16);
+    payload[1] = (byte)(m->value >> 24);
+    payload[5] = (byte)'%';
   
-  //radio.initialize(FREQUENCY, GATEWAY_ID, NETWORKID);
-  //radio.encrypt(0);
-  //radio.promiscuous(promiscuousMode);
+    #ifdef MASTER
+    radio.send(2, payload, sizeof(payload));  
+    #else
+    radio.send(1, payload, sizeof(payload));  
+    #endif      
 }
 
-#if 0
+bool radio_receive_complete()
+{
+    return radio.receiveDone();    
+}
+
+bool radio_receive(Message *m)
+{
+    if (m == NULL)
+    {
+        return false;    
+    }
+    
+    if (radio.receiveDone())
+    {
+        writeToPhone("Received: %d bytes", radio.DATALEN);
+        if (radio.DATALEN < 6)
+        {
+            return false;    
+        }
+        
+        writeToPhone("Got message from: %d\r\n", radio.SENDERID);
+        
+        m->cone    = radio.SENDERID;
+        m->command = radio.DATA[0];
+        m->value   = radio.DATA[1];
+        
+        return true;
+    }
+    
+    return false;
+}
+
+bool radio_ack_received(int cone)
+{
+    return radio.ACKReceived(cone);
+}
+
 void radio_loop()
 {
   static int counter = 0;
   
   if (radio.receiveDone()) 
   {
-     snprintf(phone_buffer, sizeof(phone_buffer), "Got data from: %d.\r\n[%s]\r\n", radio.SENDERID, radio.DATA);
+     snprintf(phone_buffer, sizeof(phone_buffer), "%d.\r\n[%s]\r\n", radio.SENDERID, radio.DATA);
      writeToPhone(phone_buffer);
 
      if (radio.ACKRequested())
@@ -53,13 +117,5 @@
         radio.sendACK();
      } 
   }    
-  
-  if (++counter > 30)
-  {
-      snprintf(radio_buffer, sizeof(radio_buffer), "timer val: %d", 8);
-      radio.sendWithRetry((uint8_t)GATEWAY_ID, radio_buffer,strlen(radio_buffer),true);
-     
-      counter = 0;
-  }    
 }
-#endif
+