NAP
artnetservice.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 <nap/service.h>
9 #include <entity.h>
10 
11 namespace nap
12 {
13  class ArtNetController;
14  class ArtNetReceiver;
15  class ArtNetInputComponentInstance;
16 
39  class NAPAPI ArtNetService : public Service
40  {
41  RTTI_ENABLE(Service)
42 
43  public:
44  using ByteChannelData = std::vector<uint8>;
45  using FloatChannelData = std::vector<float>;
46 
47  // Default Constructor
48  ArtNetService(ServiceConfiguration* configuration);
49 
50  // Default Destructor
51  virtual ~ArtNetService();
52 
53  ArtNetService(const ArtNetService& rhs) = delete;
54  ArtNetService& operator=(const ArtNetService& rhs) = delete;
55 
56  protected:
60  virtual void registerObjectCreators(rtti::Factory& factory) override;
61 
66  virtual void update(double deltaTime) override;
67 
68  private:
76  bool addController(ArtNetController& controller, utility::ErrorState& errorState);
77 
82  void removeController(ArtNetController& controller);
83 
91  bool addReceiver(ArtNetReceiver& receiver, utility::ErrorState& errorState);
92 
97  void removeReceiver(ArtNetReceiver& receiver);
98 
104  void addInputComponent(ArtNetInputComponentInstance& input);
105 
110  void removeInputComponent(ArtNetInputComponentInstance& input);
111 
121  void send(ArtNetController& controller, const FloatChannelData& channelData, int channelOffset = 0);
122 
130  void send(ArtNetController& controller, float channelData, int channel);
131 
140  void send(ArtNetController& controller, const ByteChannelData& channelData, int channelOffset = 0);
141 
148  void send(ArtNetController& controller, uint8 channelData, int channel);
149 
154  void clear(ArtNetController& controller);
155 
156  private:
157  friend class ArtNetController;
158  friend class ArtNetReceiver;
160 
161  struct ControllerData
162  {
163  ArtNetController* mController; // ArtNet controller specifying target subnet and universe
164  ByteChannelData mData; // Byte data for all channels for a single controller
165  double mLastUpdateTime; // Last time this controller was transmitted over the network
166  bool mIsDirty; // Identifies whether this controller contains new data
167  };
168 
169  using ControllerKey = uint8; // The key is the target address (subnet-universe), which is currently limited to 8 bits as it does not include net (7 bits)
170  using ReceiverKey = uint16; // The key is the port which the receiver will listen to
171 
172  using ControllerMap = std::unordered_map<ControllerKey, std::unique_ptr<ControllerData>>;
173  using ReceiverMap = std::unordered_map<ReceiverKey, ArtNetReceiver*>;
174  using InputComponents = std::vector<ArtNetInputComponentInstance*>;
175  using DirtyNodeList = std::unordered_set<ControllerKey>;
176 
177  ControllerMap mControllers; // Controller map that maps an absolute controller address to a controller
178  ReceiverMap mReceivers; // Receiver map that maps a receiver port to a receiver
179  InputComponents mInputs; // All the Art-Net input components registered to the service
180  };
181 }
nap::ArtNetService::ByteChannelData
std::vector< uint8 > ByteChannelData
Definition: artnetservice.h:44
nap::uint8
uint8_t uint8
Definition: numeric.h:16
nap::utility::ErrorState
Definition: errorstate.h:19
nap::ArtNetController
Definition: artnetcontroller.h:50
nap::ServiceConfiguration
Definition: service.h:28
nap::ArtNetInputComponentInstance
Definition: artnetinputcomponent.h:47
nap::ArtNetService
Definition: artnetservice.h:39
nap::Service
Definition: templateservice.h:8
nap::rtti::Factory
Definition: factory.h:78
nap
Definition: templateapp.h:17
nap::uint16
uint16_t uint16
Definition: numeric.h:18
nap::ArtNetReceiver
Definition: artnetreceiver.h:29
nap::ArtNetService::FloatChannelData
std::vector< float > FloatChannelData
Definition: artnetservice.h:45