OSCClient.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #pragma once
  2. #ifndef ARDUINOOSC_OSCCLIENT_H
  3. #define ARDUINOOSC_OSCCLIENT_H
  4. #ifdef ESP_PLATFORM
  5. #include <WiFi.h>
  6. #include <WiFiUdp.h>
  7. #elif defined (ESP8266)
  8. #include <ESP8266WiFi.h>
  9. #include <WiFiUdp.h>
  10. #elif defined (TEENSYDUINO) || defined (__AVR__)
  11. #include <Ethernet.h>
  12. #include <EthernetUdp.h>
  13. #endif
  14. #include "lib/oscpkt.hh"
  15. #include "lib/Packetizer.h"
  16. namespace ArduinoOSC
  17. {
  18. using OscMessage = oscpkt::Message;
  19. using TimeTag = oscpkt::TimeTag;
  20. class OscWriter
  21. {
  22. public:
  23. OscWriter() { pw.init(); }
  24. OscWriter& init() { pw.init(); return *this; }
  25. OscWriter& encode(const OscMessage &msg) { pw.encode(msg); return *this; }
  26. uint32_t size() { return pw.size(); }
  27. const uint8_t* data() { return (const uint8_t*)pw.data(); }
  28. OscWriter& begin_bundle(TimeTag ts = TimeTag::immediate())
  29. {
  30. pw.begin_bundle(ts);
  31. return *this;
  32. }
  33. OscWriter& end_bundle()
  34. {
  35. pw.end_bundle();
  36. return *this;
  37. }
  38. private:
  39. oscpkt::PacketWriter pw;
  40. };
  41. template <typename S>
  42. class OscClient
  43. {
  44. public:
  45. virtual ~OscClient() {}
  46. void attach(S& s) { stream = &s; }
  47. protected:
  48. OscWriter writer;
  49. S* stream;
  50. };
  51. template <typename S>
  52. class OscClientUdp : public OscClient<S>
  53. {
  54. public:
  55. ~OscClientUdp() {}
  56. #ifndef __AVR__
  57. template <typename... Rest>
  58. void send(const String& ip, uint16_t port, const String& addr, Rest&&... rest)
  59. {
  60. OscMessage msg(ip, port, addr);
  61. send(msg, std::forward<Rest>(rest)...);
  62. }
  63. template <typename First, typename... Rest>
  64. void send(OscMessage& msg, First&& first, Rest&&... rest)
  65. {
  66. msg.push(first);
  67. send(msg, std::forward<Rest>(rest)...);
  68. }
  69. #endif
  70. void send(OscMessage& msg)
  71. {
  72. this->writer.init().encode(msg);
  73. this->stream->beginPacket(msg.ip().c_str(), msg.port());
  74. this->stream->write(this->writer.data(), this->writer.size());
  75. this->stream->endPacket();
  76. }
  77. };
  78. class OscClientSerial : public OscClient<Stream>
  79. {
  80. public:
  81. ~OscClientSerial() {}
  82. #ifndef __AVR__
  83. template <typename... Rest>
  84. void send(const String& addr, Rest&&... rest)
  85. {
  86. OscMessage msg(addr);
  87. send(msg, std::forward<Rest>(rest)...);
  88. }
  89. template <typename First, typename... Rest>
  90. void send(OscMessage& msg, First&& first, Rest&&... rest)
  91. {
  92. msg.push(first);
  93. send(msg, std::forward<Rest>(rest)...);
  94. }
  95. #endif
  96. void send(OscMessage& msg)
  97. {
  98. this->writer.init().encode(msg);
  99. this->packer.pack(this->writer.data(), this->writer.size());
  100. this->stream->write(this->packer.data(), this->packer.size());
  101. }
  102. private:
  103. #ifdef __AVR__
  104. Packetizer::Packer_<64> packer;
  105. #else
  106. Packetizer::Packer packer;
  107. #endif
  108. };
  109. }
  110. #endif // ARDUINOOSC_OSCCLIENT_H