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

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