diff --git a/Software/Switch/Swtich/Swtich.ino b/Software/Switch/Swtich/Swtich.ino index 3608740..2a96da0 100644 --- a/Software/Switch/Swtich/Swtich.ino +++ b/Software/Switch/Swtich/Swtich.ino @@ -12,16 +12,16 @@ const uint32_t DEBUG_CAN_ID = 0x000; struct can_frame _frame; -typedef struct +struct Rule { - uint32_t sourceDevId; - byte sourceMeyPinId; - uint32_t targetDevId; + uint16_t sourceDevId; + uint8_t sourceMeyPinId; + uint16_t targetDevId; byte targetMeyPinId; bool toggle; bool inverse; - void InitSimple(uint32_t sourceDevId, byte sourceMeyPinId, uint32_t targetDevId, byte targetMeyPinId) + void InitSimple(uint16_t sourceDevId, uint8_t sourceMeyPinId, uint16_t targetDevId, byte targetMeyPinId) { this->sourceDevId = sourceDevId; this->sourceMeyPinId = sourceMeyPinId; @@ -31,7 +31,7 @@ typedef struct inverse = false; } - void InitToggle(uint32_t sourceDevId, byte sourceMeyPinId, uint32_t targetDevId, byte targetMeyPinId) + void InitToggle(uint16_t sourceDevId, uint8_t sourceMeyPinId, uint16_t targetDevId, byte targetMeyPinId) { this->sourceDevId = sourceDevId; this->sourceMeyPinId = sourceMeyPinId; @@ -41,7 +41,7 @@ typedef struct inverse = false; } - void InitToggleInverse(uint32_t sourceDevId, byte sourceMeyPinId, uint32_t targetDevId, byte targetMeyPinId) + void InitToggleInverse(uint16_t sourceDevId, uint8_t sourceMeyPinId, uint16_t targetDevId, byte targetMeyPinId) { this->sourceDevId = sourceDevId; this->sourceMeyPinId = sourceMeyPinId; @@ -50,7 +50,7 @@ typedef struct toggle = true; inverse = true; } -} Rule; +} ; typedef struct { @@ -93,7 +93,7 @@ struct RemotePinInfo int16_t Count() { if (this->next == NULL) return 1; - return this->next->Count() + 1; + return this->next->Count() + 1; } RemotePinInfo* FindOrAdd(uint16_t deviceId, byte count = 0) @@ -136,7 +136,7 @@ PinState MeyPins[8]; bool flag = false; int32_t myDeviceId; -Rule Rules[8]; +Rule Rules[7]; MCP2515 mcp2515_0(PIN_PA2); MCP2515 mcp2515_1(PIN_PA3); @@ -147,23 +147,23 @@ void setup() { SPI.begin(); Rules[0] = Rule(); - Rules[0].InitToggle(0x1EC3, 1, 0x3D2D, 2); // should switch MeyPin #2 of Device 3D2D to the settet state of MeyPin #1 of 0x055F is changed + Rules[0].InitToggleInverse(0x051F, 5, 0x3D2D, 4); // Lichtschalter Wohnzimmer Licht 1 Rules[1] = Rule(); - Rules[1].InitToggle(0xD8C0, 2, 0x3D2D, 2); // should switch MeyPin #2 of Device 3D2D to the settet state of MeyPin #1 of 0x055F is changed + Rules[1].InitToggleInverse(0x051F, 5, 0x3D2D, 3); // Lichtschalter Wohnzimmer Licht 2 + Rules[2] = Rule(); - Rules[2].InitToggle(0x1EC2, 3, 0x3D2D, 3); // should switch MeyPin #2 of Device 3D2D to the settet state of MeyPin #1 of 0x055F is changed + Rules[2].InitToggle(0x05df, 1, 0x3D2D, 4); // Licht 1 von Terassenschalter Rules[3] = Rule(); - Rules[3].InitToggle(0x1EC2, 4, 0x3D2D, 4); // should switch MeyPin #2 of Device 3D2D to the settet state of MeyPin #1 of 0x055F is changed + Rules[3].InitToggle(0x05df, 1, 0x3D2D, 3); // Licht 2 von Terassenschalter + Rules[4] = Rule(); - Rules[4].InitToggle(0x1EC2, 5, 0x3D2D, 5); // should switch MeyPin #2 of Device 3D2D to the settet state of MeyPin #1 of 0x055F is changed + Rules[4].InitToggle(0x0769, 1, 0x3D2D, 7); // Eingangstür Papa Büro Rules[5] = Rule(); - Rules[5].InitToggle(0x1EC2, 6, 0x3D2D, 6); // should switch MeyPin #2 of Device 3D2D to the settet state of MeyPin #1 of 0x055F is changed + Rules[5].InitToggle(0x0769, 2,0x3D2D, 3); // Licht 1 Wochzimmer Eingangstür Rules[6] = Rule(); - Rules[6].InitToggle(0x1EC2, 7, 0x3D2D, 7); // should switch MeyPin #2 of Device 3D2D to the settet state of MeyPin #1 of 0x055F is changed - Rules[7] = Rule(); - Rules[7].InitToggle(0x1EC2, 8, 0x3D2D, 8); // should switch MeyPin #2 of Device 3D2D to the settet state of MeyPin #1 of 0x055F is changed - + Rules[6].InitToggle(0x0769, 2,0x3D2D, 4); // Licht 2 Wochzimmer Eingangstür + MeyPins[0] = PinState(); MeyPins[0].Init(PIN_PC7, (byte) 1); @@ -213,12 +213,10 @@ void setup() { for (int i = 0; i < sizeof(MeyPins) / sizeof(PinState); i++) { pinMode(MeyPins[i].pin_id, OUTPUT); - - // digitalWrite(MeyPins[i].pin_id, HIGH); - //delay(100); digitalWrite(MeyPins[i].pin_id, LOW); } + CalculateMyDeviceId(); SendSerialPackage(&mcp2515_0); @@ -226,17 +224,10 @@ void setup() { SendSerialPackage(&mcp2515_2); SendSerialPackage(&mcp2515_3); + digitalWrite(PIN_PC6, HIGH); + } - void ToggleDebug() - { - delay(500); - byte debugState = digitalRead(PIN_PC7)^true; - if (debugState) - digitalWrite(PIN_PC7, HIGH); - else - digitalWrite(PIN_PC7, LOW); - } void loop() @@ -290,19 +281,10 @@ void HandleMeyPinTriggeredCanPackage(can_frame *frame) if (currentPinState == NULL) - { - int16_t test2[3]; - test2[0] = 'S'; - test2[1] = 'O'; - test2[2] = 'S'; - // SendDebugCanPackage(&mcp2515_3, test2, sizeof(test2) / sizeof(int16_t)); + { return; } - int16_t test[1]; - test[0] = remotePinInfos.Count(); - // SendDebugCanPackage(&mcp2515_3, test, sizeof(test) / sizeof(int16_t)); - currentPinState->setPinState(frame->data[0], frame->data[1]); @@ -340,14 +322,17 @@ void HandleTriggerMeypinCanPackage(can_frame *frame) void HandleRules(can_frame *frame) { - if (GetPackageType(frame->can_id) == SWITCH_TRIGGERED_CAN_ID) + uint32_t canId = frame->can_id; + if (GetPackageType(canId) == SWITCH_TRIGGERED_CAN_ID) { + uint8_t meyPin = frame->data[0]; + uint8_t dState = (byte) frame->data[1]; for (int i = 0; i < sizeof(Rules) / sizeof(Rule); i++) { - if ( Rules[i].sourceDevId == GetDeviceId(frame->can_id)) - if (Rules[i].sourceMeyPinId == frame->data[0]) - HandleRule(&Rules[i], _frame.data[1] ); + if ( Rules[i].sourceDevId == GetDeviceId(canId)) + if (Rules[i].sourceMeyPinId == meyPin || Rules[i].sourceMeyPinId == 255) + HandleRule(&Rules[i], dState ); } } } @@ -375,6 +360,7 @@ void HandleRule(Rule *rule, byte state) SendDoTriggerSwitchCanPackage(&mcp2515_1, rule->targetDevId, rule->targetMeyPinId, pinState); SendDoTriggerSwitchCanPackage(&mcp2515_2, rule->targetDevId, rule->targetMeyPinId, pinState); SendDoTriggerSwitchCanPackage(&mcp2515_3, rule->targetDevId, rule->targetMeyPinId, pinState); + //digitalWrite(foundPinId->pin_id, pinState); //foundPinId->pin_state = pinState; @@ -461,21 +447,3 @@ void SendDoTriggerSwitchCanPackage(MCP2515 *interface, uint16_t targetCanId, byt _frame.data[3] = state; interface->sendMessage(MCP2515::TXB1, &_frame); } - -void SendDebugCanPackage(MCP2515 *interface, int16_t *data, int len) -{ - can_frame frame = can_frame(); - frame.can_id = 0; - frame.can_id = CreateCanId(DEBUG_CAN_ID); - - - frame.can_dlc = len * 2; - - for (int i = 0; i < frame.can_dlc / 2; i++) - { - frame.data[2 * i] = ((byte[])data)[2 * i + 1]; - frame.data[2 * i + 1] = ((byte[])data)[2 * i]; - } - - interface->sendMessage(MCP2515::TXB1, &frame); -}