PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RasPiRH.cpp 3.1KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // RasPiRH.cpp
  2. //
  3. // Example program showing how to use RH_NRF24 on Raspberry Pi
  4. // Uses the bcm2835 library to access the GPIO pins to drive the NRF24L01
  5. // Requires bcm2835 library to be already installed
  6. // http://www.airspayce.com/mikem/bcm2835/
  7. // Use the Makefile in this directory:
  8. // cd example/raspi
  9. // make
  10. // sudo ./RasPiRH
  11. //
  12. // Creates a RHReliableDatagram manager and listens and prints for reliable datagrams
  13. // sent to it on the default Channel 2.
  14. //
  15. // Contributed by Mike Poublon
  16. #include <bcm2835.h>
  17. #include <stdio.h>
  18. #include <signal.h>
  19. #include <unistd.h>
  20. #include <RHReliableDatagram.h>
  21. #include <RH_NRF24.h>
  22. //Function Definitions
  23. void sig_handler(int sig);
  24. void printbuffer(uint8_t buff[], int len);
  25. #define CLIENT_ADDRESS 1
  26. #define SERVER_ADDRESS 2
  27. // Create an instance of a driver
  28. // Chip enable is pin 22
  29. // Slave Select is pin 24
  30. RH_NRF24 nrf24(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_24);
  31. RHReliableDatagram manager(nrf24, SERVER_ADDRESS);
  32. //Flag for Ctrl-C
  33. volatile sig_atomic_t flag = 0;
  34. //Main Function
  35. int main (int argc, const char* argv[] )
  36. {
  37. signal(SIGINT, sig_handler);
  38. if (!bcm2835_init())
  39. {
  40. printf( "\n\nRasPiRH Tester Startup Failed\n\n" );
  41. return 1;
  42. }
  43. printf( "\nRasPiRH Tester Startup\n\n" );
  44. /* Begin Driver Only Init Code
  45. if (!nrf24.init())
  46. Serial.println("init failed");
  47. // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  48. if (!nrf24.setChannel(1))
  49. Serial.println("setChannel failed");
  50. if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
  51. Serial.println("setRF failed");
  52. End Driver Only Init Code */
  53. /* Begin Reliable Datagram Init Code */
  54. if (!manager.init())
  55. {
  56. printf( "Init failed\n" );
  57. }
  58. /* End Reliable Datagram Init Code */
  59. uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
  60. //Begin the main body of code
  61. while (true)
  62. {
  63. uint8_t len = sizeof(buf);
  64. uint8_t from, to, id, flags;
  65. /* Begin Driver Only code
  66. if (nrf24.available())
  67. {
  68. // Should be a message for us now
  69. //uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
  70. uint8_t len = sizeof(buf);
  71. if (nrf24.recv(buf, &len))
  72. {
  73. Serial.print("got request: ");
  74. Serial.println((char*)buf);
  75. Serial.println("");
  76. }
  77. else
  78. {
  79. Serial.println("recv failed");
  80. }
  81. }
  82. End Driver Only Code*/
  83. /* Begin Reliable Datagram Code */
  84. if (manager.available())
  85. {
  86. // Wait for a message addressed to us from the client
  87. uint8_t len = sizeof(buf);
  88. uint8_t from;
  89. if (manager.recvfromAck(buf, &len, &from))
  90. {
  91. Serial.print("got request from : 0x");
  92. Serial.print(from, HEX);
  93. Serial.print(": ");
  94. Serial.println((char*)buf);
  95. }
  96. }
  97. /* End Reliable Datagram Code */
  98. if (flag)
  99. {
  100. printf("\n---CTRL-C Caught - Exiting---\n");
  101. break;
  102. }
  103. //sleep(1);
  104. delay(25);
  105. }
  106. printf( "\nRasPiRH Tester Ending\n" );
  107. bcm2835_close();
  108. return 0;
  109. }
  110. void sig_handler(int sig)
  111. {
  112. flag=1;
  113. }
  114. void printbuffer(uint8_t buff[], int len)
  115. {
  116. for (int i = 0; i< len; i++)
  117. {
  118. printf(" %2X", buff[i]);
  119. }
  120. }