From 1d2d148d119ebf8f999807d8f385dc6afcf508ce Mon Sep 17 00:00:00 2001 From: Meydin87 Date: Sun, 14 May 2023 19:57:26 +0200 Subject: [PATCH] WIP --- Software/Protocol.txt | 3 ++- Software/Switch/Swtich/Swtich.ino | 35 +++++++++++++++---------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Software/Protocol.txt b/Software/Protocol.txt index 9d15828..b30ff80 100644 --- a/Software/Protocol.txt +++ b/Software/Protocol.txt @@ -31,4 +31,5 @@ Payload: --------------------------------- 2 bytes | TargetDeviceId -> The id of the board 1 byte | PinId -> Idof the pin of the board - 1 byte | state -> 1 to swtich on, 0 to switch off \ No newline at end of file + 1 byte | state -> 1 to swtich on, 0 to switch off + \ No newline at end of file diff --git a/Software/Switch/Swtich/Swtich.ino b/Software/Switch/Swtich/Swtich.ino index 703e9f3..ea7a8e5 100644 --- a/Software/Switch/Swtich/Swtich.ino +++ b/Software/Switch/Swtich/Swtich.ino @@ -15,32 +15,36 @@ typedef struct { uint32_t sourceDevId; byte sourceMeyPinId; + uint32_t targetDevId; byte targetMeyPinId; bool trigger; bool inverse; - void InitSimple(uint32_t sourceDevId, byte sourceMeyPinId, byte targetMeyPinId) + void InitSimple(uint32_t sourceDevId, byte sourceMeyPinId, uint32_t targetDevId, byte targetMeyPinId) { this->sourceDevId = sourceDevId; this->sourceMeyPinId = sourceMeyPinId; + this->targetDevId = targetDevId; this->targetMeyPinId = targetMeyPinId; trigger = false; inverse = false; } - void InitTrigger(uint32_t sourceDevId, byte sourceMeyPinId, byte targetMeyPinId) + void InitTrigger(uint32_t sourceDevId, byte sourceMeyPinId, uint32_t targetDevId, byte targetMeyPinId) { this->sourceDevId = sourceDevId; this->sourceMeyPinId = sourceMeyPinId; + this->targetDevId = targetDevId; this->targetMeyPinId = targetMeyPinId; trigger = true; inverse = false; } - void InitTriggerInverse(uint32_t sourceDevId, byte sourceMeyPinId, byte targetMeyPinId) + void InitTriggerInverse(uint32_t sourceDevId, byte sourceMeyPinId, uint32_t targetDevId, byte targetMeyPinId) { this->sourceDevId = sourceDevId; this->sourceMeyPinId = sourceMeyPinId; + this->targetDevId = targetDevId; this->targetMeyPinId = targetMeyPinId; trigger = true; inverse = true; @@ -70,7 +74,7 @@ PinState MeyPins[8]; bool flag = false; int32_t myDeviceId; -Rule Rules[10]; +Rule Rules[0]; MCP2515 mcp2515_0(PIN_PA2); MCP2515 mcp2515_1(PIN_PA3); @@ -80,6 +84,8 @@ MCP2515 mcp2515_3(PIN_PB1); void setup() { SPI.begin(); + Rules[0] = Rule(); + Rules[0].InitTrigger(0x055F, 1, 0x3D2D, 2); // should switch MeyPin #2 of Device 3D2D to the settet state of MeyPin #1 of 0x055F is changed MeyPins[0] = PinState(); MeyPins[0].Init(PIN_PC7, (byte) 1); @@ -151,7 +157,7 @@ bool debugState = false; void loop() { - + if (mcp2515_0.readMessage(&_frame) == MCP2515::ERROR_OK) { mcp2515_1.sendMessage(MCP2515::TXB1, &_frame); @@ -185,16 +191,16 @@ void loop() { void HandleFrame(can_frame *frame) { - HandleSwitchTriggeredCanPackage(frame); HandleTriggerMeypinCanPackage(frame); + // Handle rules needs to be the last call + HandleRules(frame); } void HandleTriggerMeypinCanPackage(can_frame *frame) { if (GetDeviceId(frame->can_id) == GetMyDeviceId()) { - int meyPinId = frame->data[0]; bool state = frame->data[1] > 0; @@ -220,23 +226,16 @@ void HandleTriggerMeypinCanPackage(can_frame *frame) } } -void HandleSwitchTriggeredCanPackage(can_frame *frame) +void HandleRules(can_frame *frame) { if (GetPackageType(frame->can_id) == SWITCH_TRIGGERED_CAN_ID) - { for (int i = 0; i < sizeof(Rules) / sizeof(Rule); i++) - { - 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, Rules[i].inverse ); - } - } - } + if ( Rules[i].sourceDevId == GetDeviceId(frame->can_id) && Rules[i].sourceMeyPinId == frame->data[0] ) + HandleRule(Rules[i].targetMeyPinId, _frame.data[1], Rules[i].trigger, Rules[i].inverse ); } -void ProcessTriggerSwitchPackage( byte meyPinId, byte state, bool asTrigger, bool inverse) +void HandleRule(byte meyPinId, byte state, bool asTrigger, bool inverse) { - int pinState = state > 0; if (inverse) pinState = !pinState;