WIP
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#include <SPI.h>;
|
||||
#include <mcp2515.h>;
|
||||
|
||||
const byte SoftwareVersionHigh = 2;
|
||||
const byte SoftwareVersionHigh = 3;
|
||||
const byte SoftwareVersionLow = 0;
|
||||
const byte HardwareVersionHigh = 6;
|
||||
const byte HardwareVersionLow = 0;
|
||||
@@ -65,14 +65,12 @@ typedef struct
|
||||
} PinState;
|
||||
|
||||
|
||||
int PinCount = 8;
|
||||
PinState MeyPins[8];
|
||||
|
||||
bool flag = false;
|
||||
byte DeviceId[1];
|
||||
int32_t myDeviceId;
|
||||
|
||||
Rule Rules[10];
|
||||
int RulesCount = 10 ;
|
||||
|
||||
MCP2515 mcp2515_0(PIN_PA2);
|
||||
MCP2515 mcp2515_1(PIN_PA3);
|
||||
@@ -82,32 +80,6 @@ MCP2515 mcp2515_3(PIN_PB1);
|
||||
void setup() {
|
||||
SPI.begin();
|
||||
|
||||
Rules[0] = Rule();
|
||||
Rules[0].InitTriggerInverse(0x051F, 5, 4); // Lichtschalter Licht 1
|
||||
Rules[1] = Rule();
|
||||
Rules[1].InitTriggerInverse(0x051F, 5, 5); // Lichtschalter Licht 2
|
||||
|
||||
Rules[2] = Rule();
|
||||
Rules[2].InitTrigger(0x051F, 4, 6); // Rolle runter von Tür
|
||||
Rules[3] = Rule();
|
||||
Rules[3].InitTrigger(0x051F, 3, 7); // Rollo hoch von Tür
|
||||
|
||||
Rules[4] = Rule();
|
||||
Rules[4].InitTrigger(0x146A, 1, 4); // Licht 1 von Terassenschalter
|
||||
Rules[5] = Rule();
|
||||
Rules[5].InitTrigger(0x146A, 1, 5); // Licht 2 von Terassenschalter
|
||||
Rules[6] = Rule();
|
||||
Rules[6].InitTrigger(0x0769, 1, 5); // Licht von Engang unten
|
||||
Rules[7] = Rule();
|
||||
Rules[7].InitTrigger(0x0769, 2, 2); // Licht von Engang unten
|
||||
|
||||
Rules[8] = Rule();
|
||||
Rules[8].InitTrigger(0x05DF, 1, 4); // OK Flurlicht
|
||||
Rules[9] = Rule();
|
||||
Rules[9].InitTrigger(0x05DF, 1, 5); // OK Flurlicht
|
||||
|
||||
Rules[10] = Rule();
|
||||
Rules[10].InitTrigger(0x1177, 1, 2); // OK Flurlicht
|
||||
|
||||
MeyPins[0] = PinState();
|
||||
MeyPins[0].Init(PIN_PC7, (byte) 1);
|
||||
@@ -155,7 +127,7 @@ void setup() {
|
||||
mcp2515_3.setNormalMode();
|
||||
|
||||
//delay(20000);
|
||||
for (int i = 0; i < PinCount; i++)
|
||||
for (int i = 0; i < sizeof(MeyPins) / sizeof(PinState); i++)
|
||||
{
|
||||
pinMode(MeyPins[i].pin_id, OUTPUT);
|
||||
|
||||
@@ -164,6 +136,12 @@ void setup() {
|
||||
digitalWrite(MeyPins[i].pin_id, LOW);
|
||||
|
||||
}
|
||||
CalculateMyDeviceId();
|
||||
|
||||
SendSerialPackage(&mcp2515_0);
|
||||
SendSerialPackage(&mcp2515_1);
|
||||
SendSerialPackage(&mcp2515_2);
|
||||
SendSerialPackage(&mcp2515_3);
|
||||
|
||||
}
|
||||
|
||||
@@ -174,67 +152,89 @@ bool debugState = false;
|
||||
void loop() {
|
||||
|
||||
|
||||
SendSerialPackage(mcp2515_0);
|
||||
SendSerialPackage(mcp2515_1);
|
||||
SendSerialPackage(mcp2515_2);
|
||||
SendSerialPackage(mcp2515_3);
|
||||
|
||||
debugState = !debugState;
|
||||
if (debugState)
|
||||
digitalWrite(PIN_PC7, HIGH);
|
||||
else
|
||||
digitalWrite(PIN_PC7, LOW);
|
||||
|
||||
delay(1000);
|
||||
return;
|
||||
|
||||
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();
|
||||
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();
|
||||
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();
|
||||
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();
|
||||
HandleFrame(&_frame);
|
||||
}
|
||||
delay(20);
|
||||
}
|
||||
|
||||
void HandleFrame()
|
||||
void HandleFrame(can_frame *frame)
|
||||
{
|
||||
HandleSwitchTriggeredCanPackage(frame);
|
||||
HandleTriggerMeypinCanPackage(frame);
|
||||
|
||||
if (GetPackageType(_frame.can_id) == SWITCH_TRIGGERED_CAN_ID)
|
||||
}
|
||||
|
||||
void HandleTriggerMeypinCanPackage(can_frame *frame)
|
||||
{
|
||||
if (GetDeviceId(frame->can_id) == GetMyDeviceId())
|
||||
{
|
||||
for (int i = 0; i <= RulesCount - 1; i++)
|
||||
|
||||
int meyPinId = frame->data[0];
|
||||
bool state = frame->data[1] > 0;
|
||||
|
||||
PinState *adressedPin;
|
||||
for (int i = 0; i < sizeof(MeyPins) / sizeof(PinState); i++)
|
||||
if (MeyPins[i].meyPinId == meyPinId)
|
||||
{
|
||||
if (Rules[i].sourceDevId == 0x0000 || ( Rules[i].sourceDevId == GetDeviceId(_frame.can_id) && Rules[i].sourceMeyPinId == _frame.data[0] ))
|
||||
adressedPin = &MeyPins[i];
|
||||
break;
|
||||
}
|
||||
|
||||
if (adressedPin != NULL)
|
||||
{
|
||||
ProcessTriggerSwitchPackage(Rules[i].targetMeyPinId, _frame.data[1], Rules[i].trigger, Rules[i].inverse );
|
||||
if (adressedPin->is_input == true)
|
||||
{
|
||||
pinMode(adressedPin->pin_id, OUTPUT);
|
||||
adressedPin->is_input = false;
|
||||
}
|
||||
adressedPin->pin_state = state;
|
||||
digitalWrite(adressedPin->pin_id, state);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void HandleSwitchTriggeredCanPackage(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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessTriggerSwitchPackage(byte meyPinId, byte state, bool asTrigger, bool inverse)
|
||||
void ProcessTriggerSwitchPackage( byte meyPinId, byte state, bool asTrigger, bool inverse)
|
||||
{
|
||||
|
||||
int pinState = state > 0;
|
||||
@@ -242,7 +242,7 @@ void ProcessTriggerSwitchPackage(byte meyPinId, byte state, bool asTrigger, bool
|
||||
pinState = !pinState;
|
||||
|
||||
PinState *foundPinId = 0;
|
||||
for (int i = 0; i < PinCount; i++)
|
||||
for (int i = 0; i < sizeof(MeyPins) / sizeof(PinState); i++)
|
||||
{
|
||||
if (MeyPins[i].meyPinId == meyPinId)
|
||||
{
|
||||
@@ -278,11 +278,36 @@ uint32_t GetPackageType(uint32_t canFrameId)
|
||||
|
||||
uint32_t CreateCanId(uint32_t commandId)
|
||||
{
|
||||
return ((commandId & 0xFFF) * 0x10000) | ( DeviceId[0] << 8) | (DeviceId[1]) | CAN_EFF_FLAG;
|
||||
return ((commandId & 0xFFF) * 0x10000) | myDeviceId | CAN_EFF_FLAG;
|
||||
}
|
||||
|
||||
void CalculateMyDeviceId()
|
||||
{
|
||||
myDeviceId = (GetDeviceIdHigh() << 8) | GetDeviceIdLow();
|
||||
}
|
||||
|
||||
void SendSerialPackage(MCP2515 ss)
|
||||
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;
|
||||
@@ -291,7 +316,7 @@ void SendSerialPackage(MCP2515 ss)
|
||||
_frame.data[1] = SoftwareVersionLow;
|
||||
_frame.data[2] = HardwareVersionHigh;
|
||||
_frame.data[3] = HardwareVersionLow;
|
||||
mcp2515_0.sendMessage(MCP2515::TXB1, &_frame);
|
||||
interface->sendMessage(MCP2515::TXB1, &_frame);
|
||||
}
|
||||
|
||||
void SendSwitchedTriggeredCanPackage(byte pinId, int state)
|
||||
|
||||
Reference in New Issue
Block a user