Software for V7 refactored
This commit is contained in:
@@ -3,47 +3,119 @@
|
||||
#include <mcp2515.h>;
|
||||
|
||||
|
||||
PinState MeyPins[8];
|
||||
MCP2515* canInterfaces[4];
|
||||
PinState *MeyPin = NULL;
|
||||
CanInterface *CanBusses = NULL;
|
||||
uint16_t myDeviceId;
|
||||
|
||||
void SetupMeyPin(PinState *state)
|
||||
{
|
||||
pinMode(state->pin_id, OUTPUT);
|
||||
digitalWrite(state->pin_id, LOW);
|
||||
state->pin_state = false;
|
||||
}
|
||||
|
||||
void InitCanInterface(MCP2515 *interface, can_frame *frame) // can_frame is NULL. Reuse ForEach Method for smaller footprint
|
||||
{
|
||||
interface->reset();
|
||||
interface->setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
|
||||
interface->setNormalMode();
|
||||
SendVersionPackage(interface);
|
||||
}
|
||||
|
||||
void ForEachCanInterface(void (*handle)(MCP2515 *canInterace))
|
||||
{
|
||||
CanBusses->ForEachInterface(handle);
|
||||
}
|
||||
|
||||
void SetupMeyCan()
|
||||
{
|
||||
for (int i = 0; i < sizeof(MeyPins) / sizeof(PinState); i++)
|
||||
{
|
||||
pinMode(MeyPins[i].pin_id, OUTPUT);
|
||||
digitalWrite(MeyPins[i].pin_id, LOW);
|
||||
MeyPins[i].pin_state = false;
|
||||
}
|
||||
CalculateMyDeviceId();
|
||||
|
||||
for (int i = 0; i < sizeof(canInterfaces) / sizeof(MCP2515*); i++) {
|
||||
if (MeyPin != NULL);
|
||||
MeyPin->ForEach(SetupMeyPin);
|
||||
|
||||
canInterfaces[i]->reset();
|
||||
canInterfaces[i]->setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
|
||||
canInterfaces[i]->setNormalMode();
|
||||
|
||||
|
||||
SendVersionPackage(canInterfaces[i]);
|
||||
if (CanBusses != NULL)
|
||||
{
|
||||
CanBusses->ForEach(InitCanInterface, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void SetCanInterface(byte index, byte pinId)
|
||||
void AddCanInterface(byte pinId)
|
||||
{
|
||||
canInterfaces[index] = new MCP2515(pinId);
|
||||
MCP2515* newCanInterface = new MCP2515(pinId);
|
||||
CanInterface* canInterface = new CanInterface();
|
||||
canInterface->interface = newCanInterface;
|
||||
|
||||
if (CanBusses == NULL)
|
||||
CanBusses = canInterface;
|
||||
else
|
||||
CanBusses->AddCanInterface(canInterface);
|
||||
}
|
||||
|
||||
void SetMeyPin(byte index, byte meyPinId, byte pinId)
|
||||
{
|
||||
MeyPins[index] = PinState();
|
||||
MeyPins[index].Init(pinId, (byte) meyPinId);
|
||||
PinState* newState = new PinState();
|
||||
newState->Init(pinId, (byte) meyPinId);
|
||||
|
||||
canInterfaces[index] = new MCP2515(pinId);
|
||||
if (MeyPin == NULL)
|
||||
MeyPin = newState;
|
||||
else
|
||||
MeyPin->AddPinState(newState);
|
||||
}
|
||||
|
||||
void HandleFrame(can_frame *frame)
|
||||
|
||||
bool CheckPinStatus(PinState * state)
|
||||
{
|
||||
if (!state->is_input)
|
||||
return false;
|
||||
|
||||
bool newValue = ReadPin(state);
|
||||
|
||||
|
||||
if (newValue != state->pin_state)
|
||||
{
|
||||
delay(10);
|
||||
newValue = ReadPin(state);
|
||||
if (newValue != state->pin_state)
|
||||
{
|
||||
state->pin_state = newValue;
|
||||
SendSwitchedTriggeredCanPackage(state->meyPinId, state->pin_state);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void CheckMeyPinsTriggered()
|
||||
{
|
||||
MeyPin->ForEach(CheckPinStatus);
|
||||
}
|
||||
|
||||
bool ReadPin(PinState * state)
|
||||
{
|
||||
if (state->pin_id == PIN_PD2)
|
||||
return digitalReadFast(PIN_PD2);
|
||||
else if (state->pin_id == PIN_PC7)
|
||||
return digitalReadFast(PIN_PC7);
|
||||
else if (state->pin_id == PIN_PD1)
|
||||
return digitalReadFast(PIN_PD1);
|
||||
else if (state->pin_id == PIN_PD0)
|
||||
return digitalReadFast(PIN_PD0);
|
||||
else if (state->pin_id == PIN_PD6)
|
||||
return digitalReadFast(PIN_PD6);
|
||||
else if (state->pin_id == PIN_PD5)
|
||||
return digitalReadFast(PIN_PD5);
|
||||
else if (state->pin_id == PIN_PD4)
|
||||
return digitalReadFast(PIN_PD4);
|
||||
else if (state->pin_id == PIN_PD3)
|
||||
return digitalReadFast(PIN_PD3);
|
||||
else
|
||||
return digitalRead(state->pin_id);
|
||||
}
|
||||
|
||||
|
||||
void HandleFrame(can_frame *frame, MCP2515 *source)
|
||||
{
|
||||
CanBusses->ForEach(DoSendCanPkg, frame, source);
|
||||
HandleTriggerMeypinCanPackage(frame);
|
||||
}
|
||||
|
||||
@@ -55,16 +127,10 @@ void HandleTriggerMeypinCanPackage(can_frame *frame)
|
||||
if (adressedDeviceId != myDeviceId) return;
|
||||
|
||||
|
||||
int meyPinId = frame->data[2];
|
||||
byte meyPinId = frame->data[2];
|
||||
bool state = frame->data[3] > 0;
|
||||
|
||||
PinState *adressedPin = NULL;
|
||||
for (int i = 0; i < sizeof(MeyPins) / sizeof(PinState); i++)
|
||||
if (MeyPins[i].meyPinId == meyPinId)
|
||||
{
|
||||
adressedPin = &MeyPins[i];
|
||||
break;
|
||||
}
|
||||
PinState *adressedPin = MeyPin->Find(meyPinId);
|
||||
|
||||
if (adressedPin != NULL)
|
||||
{
|
||||
@@ -135,7 +201,7 @@ void SendVersionPackage(MCP2515 *interface)
|
||||
toSend.data[4] = (myDeviceId >> 8) & 0xFF;
|
||||
toSend.data[5] = myDeviceId & 0xFF;
|
||||
|
||||
DoSendCanPkg(interface, &toSend);
|
||||
DoSendCanPkg(interface, &toSend);
|
||||
}
|
||||
|
||||
void BroadcastTriggerMeyPinCanPackage(uint16_t targetCanId, byte pinId, byte state)
|
||||
@@ -149,8 +215,7 @@ void BroadcastTriggerMeyPinCanPackage(uint16_t targetCanId, byte pinId, byte sta
|
||||
toSend.data[2] = pinId;
|
||||
toSend.data[3] = state;
|
||||
|
||||
DoSendCanPkg(&toSend);
|
||||
HandleFrame(&toSend);
|
||||
HandleFrame(&toSend, NULL);
|
||||
}
|
||||
|
||||
void SendSwitchedTriggeredCanPackage(byte pinId, int state)
|
||||
@@ -166,11 +231,11 @@ void SendSwitchedTriggeredCanPackage(byte pinId, int state)
|
||||
|
||||
void DoSendCanPkg(can_frame *frame)
|
||||
{
|
||||
for (int i = 0; i < sizeof(canInterfaces) / sizeof(MCP2515*); i++)
|
||||
DoSendCanPkg(canInterfaces[i], frame);
|
||||
if (CanBusses != NULL)
|
||||
CanBusses->ForEach(DoSendCanPkg, frame);
|
||||
}
|
||||
|
||||
void DoSendCanPkg(MCP2515* interface, can_frame *frame)
|
||||
void DoSendCanPkg(MCP2515 *interface, can_frame *frame)
|
||||
{
|
||||
byte cnt = 0;
|
||||
while (interface->sendMessage(frame))
|
||||
|
||||
Reference in New Issue
Block a user