forked from letscontrolit/ESPEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_P019_PCF8574.ino
143 lines (129 loc) · 4.08 KB
/
_P019_PCF8574.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
//#######################################################################################################
//#################################### Plugin 019: PCF8574 ##############################################
//#######################################################################################################
#define PLUGIN_019
#define PLUGIN_ID_019 19
#define PLUGIN_NAME_019 "Switch input - PCF8574"
#define PLUGIN_VALUENAME1_019 "Switch"
boolean Plugin_019(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_019;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 8;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].ValueCount = 1;
Device[deviceCount].SendDataOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_019);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_019));
break;
}
case PLUGIN_INIT:
{
// read and store current state to prevent switching at boot time
switchstate[event->TaskIndex] = Plugin_019_Read(Settings.TaskDevicePort[event->TaskIndex]);
success = true;
break;
}
case PLUGIN_TEN_PER_SECOND:
{
int state = Plugin_019_Read(Settings.TaskDevicePort[event->TaskIndex]);
if (state != -1)
{
if (state != switchstate[event->TaskIndex])
{
String log = F("PCF : 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_WRITE:
{
String tmpString = string;
int argIndex = tmpString.indexOf(',');
if (argIndex)
tmpString = tmpString.substring(0, argIndex);
if (tmpString.equalsIgnoreCase("PCFGPIO"))
{
success = true;
Plugin_019_Write(event->Par1, event->Par2);
if (printToWeb)
{
printWebString += F("PCFGPIO ");
printWebString += event->Par1;
printWebString += F(" Set to ");
printWebString += event->Par2;
printWebString += F("<BR>");
}
}
break;
}
}
return success;
}
//********************************************************************************
// PCF8574 read
//********************************************************************************
int Plugin_019_Read(byte Par1)
{
int8_t state = -1;
byte unit = (Par1 - 1) / 8;
byte port = Par1 - (unit * 8);
uint8_t address = 0x20 + unit;
// get the current pin status
Wire.requestFrom(address, (uint8_t)0x1);
if (Wire.available())
{
state = ((Wire.read() & _BV(port - 1)) >> (port - 1));
}
return state;
}
//********************************************************************************
// PCF8574 write
//********************************************************************************
boolean Plugin_019_Write(byte Par1, byte Par2)
{
boolean success = false;
byte portvalue = 0;
byte unit = (Par1 - 1) / 8;
byte port = Par1 - (unit * 8);
uint8_t address = 0x20 + unit;
// get the current pin status
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(portvalue);
Wire.endTransmission();
success = true;
}
}