Switch Version 3

This commit is contained in:
2023-05-13 07:21:00 +02:00
parent e1dc33c72f
commit 5047ea8fea

View File

@@ -1,9 +1,9 @@
#include <SPI.h>; #include <SPI.h>;
#include <mcp2515.h>; #include <mcp2515.h>;
const byte SoftwareVersionHigh = 1; const byte SoftwareVersionHigh = 2;
const byte SoftwareVersionLow = 0; const byte SoftwareVersionLow = 0;
const byte HardwareVersionHigh = 2; const byte HardwareVersionHigh = 6;
const byte HardwareVersionLow = 0; const byte HardwareVersionLow = 0;
const uint32_t HELP_PACKAGE_CAN_ID = 0xFFFF; const uint32_t HELP_PACKAGE_CAN_ID = 0xFFFF;
const uint32_t SWITCH_TRIGGERED_CAN_ID = 0x050; const uint32_t SWITCH_TRIGGERED_CAN_ID = 0x050;
@@ -11,9 +11,6 @@ const uint32_t TRIGGER_SWITCH_CAN_ID = 0x055;
const uint32_t HELP_PACKAGE_ID = 0xFFFF; const uint32_t HELP_PACKAGE_ID = 0xFFFF;
struct can_frame _frame; struct can_frame _frame;
MCP2515 mcp2515_0(PIN_PB1);
MCP2515 mcp2515_1(PIN_PB0);
MCP2515 mcp2515_2(PIN_PA3);
typedef struct typedef struct
{ {
@@ -21,6 +18,7 @@ typedef struct
byte sourceMeyPinId; byte sourceMeyPinId;
byte targetMeyPinId; byte targetMeyPinId;
bool trigger; bool trigger;
bool inverse;
void InitSimple(uint32_t sourceDevId, byte sourceMeyPinId, byte targetMeyPinId) void InitSimple(uint32_t sourceDevId, byte sourceMeyPinId, byte targetMeyPinId)
{ {
@@ -28,6 +26,7 @@ typedef struct
this->sourceMeyPinId = sourceMeyPinId; this->sourceMeyPinId = sourceMeyPinId;
this->targetMeyPinId = targetMeyPinId; this->targetMeyPinId = targetMeyPinId;
trigger = false; trigger = false;
inverse = false;
} }
void InitTrigger(uint32_t sourceDevId, byte sourceMeyPinId, byte targetMeyPinId) void InitTrigger(uint32_t sourceDevId, byte sourceMeyPinId, byte targetMeyPinId)
@@ -36,6 +35,16 @@ typedef struct
this->sourceMeyPinId = sourceMeyPinId; this->sourceMeyPinId = sourceMeyPinId;
this->targetMeyPinId = targetMeyPinId; this->targetMeyPinId = targetMeyPinId;
trigger = true; trigger = true;
inverse = false;
}
void InitTriggerInverse(uint32_t sourceDevId, byte sourceMeyPinId, byte targetMeyPinId)
{
this->sourceDevId = sourceDevId;
this->sourceMeyPinId = sourceMeyPinId;
this->targetMeyPinId = targetMeyPinId;
trigger = true;
inverse = true;
} }
} Rule; } Rule;
@@ -63,45 +72,73 @@ PinState MeyPins[8];
bool flag = false; bool flag = false;
byte DeviceId[1]; byte DeviceId[1];
Rule Rules[4]; Rule Rules[10];
int RulesCount = 4; int RulesCount = 10 ;
MCP2515 mcp2515_0(PIN_PA2);
MCP2515 mcp2515_1(PIN_PA3);
MCP2515 mcp2515_2(PIN_PB0);
MCP2515 mcp2515_3(PIN_PB1);
void setup() { void setup() {
SPI.begin(); SPI.begin();
Rules[0] = Rule(); Rules[0] = Rule();
Rules[0].InitSimple(0x051F, 1, 3); Rules[0].InitTriggerInverse(0x051F, 5, 4); // Lichtschalter Licht 1
Rules[1] = Rule(); Rules[1] = Rule();
Rules[1].InitSimple(0x051F, 2, 1); Rules[1].InitTriggerInverse(0x051F, 5, 5); // Lichtschalter Licht 2
Rules[2] = Rule();
Rules[2].InitSimple(0x0388, 1, 3);
Rules[3] = Rule();
Rules[3].InitSimple(0x0388, 2, 4);
Rules[2] = Rule();
Rules[2].InitTrigger(0x051F, 4, 6); // Rolle runter von Tür
Rules[3] = Rule();
Rules[3].InitTrigger(0x051F, 3, 7); // Rollo hoch von Tür
Rules[4] = Rule();
Rules[4].InitTrigger(0x146A, 1, 4); // Licht 1 von Terassenschalter
Rules[5] = Rule();
Rules[5].InitTrigger(0x146A, 1, 5); // Licht 2 von Terassenschalter
Rules[6] = Rule();
Rules[6].InitTrigger(0x0769, 1, 5); // Licht von Engang unten
Rules[7] = Rule();
Rules[7].InitTrigger(0x0769, 2, 2); // Licht von Engang unten
Rules[8] = Rule();
Rules[8].InitTrigger(0x05DF, 1, 4); // OK Flurlicht
Rules[9] = Rule();
Rules[9].InitTrigger(0x05DF, 1, 5); // OK Flurlicht
Rules[10] = Rule();
Rules[10].InitTrigger(0x1177, 1, 2); // OK Flurlicht
MeyPins[0] = PinState(); MeyPins[0] = PinState();
MeyPins[0].Init(PIN_PC1, (byte) 1); MeyPins[0].Init(PIN_PC7, (byte) 1);
MeyPins[1] = PinState(); MeyPins[1] = PinState();
MeyPins[1].Init(PIN_PC2, (byte) 2); MeyPins[1].Init(PIN_PC6, (byte) 2);
MeyPins[2] = PinState(); MeyPins[2] = PinState();
MeyPins[2].Init(PIN_PC3, (byte) 3); MeyPins[2].Init(PIN_PC5, (byte) 3);
MeyPins[3] = PinState(); MeyPins[3] = PinState();
MeyPins[3].Init(PIN_PC4, (byte) 4); MeyPins[3].Init(PIN_PC4, (byte) 4);
MeyPins[4] = PinState(); MeyPins[4] = PinState();
MeyPins[4].Init(PIN_PC5, (byte) 5); MeyPins[4].Init(PIN_PC3, (byte) 5);
MeyPins[5] = PinState(); MeyPins[5] = PinState();
MeyPins[5].Init(PIN_PC6, (byte) 6); MeyPins[5].Init(PIN_PC2, (byte) 6);
MeyPins[6] = PinState(); MeyPins[6] = PinState();
MeyPins[6].Init(PIN_PC7, (byte) 7); MeyPins[6].Init(PIN_PC1, (byte) 7);
MeyPins[7] = PinState();
MeyPins[7].Init(PIN_PC0, (byte) 8);
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLA, CLKCTRL.MCLKCTRLA | 1 << 7); _PROTECTED_WRITE(CLKCTRL.MCLKCTRLA, CLKCTRL.MCLKCTRLA | 1 << 7);
mcp2515_0.reset(); mcp2515_0.reset();
mcp2515_0.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz mcp2515_0.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
mcp2515_0.setNormalMode(); mcp2515_0.setNormalMode();
@@ -113,10 +150,20 @@ void setup() {
mcp2515_2.reset(); mcp2515_2.reset();
mcp2515_2.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz mcp2515_2.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
mcp2515_2.setNormalMode(); mcp2515_2.setNormalMode();
mcp2515_3.reset();
mcp2515_3.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
mcp2515_3.setNormalMode();
for (int i = 0; i <= PinCount - 1; i++) //delay(20000);
for (int i = 0; i < PinCount; i++)
{ {
pinMode(MeyPins[i].pin_id, OUTPUT); pinMode(MeyPins[i].pin_id, OUTPUT);
// digitalWrite(MeyPins[i].pin_id, HIGH);
//delay(100);
digitalWrite(MeyPins[i].pin_id, LOW);
} }
@@ -125,25 +172,36 @@ void setup() {
int i = 0; int i = 0;
bool debugState = false; bool debugState = false;
void loop() { void loop() {
if (mcp2515_0.readMessage(&_frame) == MCP2515::ERROR_OK) if (mcp2515_0.readMessage(&_frame) == MCP2515::ERROR_OK)
{ {
HandleFrame();
mcp2515_1.sendMessage(MCP2515::TXB1, &_frame); mcp2515_1.sendMessage(MCP2515::TXB1, &_frame);
mcp2515_2.sendMessage(MCP2515::TXB1, &_frame); mcp2515_2.sendMessage(MCP2515::TXB1, &_frame);
mcp2515_3.sendMessage(MCP2515::TXB1, &_frame);
HandleFrame();
} }
if (mcp2515_1.readMessage(&_frame) == MCP2515::ERROR_OK) if (mcp2515_1.readMessage(&_frame) == MCP2515::ERROR_OK)
{ {
HandleFrame();
mcp2515_0.sendMessage(MCP2515::TXB1, &_frame); mcp2515_0.sendMessage(MCP2515::TXB1, &_frame);
mcp2515_2.sendMessage(MCP2515::TXB1, &_frame); mcp2515_2.sendMessage(MCP2515::TXB1, &_frame);
mcp2515_3.sendMessage(MCP2515::TXB1, &_frame);
HandleFrame();
} }
if (mcp2515_2.readMessage(&_frame) == MCP2515::ERROR_OK) if (mcp2515_2.readMessage(&_frame) == MCP2515::ERROR_OK)
{ {
HandleFrame();
mcp2515_0.sendMessage(MCP2515::TXB1, &_frame); mcp2515_0.sendMessage(MCP2515::TXB1, &_frame);
mcp2515_1.sendMessage(MCP2515::TXB1, &_frame); mcp2515_1.sendMessage(MCP2515::TXB1, &_frame);
mcp2515_3.sendMessage(MCP2515::TXB1, &_frame);
HandleFrame();
}
if (mcp2515_3.readMessage(&_frame) == MCP2515::ERROR_OK)
{
mcp2515_0.sendMessage(MCP2515::TXB1, &_frame);
mcp2515_1.sendMessage(MCP2515::TXB1, &_frame);
mcp2515_2.sendMessage(MCP2515::TXB1, &_frame);
HandleFrame();
} }
delay(20); delay(20);
} }
@@ -155,18 +213,20 @@ void HandleFrame()
{ {
for (int i = 0; i <= RulesCount - 1; i++) for (int i = 0; i <= RulesCount - 1; i++)
{ {
if (Rules[i].sourceDevId == GetDeviceId(_frame.can_id) && Rules[i].sourceMeyPinId == _frame.data[0] ) if (Rules[i].sourceDevId == 0x0000 || ( Rules[i].sourceDevId == GetDeviceId(_frame.can_id) && Rules[i].sourceMeyPinId == _frame.data[0] ))
{ {
ProcessTriggerSwitchPackage(Rules[i].targetMeyPinId, _frame.data[1], Rules[i].trigger ); ProcessTriggerSwitchPackage(Rules[i].targetMeyPinId, _frame.data[1], Rules[i].trigger, Rules[i].inverse );
return;
} }
} }
} }
} }
void ProcessTriggerSwitchPackage(byte meyPinId, byte state, bool asTrigger) void ProcessTriggerSwitchPackage(byte meyPinId, byte state, bool asTrigger, bool inverse)
{ {
int pinState = state > 0; int pinState = state > 0;
if (inverse)
pinState = !pinState;
PinState *foundPinId = 0; PinState *foundPinId = 0;
for (int i = 0; i < PinCount; i++) for (int i = 0; i < PinCount; i++)