zykes

Untitled

May 7th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. // For printf used below
  2. #include <stdio.h>
  3. #include <queue>
  4.  
  5. // PJON library
  6. #include <inttypes.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. // RPI serial interface
  10. #include <wiringPi.h>
  11. #include <wiringSerial.h>
  12.  
  13. #include "gateway.pb.h"
  14. #include "gateway.grpc.pb.h"
  15.  
  16. #include <grpc/grpc.h>
  17. #include <grpc++/channel.h>
  18. #include <grpc++/client_context.h>
  19. #include <grpc++/create_channel.h>
  20. #include <grpc++/security/credentials.h>
  21.  
  22. #ifndef RPI
  23. #define RPI true
  24. #endif
  25.  
  26. #define PJON_INCLUDE_TS true // Include only ThroughSerial
  27. #include "PJON/PJON.h"
  28.  
  29. using std::queue;
  30.  
  31. using grpc::Channel;
  32. using grpc::ClientAsyncResponseReader;
  33. using grpc::ClientContext;
  34. using grpc::CompletionQueue;
  35. using grpc::Status;
  36.  
  37. using gatekeeper::PJONGateway;
  38. using gatekeeper::Empty;
  39. using gatekeeper::Message;
  40. using gatekeeper::SendStatus;
  41.  
  42. class PJONAgent final {
  43. public:
  44. PJONAgent (PJON<ThroughSerial> *bus, std::unique_ptr<PJONGateway::Stub> *stub) {
  45. this->bus = bus;
  46. //this->bus->set_id(1);
  47. this->bus->strategy.set_enable_RS485_pin(1);
  48. this->addr = (":50051");
  49. }
  50.  
  51. void run () {
  52. while (true) {
  53. //printf("Attempting to send a packet... \n");
  54. //this->bus->send(2, "B", 1);
  55. this->bus->send(2, "b", 1);
  56.  
  57. printf("Attempting to roll bus... \n");
  58. this->bus->update();
  59.  
  60. printf("Attempting to receive from bus... \n");
  61. this->bus->receive(1000);
  62. break;
  63. }
  64. }
  65.  
  66. private:
  67. std::string addr;
  68. PJON<ThroughSerial> *bus;
  69. std::unique_ptr<PJONGateway::Stub> stub_;
  70. };
  71.  
  72.  
  73. PJON<ThroughSerial> initBus() {
  74. printf("PJON instantiation... \n");
  75. PJON<ThroughSerial> bus(1);
  76.  
  77. printf("Opening serial... \n");
  78. int s = serialOpen("/dev/ttyAMA0", 9600);
  79.  
  80. if (s < 0) {
  81. printf("Serial open fail!");
  82. }
  83.  
  84. if (wiringPiSetup() == -1) {
  85. printf("WiringPi setup fail");
  86. }
  87.  
  88. printf("Setting serial... \n");
  89. bus.strategy.set_serial(s);
  90.  
  91. printf("Opening bus... \n");
  92. bus.begin();
  93.  
  94. return bus;
  95. }
  96.  
  97.  
  98. int main()
  99. {
  100. PJON<ThroughSerial> bus = initBus();
  101.  
  102. std::shared_ptr<Channel> channel = grpc::CreateChannel("localost:50051", grpc::InsecureChannelCredentials());
  103. std::unique_ptr<PJONGateway::Stub> stub = PJONGateway::NewStub(channel);
  104. PJONAgent svc(&bus, &stub);
  105. svc.run();
  106.  
  107. /*printf("Attempting to send a packet... \n");
  108. bus.send(2, "B", 1);
  109.  
  110. printf("Attempting to roll bus... \n");
  111. bus.update();
  112.  
  113. printf("Attempting to receive from bus... \n");
  114. bus.receive();*/
  115.  
  116. printf("Success! \n");
  117. return 0;
  118. };
Advertisement
Add Comment
Please, Sign In to add comment