WIP Version read to deploy
This commit is contained in:
@@ -1,29 +1,124 @@
|
||||
#include <SPI.h>;
|
||||
#include <mcp2515.h>;
|
||||
|
||||
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(10);
|
||||
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[2];
|
||||
byte DeviceId[1];
|
||||
|
||||
Rule Rules[4];
|
||||
int RulesCount = 4;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
SPI.begin();
|
||||
|
||||
mcp2515.reset();
|
||||
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
|
||||
mcp2515.setNormalMode();
|
||||
Rules[0] = Rule();
|
||||
Rules[0].InitSimple(0x051F, 1, 3);
|
||||
Rules[1] = Rule();
|
||||
Rules[1].InitSimple(0x051F, 2, 1);
|
||||
Rules[2] = Rule();
|
||||
Rules[2].InitSimple(0x0388, 1, 3);
|
||||
Rules[3] = Rule();
|
||||
Rules[3].InitSimple(0x0388, 2, 4);
|
||||
|
||||
MeyPins[0] = PinState();
|
||||
MeyPins[0].Init(PIN_PC1, (byte) 1);
|
||||
|
||||
MeyPins[1] = PinState();
|
||||
MeyPins[1].Init(PIN_PC2, (byte) 2);
|
||||
|
||||
MeyPins[2] = PinState();
|
||||
MeyPins[2].Init(PIN_PC3, (byte) 3);
|
||||
|
||||
MeyPins[3] = PinState();
|
||||
MeyPins[3].Init(PIN_PC4, (byte) 4);
|
||||
|
||||
MeyPins[4] = PinState();
|
||||
MeyPins[4].Init(PIN_PC5, (byte) 5);
|
||||
|
||||
MeyPins[5] = PinState();
|
||||
MeyPins[5].Init(PIN_PC6, (byte) 6);
|
||||
|
||||
MeyPins[6] = PinState();
|
||||
MeyPins[6].Init(PIN_PC7, (byte) 7);
|
||||
|
||||
|
||||
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLA, CLKCTRL.MCLKCTRLA | 1 << 7);
|
||||
|
||||
mcp2515_0.reset();
|
||||
mcp2515_0.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
|
||||
mcp2515_0.setNormalMode();
|
||||
|
||||
mcp2515_1.reset();
|
||||
mcp2515_1.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
|
||||
mcp2515_1.setNormalMode();
|
||||
|
||||
mcp2515_2.reset();
|
||||
mcp2515_2.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
|
||||
mcp2515_2.setNormalMode();
|
||||
|
||||
for (int i = 0; i <= PinCount - 1; i++)
|
||||
{
|
||||
pinMode(MeyPins[i].pin_id, OUTPUT);
|
||||
}
|
||||
|
||||
pinMode(2, OUTPUT);
|
||||
pinMode(3, OUTPUT);
|
||||
pinMode(4, OUTPUT);
|
||||
pinMode(5, OUTPUT);
|
||||
pinMode(6, OUTPUT);
|
||||
pinMode(7, OUTPUT);
|
||||
pinMode(8, OUTPUT);
|
||||
pinMode(9, OUTPUT);
|
||||
pinMode(A3, OUTPUT);
|
||||
|
||||
}
|
||||
|
||||
@@ -32,82 +127,67 @@ bool debugState = false;
|
||||
|
||||
void loop() {
|
||||
|
||||
int analogValue = analogRead(A3);
|
||||
if (analogValue > 756 && !debugState)
|
||||
if (mcp2515_0.readMessage(&_frame) == MCP2515::ERROR_OK)
|
||||
{
|
||||
debugState = true;
|
||||
SendSwitchedTriggeredCanPackage(5, debugState);
|
||||
HandleFrame();
|
||||
mcp2515_1.sendMessage(MCP2515::TXB1, &_frame);
|
||||
mcp2515_2.sendMessage(MCP2515::TXB1, &_frame);
|
||||
}
|
||||
if (analogValue < 300 && debugState)
|
||||
if (mcp2515_1.readMessage(&_frame) == MCP2515::ERROR_OK)
|
||||
{
|
||||
debugState = false;
|
||||
SendSwitchedTriggeredCanPackage(5, debugState);
|
||||
|
||||
HandleFrame();
|
||||
mcp2515_0.sendMessage(MCP2515::TXB1, &_frame);
|
||||
mcp2515_2.sendMessage(MCP2515::TXB1, &_frame);
|
||||
}
|
||||
|
||||
if (mcp2515.readMessage(&_frame) == MCP2515::ERROR_OK)
|
||||
if (mcp2515_2.readMessage(&_frame) == MCP2515::ERROR_OK)
|
||||
{
|
||||
int meyPinId = _frame.data[0];
|
||||
int state = _frame.data[1] > 0;
|
||||
Serial.println("CAN FRAME ------------");
|
||||
Serial.print("0x");
|
||||
Serial.println( _frame.can_id ^ CAN_EFF_FLAG, HEX);
|
||||
|
||||
Serial.print("PackageType: ");
|
||||
Serial.println(GetPackageType(_frame.can_id), HEX);
|
||||
|
||||
Serial.print("DeviceId: ");
|
||||
Serial.println(GetDeviceId(_frame.can_id), HEX);
|
||||
|
||||
|
||||
Serial.println( _frame.can_dlc);
|
||||
|
||||
for (int i = 0; i < _frame.can_dlc ; i++)
|
||||
{
|
||||
Serial.println( _frame.data[i], HEX);
|
||||
}
|
||||
Serial.println("----------------------");
|
||||
|
||||
|
||||
if (meyPinId == 1)
|
||||
{
|
||||
digitalWrite(2, state);
|
||||
}
|
||||
if (meyPinId == 2)
|
||||
{
|
||||
digitalWrite(3, state);
|
||||
}
|
||||
if (meyPinId == 3)
|
||||
{
|
||||
digitalWrite(4, state);
|
||||
}
|
||||
if (meyPinId == 4)
|
||||
{
|
||||
digitalWrite(5, state);
|
||||
}
|
||||
if (meyPinId == 5)
|
||||
{
|
||||
digitalWrite(6, state);
|
||||
}
|
||||
if (meyPinId == 6)
|
||||
{
|
||||
digitalWrite(7, state);
|
||||
}
|
||||
if (meyPinId == 7)
|
||||
{
|
||||
digitalWrite(8, state);
|
||||
}
|
||||
if (meyPinId == 8)
|
||||
{
|
||||
digitalWrite(9, state);
|
||||
}
|
||||
|
||||
|
||||
HandleFrame();
|
||||
mcp2515_0.sendMessage(MCP2515::TXB1, &_frame);
|
||||
mcp2515_1.sendMessage(MCP2515::TXB1, &_frame);
|
||||
}
|
||||
|
||||
delay(20);
|
||||
}
|
||||
|
||||
void HandleFrame()
|
||||
{
|
||||
|
||||
if (GetPackageType(_frame.can_id) == SWITCH_TRIGGERED_CAN_ID)
|
||||
{
|
||||
for (int i = 0; i <= RulesCount - 1; i++)
|
||||
{
|
||||
if (Rules[i].sourceDevId == GetDeviceId(_frame.can_id) && Rules[i].sourceMeyPinId == _frame.data[0] )
|
||||
{
|
||||
ProcessTriggerSwitchPackage(Rules[i].targetMeyPinId, _frame.data[1], Rules[i].trigger );
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessTriggerSwitchPackage(byte meyPinId, byte state, bool asTrigger)
|
||||
{
|
||||
int pinState = state > 0;
|
||||
|
||||
PinState *foundPinId = 0;
|
||||
for (int i = 0; i < PinCount; i++)
|
||||
{
|
||||
if (MeyPins[i].meyPinId == meyPinId)
|
||||
{
|
||||
foundPinId = &MeyPins[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (foundPinId == 0)return;
|
||||
|
||||
if (asTrigger)
|
||||
{
|
||||
pinState = (foundPinId->pin_state ^ true) > 0;
|
||||
}
|
||||
|
||||
digitalWrite(foundPinId->pin_id, pinState);
|
||||
foundPinId->pin_state = pinState;
|
||||
}
|
||||
|
||||
byte CircularShift(byte b)
|
||||
{
|
||||
return (b << 1) | (b >> 7 & 1);
|
||||
@@ -128,14 +208,6 @@ uint32_t CreateCanId(uint32_t commandId)
|
||||
return ((commandId & 0xFFF) * 0x10000) | ( DeviceId[0] << 8) | (DeviceId[1]) | CAN_EFF_FLAG;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
void SendSerialPackage()
|
||||
{
|
||||
@@ -146,7 +218,7 @@ void SendSerialPackage()
|
||||
_frame.data[1] = SoftwareVersionLow;
|
||||
_frame.data[2] = HardwareVersionHigh;
|
||||
_frame.data[3] = HardwareVersionLow;
|
||||
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
|
||||
mcp2515_1.sendMessage(MCP2515::TXB1, &_frame);
|
||||
}
|
||||
|
||||
void SendSwitchedTriggeredCanPackage(byte pinId, int state)
|
||||
@@ -155,7 +227,7 @@ void SendSwitchedTriggeredCanPackage(byte pinId, int state)
|
||||
_frame.can_dlc = 2;
|
||||
_frame.data[0] = pinId;
|
||||
_frame.data[1] = state;
|
||||
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
|
||||
mcp2515_1.sendMessage(MCP2515::TXB1, &_frame);
|
||||
}
|
||||
|
||||
void SendDoTriggerSwitchCanPackage(uint32_t targetCanId, byte pinId, byte state)
|
||||
@@ -166,5 +238,5 @@ 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);
|
||||
mcp2515_1.sendMessage(MCP2515::TXB1, &_frame);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user