Untitled


SUBMITTED BY: Guest

DATE: Aug. 8, 2014, 9:55 a.m.

FORMAT: C++

SIZE: 8.3 kB

HITS: 23954

  1. /*
  2. Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. version 2 as published by the Free Software Foundation.
  6. 03/17/2013 : Charles-Henri Hallard (http://hallard.me)
  7. Modified to use with Arduipi board http://hallard.me/arduipi
  8. Changed to use modified bcm2835 and RF24 library
  9. TMRh20 2014 - Updated to work with optimized RF24 Arduino library
  10. */
  11. /**
  12. * Example RF Radio Ping Pair
  13. *
  14. * This is an example of how to use the RF24 class on RPi, communicating to an Arduino running
  15. * the GettingStarted sketch.
  16. */
  17. #include <cstdlib>
  18. #include <iostream>
  19. #include <sstream>
  20. #include <string>
  21. #include <RF24/RF24.h>
  22. using namespace std;
  23. //
  24. // Hardware configuration
  25. //
  26. // CE Pin, CSN Pin, SPI Speed
  27. // Setup for GPIO 22 CE and CE1 CSN with SPI Speed @ 1Mhz
  28. //RF24 radio(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_26, BCM2835_SPI_SPEED_1MHZ);
  29. // Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
  30. //RF24 radio(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ);
  31. // Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 8Mhz
  32. RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);
  33. // Radio pipe addresses for the 2 nodes to communicate.
  34. //const uint8_t pipes[][6] = {"1Node","2Node"};
  35. const uint64_t pipes[2] = { 0x544d52687CLL , 0xABCDABCD71LL };
  36. int main(int argc, char** argv){
  37. bool role_ping_out = true, role_pong_back = false;
  38. bool role = role_pong_back;
  39. // Print preamble:
  40. printf("RF24/examples/pingtest/\n");
  41. // Setup and configure rf radio
  42. radio.begin();
  43. // optionally, increase the delay between retries & # of retries
  44. radio.setRetries(15,15);
  45. // Dump the configuration of the rf unit for debugging
  46. radio.printDetails();
  47. /********* Role chooser ***********/
  48. printf("\n ************ Role Setup ***********\n");
  49. string input = "";
  50. char myChar = {0};
  51. cout << "Choose a role: Enter 0 for pong_back, 1 for ping_out (CTRL+C to exit) \n>";
  52. getline(cin,input);
  53. if(input.length() == 1) {
  54. myChar = input[0];
  55. if(myChar == '0'){
  56. cout << "Role: Pong Back, awaiting transmission " << endl << endl;
  57. }else{ cout << "Role: Ping Out, starting transmission " << endl << endl;
  58. role = role_ping_out;
  59. }
  60. }
  61. /***********************************/
  62. // This simple sketch opens two pipes for these two nodes to communicate
  63. // back and forth.
  64. if ( role == role_ping_out ) {
  65. radio.openWritingPipe(pipes[0]); ///0x544d52687CLL
  66. radio.openReadingPipe(1,pipes[1]); ///0xABCDABCD71LL
  67. } else {
  68. radio.openWritingPipe(pipes[1]);
  69. radio.openReadingPipe(1,pipes[0]);
  70. radio.startListening();
  71. }
  72. // forever loop
  73. while (1)
  74. {
  75. if (role == role_ping_out)
  76. {
  77. // First, stop listening so we can talk.
  78. radio.stopListening();
  79. // Take the time, and send it. This will block until complete
  80. printf("Now sending...\n");
  81. unsigned long message= "10";
  82. bool ok = radio.write( &message, sizeof(unsigned long) );
  83. if (!ok){
  84. printf("failed.\n");
  85. }
  86. radio.startListening();
  87. //Timeout detection
  88. unsigned long started_waiting_at = millis();
  89. bool timeout = false;
  90. while ( ! radio.available() && ! timeout ) {
  91. if (millis() - started_waiting_at > 200 )
  92. timeout = true;
  93. }
  94. // Describe the results
  95. if ( timeout )
  96. {
  97. printf("Failed, response timed out.\n");
  98. }
  99. else
  100. {
  101. unsigned long got_msg;
  102. radio.read( &got_msg, sizeof(unsigned long) );
  103. // Spew it
  104. printf("Got response %lu, message: %lu\n",got_msg);
  105. }
  106. /*
  107. unsigned long time = millis();
  108. bool ok = radio.write( &time, sizeof(unsigned long) );
  109. if (!ok){
  110. printf("failed.\n");
  111. }
  112. // Now, continue listening
  113. radio.startListening();
  114. // Wait here until we get a response, or timeout (250ms)
  115. unsigned long started_waiting_at = millis();
  116. bool timeout = false;
  117. while ( ! radio.available() && ! timeout ) {
  118. if (millis() - started_waiting_at > 200 )
  119. timeout = true;
  120. }
  121. // Describe the results
  122. if ( timeout )
  123. {
  124. printf("Failed, response timed out.\n");
  125. }
  126. else
  127. {
  128. // Grab the response, compare, and send to debugging spew
  129. unsigned long got_time;
  130. radio.read( &got_time, sizeof(unsigned long) );
  131. // Spew it
  132. printf("Got response %lu, round-trip delay: %lu\n",got_time,millis()-got_time);
  133. }
  134. */
  135. // Try again 1s later
  136. // delay(1000);
  137. sleep(1);
  138. }
  139. //
  140. // Pong back role. Receive each packet, dump it out, and send it back
  141. //
  142. if ( role == role_pong_back )
  143. {
  144. // if there is data ready
  145. //printf("Check available...\n");
  146. if ( radio.available() )
  147. {
  148. // Dump the payloads until we've gotten everything
  149. unsigned long got_time;
  150. // Fetch the payload, and see if this was the last one.
  151. radio.read( &got_time, sizeof(unsigned long) );
  152. radio.stopListening();
  153. radio.write( &got_time, sizeof(unsigned long) );
  154. // Now, resume listening so we catch the next packets.
  155. radio.startListening();
  156. // Spew it
  157. printf("Got payload(%d) %lu...\n",sizeof(unsigned long), got_time);
  158. delay(925); //Delay after payload responded to, minimize RPi CPU time
  159. }
  160. }
  161. } // forever loop
  162. return 0;
  163. }

comments powered by Disqus