forked from letscontrolit/ESPEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P011_PME.ino
232 lines (208 loc) · 7.66 KB
/
_P011_PME.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
//#######################################################################################################
//#################################### Plugin 011: Pro Mini Extender ####################################
//#######################################################################################################
#define PLUGIN_011
#define PLUGIN_ID_011 11
#define PLUGIN_NAME_011 "ProMini Extender"
#define PLUGIN_VALUENAME1_011 "Value"
#define PLUGIN_011_I2C_ADDRESS 0x7f
boolean Plugin_011(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_011;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].Ports = 14;
Device[deviceCount].ValueCount = 1;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_011);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_011));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
byte choice = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
String options[2];
options[0] = F("Digital");
options[1] = F("Analog");
int optionValues[2];
optionValues[0] = 0;
optionValues[1] = 1;
string += F("<TR><TD>Port Type:<TD><select name='plugin_011'>");
for (byte x = 0; x < 2; 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>");
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
String plugin1 = WebServer.arg("plugin_011");
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = plugin1.toInt();
success = true;
break;
}
case PLUGIN_READ:
{
UserVar[event->BaseVarIndex] = Plugin_011_Read(Settings.TaskDevicePluginConfig[event->TaskIndex][0], Settings.TaskDevicePort[event->TaskIndex]);
String log = F("PME : PortValue: ");
log += UserVar[event->BaseVarIndex];
addLog(LOG_LEVEL_INFO, log);
success = true;
break;
}
case PLUGIN_WRITE:
{
String log = "";
String command = parseString(string, 1);
if (command == F("extgpio"))
{
success = true;
Plugin_011_Write(event->Par1, event->Par2);
setPinState(PLUGIN_ID_011, event->Par1, PIN_MODE_OUTPUT, event->Par2);
log = String(F("PME : GPIO ")) + String(event->Par1) + String(F(" Set to ")) + String(event->Par2);
addLog(LOG_LEVEL_INFO, log);
SendStatus(event->Source, getPinStateJSON(SEARCH_PIN_STATE, PLUGIN_ID_011, event->Par1, log, 0));
}
if (command == F("extpwm"))
{
success = true;
uint8_t address = PLUGIN_011_I2C_ADDRESS;
Wire.beginTransmission(address);
Wire.write(3);
Wire.write(event->Par1);
Wire.write(event->Par2 & 0xff);
Wire.write((event->Par2 >> 8));
Wire.endTransmission();
setPinState(PLUGIN_ID_011, event->Par1, PIN_MODE_PWM, event->Par2);
log = String(F("PME : GPIO ")) + String(event->Par1) + String(F(" Set PWM to ")) + String(event->Par2);
addLog(LOG_LEVEL_INFO, log);
SendStatus(event->Source, getPinStateJSON(SEARCH_PIN_STATE, PLUGIN_ID_011, event->Par1, log, 0));
}
if (command == F("extpulse"))
{
success = true;
if (event->Par1 >= 0 && event->Par1 <= 13)
{
Plugin_011_Write(event->Par1, event->Par2);
delay(event->Par3);
Plugin_011_Write(event->Par1, !event->Par2);
setPinState(PLUGIN_ID_011, event->Par1, PIN_MODE_OUTPUT, event->Par2);
log = String(F("PME : GPIO ")) + String(event->Par1) + String(F(" Pulsed for ")) + String(event->Par3) + String(F(" mS"));
addLog(LOG_LEVEL_INFO, log);
SendStatus(event->Source, getPinStateJSON(SEARCH_PIN_STATE, PLUGIN_ID_011, event->Par1, log, 0));
}
}
if (command == F("extlongpulse"))
{
success = true;
if (event->Par1 >= 0 && event->Par1 <= 13)
{
Plugin_011_Write(event->Par1, event->Par2);
setSystemTimer(event->Par3 * 1000, PLUGIN_ID_011, event->Par1, !event->Par2, 0);
setPinState(PLUGIN_ID_011, event->Par1, PIN_MODE_OUTPUT, event->Par2);
log = String(F("PME : GPIO ")) + String(event->Par1) + String(F(" Pulse set for ")) + String(event->Par3) + String(F(" S"));
addLog(LOG_LEVEL_INFO, log);
SendStatus(event->Source, getPinStateJSON(SEARCH_PIN_STATE, PLUGIN_ID_011, event->Par1, log, 0));
}
}
if (command == F("status"))
{
if (parseString(string, 2) == F("ext"))
{
success = true;
String status = "";
if (hasPinState(PLUGIN_ID_011, event->Par2)) // has been set as output
status = getPinStateJSON(SEARCH_PIN_STATE, PLUGIN_ID_011, event->Par2, dummyString, 0);
else
{
byte port = event->Par2; // port 0-13 is digital, ports 20-27 are mapped to A0-A7
byte type = 0; // digital
if (port > 13)
{
type = 1;
port -= 20;
}
int state = Plugin_011_Read(type, port); // report as input (todo: analog reading)
if (state != -1)
status = getPinStateJSON(NO_SEARCH_PIN_STATE, PLUGIN_ID_011, event->Par2, dummyString, state);
}
SendStatus(event->Source, status);
}
}
break;
}
case PLUGIN_TIMER_IN:
{
Plugin_011_Write(event->Par1, event->Par2);
setPinState(PLUGIN_ID_011, event->Par1, PIN_MODE_OUTPUT, event->Par2);
break;
}
}
return success;
}
//********************************************************************************
// PME read
//********************************************************************************
int Plugin_011_Read(byte Par1, byte Par2)
{
int value = -1;
uint8_t address = PLUGIN_011_I2C_ADDRESS;
Wire.beginTransmission(address);
if (Par1 == 0)
Wire.write(2); // Digital Read
else
Wire.write(4); // Analog Read
Wire.write(Par2);
Wire.write(0);
Wire.write(0);
Wire.endTransmission();
delay(1); // remote unit needs some time for conversion...
Wire.requestFrom(address, (uint8_t)0x4);
byte buffer[4];
if (Wire.available() == 4)
{
for (byte x = 0; x < 4; x++)
buffer[x] = Wire.read();
value = buffer[0] + 256 * buffer[1];
}
return value;
}
//********************************************************************************
// PME write
//********************************************************************************
boolean Plugin_011_Write(byte Par1, byte Par2)
{
uint8_t address = 0x7f;
Wire.beginTransmission(address);
Wire.write(1);
Wire.write(Par1);
Wire.write(Par2 & 0xff);
Wire.write((Par2 >> 8));
Wire.endTransmission();
}