diff --git a/Software/Switch/Swtich/Swtich.ino b/Software/Switch/Swtich/Swtich.ino index adeb10f..310c69d 100644 --- a/Software/Switch/Swtich/Swtich.ino +++ b/Software/Switch/Swtich/Swtich.ino @@ -1,66 +1,144 @@ #include ; #include ; -#include "MeyCan.cpp"; -const byte SoftwareVersionHigh = 1; +const byte SoftwareVersionHigh = 2; const byte SoftwareVersionLow = 0; -const byte HardwareVersionHigh = 2; +const byte HardwareVersionHigh = 6; const byte HardwareVersionLow = 0; +const uint32_t HELP_PACKAGE_CAN_ID = 0xFFFF; +const uint32_t SWITCH_TRIGGERED_CAN_ID = 0x050; +const uint32_t TRIGGER_SWITCH_CAN_ID = 0x055; +const uint32_t HELP_PACKAGE_ID = 0xFFFF; struct can_frame _frame; -MCP2515 mcp2515_0(PIN_PB1); -MCP2515 mcp2515_1(PIN_PB0); -MCP2515 mcp2515_2(PIN_PA3); + +typedef struct +{ + uint32_t sourceDevId; + byte sourceMeyPinId; + byte targetMeyPinId; + bool trigger; + bool inverse; + + void InitSimple(uint32_t sourceDevId, byte sourceMeyPinId, byte targetMeyPinId) + { + this->sourceDevId = sourceDevId; + this->sourceMeyPinId = sourceMeyPinId; + this->targetMeyPinId = targetMeyPinId; + trigger = false; + inverse = false; + } + + void InitTrigger(uint32_t sourceDevId, byte sourceMeyPinId, byte targetMeyPinId) + { + this->sourceDevId = sourceDevId; + this->sourceMeyPinId = sourceMeyPinId; + this->targetMeyPinId = targetMeyPinId; + 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; + +typedef struct +{ + int pin_id; + bool pin_state; + bool is_input; + byte meyPinId; + + PinState() {} + void Init(int pin_id, byte meyPinId) + { + this->pin_id = pin_id; + this->pin_state = true; + this->is_input = true; + this->meyPinId = meyPinId; + } +} PinState; + int PinCount = 8; PinState MeyPins[8]; bool flag = false; +byte DeviceId[1]; -Rule Rules[6]; -int RulesCount = 6; +Rule Rules[10]; +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() { SPI.begin(); - + Rules[0] = Rule(); - Rules[0].InitSimple(0x051F, 1, 1); + Rules[0].InitTriggerInverse(0x051F, 5, 4); // Lichtschalter Licht 1 Rules[1] = Rule(); - Rules[1].InitSimple(0x051F, 2, 2); - Rules[2] = Rule(); - Rules[2].InitSimple(0x051F, 3, 3); - Rules[3] = Rule(); - Rules[3].InitSimple(0x051F, 4, 4); - Rules[4] = Rule(); - Rules[4].InitSimple(0x051F, 5, 5); - Rules[5] = Rule(); - Rules[5].InitSimple(0x051F, 6, 6); - Rules[6] = Rule(); - Rules[6].InitSimple(0x051F, 7, 7); + Rules[1].InitTriggerInverse(0x051F, 5, 5); // Lichtschalter Licht 2 + 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].Init(PIN_PC1, (byte) 1); + MeyPins[0].Init(PIN_PC7, (byte) 1); MeyPins[1] = PinState(); - MeyPins[1].Init(PIN_PC2, (byte) 2); + MeyPins[1].Init(PIN_PC6, (byte) 2); MeyPins[2] = PinState(); - MeyPins[2].Init(PIN_PC3, (byte) 3); + MeyPins[2].Init(PIN_PC5, (byte) 3); MeyPins[3] = PinState(); MeyPins[3].Init(PIN_PC4, (byte) 4); MeyPins[4] = PinState(); - MeyPins[4].Init(PIN_PC5, (byte) 5); + MeyPins[4].Init(PIN_PC3, (byte) 5); MeyPins[5] = PinState(); - MeyPins[5].Init(PIN_PC6, (byte) 6); + MeyPins[5].Init(PIN_PC2, (byte) 6); 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); + mcp2515_0.reset(); mcp2515_0.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz mcp2515_0.setNormalMode(); @@ -72,10 +150,20 @@ void setup() { mcp2515_2.reset(); mcp2515_2.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz 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); + + // digitalWrite(MeyPins[i].pin_id, HIGH); + //delay(100); + digitalWrite(MeyPins[i].pin_id, LOW); + } @@ -84,24 +172,35 @@ void setup() { int i = 0; bool debugState = false; + void loop() { if (mcp2515_0.readMessage(&_frame) == MCP2515::ERROR_OK) { mcp2515_1.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) { mcp2515_0.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) { mcp2515_0.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); @@ -109,22 +208,25 @@ void loop() { void HandleFrame() { + if (GetPackageType(_frame.can_id) == SWITCH_TRIGGERED_CAN_ID) { 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 ); - return; + ProcessTriggerSwitchPackage(Rules[i].targetMeyPinId, _frame.data[1], Rules[i].trigger, Rules[i].inverse ); } } } } -void ProcessTriggerSwitchPackage(byte meyPinId, byte state, bool asTrigger) +void ProcessTriggerSwitchPackage(byte meyPinId, byte state, bool asTrigger, bool inverse) { + int pinState = state > 0; + if (inverse) + pinState = !pinState; PinState *foundPinId = 0; for (int i = 0; i < PinCount; i++) @@ -146,6 +248,27 @@ void ProcessTriggerSwitchPackage(byte meyPinId, byte state, bool asTrigger) foundPinId->pin_state = pinState; } +byte CircularShift(byte b) +{ + return (b << 1) | (b >> 7 & 1); +} + +uint32_t GetDeviceId(uint32_t canFrameId) +{ + return canFrameId & 0xFFFF; +} + +uint32_t GetPackageType(uint32_t canFrameId) +{ + return (canFrameId / 0x10000) & 0xFFF; +} + +uint32_t CreateCanId(uint32_t commandId) +{ + return ((commandId & 0xFFF) * 0x10000) | ( DeviceId[0] << 8) | (DeviceId[1]) | CAN_EFF_FLAG; +} + + void SendSerialPackage() { _frame.can_id = CreateCanId(HELP_PACKAGE_CAN_ID); diff --git a/Software/UPSoftware/UPSoftware.ino b/Software/UPSoftware/UPSoftware.ino index ecc9684..3af49d2 100644 --- a/Software/UPSoftware/UPSoftware.ino +++ b/Software/UPSoftware/UPSoftware.ino @@ -1,12 +1,12 @@ -#include ; + #include ; #include ; struct can_frame _frame; -MCP2515 mcp2515(PIN_PD4); -const byte SoftwareVersionHigh = 1; +MCP2515 mcp2515(PIN_PA3); +const byte SoftwareVersionHigh = 2; const byte SoftwareVersionLow = 0; -const byte HardwareVersionHigh = 3; +const byte HardwareVersionHigh = 3; const byte HardwareVersionLow = 0; typedef struct @@ -35,33 +35,32 @@ void setup() { PinPD2[0] = PinState(); - PinPD2[0].Init(PIN_PD0, (byte) 1); + PinPD2[0].Init(PIN_PC7, (byte) 1); PinPD2[1] = PinState(); - PinPD2[1].Init(PIN_PD2, (byte) 2); + PinPD2[1].Init(PIN_PD0, (byte) 2); PinPD2[2] = PinState(); - PinPD2[2].Init(PIN_PD5, (byte) 3); + PinPD2[2].Init(PIN_PD1, (byte) 3); PinPD2[3] = PinState(); - PinPD2[3].Init(PIN_PD7, (byte) 4); + PinPD2[3].Init(PIN_PD2, (byte) 4); PinPD2[4] = PinState(); - PinPD2[4].Init(PIN_PC7, (byte) 5); + PinPD2[4].Init(PIN_PD6, (byte) 5); PinPD2[5] = PinState(); - PinPD2[5].Init(PIN_PD1, (byte) 6); + PinPD2[5].Init(PIN_PD5, (byte) 6); PinPD2[6] = PinState(); - PinPD2[6].Init(PIN_PD3, (byte) 7); + PinPD2[6].Init(PIN_PD4, (byte) 7); PinPD2[7] = PinState(); - PinPD2[7].Init(PIN_PD6, (byte) 8); + PinPD2[7].Init(PIN_PD3, (byte) 8); _PROTECTED_WRITE(CLKCTRL.MCLKCTRLA, CLKCTRL.MCLKCTRLA | 1 << 7); - delay(10); mcp2515.reset(); mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz mcp2515.setNormalMode(); @@ -82,7 +81,7 @@ void setup() { void loop() { - for (int i = 0; i <= PinCount - 1; i++) + for (int i = 0; i <= PinCount - 1; i++) { if (CheckPinStatus(&PinPD2[i])) SendSwitchedTriggeredCanPackage(PinPD2[i].meyPinId, PinPD2[i].pin_state); @@ -125,14 +124,12 @@ void loop() void SendSerialPackage() { _frame.can_id = CreateCanId(0xFFFF); - _frame.can_dlc = 6; + _frame.can_dlc = 4; _frame.data[0] = SoftwareVersionHigh; _frame.data[1] = SoftwareVersionLow; _frame.data[2] = HardwareVersionHigh; _frame.data[3] = HardwareVersionLow; - _frame.data[4] = DeviceId[0]; - _frame.data[5] = DeviceId[1]; mcp2515.sendMessage(MCP2515::TXB1, &_frame); }