Advertisement
petriojk

PongBack

Jan 5th, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.55 KB | None | 0 0
  1. /*
  2.  Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
  3.  
  4.  This program is free software; you can redistribute it and/or
  5.  modify it under the terms of the GNU General Public License
  6.  version 2 as published by the Free Software Foundation.
  7.  */
  8.  
  9. /**
  10.  * Example RF Radio Ping Pair
  11.  *
  12.  * This is an example of how to use the RF24 class.  Write this sketch to two different nodes,
  13.  * connect the role_pin to ground on one.  The ping node sends the current time to the pong node,
  14.  * which responds by sending the value back.  The ping node can then see how long the whole cycle
  15.  * took.
  16.  */
  17.  
  18. #include <SPI.h>
  19. #include "nRF24L01.h"
  20. #include "RF24.h"
  21. //#include "printf.h"
  22.  
  23. //
  24. // Hardware configuration
  25. //
  26.  
  27. // Set up nRF24L01 radio on SPI bus plus pins 9 & 10
  28.  
  29. RF24 radio(9,10);
  30.  
  31. // sets the role of this unit in hardware.  Connect to GND to be the 'pong' receiver
  32. // Leave open to be the 'ping' transmitter
  33. const int role_pin = 7;
  34.  
  35. //
  36. // Topology
  37. //
  38.  
  39. // Radio pipe addresses for the 2 nodes to communicate.
  40. const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
  41.  
  42. //
  43. // Role management
  44. //
  45. // Set up role.  This sketch uses the same software for all the nodes
  46. // in this system.  Doing so greatly simplifies testing.  The hardware itself specifies
  47. // which node it is.
  48. //
  49. // This is done through the role_pin
  50. //
  51.  
  52. // The various roles supported by this sketch
  53. typedef enum { role_ping_out = 1, role_pong_back } role_e;
  54.  
  55. // The debug-friendly names of those roles
  56. const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
  57.  
  58. // The role of the current running sketch
  59. role_e role;
  60.  
  61. void setup(void)
  62. {
  63.   //
  64.   // Role
  65.   //
  66.  
  67.     role = role_pong_back;
  68.  
  69.   //
  70.   // Print preamble
  71.   //
  72.  
  73.   Serial.begin(57600);
  74.   //printf_begin();
  75.   //printf("\n\rRF24/examples/pingpair/\n\r");
  76.   //printf("ROLE: %s\n\r",role_friendly_name[role]);
  77.  
  78.   //
  79.   // Setup and configure rf radio
  80.   //
  81.  
  82.   radio.begin();
  83.  
  84.   // optionally, increase the delay between retries & # of retries
  85.   radio.setRetries(50,30);
  86.  
  87.   // optionally, reduce the payload size.  seems to
  88.   // improve reliability
  89.   radio.setPayloadSize(8);
  90.  
  91.   //
  92.   // Open pipes to other nodes for communication
  93.   //
  94.  
  95.   // This simple sketch opens two pipes for these two nodes to communicate
  96.   // back and forth.
  97.   // Open 'our' pipe for writing
  98.   // Open the 'other' pipe for reading, in position #1 (we can have up to 5 pipes open for reading)
  99.  
  100.     radio.openWritingPipe(pipes[1]);
  101.     radio.openReadingPipe(1,pipes[0]);
  102.  
  103.  
  104.   //
  105.   // Start listening
  106.   //
  107.  
  108.   radio.startListening();
  109.  
  110.   //
  111.   // Dump the configuration of the rf unit for debugging
  112.   //
  113.  
  114.   //radio.printDetails();
  115. }
  116.  
  117. void loop(void)
  118. {
  119.  
  120.  
  121.     // if there is data ready
  122.     if ( radio.available() )
  123.     {
  124.       // Dump the payloads until we've gotten everything
  125.       unsigned long got_time;
  126.       bool done = false;
  127.       while (!done)
  128.       {
  129.         // Fetch the payload, and see if this was the last one.
  130.         done = radio.read( &got_time, sizeof(unsigned long) );
  131.  
  132.         // Spew it
  133.         //printf("Got payload %lu...",got_time);
  134.  
  135.     // Delay just a little bit to let the other unit
  136.     // make the transition to receiver
  137.     delay(5);
  138.       }
  139.  
  140.       // First, stop listening so we can talk
  141.       radio.stopListening();
  142.  
  143.       // Send the final one back.
  144.       radio.write( &got_time, sizeof(unsigned long) );
  145.       //printf("Sent response.\n\r");
  146.  
  147.       // Now, resume listening so we catch the next packets.
  148.       radio.startListening();
  149.     }
  150. }
  151. // vim:cin:ai:sts=2 sw=2 ft=cpp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement