forked from letscontrolit/ESPEasyPluginPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_P160_OutputMulti.ino
270 lines (238 loc) · 10.6 KB
/
_P160_OutputMulti.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* Multiple output plugin for ESPEasy */
/* One dummy "inhibitor" values supported per outputs */
#ifdef PLUGIN_BUILD_TESTING
#define PLUGIN_160
#define PLUGIN_ID_160 160
#define PLUGIN_NAME_160 "Output - Multiple GPIO"
#define PLUGIN_VALUENAME1_160 "O1"
#define PLUGIN_VALUENAME2_160 ""
#define PLUGIN_VALUENAME3_160 ""
#define PLUGIN_VALUENAME4_160 ""
#define P160_Nlines 4
#define P160_MaxInstances 3
#define P160_Nchars 32
char P160_deviceTemplate[P160_MaxInstances][P160_Nlines][P160_Nchars];
boolean Plugin_160(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_160;
Device[deviceCount].Type = DEVICE_TYPE_TRIPLE;
Device[deviceCount].VType = SENSOR_TYPE_SWITCH;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].ValueCount = 4;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = false;
Device[deviceCount].TimerOptional = false;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_160);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_160));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_160));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], PSTR(PLUGIN_VALUENAME3_160));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[3], PSTR(PLUGIN_VALUENAME4_160));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
//addFormPinSelect(F("Relay 1"), F("taskdevicepin1"), Settings.TaskDevicePin1[event->TaskIndex]);
//addFormPinSelect(F("Relay 2"), F("taskdevicepin2"), Settings.TaskDevicePin2[event->TaskIndex]);
//addFormPinSelect(F("Relay 3"), F("taskdevicepin3"), Settings.TaskDevicePin3[event->TaskIndex]);
addFormPinSelect(F("4th GPIO"), F("taskdevicepin4"), Settings.TaskDevicePluginConfig[event->TaskIndex][0]);
addFormCheckBox(F("Active state is LOW"), F("Plugin_160_inverted"), Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
addFormNote(F("You can specify 4 output pin above."));
byte baseaddr = 0;
if (event->TaskIndex > 0) {
for (byte TaskIndex = 0; TaskIndex < event->TaskIndex; TaskIndex++)
{
if (Settings.TaskDeviceNumber[TaskIndex] == PLUGIN_ID_160) {
baseaddr = baseaddr + 1;
}
}
}
Settings.TaskDevicePluginConfig[event->TaskIndex][3] = baseaddr;
//String logs = String(F("MultiOut : Task:")) + String(event->TaskIndex) + F(" instance ") + baseaddr;
//addLog(LOG_LEVEL_INFO, logs);
LoadCustomTaskSettings(event->TaskIndex, (byte*)&P160_deviceTemplate[baseaddr], sizeof(P160_deviceTemplate[baseaddr]));
for (byte varNr = 0; varNr < P160_Nlines; varNr++)
{
addFormTextBox(String(F("Inhibit ")) + (varNr + 1), String(F("Plugin_160_template")) + (varNr + 1), P160_deviceTemplate[baseaddr][varNr], P160_Nchars);
}
addFormNote(F("You can specify 4 inhibitor expression - for example dummy values - one for every GPIO output. 0 or no expression means no restirection, 1 means it can not be activated."));
if (Settings.TaskDevicePluginConfig[event->TaskIndex][3] < P160_MaxInstances) {
success = true;
} else {
success = false;
}
break;
}
case PLUGIN_WEBFORM_SAVE:
{
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = getFormItemInt(F("taskdevicepin4"));
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = isFormItemChecked(F("Plugin_160_inverted"));
//String logs = String(F("MultiOut : Task:")) + String(event->TaskIndex) + F(" instance ") + Settings.TaskDevicePluginConfig[event->TaskIndex][3];
//addLog(LOG_LEVEL_INFO, logs);
String argName;
byte raddr = Settings.TaskDevicePluginConfig[event->TaskIndex][3];
for (byte varNr = 0; varNr < P160_Nlines; varNr++)
{
argName = F("Plugin_160_template");
argName += varNr + 1;
strncpy(P160_deviceTemplate[raddr][varNr], WebServer.arg(argName).c_str(), sizeof(P160_deviceTemplate[raddr][varNr]));
}
SaveCustomTaskSettings(event->TaskIndex, (byte*)&P160_deviceTemplate[raddr], sizeof(P160_deviceTemplate[raddr]));
if (Settings.TaskDevicePluginConfig[event->TaskIndex][3] < P160_MaxInstances) {
success = true;
} else {
success = false;
}
break;
}
case PLUGIN_INIT:
{
byte p160_relaycount = 0;
char echr[41] = {0};
if (Settings.TaskDevicePin1[event->TaskIndex] != -1)
{
p160_relaycount = 1;
pinMode(Settings.TaskDevicePin1[event->TaskIndex], OUTPUT);
digitalWrite(Settings.TaskDevicePin1[event->TaskIndex], Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
UserVar[event->BaseVarIndex] = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
}
if (Settings.TaskDevicePin2[event->TaskIndex] != -1)
{
if (p160_relaycount > 0) {
p160_relaycount = 2;
pinMode(Settings.TaskDevicePin2[event->TaskIndex], OUTPUT);
digitalWrite(Settings.TaskDevicePin2[event->TaskIndex], Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
UserVar[event->BaseVarIndex+1] = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
}
}
if (Settings.TaskDevicePin3[event->TaskIndex] != -1)
{
if (p160_relaycount > 1) {
p160_relaycount = 3;
pinMode(Settings.TaskDevicePin3[event->TaskIndex], OUTPUT);
digitalWrite(Settings.TaskDevicePin3[event->TaskIndex], Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
UserVar[event->BaseVarIndex+2] = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
}
}
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] != -1)
{
if (p160_relaycount > 2) {
p160_relaycount = 4;
pinMode(Settings.TaskDevicePluginConfig[event->TaskIndex][0], OUTPUT);
digitalWrite(Settings.TaskDevicePluginConfig[event->TaskIndex][0], Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
UserVar[event->BaseVarIndex+3] = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
}
}
if (p160_relaycount < 4) {
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[3], echr);
}
if (p160_relaycount < 3) {
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], echr);
}
if (p160_relaycount < 2) {
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], echr);
}
if (p160_relaycount < 1) {
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], echr);
}
Settings.TaskDevicePluginConfig[event->TaskIndex][2] = p160_relaycount;
byte baseaddr = 0;
if (event->TaskIndex > 0) {
for (byte TaskIndex = 0; TaskIndex < event->TaskIndex; TaskIndex++)
{
if (Settings.TaskDeviceNumber[TaskIndex] == PLUGIN_ID_160) {
baseaddr = baseaddr + 1;
}
}
}
Settings.TaskDevicePluginConfig[event->TaskIndex][3] = baseaddr;
String logs = String(F("MultiOut : Task:")) + String(event->TaskIndex) + F(" instance ") + String(Settings.TaskDevicePluginConfig[event->TaskIndex][3]) + F(" gpios ")+ String(Settings.TaskDevicePluginConfig[event->TaskIndex][2]);
addLog(LOG_LEVEL_INFO, logs);
LoadCustomTaskSettings(event->TaskIndex, (byte*)&P160_deviceTemplate[baseaddr], sizeof(P160_deviceTemplate[baseaddr]));
if (Settings.TaskDevicePluginConfig[event->TaskIndex][3] < P160_MaxInstances) {
success = true;
} else {
success = false;
}
break;
}
case PLUGIN_WRITE:
{
String command = parseString(string, 1);
if (command == F("output"))
{
String taskName = parseString(string, 2);
int8_t taskIndex = getTaskIndexByName(taskName);
if (taskIndex != -1)
{
String logs = F("");
success = true;
LoadTaskSettings(taskIndex);
byte BaseVarIndex1 = taskIndex * VARS_PER_TASK;
String valueNum = parseString(string, 3);
byte relnum = (byte)valueNum.toInt();
byte pinnum = -1;
switch (relnum) {
case 0:
pinnum = Settings.TaskDevicePin1[taskIndex];
break;
case 1:
pinnum = Settings.TaskDevicePin2[taskIndex];
break;
case 2:
pinnum = Settings.TaskDevicePin3[taskIndex];
break;
case 3:
pinnum = Settings.TaskDevicePluginConfig[taskIndex][0];
break;
}
byte pinvalue = -1;
String vvalue = parseString(string, 4);
if (vvalue.length() < 1) {
pinvalue = !UserVar[BaseVarIndex1 + relnum];
} else {
pinvalue = (byte)vvalue.toInt();
}
byte inhibaddr = Settings.TaskDevicePluginConfig[taskIndex][3];
String tmpString = String(P160_deviceTemplate[inhibaddr][relnum]);
byte inhibitvalue = parseTemplate(tmpString, 20).toInt();
if (inhibitvalue >= 1) {
logs = F("Inhibit active ");
logs += String(inhibitvalue)+ F(" set to LOW");
addLog(LOG_LEVEL_INFO, logs);
pinvalue = Settings.TaskDevicePluginConfig[taskIndex][1];
}
if (pinvalue != -1) {
if (pinnum != -1) {
pinMode(pinnum, OUTPUT);
digitalWrite(pinnum, pinvalue);
}
UserVar[BaseVarIndex1 + relnum] = pinvalue;
}
logs = String(F("MultiOut : ")) + taskName + F(" GPIO ") + pinnum + F(" value: ") + pinvalue;
addLog(LOG_LEVEL_INFO, logs);
logs = F("\nOk");
SendStatus(event->Source, logs);
}
}
break;
}
} // switch
return success;
} //
#endif