Version6 draft of UP
This commit is contained in:
@@ -1,82 +1,42 @@
|
||||
#include <SPI.h>;
|
||||
#include <mcp2515.h>;
|
||||
#include "MeyCan.cpp";
|
||||
|
||||
const byte SoftwareVersionHigh = 1;
|
||||
const byte SoftwareVersionLow = 0;
|
||||
const byte HardwareVersionHigh = 2;
|
||||
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 HELP_PACKAGE_ID = 0xFFFF;
|
||||
|
||||
struct can_frame _frame;
|
||||
MCP2515 mcp2515_0(PIN_PB1);
|
||||
MCP2515 mcp2515_1(PIN_PB0);
|
||||
MCP2515 mcp2515_2(PIN_PA3);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t sourceDevId;
|
||||
byte sourceMeyPinId;
|
||||
byte targetMeyPinId;
|
||||
bool trigger;
|
||||
|
||||
void InitSimple(uint32_t sourceDevId, byte sourceMeyPinId, byte targetMeyPinId)
|
||||
{
|
||||
this->sourceDevId = sourceDevId;
|
||||
this->sourceMeyPinId = sourceMeyPinId;
|
||||
this->targetMeyPinId = targetMeyPinId;
|
||||
trigger = false;
|
||||
}
|
||||
|
||||
void InitTrigger(uint32_t sourceDevId, byte sourceMeyPinId, byte targetMeyPinId)
|
||||
{
|
||||
this->sourceDevId = sourceDevId;
|
||||
this->sourceMeyPinId = sourceMeyPinId;
|
||||
this->targetMeyPinId = targetMeyPinId;
|
||||
trigger = true;
|
||||
}
|
||||
} Rule;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int pin_id;
|
||||
bool pin_state;
|
||||
bool is_input;
|
||||
byte meyPinId;
|
||||
|
||||
PinState() {}
|
||||
void Init(int pin_id, byte meyPinId)
|
||||
{
|
||||
this->pin_id = pin_id;
|
||||
this->pin_state = true;
|
||||
this->is_input = true;
|
||||
this->meyPinId = meyPinId;
|
||||
}
|
||||
} PinState;
|
||||
|
||||
|
||||
int PinCount = 8;
|
||||
PinState MeyPins[8];
|
||||
|
||||
bool flag = false;
|
||||
byte DeviceId[1];
|
||||
|
||||
Rule Rules[4];
|
||||
int RulesCount = 4;
|
||||
Rule Rules[6];
|
||||
int RulesCount = 6;
|
||||
|
||||
void setup() {
|
||||
SPI.begin();
|
||||
|
||||
Rules[0] = Rule();
|
||||
Rules[0].InitSimple(0x051F, 1, 3);
|
||||
Rules[0].InitSimple(0x051F, 1, 1);
|
||||
Rules[1] = Rule();
|
||||
Rules[1].InitSimple(0x051F, 2, 1);
|
||||
Rules[1].InitSimple(0x051F, 2, 2);
|
||||
Rules[2] = Rule();
|
||||
Rules[2].InitSimple(0x0388, 1, 3);
|
||||
Rules[2].InitSimple(0x051F, 3, 3);
|
||||
Rules[3] = Rule();
|
||||
Rules[3].InitSimple(0x0388, 2, 4);
|
||||
Rules[3].InitSimple(0x051F, 4, 4);
|
||||
Rules[4] = Rule();
|
||||
Rules[4].InitSimple(0x051F, 5, 5);
|
||||
Rules[5] = Rule();
|
||||
Rules[5].InitSimple(0x051F, 6, 6);
|
||||
Rules[6] = Rule();
|
||||
Rules[6].InitSimple(0x051F, 7, 7);
|
||||
|
||||
MeyPins[0] = PinState();
|
||||
MeyPins[0].Init(PIN_PC1, (byte) 1);
|
||||
@@ -99,7 +59,6 @@ void setup() {
|
||||
MeyPins[6] = PinState();
|
||||
MeyPins[6].Init(PIN_PC7, (byte) 7);
|
||||
|
||||
|
||||
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLA, CLKCTRL.MCLKCTRLA | 1 << 7);
|
||||
|
||||
mcp2515_0.reset();
|
||||
@@ -129,28 +88,27 @@ void loop() {
|
||||
|
||||
if (mcp2515_0.readMessage(&_frame) == MCP2515::ERROR_OK)
|
||||
{
|
||||
HandleFrame();
|
||||
mcp2515_1.sendMessage(MCP2515::TXB1, &_frame);
|
||||
mcp2515_2.sendMessage(MCP2515::TXB1, &_frame);
|
||||
HandleFrame();
|
||||
}
|
||||
if (mcp2515_1.readMessage(&_frame) == MCP2515::ERROR_OK)
|
||||
{
|
||||
HandleFrame();
|
||||
mcp2515_0.sendMessage(MCP2515::TXB1, &_frame);
|
||||
mcp2515_2.sendMessage(MCP2515::TXB1, &_frame);
|
||||
HandleFrame();
|
||||
}
|
||||
if (mcp2515_2.readMessage(&_frame) == MCP2515::ERROR_OK)
|
||||
{
|
||||
HandleFrame();
|
||||
mcp2515_0.sendMessage(MCP2515::TXB1, &_frame);
|
||||
mcp2515_1.sendMessage(MCP2515::TXB1, &_frame);
|
||||
HandleFrame();
|
||||
}
|
||||
delay(20);
|
||||
}
|
||||
|
||||
void HandleFrame()
|
||||
{
|
||||
|
||||
if (GetPackageType(_frame.can_id) == SWITCH_TRIGGERED_CAN_ID)
|
||||
{
|
||||
for (int i = 0; i <= RulesCount - 1; i++)
|
||||
@@ -188,27 +146,6 @@ void ProcessTriggerSwitchPackage(byte meyPinId, byte state, bool asTrigger)
|
||||
foundPinId->pin_state = pinState;
|
||||
}
|
||||
|
||||
byte CircularShift(byte b)
|
||||
{
|
||||
return (b << 1) | (b >> 7 & 1);
|
||||
}
|
||||
|
||||
uint32_t GetDeviceId(uint32_t canFrameId)
|
||||
{
|
||||
return canFrameId & 0xFFFF;
|
||||
}
|
||||
|
||||
uint32_t GetPackageType(uint32_t canFrameId)
|
||||
{
|
||||
return (canFrameId / 0x10000) & 0xFFF;
|
||||
}
|
||||
|
||||
uint32_t CreateCanId(uint32_t commandId)
|
||||
{
|
||||
return ((commandId & 0xFFF) * 0x10000) | ( DeviceId[0] << 8) | (DeviceId[1]) | CAN_EFF_FLAG;
|
||||
}
|
||||
|
||||
|
||||
void SendSerialPackage()
|
||||
{
|
||||
_frame.can_id = CreateCanId(HELP_PACKAGE_CAN_ID);
|
||||
|
||||
Reference in New Issue
Block a user