#include ; #include ; const byte SoftwareVersionHigh = 3; const byte SoftwareVersionLow = 0; 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 DEBUG_CAN_ID = 0x000; struct can_frame _frame; struct Rule { uint16_t sourceDevId; uint8_t sourceMeyPinId; uint16_t targetDevId; byte targetMeyPinId; bool toggle; bool inverse; void InitSimple(uint16_t sourceDevId, uint8_t sourceMeyPinId, uint16_t targetDevId, byte targetMeyPinId) { this->sourceDevId = sourceDevId; this->sourceMeyPinId = sourceMeyPinId; this->targetDevId = targetDevId; this->targetMeyPinId = targetMeyPinId; toggle = false; inverse = false; } void InitToggle(uint16_t sourceDevId, uint8_t sourceMeyPinId, uint16_t targetDevId, byte targetMeyPinId) { this->sourceDevId = sourceDevId; this->sourceMeyPinId = sourceMeyPinId; this->targetDevId = targetDevId; this->targetMeyPinId = targetMeyPinId; toggle = true; inverse = false; } void InitToggleInverse(uint16_t sourceDevId, uint8_t sourceMeyPinId, uint16_t targetDevId, byte targetMeyPinId) { this->sourceDevId = sourceDevId; this->sourceMeyPinId = sourceMeyPinId; this->targetDevId = targetDevId; this->targetMeyPinId = targetMeyPinId; toggle = true; inverse = true; } } ; 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; struct RemotePinInfo { const byte MAX_REMOTE_PIN_COUNT = 50; uint16_t DeviceId = 0; // the id of the device uint16_t pinState = 0; // bitmap of 8 MeyPin states of the device. 0000 0100, MeyPin #3 is HIGH in this example RemotePinInfo *next = NULL; bool getPinState(byte meyPin) { return (this->pinState >> (meyPin - 1)) & 1; } void setPinState(byte meyPin, bool state) { if (state) this->pinState = this->pinState | (1 << (meyPin - 1)); // 0001 0000 else this->pinState = this->pinState & (~(1 << (meyPin - 1))); // 1110 1111 -> not } int16_t Count() { if (this->next == NULL) return 1; return this->next->Count() + 1; } RemotePinInfo* FindOrAdd(uint16_t deviceId, byte count = 0) { if (count > MAX_REMOTE_PIN_COUNT) return NULL; if (this->DeviceId == 0 && this->pinState == 0) { this->DeviceId = deviceId; this->pinState = 0; } if (this->DeviceId == deviceId) { //ToggleDebug(); return this; } if (next != NULL) { return next->FindOrAdd(deviceId, count + 1); } RemotePinInfo *theNext = new RemotePinInfo; theNext->DeviceId = deviceId; theNext->pinState = 0; theNext->next = NULL; this->next = theNext; return this->next; } }; struct RemotePinInfo remotePinInfos = RemotePinInfo(); PinState MeyPins[8]; bool flag = false; int32_t myDeviceId; Rule Rules[7]; 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].InitToggleInverse(0x051F, 5, 0x3D2D, 4); // Lichtschalter Wohnzimmer Licht 1 Rules[1] = Rule(); Rules[1].InitToggleInverse(0x051F, 5, 0x3D2D, 3); // Lichtschalter Wohnzimmer Licht 2 Rules[2] = Rule(); Rules[2].InitToggle(0x05df, 1, 0x3D2D, 4); // Licht 1 von Terassenschalter Rules[3] = Rule(); Rules[3].InitToggle(0x05df, 1, 0x3D2D, 3); // Licht 2 von Terassenschalter Rules[4] = Rule(); Rules[4].InitToggle(0x0769, 1, 0x3D2D, 7); // Eingangstür Papa Büro Rules[5] = Rule(); Rules[5].InitToggle(0x0769, 2,0x3D2D, 3); // Licht 1 Wochzimmer Eingangstür Rules[6] = Rule(); Rules[6].InitToggle(0x0769, 2,0x3D2D, 4); // Licht 2 Wochzimmer Eingangstür MeyPins[0] = PinState(); MeyPins[0].Init(PIN_PC7, (byte) 1); MeyPins[1] = PinState(); MeyPins[1].Init(PIN_PC6, (byte) 2); MeyPins[2] = PinState(); MeyPins[2].Init(PIN_PC5, (byte) 3); MeyPins[3] = PinState(); MeyPins[3].Init(PIN_PC4, (byte) 4); MeyPins[4] = PinState(); MeyPins[4].Init(PIN_PC3, (byte) 5); MeyPins[5] = PinState(); MeyPins[5].Init(PIN_PC2, (byte) 6); MeyPins[6] = PinState(); 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(); mcp2515_1.reset(); mcp2515_1.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz mcp2515_1.setNormalMode(); 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(); //delay(20000); for (int i = 0; i < sizeof(MeyPins) / sizeof(PinState); i++) { pinMode(MeyPins[i].pin_id, OUTPUT); digitalWrite(MeyPins[i].pin_id, LOW); } CalculateMyDeviceId(); SendSerialPackage(&mcp2515_0); SendSerialPackage(&mcp2515_1); SendSerialPackage(&mcp2515_2); SendSerialPackage(&mcp2515_3); digitalWrite(PIN_PC6, HIGH); } 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(&_frame); } 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(&_frame); } 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(&_frame); } 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(&_frame); } delay(10); } void HandleFrame(can_frame *frame) { HandleMeyPinTriggeredCanPackage(frame); HandleTriggerMeypinCanPackage(frame); // Handle rules needs to be the last call HandleRules(frame); } // this method will save the state of the triggered pin to be present for the rules void HandleMeyPinTriggeredCanPackage(can_frame *frame) { if (GetPackageType(frame->can_id) == SWITCH_TRIGGERED_CAN_ID) { RemotePinInfo *currentPinState = remotePinInfos.FindOrAdd(GetDeviceId(frame->can_id) ); if (currentPinState == NULL) { return; } currentPinState->setPinState(frame->data[0], frame->data[1]); } } void HandleTriggerMeypinCanPackage(can_frame *frame) { if (GetPackageType(frame->can_id) == TRIGGER_SWITCH_CAN_ID && (GetDeviceId(frame->can_id) == GetMyDeviceId())) { int meyPinId = frame->data[2]; bool state = frame->data[3] > 0; PinState *adressedPin; for (int i = 0; i < sizeof(MeyPins) / sizeof(PinState); i++) if (MeyPins[i].meyPinId == meyPinId) { adressedPin = &MeyPins[i]; break; } if (adressedPin != NULL) { if (adressedPin->is_input == true) { pinMode(adressedPin->pin_id, OUTPUT); adressedPin->is_input = false; } adressedPin->pin_state = state; digitalWrite(adressedPin->pin_id, state); } } } void HandleRules(can_frame *frame) { 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(canId)) if (Rules[i].sourceMeyPinId == meyPin || Rules[i].sourceMeyPinId == 255) HandleRule(&Rules[i], dState ); } } } void HandleRule(Rule *rule, byte state) { struct RemotePinInfo *currentPinState = remotePinInfos.FindOrAdd(rule->targetDevId); if (currentPinState == NULL) { return; } int pinState = state > 0; if (rule->inverse) pinState = !pinState; if (rule->toggle) { pinState = (currentPinState->getPinState(rule->targetMeyPinId) ^ true) > 0; } SendDoTriggerSwitchCanPackage(&mcp2515_0, rule->targetDevId, rule->targetMeyPinId, pinState); 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; HandleFrame(&_frame); currentPinState->setPinState(rule->targetMeyPinId, 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) | myDeviceId | CAN_EFF_FLAG; } void CalculateMyDeviceId() { myDeviceId = (GetDeviceIdHigh() << 8) | GetDeviceIdLow(); } uint32_t GetMyDeviceId() { return myDeviceId; } byte GetDeviceIdLow() { return (SIGROW.SERNUM0 ^ CircularShift(SIGROW.SERNUM2) << 1 ^ CircularShift( CircularShift(SIGROW.SERNUM4)) ^ CircularShift( CircularShift( CircularShift(SIGROW.SERNUM6))) ^ CircularShift( CircularShift( CircularShift( CircularShift(SIGROW.SERNUM8))))); } byte GetDeviceIdHigh() { return (SIGROW.SERNUM1 ^ CircularShift(SIGROW.SERNUM3) << 1 ^ CircularShift( CircularShift(SIGROW.SERNUM5)) ^ CircularShift( CircularShift( CircularShift(SIGROW.SERNUM7))) ^ CircularShift( CircularShift( CircularShift( CircularShift(SIGROW.SERNUM9))))); } void SendSerialPackage(MCP2515 *interface) { _frame.can_id = CreateCanId(HELP_PACKAGE_CAN_ID); _frame.can_dlc = 4; _frame.data[0] = SoftwareVersionHigh; _frame.data[1] = SoftwareVersionLow; _frame.data[2] = HardwareVersionHigh; _frame.data[3] = HardwareVersionLow; interface->sendMessage(MCP2515::TXB1, &_frame); } void SendSwitchedTriggeredCanPackage(MCP2515 *interface, byte pinId, int state) { _frame.can_id = CreateCanId(SWITCH_TRIGGERED_CAN_ID); _frame.can_dlc = 2; _frame.data[0] = pinId; _frame.data[1] = state; interface->sendMessage(MCP2515::TXB1, &_frame); } void SendDoTriggerSwitchCanPackage(MCP2515 *interface, uint16_t targetCanId, byte pinId, byte state) { _frame.can_id = CreateCanId(TRIGGER_SWITCH_CAN_ID); _frame.can_dlc = 4; _frame.data[1] = (targetCanId & 0xFF00) >> 8; _frame.data[0] = targetCanId & 0xFF; _frame.data[2] = pinId; _frame.data[3] = state; interface->sendMessage(MCP2515::TXB1, &_frame); }