Loom 4 v4.5
Arduino library for Internet of Things Rapid Prototyping in environmental sensing
Loading...
Searching...
No Matches
Loom_Max.h
1#pragma once
2
3#include "Module.h"
4#include "../Connectivity/Loom_Wifi/Loom_Wifi.h"
5#include "Actuators.h"
6
7#include <Udp.h>
8#include <vector>
9#include <memory>
10
11// Base ports to send and receive on
12#define SEND_BASE_UDP_PORT 8000
13#define RECV_BASE_UDP_PORT 9000
14
20class Loom_Max : public Module{
21 protected:
22 /* These aren't used with the Max modules */
23 void measure() override {};
24
25 void power_up() override {};
26 void power_down() override {};
27 void package() override;
28
29 public:
30
31
33 struct UDPDeletor {
34 void operator() (UDP* p) {
35 if (p != nullptr) {
36 p->stop();
37 delete p;
38 }
39 }
40 };
41
42 using UDPPtr = std::unique_ptr<UDP, UDPDeletor>;
43
44 /* Initialize from the manager */
45 void initialize() override;
46
50 bool publish();
51
55 bool subscribe();
56
63 Loom_Max(Manager& man, Loom_WIFI& wifi);
64
72 template<typename T>
73 Loom_Max(Manager& man, Loom_WIFI& wifi, T* firstAct) : Module("Max Pub/Sub"), manInst(&man), wifiInst(&wifi){
74
75 actuators.push_back(firstAct);
76 manInst->registerModule(this);
77 };
78
87 template<typename T, typename... Args>
88 Loom_Max(Manager& man, Loom_WIFI& wifi, T* firstAct, Args*... additionalActuators) : Module("Max Pub/Sub"), manInst(&man), wifiInst(&wifi){
89 get_variadic_parameters((Actuator*)firstAct, (Actuator*)additionalActuators...);
90 manInst->registerModule(this);
91 };
92
93 ~Loom_Max();
94
95 private:
96 Manager* manInst; // Instance of the manager
97 Loom_WIFI* wifiInst; // Instance of the WiFi Manager
98
99 UDPPtr udpSend; // Instance of the UDP controller for sending
100 UDPPtr udpRecv; // Instance of the UDP controller for recieving
101
102 uint16_t sendPort; // Port to send the UDP packets to
103 uint16_t recvPort; // Port to receive the packets on
104
105 IPAddress remoteIP; // IP Address to send the packets to
106
107 void setUDPPort(); // Set the UDP port to the correct port number
108 void setIP(); // Set the remote IP to send the packets too
109
110 StaticJsonDocument<1000> messageJson; // Response packet
111
112 std::vector<Actuator*> actuators; // List of actuators we want to control with max
113
114
115 /*
116 * The following two functions are some sorcery to get the variadic parameters without the need for passing in a size variable
117 * I don't fully understand it so don't touch it just works
118 * Based off: https://eli.thegreenplace.net/2014/variadic-templates-in-c/
119 */
120 template<typename T>
121 T* get_variadic_parameters(T* v) {
122 actuators.push_back(v);
123 return v;
124 };
125
126 template<typename T, typename... Args>
127 T* get_variadic_parameters(T* first, Args*... args) {
128 actuators.push_back(first);
129 return get_variadic_parameters(args...);
130 };
131
132
133};
Definition: Actuators.h:19
Definition: Loom_Max.h:20
void initialize() override
Definition: Loom_Max.cpp:33
bool publish()
Definition: Loom_Max.cpp:59
Loom_Max(Manager &man, Loom_WIFI &wifi, T *firstAct)
Definition: Loom_Max.h:73
bool subscribe()
Definition: Loom_Max.cpp:105
Loom_Max(Manager &man, Loom_WIFI &wifi, T *firstAct, Args *... additionalActuators)
Definition: Loom_Max.h:88
Definition: Loom_Wifi.h:32
Definition: Loom_Manager.h:18
void registerModule(Module *module)
Definition: Loom_Manager.cpp:13
Definition: Module.h:30
Close the socket and delete the UDP object when the unique ptr dissapears.
Definition: Loom_Max.h:33