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,12 +1,13 @@
#include <SPI.h>;
#include <mcp2515.h>;
struct can_frame _frame;
MCP2515 mcp2515(PIN_PD4);
const byte SoftwareVersionHigh = 3;
const byte SoftwareVersionHigh = 4;
const byte SoftwareVersionLow = 0;
const byte HardwareVersionHigh = 5;
const byte HardwareVersionHigh = 2;
const byte HardwareVersionLow = 0;
typedef struct PinState
@@ -33,30 +34,29 @@ int16_t myDeviceId;
void setup() {
SPI.begin();
MeyPins[0] = PinState();
MeyPins[0].Init(PIN_PD0, (byte) 1);
MeyPins[0].Init(PIN_PC7, (byte) 1);
MeyPins[1] = PinState();
MeyPins[1].Init(PIN_PD2, (byte) 2);
MeyPins[1].Init(PIN_PD0, (byte) 2);
MeyPins[2] = PinState();
MeyPins[2].Init(PIN_PD5, (byte) 3);
MeyPins[2].Init(PIN_PD1, (byte) 3);
MeyPins[3] = PinState();
MeyPins[3].Init(PIN_PD7, (byte) 4);
MeyPins[3].Init(PIN_PD2, (byte) 4);
MeyPins[4] = PinState();
MeyPins[4].Init(PIN_PC7, (byte) 5);
MeyPins[4].Init(PIN_PD6, (byte) 5);
MeyPins[5] = PinState();
MeyPins[5].Init(PIN_PD1, (byte) 6);
MeyPins[5].Init(PIN_PD5, (byte) 6);
MeyPins[6] = PinState();
MeyPins[6].Init(PIN_PD3, (byte) 7);
MeyPins[6].Init(PIN_PD4, (byte) 7);
MeyPins[7] = PinState();
MeyPins[7].Init(PIN_PD6, (byte) 8);
MeyPins[7].Init(PIN_PD3, (byte) 8);
@@ -80,6 +80,9 @@ void setup() {
void loop()
{
delay(1000);
SendSerialPackage();
for (int i = 0; i <= PinCount - 1; i++)
{
if (CheckPinStatus(&MeyPins[i]))
@@ -88,10 +91,8 @@ void loop()
if (mcp2515.readMessage(&_frame) == MCP2515::ERROR_OK)
{
if (GetDeviceId(_frame.can_id) == GetMyDeviceId())
{
int meyPinId = _frame.data[0];
bool state = _frame.data[1] > 0;
@@ -116,7 +117,6 @@ void loop()
}
}
delay(20);
}
@@ -129,7 +129,7 @@ void SendSerialPackage()
_frame.data[1] = SoftwareVersionLow;
_frame.data[2] = HardwareVersionHigh;
_frame.data[3] = HardwareVersionLow;
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
DoSendCanPkg(&mcp2515, &_frame);
}
@@ -139,9 +139,19 @@ void SendSwitchedTriggeredCanPackage(byte pinId, int state)
_frame.can_dlc = 2;
_frame.data[0] = pinId;
_frame.data[1] = state;
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
DoSendCanPkg(&mcp2515, &_frame);
}
void DoSendCanPkg(MCP2515 *interface, can_frame *frame)
{
// ToggleDebug();
byte cnt = 0;
while (interface->sendMessage(&_frame)) {
cnt++;
if (cnt > 10) return;
}
}
void SendDoTriggerSwitchCanPackage(uint32_t targetCanId, byte pinId, byte state)
{
@@ -151,7 +161,7 @@ void SendDoTriggerSwitchCanPackage(uint32_t targetCanId, byte pinId, byte state)
_frame.data[1] = (targetCanId & 0xFF00) >> 8;
_frame.data[2] = pinId;
_frame.data[3] = state;
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
DoSendCanPkg(&mcp2515, &_frame);
}
bool ReadPin(PinState * state)