Add BASE 2 for absolute relays

This commit is contained in:
Theo Arends 2023-03-25 17:50:23 +01:00
parent 3daedae2da
commit 4645af038c
2 changed files with 36 additions and 27 deletions

View File

@ -124,9 +124,9 @@ struct PCF8574 {
uint8_t relay_offset; uint8_t relay_offset;
uint8_t button_max; uint8_t button_max;
uint8_t switch_max; uint8_t switch_max;
uint8_t base;
int8_t button_offset; int8_t button_offset;
int8_t switch_offset; int8_t switch_offset;
bool base;
bool interrupt; bool interrupt;
} Pcf8574; } Pcf8574;
@ -234,9 +234,10 @@ bool Pcf8574LoadTemplate(void) {
// rule3 on file#pcf8574.dat do {"NAME":"PCF8574 A=B1234,Ri4321,B=B5678,Ri8765","GPIO":[32,33,34,35,259,258,257,256,36,37,38,39,263,262,261,260]} endon // rule3 on file#pcf8574.dat do {"NAME":"PCF8574 A=B1234,Ri4321,B=B5678,Ri8765","GPIO":[32,33,34,35,259,258,257,256,36,37,38,39,263,262,261,260]} endon
// rule3 on file#pcf8574.dat do {"NAME":"PCF8574 A=B3456,Ri4321,B=B78910,Ri8765","BASE":1,"GPIO":[34,35,36,37,259,258,257,256,38,39,40,41,263,262,261,260]} endon // rule3 on file#pcf8574.dat do {"NAME":"PCF8574 A=B3456,Ri4321,B=B78910,Ri8765","BASE":1,"GPIO":[34,35,36,37,259,258,257,256,38,39,40,41,263,262,261,260]} endon
// rule3 on file#pcf8574.dat do {"NAME":"PCF8574 A=B3456,Ri6543,B=B78910,Ri10987","BASE":2,"GPIO":[34,35,36,37,261,260,259,258,38,39,40,41,265,264,263,262]} endon
JsonParserToken val = root[PSTR(D_JSON_BASE)]; JsonParserToken val = root[PSTR(D_JSON_BASE)];
if (val) { if (val) {
Pcf8574.base = (val.getUInt()) ? true : false; Pcf8574.base = val.getUInt();
} }
val = root[PSTR(D_JSON_NAME)]; val = root[PSTR(D_JSON_NAME)];
if (val) { if (val) {
@ -358,8 +359,13 @@ void Pcf8574Init(void) {
void Pcf8574Power(void) { void Pcf8574Power(void) {
// XdrvMailbox.index = 32-bit rpower bit mask // XdrvMailbox.index = 32-bit rpower bit mask
power_t rpower = XdrvMailbox.index >> Pcf8574.relay_offset; power_t rpower = XdrvMailbox.index;
for (uint32_t index = 0; index < Pcf8574.relay_max; index++) { uint32_t relay_max = TasmotaGlobal.devices_present;
if (Pcf8574.base < 2) {
rpower >>= Pcf8574.relay_offset;
relay_max = Pcf8574.relay_max;
}
for (uint32_t index = 0; index < relay_max; index++) {
power_t state = rpower &1; power_t state = rpower &1;
if (Pcf8574PinUsed(GPIO_REL1, index)) { if (Pcf8574PinUsed(GPIO_REL1, index)) {
uint32_t pin = Pcf8574Pin(GPIO_REL1, index) & 0x3F; // Fix possible overflow over 63 gpios uint32_t pin = Pcf8574Pin(GPIO_REL1, index) & 0x3F; // Fix possible overflow over 63 gpios
@ -372,13 +378,13 @@ void Pcf8574Power(void) {
bool Pcf8574AddButton(void) { bool Pcf8574AddButton(void) {
// XdrvMailbox.index = button/switch index // XdrvMailbox.index = button/switch index
uint32_t index = XdrvMailbox.index; uint32_t index = XdrvMailbox.index;
if (Pcf8574.base) { if (Pcf8574.base < 1) {
if (!bitRead(Pcf8574.button_used, index)) { return false; }
Pcf8574.button_offset = 0;
} else {
if (Pcf8574.button_offset < 0) { Pcf8574.button_offset = index; } if (Pcf8574.button_offset < 0) { Pcf8574.button_offset = index; }
index -= Pcf8574.button_offset; index -= Pcf8574.button_offset;
if (index >= Pcf8574.button_max) { return false; } if (index >= Pcf8574.button_max) { return false; }
} else {
if (!bitRead(Pcf8574.button_used, index)) { return false; }
Pcf8574.button_offset = 0;
} }
XdrvMailbox.index = (Pcf8574DigitalRead(Pcf8574Pin(GPIO_KEY1, index)) != bitRead(Pcf8574.button_inverted, index)); XdrvMailbox.index = (Pcf8574DigitalRead(Pcf8574Pin(GPIO_KEY1, index)) != bitRead(Pcf8574.button_inverted, index));
return true; return true;
@ -387,13 +393,13 @@ bool Pcf8574AddButton(void) {
bool Pcf8574AddSwitch(void) { bool Pcf8574AddSwitch(void) {
// XdrvMailbox.index = button/switch index // XdrvMailbox.index = button/switch index
uint32_t index = XdrvMailbox.index; uint32_t index = XdrvMailbox.index;
if (Pcf8574.base) { if (Pcf8574.base < 1) {
if (!bitRead(Pcf8574.switch_used, index)) { return false; }
Pcf8574.switch_offset = 0;
} else {
if (Pcf8574.switch_offset < 0) { Pcf8574.switch_offset = index; } if (Pcf8574.switch_offset < 0) { Pcf8574.switch_offset = index; }
index -= Pcf8574.switch_offset; index -= Pcf8574.switch_offset;
if (index >= Pcf8574.switch_max) { return false; } if (index >= Pcf8574.switch_max) { return false; }
} else {
if (!bitRead(Pcf8574.switch_used, index)) { return false; }
Pcf8574.switch_offset = 0;
} }
XdrvMailbox.index = Pcf8574DigitalRead(Pcf8574Pin(GPIO_SWT1, index)); XdrvMailbox.index = Pcf8574DigitalRead(Pcf8574Pin(GPIO_SWT1, index));
return true; return true;

View File

@ -154,9 +154,9 @@ struct MCP230 {
uint8_t relay_offset; uint8_t relay_offset;
uint8_t button_max; uint8_t button_max;
uint8_t switch_max; uint8_t switch_max;
uint8_t base;
int8_t button_offset; int8_t button_offset;
int8_t switch_offset; int8_t switch_offset;
bool base;
bool interrupt; bool interrupt;
} Mcp23x; } Mcp23x;
@ -491,7 +491,7 @@ bool MCP23xLoadTemplate(void) {
// {"NAME":"MCP23017","GPIO":[32,33,34,35,36,37,38,39,224,225,226,227,228,229,230,231,40,41,42,43,44,45,46,47,232,233,234,235,236,237,238,239]} // {"NAME":"MCP23017","GPIO":[32,33,34,35,36,37,38,39,224,225,226,227,228,229,230,231,40,41,42,43,44,45,46,47,232,233,234,235,236,237,238,239]}
JsonParserToken val = root[PSTR(D_JSON_BASE)]; JsonParserToken val = root[PSTR(D_JSON_BASE)];
if (val) { if (val) {
Mcp23x.base = (val.getUInt()) ? true : false; Mcp23x.base = val.getUInt();
} }
val = root[PSTR(D_JSON_NAME)]; val = root[PSTR(D_JSON_NAME)];
if (val) { if (val) {
@ -745,14 +745,17 @@ void MCP23xInit(void) {
} }
void MCP23xPower(void) { void MCP23xPower(void) {
power_t rpower = XdrvMailbox.index >> Mcp23x.relay_offset; // XdrvMailbox.index = 32-bit rpower bit mask
for (uint32_t index = 0; index < Mcp23x.relay_max; index++) { power_t rpower = XdrvMailbox.index;
uint32_t relay_max = TasmotaGlobal.devices_present;
if (Mcp23x.base < 2) {
rpower >>= Mcp23x.relay_offset;
relay_max = Mcp23x.relay_max;
}
for (uint32_t index = 0; index < relay_max; index++) {
power_t state = rpower &1; power_t state = rpower &1;
if (MCP23xPinUsed(GPIO_REL1, index)) { if (MCP23xPinUsed(GPIO_REL1, index)) {
uint32_t pin = MCP23xPin(GPIO_REL1, index) & 0x3F; // Fix possible overflow over 63 gpios uint32_t pin = MCP23xPin(GPIO_REL1, index) & 0x3F; // Fix possible overflow over 63 gpios
// AddLog(LOG_LEVEL_DEBUG, PSTR("MCP: Power pin %d, state %d(%d)"), pin, state, bitRead(Mcp23x.relay_inverted, index));
MCP23xDigitalWrite(pin, bitRead(Mcp23x.relay_inverted, index) ? !state : state); MCP23xDigitalWrite(pin, bitRead(Mcp23x.relay_inverted, index) ? !state : state);
} }
rpower >>= 1; // Select next power rpower >>= 1; // Select next power
@ -762,13 +765,13 @@ void MCP23xPower(void) {
bool MCP23xAddButton(void) { bool MCP23xAddButton(void) {
// XdrvMailbox.index = button/switch index // XdrvMailbox.index = button/switch index
uint32_t index = XdrvMailbox.index; uint32_t index = XdrvMailbox.index;
if (Mcp23x.base) { if (Mcp23x.base < 1) {
if (!bitRead(Mcp23x.button_used, index)) { return false; }
Mcp23x.button_offset = 0;
} else {
if (Mcp23x.button_offset < 0) { Mcp23x.button_offset = index; } if (Mcp23x.button_offset < 0) { Mcp23x.button_offset = index; }
index -= Mcp23x.button_offset; index -= Mcp23x.button_offset;
if (index >= Mcp23x.button_max) { return false; } if (index >= Mcp23x.button_max) { return false; }
} else {
if (!bitRead(Mcp23x.button_used, index)) { return false; }
Mcp23x.button_offset = 0;
} }
XdrvMailbox.index = (MCP23xDigitalRead(MCP23xPin(GPIO_KEY1, index)) != bitRead(Mcp23x.button_inverted, index)); XdrvMailbox.index = (MCP23xDigitalRead(MCP23xPin(GPIO_KEY1, index)) != bitRead(Mcp23x.button_inverted, index));
return true; return true;
@ -777,13 +780,13 @@ bool MCP23xAddButton(void) {
bool MCP23xAddSwitch(void) { bool MCP23xAddSwitch(void) {
// XdrvMailbox.index = button/switch index // XdrvMailbox.index = button/switch index
uint32_t index = XdrvMailbox.index; uint32_t index = XdrvMailbox.index;
if (Mcp23x.base) { if (Mcp23x.base < 1) {
if (!bitRead(Mcp23x.switch_used, index)) { return false; }
Mcp23x.switch_offset = 0;
} else {
if (Mcp23x.switch_offset < 0) { Mcp23x.switch_offset = index; } if (Mcp23x.switch_offset < 0) { Mcp23x.switch_offset = index; }
index -= Mcp23x.switch_offset; index -= Mcp23x.switch_offset;
if (index >= Mcp23x.switch_max) { return false; } if (index >= Mcp23x.switch_max) { return false; }
} else {
if (!bitRead(Mcp23x.switch_used, index)) { return false; }
Mcp23x.switch_offset = 0;
} }
XdrvMailbox.index = MCP23xDigitalRead(MCP23xPin(GPIO_SWT1, index)); XdrvMailbox.index = MCP23xDigitalRead(MCP23xPin(GPIO_SWT1, index));
return true; return true;