This commit is contained in:
Meydin87
2023-05-14 19:57:26 +02:00
parent 34ac0aabc2
commit 1d2d148d11
2 changed files with 19 additions and 19 deletions

View File

@@ -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;