Extended Rules defined for Switch / UP Fixed
This commit is contained in:
@@ -32,4 +32,18 @@ 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
|
||||
|
||||
|
||||
|
||||
0x010: Request Remote Pin State
|
||||
-> Broadcast
|
||||
---------------------------------
|
||||
2 bytes | TargetDeviceId -> The id of the board to request the remote pin states from. This needs to be a control device (not UP)
|
||||
1 byte | 0: Request
|
||||
| 1: Response
|
||||
DAT |
|
||||
-- n Items of format ------------
|
||||
* 2 byte: DeviceId
|
||||
* 1 byte: The State
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ 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;
|
||||
|
||||
@@ -17,7 +18,7 @@ typedef struct
|
||||
byte sourceMeyPinId;
|
||||
uint32_t targetDevId;
|
||||
byte targetMeyPinId;
|
||||
bool trigger;
|
||||
bool toggle;
|
||||
bool inverse;
|
||||
|
||||
void InitSimple(uint32_t sourceDevId, byte sourceMeyPinId, uint32_t targetDevId, byte targetMeyPinId)
|
||||
@@ -26,27 +27,27 @@ typedef struct
|
||||
this->sourceMeyPinId = sourceMeyPinId;
|
||||
this->targetDevId = targetDevId;
|
||||
this->targetMeyPinId = targetMeyPinId;
|
||||
trigger = false;
|
||||
toggle = false;
|
||||
inverse = false;
|
||||
}
|
||||
|
||||
void InitTrigger(uint32_t sourceDevId, byte sourceMeyPinId, uint32_t targetDevId, byte targetMeyPinId)
|
||||
void InitToggle(uint32_t sourceDevId, byte sourceMeyPinId, uint32_t targetDevId, byte targetMeyPinId)
|
||||
{
|
||||
this->sourceDevId = sourceDevId;
|
||||
this->sourceMeyPinId = sourceMeyPinId;
|
||||
this->targetDevId = targetDevId;
|
||||
this->targetMeyPinId = targetMeyPinId;
|
||||
trigger = true;
|
||||
toggle = true;
|
||||
inverse = false;
|
||||
}
|
||||
|
||||
void InitTriggerInverse(uint32_t sourceDevId, byte sourceMeyPinId, uint32_t targetDevId, byte targetMeyPinId)
|
||||
void InitToggleInverse(uint32_t sourceDevId, byte sourceMeyPinId, uint32_t targetDevId, byte targetMeyPinId)
|
||||
{
|
||||
this->sourceDevId = sourceDevId;
|
||||
this->sourceMeyPinId = sourceMeyPinId;
|
||||
this->targetDevId = targetDevId;
|
||||
this->targetMeyPinId = targetMeyPinId;
|
||||
trigger = true;
|
||||
toggle = true;
|
||||
inverse = true;
|
||||
}
|
||||
} Rule;
|
||||
@@ -68,13 +69,74 @@ typedef struct
|
||||
}
|
||||
} 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[0];
|
||||
Rule Rules[8];
|
||||
|
||||
MCP2515 mcp2515_0(PIN_PA2);
|
||||
MCP2515 mcp2515_1(PIN_PA3);
|
||||
@@ -85,7 +147,22 @@ 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
|
||||
Rules[0].InitToggle(0x1EC3, 1, 0x3D2D, 2); // should switch MeyPin #2 of Device 3D2D to the settet state of MeyPin #1 of 0x055F is changed
|
||||
Rules[1] = Rule();
|
||||
Rules[1].InitToggle(0xD8C0, 2, 0x3D2D, 2); // should switch MeyPin #2 of Device 3D2D to the settet state of MeyPin #1 of 0x055F is changed
|
||||
Rules[2] = Rule();
|
||||
Rules[2].InitToggle(0x1EC2, 3, 0x3D2D, 3); // should switch MeyPin #2 of Device 3D2D to the settet state of MeyPin #1 of 0x055F is changed
|
||||
Rules[3] = Rule();
|
||||
Rules[3].InitToggle(0x1EC2, 4, 0x3D2D, 4); // should switch MeyPin #2 of Device 3D2D to the settet state of MeyPin #1 of 0x055F is changed
|
||||
Rules[4] = Rule();
|
||||
Rules[4].InitToggle(0x1EC2, 5, 0x3D2D, 5); // should switch MeyPin #2 of Device 3D2D to the settet state of MeyPin #1 of 0x055F is changed
|
||||
Rules[5] = Rule();
|
||||
Rules[5].InitToggle(0x1EC2, 6, 0x3D2D, 6); // should switch MeyPin #2 of Device 3D2D to the settet state of MeyPin #1 of 0x055F is changed
|
||||
Rules[6] = Rule();
|
||||
Rules[6].InitToggle(0x1EC2, 7, 0x3D2D, 7); // should switch MeyPin #2 of Device 3D2D to the settet state of MeyPin #1 of 0x055F is changed
|
||||
Rules[7] = Rule();
|
||||
Rules[7].InitToggle(0x1EC2, 8, 0x3D2D, 8); // 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,13 +228,19 @@ void setup() {
|
||||
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
bool debugState = false;
|
||||
|
||||
|
||||
void loop() {
|
||||
void ToggleDebug()
|
||||
{
|
||||
delay(500);
|
||||
byte debugState = digitalRead(PIN_PC7)^true;
|
||||
if (debugState)
|
||||
digitalWrite(PIN_PC7, HIGH);
|
||||
else
|
||||
digitalWrite(PIN_PC7, LOW);
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (mcp2515_0.readMessage(&_frame) == MCP2515::ERROR_OK)
|
||||
{
|
||||
mcp2515_1.sendMessage(MCP2515::TXB1, &_frame);
|
||||
@@ -186,23 +269,52 @@ void loop() {
|
||||
mcp2515_2.sendMessage(MCP2515::TXB1, &_frame);
|
||||
HandleFrame(&_frame);
|
||||
}
|
||||
delay(20);
|
||||
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)
|
||||
{
|
||||
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 (GetDeviceId(frame->can_id) == GetMyDeviceId())
|
||||
if (GetPackageType(frame->can_id) == TRIGGER_SWITCH_CAN_ID && (GetDeviceId(frame->can_id) == GetMyDeviceId()))
|
||||
{
|
||||
int meyPinId = frame->data[0];
|
||||
bool state = frame->data[1] > 0;
|
||||
int meyPinId = frame->data[2];
|
||||
bool state = frame->data[3] > 0;
|
||||
|
||||
PinState *adressedPin;
|
||||
for (int i = 0; i < sizeof(MeyPins) / sizeof(PinState); i++)
|
||||
@@ -229,35 +341,46 @@ void HandleTriggerMeypinCanPackage(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 == GetDeviceId(frame->can_id) && Rules[i].sourceMeyPinId == frame->data[0] )
|
||||
HandleRule(Rules[i].targetMeyPinId, _frame.data[1], Rules[i].trigger, Rules[i].inverse );
|
||||
}
|
||||
|
||||
void HandleRule(byte meyPinId, byte state, bool asTrigger, bool inverse)
|
||||
{
|
||||
int pinState = state > 0;
|
||||
if (inverse)
|
||||
pinState = !pinState;
|
||||
|
||||
PinState *foundPinId = 0;
|
||||
for (int i = 0; i < sizeof(MeyPins) / sizeof(PinState); i++)
|
||||
{
|
||||
if (MeyPins[i].meyPinId == meyPinId)
|
||||
|
||||
for (int i = 0; i < sizeof(Rules) / sizeof(Rule); i++)
|
||||
{
|
||||
foundPinId = &MeyPins[i];
|
||||
break;
|
||||
if ( Rules[i].sourceDevId == GetDeviceId(frame->can_id))
|
||||
if (Rules[i].sourceMeyPinId == frame->data[0])
|
||||
HandleRule(&Rules[i], _frame.data[1] );
|
||||
}
|
||||
}
|
||||
if (foundPinId == 0)return;
|
||||
}
|
||||
|
||||
if (asTrigger)
|
||||
void HandleRule(Rule *rule, byte state)
|
||||
{
|
||||
struct RemotePinInfo *currentPinState = remotePinInfos.FindOrAdd(rule->targetDevId);
|
||||
|
||||
if (currentPinState == NULL)
|
||||
{
|
||||
pinState = (foundPinId->pin_state ^ true) > 0;
|
||||
return;
|
||||
}
|
||||
|
||||
digitalWrite(foundPinId->pin_id, pinState);
|
||||
foundPinId->pin_state = pinState;
|
||||
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)
|
||||
@@ -318,22 +441,41 @@ void SendSerialPackage(MCP2515 *interface)
|
||||
interface->sendMessage(MCP2515::TXB1, &_frame);
|
||||
}
|
||||
|
||||
void SendSwitchedTriggeredCanPackage(byte pinId, int state)
|
||||
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;
|
||||
mcp2515_1.sendMessage(MCP2515::TXB1, &_frame);
|
||||
interface->sendMessage(MCP2515::TXB1, &_frame);
|
||||
}
|
||||
|
||||
void SendDoTriggerSwitchCanPackage(uint32_t targetCanId, byte pinId, byte state)
|
||||
|
||||
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[0] = targetCanId & 0xFF;
|
||||
_frame.data[1] = (targetCanId & 0xFF00) >> 8;
|
||||
_frame.data[0] = targetCanId & 0xFF;
|
||||
_frame.data[2] = pinId;
|
||||
_frame.data[3] = state;
|
||||
mcp2515_1.sendMessage(MCP2515::TXB1, &_frame);
|
||||
interface->sendMessage(MCP2515::TXB1, &_frame);
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
interface->sendMessage(MCP2515::TXB1, &frame);
|
||||
}
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
|
||||
|
||||
struct can_frame _frame;
|
||||
MCP2515 mcp2515(PIN_PA3);
|
||||
const byte SoftwareVersionHigh = 2;
|
||||
MCP2515 mcp2515(PIN_PD4);
|
||||
const byte SoftwareVersionHigh = 3;
|
||||
const byte SoftwareVersionLow = 0;
|
||||
const byte HardwareVersionHigh = 3;
|
||||
const byte HardwareVersionHigh = 5;
|
||||
const byte HardwareVersionLow = 0;
|
||||
|
||||
typedef struct
|
||||
typedef struct PinState
|
||||
{
|
||||
int pin_id;
|
||||
bool pin_state;
|
||||
@@ -24,57 +24,56 @@ typedef struct
|
||||
this->is_input = true;
|
||||
this->meyPinId = meyPinId;
|
||||
}
|
||||
} PinState;
|
||||
} ;
|
||||
|
||||
int PinCount = 8;
|
||||
PinState PinPD2[8];
|
||||
byte DeviceId[2];
|
||||
PinState MeyPins[8];
|
||||
int16_t myDeviceId;
|
||||
|
||||
void setup() {
|
||||
SPI.begin();
|
||||
|
||||
|
||||
PinPD2[0] = PinState();
|
||||
PinPD2[0].Init(PIN_PC7, (byte) 1);
|
||||
MeyPins[0] = PinState();
|
||||
MeyPins[0].Init(PIN_PD0, (byte) 1);
|
||||
|
||||
PinPD2[1] = PinState();
|
||||
PinPD2[1].Init(PIN_PD0, (byte) 2);
|
||||
MeyPins[1] = PinState();
|
||||
MeyPins[1].Init(PIN_PD2, (byte) 2);
|
||||
|
||||
PinPD2[2] = PinState();
|
||||
PinPD2[2].Init(PIN_PD1, (byte) 3);
|
||||
MeyPins[2] = PinState();
|
||||
MeyPins[2].Init(PIN_PD5, (byte) 3);
|
||||
|
||||
PinPD2[3] = PinState();
|
||||
PinPD2[3].Init(PIN_PD2, (byte) 4);
|
||||
MeyPins[3] = PinState();
|
||||
MeyPins[3].Init(PIN_PD7, (byte) 4);
|
||||
|
||||
PinPD2[4] = PinState();
|
||||
PinPD2[4].Init(PIN_PD6, (byte) 5);
|
||||
MeyPins[4] = PinState();
|
||||
MeyPins[4].Init(PIN_PC7, (byte) 5);
|
||||
|
||||
PinPD2[5] = PinState();
|
||||
PinPD2[5].Init(PIN_PD5, (byte) 6);
|
||||
MeyPins[5] = PinState();
|
||||
MeyPins[5].Init(PIN_PD1, (byte) 6);
|
||||
|
||||
PinPD2[6] = PinState();
|
||||
PinPD2[6].Init(PIN_PD4, (byte) 7);
|
||||
MeyPins[6] = PinState();
|
||||
MeyPins[6].Init(PIN_PD3, (byte) 7);
|
||||
|
||||
PinPD2[7] = PinState();
|
||||
PinPD2[7].Init(PIN_PD3, (byte) 8);
|
||||
MeyPins[7] = PinState();
|
||||
MeyPins[7].Init(PIN_PD6, (byte) 8);
|
||||
|
||||
|
||||
|
||||
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLA, CLKCTRL.MCLKCTRLA | 1 << 7);
|
||||
delay(20);
|
||||
mcp2515.reset();
|
||||
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
|
||||
mcp2515.setNormalMode();
|
||||
|
||||
for (int i = 0; i <= PinCount - 1; i++)
|
||||
{
|
||||
pinMode(PinPD2[i].pin_id, INPUT_PULLUP);
|
||||
PinPD2[i].pin_state = ReadPin(&PinPD2[i]);
|
||||
pinMode(MeyPins[i].pin_id, INPUT_PULLUP);
|
||||
MeyPins[i].pin_state = ReadPin(&MeyPins[i]);
|
||||
}
|
||||
|
||||
|
||||
DeviceId[0] = GetDeviceIdLow();
|
||||
DeviceId[1] = GetDeviceIdHigh();
|
||||
|
||||
CalculateMyDeviceId();
|
||||
SendSerialPackage();
|
||||
}
|
||||
|
||||
@@ -83,8 +82,8 @@ void loop()
|
||||
{
|
||||
for (int i = 0; i <= PinCount - 1; i++)
|
||||
{
|
||||
if (CheckPinStatus(&PinPD2[i]))
|
||||
SendSwitchedTriggeredCanPackage(PinPD2[i].meyPinId, PinPD2[i].pin_state);
|
||||
if (CheckPinStatus(&MeyPins[i]))
|
||||
SendSwitchedTriggeredCanPackage(MeyPins[i].meyPinId, MeyPins[i].pin_state);
|
||||
}
|
||||
|
||||
if (mcp2515.readMessage(&_frame) == MCP2515::ERROR_OK)
|
||||
@@ -98,9 +97,9 @@ void loop()
|
||||
|
||||
PinState *adressedPin;
|
||||
for (int i = 0; i <= PinCount - 1; i++)
|
||||
if (PinPD2[i].meyPinId == meyPinId)
|
||||
if (MeyPins[i].meyPinId == meyPinId)
|
||||
{
|
||||
adressedPin = &PinPD2[i];
|
||||
adressedPin = &MeyPins[i];
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -195,12 +194,17 @@ 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) | GetMyDeviceId() | CAN_EFF_FLAG;
|
||||
}
|
||||
|
||||
uint32_t GetMyDeviceId()
|
||||
void CalculateMyDeviceId()
|
||||
{
|
||||
return ( DeviceId[0] << 8) | (DeviceId[1]);
|
||||
myDeviceId = (GetDeviceIdHigh() << 8) | GetDeviceIdLow();
|
||||
}
|
||||
|
||||
uint16_t GetMyDeviceId()
|
||||
{
|
||||
return myDeviceId;
|
||||
}
|
||||
|
||||
byte GetDeviceIdLow() {
|
||||
|
||||
Reference in New Issue
Block a user