This commit is contained in:
Plebs
2020-12-16 19:46:39 +01:00
parent 5fb692578a
commit 86cfe19dd6
6 changed files with 310 additions and 168 deletions
-57
View File
@@ -245,63 +245,6 @@ boolean Plugin_009(byte function, struct EventStruct *event, String& string)
break;
}
/*
case PLUGIN_UNCONDITIONAL_POLL:
{
// port monitoring, generates an event by rule command 'monitor,pcf,port#'
for (std::map<uint32_t,portStatusStruct>::iterator it=globalMapPortStatus.begin(); it!=globalMapPortStatus.end(); ++it) {
if ((it->second.monitor || it->second.command || it->second.init) && getPluginFromKey(it->first)==PLUGIN_ID_009) {
const uint16_t port = getPortFromKey(it->first);
int8_t state = Plugin_009_Read(port);
if (it->second.state != state || it->second.forceMonitor) {
if (it->second.mode == PIN_MODE_OFFLINE) it->second.mode=PIN_MODE_UNDEFINED; //changed from offline to online
if (state == -1) it->second.mode=PIN_MODE_OFFLINE; //changed from online to offline
if (!it->second.task) it->second.state = state; //do not update state if task flag=1 otherwise it will not be picked up
by 10xSEC function
if (it->second.monitor) {
it->second.forceMonitor=0; //reset flag
String eventString = F("MCP#");
eventString += port;
eventString += '=';
eventString += state;
rulesProcessing(eventString);
}
}
}
}
break;
}
}
break;
}
*/
/*
case PLUGIN_MONITOR:
{
// port monitoring, generates an event by rule command 'monitor,gpio,port#'
const uint32_t key = createKey(PLUGIN_ID_009,event->Par1);
const portStatusStruct currentStatus = globalMapPortStatus[key];
//if (currentStatus.monitor || currentStatus.command || currentStatus.init) {
const int8_t state = Plugin_009_Read(event->Par1);
if (currentStatus.state != state || currentStatus.forceMonitor) {
if (!currentStatus.task) globalMapPortStatus[key].state = state; //do not update state if task flag=1 otherwise it will not be picked up by 10xSEC function
if (currentStatus.monitor) {
globalMapPortStatus[key].forceMonitor=0; //reset flag
String eventString = F("MCP#");
eventString += event->Par1;
eventString += '=';
eventString += state;
rulesProcessing(eventString);
}
}
//}
if ((currentStatus.state != state) || currentStatus.forceMonitor) {
if (!currentStatus.task) { globalMapPortStatus[key].state = state; // do not update state if task flag=1 otherwise it will not be
// picked up by 10xSEC function
}
*/
case PLUGIN_TEN_PER_SECOND:
{
const int8_t state = GPIO_MCP_Read(CONFIG_PORT);
+192 -19
View File
@@ -23,11 +23,17 @@ bool getPluginIDAndPrefix(char selection, pluginID_t &pluginID, String &logPrefi
void logErrorGpioOffline(const String& prefix, int port);
void logErrorGpioOutOfRange(const String& prefix, int port, const char* Line = nullptr);
void logErrorGpioNotOutput(const String& prefix, int port);
void logErrorModeOutOfRange(const String& prefix, int port);
bool gpio_monitor_helper(int port, EventValueSource::Enum source, const char* Line);
bool gpio_unmonitor_helper(int port, EventValueSource::Enum source, const char* Line);
bool mcpgpio_range_pattern_helper(struct EventStruct *event, const char* Line, bool isWritePattern);
bool pcfgpio_range_pattern_helper(struct EventStruct *event, const char* Line, bool isWritePattern);
bool gpio_mode_range_helper(byte pin, byte pinMode, EventValueSource::Enum source, const char* Line);
byte getPcfAddress(uint8_t pin);
bool setGPIOMode(byte pin, byte mode);
bool setPCFMode(byte pin, byte mode);
bool setMCPMode(byte pin, byte mode);
/*************************************************************************/
@@ -390,8 +396,11 @@ String Command_GPIO(struct EventStruct *event, const char* Line)
state = GPIO_Read_Switch_State(event->Par1, PIN_MODE_INPUT_PULLUP);
break;
case PLUGIN_MCP:
setMCPInputAndPullupMode(event->Par1,true);
GPIO_Read(PLUGIN_MCP, event->Par1, state);
break;
case PLUGIN_PCF:
// PCF8574/MCP specific: only can read 0/low state, so we must send 1
// PCF8574 specific: only can read 0/low state, so we must send 1
state = 1;
break;
}
@@ -412,7 +421,7 @@ String Command_GPIO(struct EventStruct *event, const char* Line)
}
createAndSetPortStatus_Mode_State(key,mode,state);
GPIO_Write(pluginID,event->Par1,state,mode);
if (mode==PIN_MODE_OUTPUT || pluginID==PLUGIN_PCF) GPIO_Write(pluginID,event->Par1,state,mode);
String log = logPrefix + String(F(" : port#")) + String(event->Par1) + String(F(": set to ")) + String(state);
addLog(LOG_LEVEL_INFO, log);
@@ -435,6 +444,11 @@ void logErrorGpio(const String& prefix, int port, const String& description)
}
}
void logErrorModeOutOfRange(const String& prefix, int port)
{
logErrorGpio(prefix, port, F(" mode selection is incorrect. Valid values are: 0, 1 or 2."));
}
void logErrorGpioOffline(const String& prefix, int port)
{
logErrorGpio(prefix, port, F(" is offline."));
@@ -562,8 +576,8 @@ String Command_GPIO_PcfGPIORange(struct EventStruct *event, const char* Line)
return pcfgpio_range_pattern_helper(event, Line, false)?return_command_success():return_command_failed();
}
bool mcpgpio_range_pattern_helper(struct EventStruct *event, const char* Line, bool isWritePattern) {
bool mcpgpio_range_pattern_helper(struct EventStruct *event, const char* Line, bool isWritePattern)
{
String log;
String logPrefix = isWritePattern?String(F("McpGPIOPattern")):String(F("McpGPIORange"));
@@ -589,8 +603,9 @@ bool mcpgpio_range_pattern_helper(struct EventStruct *event, const char* Line, b
bool onLine = false;
if (isMask) {
mask = event->Par4 & (byte(pow(256,numBytes))-1);
mask &= (byte(pow(2,numBits))-1);
mask = event->Par4 & ((1 << numBytes*8)-1);
//mask &= (byte(pow(2,numBits))-1);
mask &= ((1 << numBits)-1);
mask = mask << deltaStart;
} else {
mask = (1 << numBits) - 1;
@@ -598,8 +613,8 @@ bool mcpgpio_range_pattern_helper(struct EventStruct *event, const char* Line, b
}
if (isWritePattern) { //write pattern is present
write = event->Par3 & (byte(pow(256,numBytes))-1); //limit number of bytes
write &= (byte(pow(2,numBits))-1); //limit to number of bits
write = event->Par3 & ((1 << numBytes*8)-1); //limit number of bytes
write &= ((1 << numBits)-1); //limit to number of bits
write = write << deltaStart; //shift to start from starting pin
} else { //write pattern not present
if (event->Par3 == 0) {
@@ -644,11 +659,13 @@ bool mcpgpio_range_pattern_helper(struct EventStruct *event, const char* Line, b
int8_t state;
for (byte j=0; j<8; j++) {
if ((currentMask & byte(pow(2,j))) >> j) { //only for the pins in the mask
//if ((currentMask & (byte(pow(2,j)))) >> j) { //only for the pins in the mask
if ((currentMask & (1 << j)) >> j) { //only for the pins in the mask
byte currentPin = firstPin + j + 8*i;
const uint32_t key = createKey(PLUGIN_MCP,currentPin);
state = onLine ? ((writeGPIOValue & byte(pow(2,j))) >> j) : -1;
//state = onLine ? ((writeGPIOValue & byte(pow(2,j))) >> j) : -1;
state = onLine ? ((writeGPIOValue & (1 << j)) >> j) : -1;
createAndSetPortStatus_Mode_State(key,mode,state);
log = logPrefix + String(F(": port#")) + String(currentPin) + String(F(": set to ")) + String(state);
@@ -667,8 +684,8 @@ byte getPcfAddress(uint8_t pin)
return retValue;
}
bool pcfgpio_range_pattern_helper(struct EventStruct *event, const char* Line, bool isWritePattern) {
bool pcfgpio_range_pattern_helper(struct EventStruct *event, const char* Line, bool isWritePattern)
{
String log;
String logPrefix = isWritePattern?String(F("PcfGPIOPattern")):String(F("PcfGPIORange"));
@@ -690,8 +707,9 @@ bool pcfgpio_range_pattern_helper(struct EventStruct *event, const char* Line, b
byte numBits = event->Par2 - event->Par1 + 1;
if (isMask) {
mask = event->Par4 & (byte(pow(256,numBytes))-1);
mask &= (byte(pow(2,numBits))-1);
// mask = event->Par4 & (byte(pow(256,numBytes))-1);
mask = event->Par4 & ((1 << numBytes*8)-1);
mask &= ((1 << numBits)-1);
mask = mask << deltaStart;
} else {
mask = (1 << numBits) - 1;
@@ -699,8 +717,8 @@ bool pcfgpio_range_pattern_helper(struct EventStruct *event, const char* Line, b
}
if (isWritePattern) { //write pattern is present
write = event->Par3 & (byte(pow(256,numBytes))-1); //limit number of bytes
write &= (byte(pow(2,numBits))-1); //limit to number of bits
write = event->Par3 & ((1 << numBytes*8)-1); //limit number of bytes
write &= ((1 << numBits)-1); //limit to number of bits
write = write << deltaStart; //shift to start from starting pin
} else { //write pattern not present
if (event->Par3 == 0) {
@@ -736,8 +754,8 @@ bool pcfgpio_range_pattern_helper(struct EventStruct *event, const char* Line, b
byte currentPin = firstPin + j + 8*i;
const uint32_t key = createKey(PLUGIN_PCF,currentPin);
if ((currentMask & byte(pow(2,j))) >> j) { //only for the pins in the mask
state = onLine ? ((writeGPIOValue & byte(pow(2,j))) >> j) : -1;
if ((currentMask & (1 << j)) >> j) { //only for the pins in the mask
state = onLine ? ((writeGPIOValue & (1 << j) ) >> j) : -1;
createAndSetPortStatus_Mode_State(key,mode,state);
log = logPrefix + String(F(": port#")) + String(currentPin) + String(F(": set to ")) + String(state);
@@ -746,7 +764,7 @@ bool pcfgpio_range_pattern_helper(struct EventStruct *event, const char* Line, b
} else {
//set to 1 the INPUT pins and the PIN that have not been initialized yet.
if (!existPortStatus(key) || (existPortStatus(key) && (globalMapPortStatus[key].mode == PIN_MODE_INPUT || globalMapPortStatus[key].mode == PIN_MODE_INPUT_PULLUP)))
readValue |= byte(pow(2,j)); //set port j = 1
readValue |= (1 << j); //set port j = 1
}
}
if (onLine) {
@@ -757,3 +775,158 @@ bool pcfgpio_range_pattern_helper(struct EventStruct *event, const char* Line, b
}
return onLine;
}
bool setGPIOMode(byte pin, byte mode)
{
if (checkValidPortRange(PLUGIN_GPIO, pin)) {
switch (mode) {
case PIN_MODE_OUTPUT:
pinMode(pin, OUTPUT);
break;
case PIN_MODE_INPUT_PULLUP:
setInternalGPIOPullupMode(pin);
break;
case PIN_MODE_INPUT:
pinMode(pin, INPUT);
break;
}
return true;
} else
return false;
}
bool setMCPMode(byte pin, byte mode)
{
if (checkValidPortRange(PLUGIN_MCP, pin)) {
switch (mode) {
case PIN_MODE_OUTPUT:
setMCPOutputMode(pin);
break;
case PIN_MODE_INPUT_PULLUP:
setMCPInputAndPullupMode(pin,true);
break;
case PIN_MODE_INPUT:
setMCPInputAndPullupMode(pin,false);
break;
}
return true;
} else
return false;
}
bool setPCFMode(byte pin, byte mode)
{
if (checkValidPortRange(PLUGIN_PCF, pin)) {
switch (mode) {
case PIN_MODE_OUTPUT:
//do nothing
break;
case PIN_MODE_INPUT_PULLUP:
case PIN_MODE_INPUT:
setPCFInputMode(pin);
break;
}
return true;
} else
return false;
}
/***********************************************
*event->Par1: PIN to be set
*event->Par2: MODE to be set:
* 0 = OUTPUT
* 1 = INPUT PULLUP or INPUT PULLDOWN (only for GPIO16)
* 2 = INPUT
**********************************************/
String Command_GPIO_Mode(struct EventStruct *event, const char* Line)
{
if (gpio_mode_range_helper(event->Par1, event->Par2, event->Source, Line))
return return_command_success();
else
return return_command_failed();
}
String Command_GPIO_ModeRange(struct EventStruct *event, const char* Line)
{
bool success=true;
for (byte i=event->Par1;i<=event->Par2;i++) {
success &= gpio_mode_range_helper(i, event->Par3, event->Source, Line);
}
return success?return_command_success():return_command_failed();
}
bool gpio_mode_range_helper(byte pin, byte pinMode, EventValueSource::Enum source, const char* Line)
{
String logPrefix;// = new char;
String logPostfix;// = new char;
pluginID_t pluginID=INVALID_PLUGIN_ID;
//Line[0]='g':gpio; ='p':pcfgpio; ='m':mcpgpio
bool success = getPluginIDAndPrefix(Line[0], pluginID, logPrefix);
if (success && checkValidPortRange(pluginID, pin))
{
int8_t state=0;
byte mode=255;
bool setSuccess=false;
switch (pinMode) {
case 0:
mode = PIN_MODE_OUTPUT;
logPostfix = F("OUTPUT");
break;
case 1:
mode = PIN_MODE_INPUT_PULLUP;
logPostfix = F("INPUT PULLUP");
break;
case 2:
mode = PIN_MODE_INPUT;
logPostfix = F("INPUT");
break;
}
if (mode < 255) {
switch(pluginID) {
case PLUGIN_GPIO:
setSuccess = setGPIOMode(pin, mode);
break;
case PLUGIN_PCF:
//set pin = 1 when INPUT
setSuccess = setPCFMode(pin, mode);
break;
case PLUGIN_MCP:
setSuccess = setMCPMode(pin, mode);
break;
}
const uint32_t key = createKey(pluginID,pin);
if (globalMapPortStatus[key].mode != PIN_MODE_OFFLINE)
{
int8_t currentState;
GPIO_Read(pluginID, pin, currentState);
//state = currentState;
if (currentState==-1) {
mode=PIN_MODE_OFFLINE;
//state = -1;
}
createAndSetPortStatus_Mode_State(key,mode,currentState);
String log = logPrefix + String(F(" : port#")) + String(pin) + String(F(": MODE set to ")) + logPostfix + String(F(". Value = ")) + String(currentState);
addLog(LOG_LEVEL_INFO, log);
SendStatusOnlyIfNeeded(source, SEARCH_PIN_STATE, key, log, 0);
return return_command_success();
} else {
logErrorGpioOffline(logPrefix,pin);
return return_command_failed();
}
} else {
logErrorModeOutOfRange(logPrefix,pin);
return return_command_failed();
}
} else {
logErrorGpioOutOfRange(logPrefix,pin, Line);
return return_command_failed();
}
}
+2 -17
View File
@@ -31,22 +31,7 @@ String Command_GPIO_McpGPIOPattern(struct EventStruct *event, const char* Line);
String Command_GPIO_PcfGPIORange(struct EventStruct *event, const char* Line);
String Command_GPIO_PcfGPIOPattern(struct EventStruct *event, const char* Line);
//String Command_GPIO_mcptest_write(struct EventStruct *event, const char* Line);
//String Command_GPIO_mcptest_read(struct EventStruct *event, const char* Line);
/*
String Command_GPIO_PinMode(struct EventStruct *event, const char* Line);
String Command_GPIO_PinAll(struct EventStruct *event, const char* Line);
String Command_GPIO_McpPinAll(struct EventStruct *event, const char* Line);
String Command_GPIO_McpPinAllA(struct EventStruct *event, const char* Line);
String Command_GPIO_McpPinAllB(struct EventStruct *event, const char* Line);
String Command_GPIO_McpModeAll(struct EventStruct *event, const char* Line);
String Command_GPIO_McpModeAllA(struct EventStruct *event, const char* Line);
String Command_GPIO_McpModeAllB(struct EventStruct *event, const char* Line);
String Command_GPIO_PcfPinAll(struct EventStruct *event, const char* Line);
String Command_GPIO_PcfModeAll(struct EventStruct *event, const char* Line);
*/
String Command_GPIO_Mode(struct EventStruct *event, const char* Line);
String Command_GPIO_ModeRange(struct EventStruct *event, const char* Line);
#endif // COMMAND_GPIO_H
+8 -8
View File
@@ -261,8 +261,8 @@ bool executeInternalCommand(command_case_data & data)
COMMAND_CASE_A( "longpulse", Command_GPIO_LongPulse, 3); // GPIO.h
COMMAND_CASE_A( "longpulse_ms", Command_GPIO_LongPulse_Ms, 3); // GPIO.h
#ifndef BUILD_NO_DIAGNOSTIC_COMMANDS
COMMAND_CASE_A("logportstatus", Command_logPortStatus, 0); // Diagnostic.h
COMMAND_CASE_A( "lowmem", Command_Lowmem, 0); // Diagnostic.h
COMMAND_CASE_A( "logportstatus", Command_logPortStatus, 0); // Diagnostic.h
COMMAND_CASE_A( "lowmem", Command_Lowmem, 0); // Diagnostic.h
#endif // ifndef BUILD_NO_DIAGNOSTIC_COMMANDS
break;
}
@@ -275,12 +275,12 @@ bool executeInternalCommand(command_case_data & data)
} else if (data.cmd_lc[1] == 'c') {
COMMAND_CASE_A( "mcplongpulse", Command_GPIO_LongPulse, 3); // GPIO.h
COMMAND_CASE_A("mcplongpulse_ms", Command_GPIO_LongPulse_Ms, 3); // GPIO.h
//COMMAND_CASE_A( "mcpmode", Command_GPIO_Mode, 2); // Gpio.h ************
//COMMAND_CASE_A( "mcpmoderange", Command_GPIO_McpModeRange, 4); // Gpio.h ************
COMMAND_CASE_A( "mcpmode", Command_GPIO_Mode, 2); // Gpio.h
COMMAND_CASE_A( "mcpmoderange", Command_GPIO_ModeRange, 3); // Gpio.h
COMMAND_CASE_A( "mcppulse", Command_GPIO_Pulse, 3); // GPIO.h
}
COMMAND_CASE_A( "monitor", Command_GPIO_Monitor, 2); // GPIO.h
COMMAND_CASE_A( "monitorrange", Command_GPIO_MonitorRange, 3); // GPIO.h ************
COMMAND_CASE_A( "monitorrange", Command_GPIO_MonitorRange, 3); // GPIO.h
#ifndef BUILD_NO_DIAGNOSTIC_COMMANDS
COMMAND_CASE_A( "malloc", Command_Malloc, 1); // Diagnostic.h
COMMAND_CASE_A( "meminfo", Command_MemInfo, 0); // Diagnostic.h
@@ -305,14 +305,14 @@ bool executeInternalCommand(command_case_data & data)
} else if (data.cmd_lc[1] == 'c') {
COMMAND_CASE_A( "pcflongpulse", Command_GPIO_LongPulse, 3); // GPIO.h
COMMAND_CASE_A("pcflongpulse_ms", Command_GPIO_LongPulse_Ms, 3); // GPIO.h
//COMMAND_CASE_A( "pcfmode", Command_GPIO_Mode, 2); // Gpio.h ************
//COMMAND_CASE_A( "pcfmoderange", Command_GPIO_McpModeRange, 4); // Gpio.h ************
COMMAND_CASE_A( "pcfmode", Command_GPIO_Mode, 2); // Gpio.h
COMMAND_CASE_A( "pcfmoderange", Command_GPIO_ModeRange, 3); // Gpio.h ************
COMMAND_CASE_A( "pcfpulse", Command_GPIO_Pulse, 3); // GPIO.h
}
COMMAND_CASE_R("password", Command_Settings_Password, 1); // Settings.h
COMMAND_CASE_A( "pulse", Command_GPIO_Pulse, 3); // GPIO.h
#ifdef USES_MQTT
COMMAND_CASE_A( "publish", Command_MQTT_Publish, 2); // MQTT.h
COMMAND_CASE_A( "publish", Command_MQTT_Publish, 2); // MQTT.h
#endif // USES_MQTT
COMMAND_CASE_A( "pwm", Command_GPIO_PWM, 4); // GPIO.h
break;
+99 -39
View File
@@ -96,7 +96,7 @@ int8_t GPIO_MCP_Read(int Par1)
byte unit = (Par1 - 1) / 16;
byte port = Par1 - (unit * 16);
uint8_t address = 0x20 + unit;
byte IOBankValueReg = 0x12;
byte IOBankValueReg = MCP23017_GPIOA;
if (port > 8)
{
port = port - 8;
@@ -109,7 +109,7 @@ int8_t GPIO_MCP_Read(int Par1)
Wire.requestFrom(address, (uint8_t)0x1);
if (Wire.available())
{
state = ((Wire.read() & _BV(port - 1)) >> (port - 1));
state = (Wire.read() & (1 << (port - 1))) >> (port - 1);
}
}
return state;
@@ -160,14 +160,19 @@ bool GPIO_MCP_Write(int Par1, byte Par2)
byte unit = (Par1 - 1) / 16;
byte port = Par1 - (unit * 16);
uint8_t address = 0x20 + unit;
byte IOBankConfigReg = 0;
byte IOBankValueReg = 0x12;
if (port > 8)
byte IOBankConfigReg;
byte IOBankValueReg;
if (port <= 8)
{
IOBankConfigReg = MCP23017_IODIRA;
IOBankValueReg = MCP23017_GPIOA;
} else {
port = port - 8;
IOBankConfigReg++;
IOBankValueReg++;
IOBankConfigReg = MCP23017_IODIRB;
IOBankValueReg = MCP23017_GPIOB;
}
// turn this port into output, first read current config
Wire.beginTransmission(address);
Wire.write(IOBankConfigReg); // IO config register
@@ -209,47 +214,74 @@ bool GPIO_MCP_Write(int Par1, byte Par2)
//********************************************************************************
// MCP23017 config
// Par2: 0: Pullup disabled
// Par2: 1: Pullup enabled
//********************************************************************************
void GPIO_MCP_Config(int Par1, byte Par2)
bool setMCPInputAndPullupMode(uint8_t Par1, bool enablePullUp)
{
if (!checkValidPortRange(PLUGIN_MCP, Par1)) {
return;
return false;
}
// bool success = false;
byte portvalue = 0;
bool success = false;
byte retValue;
byte unit = (Par1 - 1) / 16;
byte port = Par1 - (unit * 16);
uint8_t address = 0x20 + unit;
byte IOBankConfigReg = 0xC;
byte IOBankPullUpReg = MCP23017_GPPUA;
byte IOBankIODirReg = MCP23017_IODIRA;
if (port > 8)
{
port = port - 8;
IOBankConfigReg++;
IOBankPullUpReg++;
IOBankIODirReg++;
}
// 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));
// set this port mode to INPUT (bit=1)
if (GPIO_MCP_ReadRegister(address, IOBankIODirReg, &retValue)) {
retValue |= (1 << (port - 1));
GPIO_MCP_WriteRegister(address, IOBankIODirReg, retValue);
// write new IO config
Wire.beginTransmission(address);
Wire.write(IOBankConfigReg); // IO config register
Wire.write(portvalue);
Wire.endTransmission();
// turn this port pullup on or off
if (GPIO_MCP_ReadRegister(address, IOBankPullUpReg, &retValue)) {
enablePullUp ? retValue |= (1 << (port - 1)) : retValue &= ~(1 << (port - 1));
GPIO_MCP_WriteRegister(address, IOBankPullUpReg, retValue);
success=true;
}
}
return success;
}
bool setMCPOutputMode(uint8_t Par1)
{
if (!checkValidPortRange(PLUGIN_MCP, Par1)) {
return false;
}
bool success = false;
byte retValue;
byte unit = (Par1 - 1) / 16;
byte port = Par1 - (unit * 16);
uint8_t address = 0x20 + unit;
byte IOBankIODirReg = MCP23017_IODIRA;
if (port > 8)
{
port = port - 8;
IOBankIODirReg++;
}
// set this port mode to OUTPUT (bit=0)
if (GPIO_MCP_ReadRegister(address, IOBankIODirReg, &retValue)) {
retValue &= ~(1 << (port - 1));
GPIO_MCP_WriteRegister(address, IOBankIODirReg, retValue);
success=true;
}
return success;
}
//********************************************************************************
// PCF8574 read
// PCF8574 read pin
//********************************************************************************
//@giig1967g-20181023: changed to int8_t
int8_t GPIO_PCF_Read(int Par1)
@@ -286,7 +318,7 @@ bool GPIO_PCF_ReadAllPins(uint8_t address, uint8_t *retValue)
//********************************************************************************
// PCF8574 write
// PCF8574 write pin
//*******************************************************************************
void GPIO_PCF_WriteAllPins(uint8_t address, uint8_t value)
{
@@ -320,20 +352,48 @@ bool GPIO_PCF_Write(int Par1, byte Par2)
portmask &= ~(1 << i); //set port i = 0
}
//key = createKey(PLUGIN_PCF,Par1);
if (Par2 == 1)
portmask |= (1 << (port-1));
else
portmask &= ~(1 << (port-1));
Wire.beginTransmission(address);
Wire.write(portmask);
Wire.endTransmission();
GPIO_PCF_WriteAllPins(address,portmask);
return true;
}
bool setPCFInputMode(uint8_t pin)
{
if (!checkValidPortRange(PLUGIN_PCF, pin)) {
return false;
}
uint8_t unit = (pin - 1) / 8;
uint8_t port = pin - (unit * 8);
uint8_t address = 0x20 + unit;
if (unit > 7) address += 0x10;
//generate bitmask
int i = 0;
uint8_t portmask;
if (GPIO_PCF_ReadAllPins(address, &portmask)) {
unit = unit * 8 + 1; // calculate first pin
uint32_t key;
//REMEMBER: all input pins must be set to 1 when writing to the unit
for(i=0; i<8; i++){
key = createKey(PLUGIN_PCF,unit+i);
if (!existPortStatus(key) ||
(existPortStatus(key) && (globalMapPortStatus[key].mode == PIN_MODE_INPUT_PULLUP || globalMapPortStatus[key].mode == PIN_MODE_INPUT)) ||
port==i) //set to 1 the PIN to be set as INPUT
portmask |= (1 << i); //set port i = 1
}
GPIO_PCF_WriteAllPins(address,portmask);
return true;
} else
return false;
}
//*********************************************************
// GPIO_Monitor10xSec:
// What it does:
@@ -424,7 +484,7 @@ bool checkValidPortRange(pluginID_t pluginID, int port)
return false;
}
void setInternalGPIOPullupMode(int port)
void setInternalGPIOPullupMode(uint8_t port)
{
if (checkValidPortRange(PLUGIN_GPIO, port)) {
#if defined(ESP8266)
+9 -28
View File
@@ -7,30 +7,11 @@
// MCP registers
#define MCP23017_IODIRA 0x00 //!< I/O direction register A
#define MCP23017_IPOLA 0x02 //!< Input polarity port register A
#define MCP23017_GPINTENA 0x04 //!< Interrupt-on-change pins A
#define MCP23017_DEFVALA 0x06 //!< Default value register A
#define MCP23017_INTCONA 0x08 //!< Interrupt-on-change control register A
#define MCP23017_IOCONA 0x0A //!< I/O expander configuration register A
#define MCP23017_GPPUA 0x0C //!< GPIO pull-up resistor register A
#define MCP23017_INTFA 0x0E //!< Interrupt flag register A
#define MCP23017_INTCAPA 0x10 //!< Interrupt captured value for port register A
#define MCP23017_GPIOA 0x12 //!< General purpose I/O port register A
#define MCP23017_OLATA 0x14 //!< Output latch register 0 A
#define MCP23017_GPPUA 0x0C //!< Pullup resistor register A
#define MCP23017_GPIOA 0x12 //!< General purpose I/O port register A
#define MCP23017_IODIRB 0x01 //!< I/O direction register B
#define MCP23017_IPOLB 0x03 //!< Input polarity port register B
#define MCP23017_GPINTENB 0x05 //!< Interrupt-on-change pins B
#define MCP23017_DEFVALB 0x07 //!< Default value register B
#define MCP23017_INTCONB 0x09 //!< Interrupt-on-change control register B
#define MCP23017_IOCONB 0x0B //!< I/O expander configuration register B
#define MCP23017_GPPUB 0x0D //!< GPIO pull-up resistor register B
#define MCP23017_INTFB 0x0F //!< Interrupt flag register B
#define MCP23017_INTCAPB 0x11 //!< Interrupt captured value for port register B
#define MCP23017_GPIOB 0x13 //!< General purpose I/O port register B
#define MCP23017_OLATB 0x15 //!< Output latch register 0 B
#define MCP23017_INT_ERR 255 //!< Interrupt error
#define MCP23017_GPPUB 0x0D //!< Pullup resistor register B
#define MCP23017_GPIOB 0x13 //!< General purpose I/O port register B
//********************************************************************************
// Internal GPIO write
@@ -41,9 +22,7 @@ void GPIO_Internal_Write(int pin, byte value);
// Internal GPIO read
//********************************************************************************
bool GPIO_Internal_Read(int pin);
bool GPIO_Read_Switch_State(struct EventStruct *event);
bool GPIO_Read_Switch_State(int pinNumber, byte pinMode);
//********************************************************************************
@@ -59,9 +38,8 @@ bool GPIO_MCP_Write(int Par1, byte Par2);
void GPIO_MCP_WriteRegister(byte mcpAddr, uint8_t regAddr, uint8_t regValue);
//********************************************************************************
// MCP23017 config
// MCP23017 pullUP
//********************************************************************************
void GPIO_MCP_Config(int Par1, byte Par2);
//********************************************************************************
// PCF8574 read
@@ -93,7 +71,10 @@ void sendMonitorEvent(const char* prefix, int port, int8_t state);
bool checkValidPortRange(pluginID_t pluginID, int port);
bool checkValidPortAddress(pluginID_t pluginID, byte address);
void setInternalGPIOPullupMode(int port);
void setInternalGPIOPullupMode(uint8_t port);
bool setMCPInputAndPullupMode(uint8_t Par1, bool enablePullUp);
bool setMCPOutputMode(uint8_t Par1);
bool setPCFInputMode(uint8_t pin);
bool GPIO_Write(pluginID_t pluginID, int port, byte value, byte pinMode=PIN_MODE_OUTPUT);
bool GPIO_Read(pluginID_t pluginID, int port, int8_t &value);