NAP
artnetcontroller.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // External Includes
8 #include <rtti/factory.h>
9 #include <nap/device.h>
10 #include <future>
11 #include <nap/numeric.h>
12 #include <condition_variable>
13 #include <mutex>
14 #include <nap/timer.h>
15 
16 namespace nap
17 {
18  using ArtNetNode = void*;
19 
20  // Forward Declares
21  class ArtNetService;
22 
23  namespace artnet
24  {
25  // This maximum is defined as the maximum refresh rate that can be achieved on the DMX512-A
26  // physical layer with a full 512 channel(data slot) payload. The actual value is 44 packets per second.
27  inline constexpr int refreshRate = 44;
28  }
29 
30 
37  enum class EArtnetMode : int
38  {
39  Broadcast = 0,
40  Unicast = 1
41  };
42 
43 
50  class NAPAPI ArtNetController : public Device
51  {
52  RTTI_ENABLE(Device)
53 
54  public:
55  using ByteChannelData = std::vector<uint8>;
56  using FloatChannelData = std::vector<float>;
57  using Address = uint8;
58 
59  // Default constructor
60  ArtNetController() = default;
61 
62  // Constructor used by factory
64 
70  virtual bool start(nap::utility::ErrorState& errorState) override;
71 
75  virtual void stop() override;
76 
84  void send(const FloatChannelData& channelData, int channelOffset = 0);
85 
92  void send(float channelData, int channel);
93 
100  void send(const ByteChannelData& channelData, int channelOffset = 0);
101 
107  void send(uint8 channelData, int channel);
108 
112  void clear();
113 
117  Address getAddress() const { return createAddress(mSubnet, mUniverse); }
118 
124  static Address createAddress(uint8 subnet, uint8 universe);
125 
132  static void convertAddress(Address address, uint8& subnet, uint8& universe);
133 
134  uint8 mSubnet = 0;
135  uint8 mUniverse = 0;
136  int mUpdateFrequency = artnet::refreshRate;
137  float mWaitTime = 2.0f;
139  int mUnicastLimit = 10;
140  bool mVerbose = false;
141  float mReadTimeout = 2.0f;
142  std::string mIpAddress = "";
143  uint16 mChannelCount = 512;
144 
145  private:
146 
147  friend class ArtNetService;
148 
152  ArtNetNode getNode() const { return mNode; }
153 
159  void exePollTask();
160 
165  void stopPolling();
166 
171  void startPolling();
172 
176  void poll();
177 
181  void update(double deltaTime);
182 
183  ArtNetService* mService = nullptr;
184  ArtNetNode mNode = nullptr;
185  int mFoundNodes = 0;
186  int mSocketDescriptor = -1;
187 
188  // Polling
189  std::future<void> mReadTask;
190  std::condition_variable mConditionVar;
191  std::mutex mPollMutex;
192  nap::SystemTimer mPollTimer;
193  bool mPoll = true;
194  std::atomic<bool> mExit = { false };
195  };
196 
198 }
nap::artnet::refreshRate
constexpr int refreshRate
The max supported artnet refresh rate.
Definition: artnetcontroller.h:27
nap::uint8
uint8_t uint8
Definition: numeric.h:16
nap::EArtnetMode
EArtnetMode
Definition: artnetcontroller.h:37
nap::rtti::ObjectCreator
Definition: factory.h:49
nap::utility::ErrorState
Definition: errorstate.h:19
nap::ArtNetController
Definition: artnetcontroller.h:50
nap::Timer< SystemClock >
nap::ArtNetService
Definition: artnetservice.h:39
nap::ArtNetController::ByteChannelData
std::vector< uint8 > ByteChannelData
Definition: artnetcontroller.h:55
nap
Definition: templateapp.h:17
nap::uint16
uint16_t uint16
Definition: numeric.h:18
nap::ArtNetController::Address
uint8 Address
Definition: artnetcontroller.h:57
nap::ArtNetNode
void * ArtNetNode
Definition: artnetcontroller.h:18
nap::EArtnetMode::Broadcast
@ Broadcast
Artnet data is broadcasted over the network.
nap::ArtNetController::FloatChannelData
std::vector< float > FloatChannelData
Definition: artnetcontroller.h:56
nap::ArtNetController::getAddress
Address getAddress() const
Definition: artnetcontroller.h:117
nap::EArtnetMode::Unicast
@ Unicast
Artnet data is sent only to compatible nodes that share the same universe and subnet.
nap::Device
Definition: device.h:20