-
Notifications
You must be signed in to change notification settings - Fork 30
/
_P009_MCP.ino
314 lines (280 loc) · 9.72 KB
/
_P009_MCP.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//#######################################################################################################
//#################################### Plugin 009: MCP23017 input #######################################
//#######################################################################################################
#define PLUGIN_009
#define PLUGIN_ID_009 9
#define PLUGIN_NAME_009 "Switch input - MCP23017"
#define PLUGIN_VALUENAME1_009 "Switch"
boolean Plugin_009(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
static byte switchstate[TASKS_MAX];
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_009;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 16;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].ValueCount = 1;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].TimerOptional = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_009);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_009));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
string += F("<TR><TD>Send Boot state:<TD>");
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0])
string += F("<input type=checkbox name=plugin_009_boot checked>");
else
string += F("<input type=checkbox name=plugin_009_boot>");
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
String plugin1 = WebServer.arg(F("plugin_009_boot"));
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = (plugin1 == "on");
success = true;
break;
}
case PLUGIN_INIT:
{
// Turn on Pullup resistor
Plugin_009_Config(Settings.TaskDevicePort[event->TaskIndex], 1);
// read and store current state to prevent switching at boot time
switchstate[event->TaskIndex] = Plugin_009_Read(Settings.TaskDevicePort[event->TaskIndex]);
// if boot state must be send, inverse default state
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0])
switchstate[event->TaskIndex] = !switchstate[event->TaskIndex];
setPinState(PLUGIN_ID_009, Settings.TaskDevicePort[event->TaskIndex], PIN_MODE_INPUT, 0);
success = true;
break;
}
case PLUGIN_TEN_PER_SECOND:
{
int state = Plugin_009_Read(Settings.TaskDevicePort[event->TaskIndex]);
if (state != -1)
{
if (state != switchstate[event->TaskIndex])
{
String log = F("MCP : State ");
log += state;
addLog(LOG_LEVEL_INFO, log);
switchstate[event->TaskIndex] = state;
UserVar[event->BaseVarIndex] = state;
event->sensorType = SENSOR_TYPE_SWITCH;
sendData(event);
}
}
success = true;
break;
}
case PLUGIN_READ:
{
// We do not actually read the pin state as this is already done 10x/second
// Instead we just send the last known state stored in Uservar
String log = F("MCP : State ");
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("mcpgpio"))
{
success = true;
Plugin_009_Write(event->Par1, event->Par2);
setPinState(PLUGIN_ID_009, event->Par1, PIN_MODE_OUTPUT, event->Par2);
log = String(F("MCP : 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_009, event->Par1, log, 0));
}
if (command == F("mcppulse"))
{
success = true;
if (event->Par1 >= 0 && event->Par1 <= 128)
{
Plugin_009_Write(event->Par1, event->Par2);
delay(event->Par3);
Plugin_009_Write(event->Par1, !event->Par2);
setPinState(PLUGIN_ID_009, event->Par1, PIN_MODE_OUTPUT, event->Par2);
log = String(F("MCP : 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_009, event->Par1, log, 0));
}
}
if (command == F("mcplongpulse"))
{
success = true;
if (event->Par1 >= 0 && event->Par1 <= 128)
{
Plugin_009_Write(event->Par1, event->Par2);
setPinState(PLUGIN_ID_009, event->Par1, PIN_MODE_OUTPUT, event->Par2);
setSystemTimer(event->Par3 * 1000, PLUGIN_ID_009, event->Par1, !event->Par2, 0);
log = String(F("MCP : 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_009, event->Par1, log, 0));
}
}
if (command == F("status"))
{
if (parseString(string, 2) == F("mcp"))
{
success = true;
String status = "";
if (hasPinState(PLUGIN_ID_009, event->Par2)) // has been set as output
status = getPinStateJSON(SEARCH_PIN_STATE, PLUGIN_ID_009, event->Par2, dummyString, 0);
else
{
int state = Plugin_009_Read(event->Par2); // report as input
if (state != -1)
status = getPinStateJSON(NO_SEARCH_PIN_STATE, PLUGIN_ID_009, event->Par2, dummyString, state);
}
SendStatus(event->Source, status);
}
}
break;
}
case PLUGIN_TIMER_IN:
{
Plugin_009_Write(event->Par1, event->Par2);
setPinState(PLUGIN_ID_009, event->Par1, PIN_MODE_OUTPUT, event->Par2);
break;
}
}
return success;
}
//********************************************************************************
// MCP23017 read
//********************************************************************************
int Plugin_009_Read(byte Par1)
{
int8_t state = -1;
byte unit = (Par1 - 1) / 16;
byte port = Par1 - (unit * 16);
uint8_t address = 0x20 + unit;
byte IOBankValueReg = 0x12;
if (port > 8)
{
port = port - 8;
IOBankValueReg++;
}
// get the current pin status
Wire.beginTransmission(address);
Wire.write(IOBankValueReg); // IO data register
Wire.endTransmission();
Wire.requestFrom(address, (uint8_t)0x1);
if (Wire.available())
{
state = ((Wire.read() & _BV(port - 1)) >> (port - 1));
}
return state;
}
//********************************************************************************
// MCP23017 write
//********************************************************************************
boolean Plugin_009_Write(byte Par1, byte Par2)
{
boolean success = false;
byte portvalue = 0;
byte unit = (Par1 - 1) / 16;
byte port = Par1 - (unit * 16);
uint8_t address = 0x20 + unit;
byte IOBankConfigReg = 0;
byte IOBankValueReg = 0x12;
if (port > 8)
{
port = port - 8;
IOBankConfigReg++;
IOBankValueReg++;
}
// turn this port into output, first read current config
Wire.beginTransmission(address);
Wire.write(IOBankConfigReg); // IO config register
Wire.endTransmission();
Wire.requestFrom(address, (uint8_t)0x1);
if (Wire.available())
{
portvalue = Wire.read();
portvalue &= ~(1 << (port - 1)); // change pin from (default) input to output
// write new IO config
Wire.beginTransmission(address);
Wire.write(IOBankConfigReg); // IO config register
Wire.write(portvalue);
Wire.endTransmission();
}
// get the current pin status
Wire.beginTransmission(address);
Wire.write(IOBankValueReg); // IO data register
Wire.endTransmission();
Wire.requestFrom(address, (uint8_t)0x1);
if (Wire.available())
{
portvalue = Wire.read();
if (Par2 == 1)
portvalue |= (1 << (port - 1));
else
portvalue &= ~(1 << (port - 1));
// write back new data
Wire.beginTransmission(address);
Wire.write(IOBankValueReg);
Wire.write(portvalue);
Wire.endTransmission();
success = true;
}
}
//********************************************************************************
// MCP23017 config
//********************************************************************************
boolean Plugin_009_Config(byte Par1, byte Par2)
{
boolean success = false;
byte portvalue = 0;
byte unit = (Par1 - 1) / 16;
byte port = Par1 - (unit * 16);
uint8_t address = 0x20 + unit;
byte IOBankConfigReg = 0xC;
if (port > 8)
{
port = port - 8;
IOBankConfigReg++;
}
// turn this port pullup on
Wire.beginTransmission(address);
Wire.write(IOBankConfigReg);
Wire.endTransmission();
Wire.requestFrom(address, (uint8_t)0x1);
if (Wire.available())
{
portvalue = Wire.read();
if (Par2 == 1)
portvalue |= (1 << (port - 1));
else
portvalue &= ~(1 << (port - 1));
// write new IO config
Wire.beginTransmission(address);
Wire.write(IOBankConfigReg); // IO config register
Wire.write(portvalue);
Wire.endTransmission();
}
}