forked from letscontrolit/ESPEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P003_Pulse.ino
266 lines (242 loc) · 8.78 KB
/
_P003_Pulse.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
//#######################################################################################################
//#################################### Plugin 003: Pulse ###############################################
//#######################################################################################################
#define PLUGIN_003
#define PLUGIN_ID_003 3
#define PLUGIN_NAME_003 "Pulse Counter"
#define PLUGIN_VALUENAME1_003 "Count"
#define PLUGIN_VALUENAME2_003 "Total"
#define PLUGIN_VALUENAME3_003 "Time"
void Plugin_003_pulse_interrupt1() ICACHE_RAM_ATTR;
void Plugin_003_pulse_interrupt2() ICACHE_RAM_ATTR;
void Plugin_003_pulse_interrupt3() ICACHE_RAM_ATTR;
void Plugin_003_pulse_interrupt4() ICACHE_RAM_ATTR;
void Plugin_003_pulse_interrupt5() ICACHE_RAM_ATTR;
void Plugin_003_pulse_interrupt6() ICACHE_RAM_ATTR;
void Plugin_003_pulse_interrupt7() ICACHE_RAM_ATTR;
void Plugin_003_pulse_interrupt8() ICACHE_RAM_ATTR;
unsigned long Plugin_003_pulseCounter[TASKS_MAX];
unsigned long Plugin_003_pulseTotalCounter[TASKS_MAX];
unsigned long Plugin_003_pulseTime[TASKS_MAX];
unsigned long Plugin_003_pulseTimePrevious[TASKS_MAX];
boolean Plugin_003(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_003;
Device[deviceCount].Type = DEVICE_TYPE_SINGLE;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 3;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_003);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_003));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_003));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], PSTR(PLUGIN_VALUENAME3_003));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
char tmpString[128];
sprintf_P(tmpString, PSTR("<TR><TD>Debounce Time (mSec):<TD><input type='text' name='plugin_003' value='%u'>"), Settings.TaskDevicePluginConfig[event->TaskIndex][0]);
string += tmpString;
byte choice = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
String options[3];
options[0] = F("Delta");
options[1] = F("Delta/Total/Time");
options[2] = F("Total");
int optionValues[3];
optionValues[0] = 0;
optionValues[1] = 1;
optionValues[2] = 2;
string += F("<TR><TD>Counter Type:<TD><select name='plugin_003_countertype'>");
for (byte x = 0; x < 3; x++)
{
string += F("<option value='");
string += optionValues[x];
string += "'";
if (choice == optionValues[x])
string += F(" selected");
string += ">";
string += options[x];
string += F("</option>");
}
string += F("</select>");
if (choice !=0)
string += F("<span style=\"color:red\">Total count is not persistent!</span>");
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
String plugin1 = WebServer.arg("plugin_003");
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = plugin1.toInt();
String plugin2 = WebServer.arg("plugin_003_countertype");
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = plugin2.toInt();
success = true;
break;
}
case PLUGIN_WEBFORM_SHOW_VALUES:
{
string += F("<div class=\"div_l\">");
string += ExtraTaskSettings.TaskDeviceValueNames[0];
string += F(":</div><div class=\"div_r\">");
string += Plugin_003_pulseCounter[event->TaskIndex];
string += F("</div><div class=\"div_br\"></div><div class=\"div_l\">");
string += ExtraTaskSettings.TaskDeviceValueNames[1];
string += F(":</div><div class=\"div_r\">");
string += Plugin_003_pulseTotalCounter[event->TaskIndex];
string += F("</div><div class=\"div_br\"></div><div class=\"div_l\">");
string += ExtraTaskSettings.TaskDeviceValueNames[2];
string += F(":</div><div class=\"div_r\">");
string += Plugin_003_pulseTime[event->TaskIndex];
string += F("</div>");
success = true;
break;
}
case PLUGIN_INIT:
{
String log = F("INIT : Pulse ");
log += Settings.TaskDevicePin1[event->TaskIndex];
addLog(LOG_LEVEL_INFO,log);
pinMode(Settings.TaskDevicePin1[event->TaskIndex], INPUT_PULLUP);
Plugin_003_pulseinit(Settings.TaskDevicePin1[event->TaskIndex], event->TaskIndex);
success = true;
break;
}
case PLUGIN_READ:
{
UserVar[event->BaseVarIndex] = Plugin_003_pulseCounter[event->TaskIndex];
UserVar[event->BaseVarIndex+1] = Plugin_003_pulseTotalCounter[event->TaskIndex];
UserVar[event->BaseVarIndex+2] = Plugin_003_pulseTime[event->TaskIndex];
switch (Settings.TaskDevicePluginConfig[event->TaskIndex][1])
{
case 0:
{
event->sensorType = SENSOR_TYPE_SINGLE;
UserVar[event->BaseVarIndex] = Plugin_003_pulseCounter[event->TaskIndex];
break;
}
case 1:
{
event->sensorType = SENSOR_TYPE_TRIPLE;
UserVar[event->BaseVarIndex] = Plugin_003_pulseCounter[event->TaskIndex];
UserVar[event->BaseVarIndex+1] = Plugin_003_pulseTotalCounter[event->TaskIndex];
UserVar[event->BaseVarIndex+2] = Plugin_003_pulseTime[event->TaskIndex];
break;
}
case 2:
{
event->sensorType = SENSOR_TYPE_SINGLE;
UserVar[event->BaseVarIndex] = Plugin_003_pulseTotalCounter[event->TaskIndex];
break;
}
}
Plugin_003_pulseCounter[event->TaskIndex] = 0;
success = true;
break;
}
}
return success;
}
/*********************************************************************************************\
* Check Pulse Counters (called from irq handler)
\*********************************************************************************************/
void Plugin_003_pulsecheck(byte Index)
{
unsigned long PulseTime=millis() - Plugin_003_pulseTimePrevious[Index];
if(PulseTime > Settings.TaskDevicePluginConfig[Index][0]) // check with debounce time for this task
{
Plugin_003_pulseCounter[Index]++;
Plugin_003_pulseTotalCounter[Index]++;
Plugin_003_pulseTime[Index] = PulseTime;
Plugin_003_pulseTimePrevious[Index]=millis();
}
}
/*********************************************************************************************\
* Pulse Counter IRQ handlers
\*********************************************************************************************/
void Plugin_003_pulse_interrupt1()
{
Plugin_003_pulsecheck(0);
}
void Plugin_003_pulse_interrupt2()
{
Plugin_003_pulsecheck(1);
}
void Plugin_003_pulse_interrupt3()
{
Plugin_003_pulsecheck(2);
}
void Plugin_003_pulse_interrupt4()
{
Plugin_003_pulsecheck(3);
}
void Plugin_003_pulse_interrupt5()
{
Plugin_003_pulsecheck(4);
}
void Plugin_003_pulse_interrupt6()
{
Plugin_003_pulsecheck(5);
}
void Plugin_003_pulse_interrupt7()
{
Plugin_003_pulsecheck(6);
}
void Plugin_003_pulse_interrupt8()
{
Plugin_003_pulsecheck(7);
}
/*********************************************************************************************\
* Init Pulse Counters
\*********************************************************************************************/
void Plugin_003_pulseinit(byte Par1, byte Index)
{
// Init IO pins
String log = F("PULSE: Init");
addLog(LOG_LEVEL_INFO,log);
switch (Index)
{
case 0:
attachInterrupt(Par1, Plugin_003_pulse_interrupt1, FALLING);
break;
case 1:
attachInterrupt(Par1, Plugin_003_pulse_interrupt2, FALLING);
break;
case 2:
attachInterrupt(Par1, Plugin_003_pulse_interrupt3, FALLING);
break;
case 3:
attachInterrupt(Par1, Plugin_003_pulse_interrupt4, FALLING);
break;
case 4:
attachInterrupt(Par1, Plugin_003_pulse_interrupt5, FALLING);
break;
case 5:
attachInterrupt(Par1, Plugin_003_pulse_interrupt6, FALLING);
break;
case 6:
attachInterrupt(Par1, Plugin_003_pulse_interrupt7, FALLING);
break;
case 7:
attachInterrupt(Par1, Plugin_003_pulse_interrupt8, FALLING);
break;
}
}