WIP / Refactored Switch Software

This commit is contained in:
Meydin87
2023-07-08 13:08:06 +02:00
parent 8141386359
commit 58325fdd88
26 changed files with 3687 additions and 3888 deletions

View File

@@ -1,527 +1,69 @@
#include <SPI.h>;
#include <mcp2515.h>;
#include "MeyCan.h";
#include "MeyRule.h";
const byte SoftwareVersionHigh = 3;
const byte SoftwareVersionLow = 1;
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;
typedef struct
{
uint32_t sourceDevId;
byte sourceMeyPinId;
uint32_t targetDevId;
byte targetMeyPinId;
bool toggle;
bool inverse;
void InitSimple(uint32_t sourceDevId, byte sourceMeyPinId, uint32_t targetDevId, byte targetMeyPinId)
{
this->sourceDevId = sourceDevId;
this->sourceMeyPinId = sourceMeyPinId;
this->targetDevId = targetDevId;
this->targetMeyPinId = targetMeyPinId;
toggle = false;
inverse = false;
}
void InitToggle(uint32_t sourceDevId, byte sourceMeyPinId, uint32_t targetDevId, byte targetMeyPinId)
{
this->sourceDevId = sourceDevId;
this->sourceMeyPinId = sourceMeyPinId;
this->targetDevId = targetDevId;
this->targetMeyPinId = targetMeyPinId;
toggle = true;
inverse = false;
}
void InitToggleInverse(uint32_t sourceDevId, byte sourceMeyPinId, uint32_t targetDevId, byte targetMeyPinId)
{
this->sourceDevId = sourceDevId;
this->sourceMeyPinId = sourceMeyPinId;
this->targetDevId = targetDevId;
this->targetMeyPinId = targetMeyPinId;
toggle = 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;
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;
int16_t myDeviceId;
Rule Rules[16];
MCP2515 mcp2515_0(PIN_PA2);
MCP2515 mcp2515_1(PIN_PA3);
MCP2515 mcp2515_2(PIN_PB0);
MCP2515 mcp2515_3(PIN_PB1);
struct can_frame incomingCanFrame;
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, 6); // Eingangstür Flur Licht 2
Rules[5] = Rule();
Rules[5].InitToggle(0x0769, 1, 0x9829, 1); //Eingangstür Flur Licht 1
Rules[6] = Rule();
Rules[6].InitSimple(0x051F, 3, 0x3D2D, 1); // Licht 1 Wochzimmer Eingangstür
Rules[7] = Rule();
Rules[7].InitSimple(0x051F, 4, 0x3D2D, 2); // Licht 2 Wochzimmer Eingangstür
Rules[8] = Rule();
Rules[8].InitToggle(0x1177, 1, 0x3D2D, 7); // Licht Papa Büro
Rules[9] = Rule();
Rules[9].InitToggle(0x05A1, 1, 0x3D2D, 6); // Flurlicht von Papas Büro
Rules[10] = Rule();
Rules[10].InitToggle(0x05A1, 1, 0x9829, 1); // Flurlicht von Papas Büro
Rules[11] = Rule();
Rules[11].InitToggle(0x01EF, 1, 0x3D2D, 6); // Flurlicht von Papas Büro
Rules[12] = Rule();
Rules[12].InitToggle(0x01EF, 1, 0x9829, 1); // Flurlicht von Papas Büro#
Rules[13] = Rule();
Rules[13].InitToggle(0x0196, 3, 0x3D2D, 6); // Flurlicht von Papas Büro
Rules[14] = Rule();
Rules[14].InitToggle(0x0196, 3, 0x9829, 1); // Flurlicht von Papas Büro
Rules[15] = Rule();
Rules[15].InitToggle(0x0632, 1, 0x9829, 5); // Flurlicht von Papas Büro
delay(10);
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);
AddToggleInverse(0x051F, 5, 0x3D2D, 4); // Lichtschalter Wohnzimmer Licht 1
AddToggleInverse(0x051F, 5, 0x3D2D, 3); // Lichtschalter Wohnzimmer Licht 2
AddToggle(0x05df, 1, 0x3D2D, 4); // Licht 1 von Terassenschalter
AddToggle(0x05df, 1, 0x3D2D, 3); // Licht 2 von Terassenschalter
AddToggle(0x0769, 1, 0x3D2D, 6); // Eingangstür Flur Licht 2
AddToggle(0x0769, 1, 0x9829, 1); //Eingangstür Flur Licht 1
AddSimple(0x051F, 3, 0x3D2D, 1); // Licht 1 Wochzimmer Eingangstür
AddSimple(0x051F, 4, 0x3D2D, 2); // Licht 2 Wochzimmer Eingangstür
AddToggle(0x1177, 1, 0x3D2D, 7); // Licht Papa Büro
AddToggle(0x05A1, 1, 0x3D2D, 6); // Flurlicht von Papas Büro
AddToggle(0x05A1, 1, 0x9829, 1); // Flurlicht von Papas Büro
AddToggle(0x01EF, 1, 0x3D2D, 6); // Flurlicht von Papas Büro
AddToggle(0x01EF, 1, 0x9829, 1); // Flurlicht von Papas Büro
AddToggle(0x0196, 3, 0x3D2D, 6); // Flurlicht von Papas Büro
AddToggle(0x0196, 3, 0x9829, 1); // Flurlicht von Papas Büro
AddToggle(0x0632, 1, 0x9829, 5); // Flurlicht von Papas Büro
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLA, CLKCTRL.MCLKCTRLA | 1 << 7);
delay(10); // a bit delay for mcp2515 to get the clock
mcp2515_0.reset();
mcp2515_0.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
mcp2515_0.setNormalMode();
SetMeyPin(0, 1, PIN_PC7);
SetMeyPin(1, 2, PIN_PC6);
SetMeyPin(2, 3, PIN_PC5);
SetMeyPin(3, 4, PIN_PC4);
SetMeyPin(4, 5, PIN_PC3);
SetMeyPin(5, 6, PIN_PC2);
SetMeyPin(6, 7, PIN_PC1);
SetMeyPin(7, 8, PIN_PC0);
mcp2515_1.reset();
mcp2515_1.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
mcp2515_1.setNormalMode();
SetCanInterface(0, PIN_PA2);
SetCanInterface(1, PIN_PA3);
SetCanInterface(2, PIN_PB0);
SetCanInterface(3, PIN_PB1);
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 < 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);
}
bool debugState = false;
void ToggleDebug()
{
debugState = debugState ^ true;
if (debugState)
digitalWrite(PIN_PC1, HIGH);
else
digitalWrite(PIN_PC1, LOW);
SetupMeyCan();
}
void loop()
{
if (mcp2515_0.readMessage(&_frame) == MCP2515::ERROR_OK)
for (int i = 0; i < sizeof(canInterfaces) / sizeof(MCP2515*); i++)
{
DoSendCanPkg(&mcp2515_1, &_frame);
DoSendCanPkg(&mcp2515_2, &_frame);
DoSendCanPkg(&mcp2515_3, &_frame);
HandleFrame(&_frame);
}
if (mcp2515_1.readMessage(&_frame) == MCP2515::ERROR_OK)
{
DoSendCanPkg(&mcp2515_0, &_frame);
DoSendCanPkg(&mcp2515_2, &_frame);
DoSendCanPkg(&mcp2515_3, &_frame);
HandleFrame(&_frame);
}
if (mcp2515_2.readMessage(&_frame) == MCP2515::ERROR_OK)
{
DoSendCanPkg(&mcp2515_0, &_frame);
DoSendCanPkg(&mcp2515_1, &_frame);
DoSendCanPkg(&mcp2515_3, &_frame);
HandleFrame(&_frame);
}
if (mcp2515_3.readMessage(&_frame) == MCP2515::ERROR_OK)
{
DoSendCanPkg(&mcp2515_0, &_frame);
DoSendCanPkg(&mcp2515_1, &_frame);
DoSendCanPkg(&mcp2515_2, &_frame);;
HandleFrame(&_frame);
}
}
void HandleFrame(can_frame *frame)
{
// ToggleDebug();
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)
if (canInterfaces[i]->readMessage(&incomingCanFrame) == MCP2515::ERROR_OK)
{
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]);
}
}
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)
// A received CAN Frame is forwarded to all other MCPs given, but not to the receiving one (no echo )
for (int j = 0; j < sizeof(canInterfaces) / sizeof(MCP2515*); j++)
{
adressedPin = &MeyPins[i];
break;
if (j == i) continue; // thats for the echo
DoSendCanPkg(canInterfaces[j], &incomingCanFrame);
}
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)
{
if (GetPackageType(frame->can_id) == SWITCH_TRIGGERED_CAN_ID)
{
uint16_t deviceId = GetDeviceId(frame->can_id);
uint8_t dt = _frame.data[1];
uint8_t state = frame->data[0];
for (int i = 0; i < sizeof(Rules) / sizeof(Rule); i++)
{
if ( Rules[i].sourceDevId == deviceId)
if (Rules[i].sourceMeyPinId == state)
HandleRule(&Rules[i], dt );
HandleFrame(&incomingCanFrame);
HandleRules(&incomingCanFrame);
}
}
}
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);
}
uint16_t GetDeviceId(uint32_t canFrameId)
{
return canFrameId & 0xFFFF;
}
uint32_t GetPackageType(uint32_t canFrameId)
{
return (canFrameId / 0x10000) & 0xFFF;
}
uint32_t CreateCanId(uint16_t commandId)
{
return ((commandId & 0xFFF) * 0x10000) | myDeviceId | CAN_EFF_FLAG;
}
uint32_t CreateCanId(uint16_t commandId, uint16_t deviceId)
{
return ((commandId & 0xFFF) * 0x10000) | deviceId | CAN_EFF_FLAG;
}
void CalculateMyDeviceId()
{
myDeviceId = (GetDeviceIdHigh() << 8) | GetDeviceIdLow();
}
uint16_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;
DoSendCanPkg(interface, &_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;
DoSendCanPkg(interface, &_frame);
}
void SendDoTriggerSwitchCanPackage(MCP2515 *interface, uint16_t targetCanId, byte pinId, byte state)
{
_frame.can_id = CreateCanId(TRIGGER_SWITCH_CAN_ID, targetCanId);
_frame.can_dlc = 4;
_frame.data[0] = (targetCanId & 0xFF00) >> 8;
_frame.data[1] = targetCanId & 0xFF;
_frame.data[2] = pinId;
_frame.data[3] = state;
DoSendCanPkg(interface, &_frame);
}
void DoSendCanPkg(MCP2515 *interface, can_frame *frame)
{
// ToggleDebug();
byte cnt = 0;
while (interface->sendMessage(&_frame)) {
cnt++;
if (cnt > 10) return;
}
}
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];
}
DoSendCanPkg(interface, &frame);
}