Files
ESPEasy/_P001_Switch.ino
T
mvdbro 11b306f681 MCP23017 changes
Changed MCP23017 plugin to scan inputs realtime instead of using the
system timer mechanism
Changed MCP23017 default pullup resistors on input pins
Moved MCP23017 output and generic GPIO and PWM to device plugins
Added simple pulse option for GPIO outputs (only for small pulses less
than a few seconds).
2015-10-02 11:34:23 +02:00

239 lines
7.9 KiB
Arduino

//#######################################################################################################
//#################################### Plugin 001: Input Switch #########################################
//#######################################################################################################
#define PLUGIN_001
#define PLUGIN_ID_001 1
#define PLUGIN_NAME_001 "Switch input"
#define PLUGIN_VALUENAME1_001 "Switch"
boolean Plugin_001(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
static byte switchstate[TASKS_MAX];
static byte outputstate[TASKS_MAX];
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_001;
Device[deviceCount].Type = DEVICE_TYPE_SINGLE;
Device[deviceCount].VType = SENSOR_TYPE_SWITCH;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = true;
Device[deviceCount].InverseLogicOption = true;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].ValueCount = 1;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_001);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_001));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
byte choice = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
String options[2];
options[0] = F("Switch");
options[1] = F("Dimmer");
int optionValues[2];
optionValues[0] = 1;
optionValues[1] = 2;
string += F("<TR><TD>Switch Type:<TD><select name='plugin_001_type'>");
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>");
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == 2)
{
char tmpString[80];
sprintf_P(tmpString, PSTR("<TR><TD>Dim value:<TD><input type='text' name='plugin_001_dimvalue' value='%u'>"), Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
string += tmpString;
}
choice = Settings.TaskDevicePluginConfig[event->TaskIndex][2];
String buttonOptions[3];
buttonOptions[0] = F("Normal Switch");
buttonOptions[1] = F("Push Button Active Low");
buttonOptions[2] = F("Push Button Active High");
int buttonOptionValues[3];
buttonOptionValues[0] = 0;
buttonOptionValues[1] = 1;
buttonOptionValues[2] = 2;
string += F("<TR><TD>Switch Button Type:<TD><select name='plugin_001_button'>");
for (byte x = 0; x < 3; x++)
{
string += F("<option value='");
string += buttonOptionValues[x];
string += "'";
if (choice == buttonOptionValues[x])
string += F(" selected");
string += ">";
string += buttonOptions[x];
string += F("</option>");
}
string += F("</select>");
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
String plugin1 = WebServer.arg("plugin_001_type");
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = plugin1.toInt();
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == 2)
{
String plugin2 = WebServer.arg("plugin_001_dimvalue");
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = plugin2.toInt();
}
String plugin3 = WebServer.arg("plugin_001_button");
Settings.TaskDevicePluginConfig[event->TaskIndex][2] = plugin3.toInt();
success = true;
break;
}
case PLUGIN_INIT:
{
if (Settings.TaskDevicePin1PullUp[event->TaskIndex])
pinMode(Settings.TaskDevicePin1[event->TaskIndex], INPUT_PULLUP);
else
pinMode(Settings.TaskDevicePin1[event->TaskIndex], INPUT);
switchstate[event->TaskIndex] = digitalRead(Settings.TaskDevicePin1[event->TaskIndex]);
success = true;
break;
}
case PLUGIN_TEN_PER_SECOND:
{
byte state = digitalRead(Settings.TaskDevicePin1[event->TaskIndex]);
if (state != switchstate[event->TaskIndex])
{
Serial.print(F("SW : State "));
Serial.println(state);
switchstate[event->TaskIndex] = state;
byte currentOutputState = outputstate[event->TaskIndex];
if (Settings.TaskDevicePluginConfig[event->TaskIndex][2] == 0) //normal switch
outputstate[event->TaskIndex] = state;
else
{
if (Settings.TaskDevicePluginConfig[event->TaskIndex][2] == 1) // active low push button
{
if (state == 0)
outputstate[event->TaskIndex] = !outputstate[event->TaskIndex];
}
else // active high push button
{
if (state == 1)
outputstate[event->TaskIndex] = !outputstate[event->TaskIndex];
}
}
// send if output needs to be changed
if (currentOutputState != outputstate[event->TaskIndex])
{
byte sendState = outputstate[event->TaskIndex];
if (Settings.TaskDevicePin1Inversed[event->TaskIndex])
sendState = !outputstate[event->TaskIndex];
UserVar[event->BaseVarIndex] = sendState;
event->sensorType = SENSOR_TYPE_SWITCH;
if ((sendState == 1) && (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == 2))
{
event->sensorType = SENSOR_TYPE_DIMMER;
UserVar[event->BaseVarIndex] = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
}
sendData(event);
}
}
success = true;
break;
}
case PLUGIN_WRITE:
{
String tmpString = string;
int argIndex = tmpString.indexOf(',');
if (argIndex)
tmpString = tmpString.substring(0, argIndex);
if (tmpString.equalsIgnoreCase("GPIO"))
{
success = true;
if (event->Par1 >= 0 && event->Par1 <= 16)
{
pinMode(event->Par1, OUTPUT);
digitalWrite(event->Par1, event->Par2);
if (printToWeb)
{
printWebString += F("GPIO ");
printWebString += event->Par1;
printWebString += F(" Set to ");
printWebString += event->Par2;
printWebString += F("<BR>");
}
}
}
if (tmpString.equalsIgnoreCase("PWM"))
{
success = true;
if (event->Par1 >= 0 && event->Par1 <= 1023)
{
pinMode(event->Par1, OUTPUT);
analogWrite(event->Par1, event->Par2);
if (printToWeb)
{
printWebString += F("GPIO ");
printWebString += event->Par1;
printWebString += F(" Set PWM to ");
printWebString += event->Par2;
printWebString += F("<BR>");
}
}
}
if (tmpString.equalsIgnoreCase("Pulse"))
{
success = true;
if (event->Par1 >= 0 && event->Par1 <= 1023)
{
pinMode(event->Par1, OUTPUT);
digitalWrite(event->Par1, event->Par2);
delay(event->Par3);
digitalWrite(event->Par1, !event->Par2);
if (printToWeb)
{
printWebString += F("GPIO ");
printWebString += event->Par1;
printWebString += F(" Pulsed for ");
printWebString += event->Par3;
printWebString += F(" mS<BR>");
}
}
}
break;
}
}
return success;
}