Software for V7 refactored

This commit is contained in:
Meydin87
2023-07-18 07:31:20 +02:00
parent 21fab157da
commit ad72be85b1
5 changed files with 248 additions and 94 deletions

View File

@@ -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))

View File

@@ -4,13 +4,48 @@
#include <SPI.h>
#include <mcp2515.h>
typedef struct CanInterface
{
MCP2515 *interface;
CanInterface *next = NULL;
typedef struct
void ForEachInterface(void (*handle)( MCP2515 *interface))
{
if (this->interface != NULL)
handle(this->interface);
if (this->next != NULL)
this->next->ForEachInterface(handle);
}
void ForEach(void (*handle)( MCP2515 *interface, can_frame *frame), can_frame *frame, MCP2515* exclude = NULL)
{
if (this->interface != NULL && this->interface != exclude)
handle(this->interface, frame);
if (this->next != NULL)
this->next->ForEach(handle, frame);
}
void AddCanInterface(CanInterface *newCanInterface)
{
if (next == NULL)
{
this->next = newCanInterface;
newCanInterface->next = NULL;
} else {
next->AddCanInterface(newCanInterface);
}
}
};
typedef struct PinState
{
int pin_id;
bool pin_state;
bool is_input;
byte meyPinId;
PinState *next = NULL;
PinState() {}
void Init(int pin_id, byte meyPinId)
@@ -20,10 +55,37 @@ typedef struct
this->is_input = true;
this->meyPinId = meyPinId;
}
} PinState;
void AddPinState(PinState *nextPinState)
{
if (next == NULL)
{
this->next = nextPinState;
nextPinState->next = NULL;
} else {
next->AddPinState(nextPinState);
}
}
PinState* Find(byte meyPinId)
{
if (this->meyPinId == meyPinId)
return this;
if (this->next != NULL)
return this->next->Find(meyPinId);
return NULL;
}
void ForEach(void (*handle)(PinState *theState))
{
handle(this);
if (this->next != NULL)
this->next->ForEach(handle);
}
};
/* START MEYCAN */
const byte SOFTWARE_VERSION_HIGH = 5;
const byte SOFTWARE_VERSION_LOW = 0;
@@ -34,14 +96,16 @@ const uint16_t HELP_PACKAGE_CAN_ID = 0x0FFFUL;
const uint16_t SWITCH_TRIGGERED_CAN_ID = 0x0050;
const uint16_t TRIGGER_SWITCH_CAN_ID = 0x0055;
extern MCP2515* canInterfaces[4];
void SetCanInterface(byte index, byte pinId);
void AddCanInterface(byte pinId);
void SetMeyPin(byte index, byte meyPinId, byte pinId);
void SetupMeyCan();
void HandleFrame(can_frame *frame);
void ForEachCanInterface(void (*handle)(MCP2515 *canInterace));
bool ReadPin(PinState *state);
bool CheckPinStatus(PinState * state);
void CheckMeyPinsTriggered(); /* checks weather a meypin triggered and sends a can pkg is neccessary */
void HandleFrame(can_frame *frame, MCP2515 *source);
void HandleTriggerMeypinCanPackage(can_frame *frame);
byte CircularShift(byte b);
uint16_t GetDeviceId(uint32_t canFrameId);
@@ -61,7 +125,4 @@ void DoSendCanPkg(MCP2515 *interface, can_frame *frame);
void CalculateMyDeviceId();
/* STOP MEYCAN */
#endif

View File

@@ -1,11 +1,8 @@
#include <avr/io.h>;
#include <avr/xmega.h>;
#include <SPI.h>;
#include <mcp2515.h>;
#include "MeyCan.h";
#include "MeyRule.h";
struct can_frame incomingCanFrame;
void setup() {
@@ -28,8 +25,7 @@ void setup() {
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);
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLA, CLKCTRL.MCLKCTRLA | 1 << 7);
delay(10); // a bit delay for mcp2515 to get the clock
@@ -42,10 +38,10 @@ void setup() {
SetMeyPin(6, 7, PIN_PC1);
SetMeyPin(7, 8, PIN_PC0);
SetCanInterface(0, PIN_PA2);
SetCanInterface(1, PIN_PA3);
SetCanInterface(2, PIN_PB0);
SetCanInterface(3, PIN_PB1);
AddCanInterface(PIN_PA2);
AddCanInterface(PIN_PA3);
AddCanInterface(PIN_PB0);
AddCanInterface(PIN_PB1);
SetupMeyCan();
@@ -54,19 +50,13 @@ void setup() {
void loop()
{
for (int i = 0; i < sizeof(canInterfaces) / sizeof(MCP2515*); i++)
{
if (canInterfaces[i]->readMessage(&incomingCanFrame) == MCP2515::ERROR_OK)
{
// 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++)
{
if (j == i) continue; // thats for the echo
DoSendCanPkg(canInterfaces[j], &incomingCanFrame);
}
ForEachCanInterface(CheckCanInterface);
}
HandleFrame(&incomingCanFrame);
// HandleRules(&incomingCanFrame);
}
void CheckCanInterface(MCP2515 *interface)
{
if (interface->readMessage(&incomingCanFrame) == MCP2515::ERROR_OK)
{
HandleFrame(&incomingCanFrame, interface);
}
}