WIP
This commit is contained in:
@@ -5,7 +5,7 @@ struct can_frame _frame;
|
|||||||
MCP2515 mcp2515(10);
|
MCP2515 mcp2515(10);
|
||||||
|
|
||||||
bool flag = false;
|
bool flag = false;
|
||||||
|
byte DeviceId[2];
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
@@ -30,31 +30,18 @@ void setup() {
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
bool debugState = false;
|
bool debugState = false;
|
||||||
|
|
||||||
void SendCanPackage(bool state)
|
|
||||||
{
|
|
||||||
_frame.can_id = 0x001;
|
|
||||||
_frame.can_dlc = 4;
|
|
||||||
_frame.data[0] = 5;
|
|
||||||
_frame.data[1] = state;
|
|
||||||
_frame.data[2] = 0x00;
|
|
||||||
_frame.data[3] = 0x00;
|
|
||||||
mcp2515.sendMessage(&_frame);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
int analogValue = analogRead(A3);
|
int analogValue = analogRead(A3);
|
||||||
if (analogValue > 756 && !debugState)
|
if (analogValue > 756 && !debugState)
|
||||||
{
|
{
|
||||||
debugState = true;
|
debugState = true;
|
||||||
SendCanPackage(debugState);
|
SendSwitchedTriggeredCanPackage(5, debugState);
|
||||||
}
|
}
|
||||||
if (analogValue < 300 && debugState)
|
if (analogValue < 300 && debugState)
|
||||||
{
|
{
|
||||||
debugState = false;
|
debugState = false;
|
||||||
SendCanPackage(debugState);
|
SendSwitchedTriggeredCanPackage(5, debugState);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +52,14 @@ void loop() {
|
|||||||
Serial.println("CAN FRAME ------------");
|
Serial.println("CAN FRAME ------------");
|
||||||
Serial.print("0x");
|
Serial.print("0x");
|
||||||
Serial.println( _frame.can_id ^ CAN_EFF_FLAG, HEX);
|
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);
|
Serial.println( _frame.can_dlc);
|
||||||
|
|
||||||
for (int i = 0; i < _frame.can_dlc ; i++)
|
for (int i = 0; i < _frame.can_dlc ; i++)
|
||||||
@@ -111,5 +106,65 @@ void loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
delay(20);
|
delay(20);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
_frame.can_id = CreateCanId(HELP_PACKAGE_CAN_ID);
|
||||||
|
_frame.can_dlc = 4;
|
||||||
|
|
||||||
|
_frame.data[0] = SoftwareVersionHigh;
|
||||||
|
_frame.data[1] = SoftwareVersionLow;
|
||||||
|
_frame.data[2] = HardwareVersionHigh;
|
||||||
|
_frame.data[3] = HardwareVersionLow;
|
||||||
|
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SendSwitchedTriggeredCanPackage(byte pinId, int state)
|
||||||
|
{
|
||||||
|
_frame.can_id = CreateCanId(SWITCH_TRIGGERED_CAN_ID);
|
||||||
|
_frame.can_dlc = 2;
|
||||||
|
_frame.data[0] = pinId;
|
||||||
|
_frame.data[1] = state;
|
||||||
|
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SendDoTriggerSwitchCanPackage(uint32_t targetCanId, byte pinId, byte state)
|
||||||
|
{
|
||||||
|
_frame.can_id = CreateCanId(TRIGGER_SWITCH_CAN_ID);
|
||||||
|
_frame.can_dlc = 4;
|
||||||
|
_frame.data[0] = targetCanId & 0xFF;
|
||||||
|
_frame.data[1] = (targetCanId & 0xFF00) >> 8;
|
||||||
|
_frame.data[2] = pinId;
|
||||||
|
_frame.data[3] = state;
|
||||||
|
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,16 +74,8 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
DeviceId[0] = (SIGROW.SERNUM0 ^
|
DeviceId[0] = GetDeviceIdLow();
|
||||||
CircularShift(SIGROW.SERNUM2) << 1 ^
|
DeviceId[1] = GetDeviceIdHigh();
|
||||||
CircularShift( CircularShift(SIGROW.SERNUM4)) ^
|
|
||||||
CircularShift( CircularShift( CircularShift(SIGROW.SERNUM6))) ^
|
|
||||||
CircularShift( CircularShift( CircularShift( CircularShift(SIGROW.SERNUM8)))));
|
|
||||||
DeviceId[1] = (SIGROW.SERNUM1 ^
|
|
||||||
CircularShift(SIGROW.SERNUM3) << 1 ^
|
|
||||||
CircularShift( CircularShift(SIGROW.SERNUM5)) ^
|
|
||||||
CircularShift( CircularShift( CircularShift(SIGROW.SERNUM7))) ^
|
|
||||||
CircularShift( CircularShift( CircularShift( CircularShift(SIGROW.SERNUM9)))));
|
|
||||||
|
|
||||||
SendSerialPackage();
|
SendSerialPackage();
|
||||||
}
|
}
|
||||||
@@ -111,7 +103,7 @@ void loop()
|
|||||||
for (int i = 0; i <= PinCount - 1; i++)
|
for (int i = 0; i <= PinCount - 1; i++)
|
||||||
{
|
{
|
||||||
if (CheckPinStatus(&PinPD2[i]))
|
if (CheckPinStatus(&PinPD2[i]))
|
||||||
SendSwitchedTriggeredCanPackage(&PinPD2[i]);
|
SendSwitchedTriggeredCanPackage(PinPD2[i].meyPinId, PinPD2[i].pin_state);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mcp2515.readMessage(&_frame) == MCP2515::ERROR_OK)
|
if (mcp2515.readMessage(&_frame) == MCP2515::ERROR_OK)
|
||||||
@@ -159,15 +151,16 @@ void SendSerialPackage()
|
|||||||
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
|
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SendSwitchedTriggeredCanPackage(PinState * state)
|
void SendSwitchedTriggeredCanPackage(byte pinId, int state)
|
||||||
{
|
{
|
||||||
_frame.can_id = CreateCanId(0x050);
|
_frame.can_id = CreateCanId(0x050);
|
||||||
_frame.can_dlc = 2;
|
_frame.can_dlc = 2;
|
||||||
_frame.data[0] = state->meyPinId;
|
_frame.data[0] = pinId;
|
||||||
_frame.data[1] = state->pin_state;
|
_frame.data[1] = state;
|
||||||
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
|
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void SendDoTriggerSwitchCanPackage(uint32_t targetCanId, byte pinId, byte state)
|
void SendDoTriggerSwitchCanPackage(uint32_t targetCanId, byte pinId, byte state)
|
||||||
{
|
{
|
||||||
_frame.can_id = CreateCanId(0x050);
|
_frame.can_id = CreateCanId(0x050);
|
||||||
@@ -201,6 +194,22 @@ bool ReadPin(PinState * state)
|
|||||||
return digitalRead(state->pin_id);
|
return digitalRead(state->pin_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
byte GetDeviceIdLow(){
|
||||||
|
return (SIGROW.SERNUM0 ^
|
||||||
|
CircularShift(SIGROW.SERNUM2) << 1 ^
|
||||||
|
CircularShift( CircularShift(SIGROW.SERNUM4)) ^
|
||||||
|
CircularShift( CircularShift( CircularShift(SIGROW.SERNUM6))) ^
|
||||||
|
CircularShift( CircularShift( CircularShift( CircularShift(SIGROW.SERNUM8)))));
|
||||||
|
}
|
||||||
|
|
||||||
|
byte GetDeviceIdHigh(){
|
||||||
|
return (SIGROW.SERNUM1 ^
|
||||||
|
CircularShift(SIGROW.SERNUM3) << 1 ^
|
||||||
|
CircularShift( CircularShift(SIGROW.SERNUM5)) ^
|
||||||
|
CircularShift( CircularShift( CircularShift(SIGROW.SERNUM7))) ^
|
||||||
|
CircularShift( CircularShift( CircularShift( CircularShift(SIGROW.SERNUM9)))));
|
||||||
|
}
|
||||||
|
|
||||||
bool CheckPinStatus(PinState * state)
|
bool CheckPinStatus(PinState * state)
|
||||||
{
|
{
|
||||||
if (!state->is_input)
|
if (!state->is_input)
|
||||||
@@ -222,4 +231,3 @@ bool CheckPinStatus(PinState * state)
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
221
Software/UPSoftware/sketch_jun30b/sketch_jun30b.ino
Normal file
221
Software/UPSoftware/sketch_jun30b/sketch_jun30b.ino
Normal file
@@ -0,0 +1,221 @@
|
|||||||
|
#include <SPI.h>;
|
||||||
|
#include <mcp2515.h>;
|
||||||
|
|
||||||
|
|
||||||
|
struct can_frame _frame;
|
||||||
|
MCP2515 mcp2515(PIN_PA3);
|
||||||
|
const byte SoftwareVersionHigh = 1;
|
||||||
|
const byte SoftwareVersionLow = 0;
|
||||||
|
const byte HardwareVersionHigh = 2;
|
||||||
|
const byte HardwareVersionLow = 0;
|
||||||
|
|
||||||
|
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 PinPD2[8];
|
||||||
|
byte DeviceId[2];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
SPI.begin();
|
||||||
|
|
||||||
|
|
||||||
|
PinPD2[0] = PinState();
|
||||||
|
PinPD2[0].Init(PIN_PC7, (byte) 1);
|
||||||
|
|
||||||
|
PinPD2[1] = PinState();
|
||||||
|
PinPD2[1].Init(PIN_PD0, (byte) 2);
|
||||||
|
|
||||||
|
PinPD2[2] = PinState();
|
||||||
|
PinPD2[2].Init(PIN_PD1, (byte) 3);
|
||||||
|
|
||||||
|
PinPD2[3] = PinState();
|
||||||
|
PinPD2[3].Init(PIN_PD2, (byte) 4);
|
||||||
|
|
||||||
|
PinPD2[4] = PinState();
|
||||||
|
PinPD2[4].Init(PIN_PD6, (byte) 5);
|
||||||
|
|
||||||
|
PinPD2[5] = PinState();
|
||||||
|
PinPD2[5].Init(PIN_PD5, (byte) 6);
|
||||||
|
|
||||||
|
PinPD2[6] = PinState();
|
||||||
|
PinPD2[6].Init(PIN_PD4, (byte) 7);
|
||||||
|
|
||||||
|
PinPD2[7] = PinState();
|
||||||
|
PinPD2[7].Init(PIN_PD3, (byte) 8);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLA, CLKCTRL.MCLKCTRLA | 1 << 7);
|
||||||
|
mcp2515.reset();
|
||||||
|
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
|
||||||
|
mcp2515.setNormalMode();
|
||||||
|
|
||||||
|
for (int i = 0; i <= PinCount - 1; i++)
|
||||||
|
{
|
||||||
|
pinMode(PinPD2[i].pin_id, INPUT_PULLUP);
|
||||||
|
PinPD2[i].pin_state = ReadPin(&PinPD2[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DeviceId[0] = (SIGROW.SERNUM0 ^
|
||||||
|
CircularShift(SIGROW.SERNUM2) << 1 ^
|
||||||
|
CircularShift( CircularShift(SIGROW.SERNUM4)) ^
|
||||||
|
CircularShift( CircularShift( CircularShift(SIGROW.SERNUM6))) ^
|
||||||
|
CircularShift( CircularShift( CircularShift( CircularShift(SIGROW.SERNUM8)))));
|
||||||
|
DeviceId[1] = (SIGROW.SERNUM1 ^
|
||||||
|
CircularShift(SIGROW.SERNUM3) << 1 ^
|
||||||
|
CircularShift( CircularShift(SIGROW.SERNUM5)) ^
|
||||||
|
CircularShift( CircularShift( CircularShift(SIGROW.SERNUM7))) ^
|
||||||
|
CircularShift( CircularShift( CircularShift( CircularShift(SIGROW.SERNUM9)))));
|
||||||
|
|
||||||
|
SendSerialPackage();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool debugFlag = false;
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
for (int i = 0; i <= PinCount - 1; i++)
|
||||||
|
{
|
||||||
|
if (CheckPinStatus(&PinPD2[i]))
|
||||||
|
SendSwitchedTriggeredCanPackage(&PinPD2[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mcp2515.readMessage(&_frame) == MCP2515::ERROR_OK)
|
||||||
|
{
|
||||||
|
debugFlag ^= true;
|
||||||
|
|
||||||
|
int meyPinId = _frame.data[0];
|
||||||
|
bool state = _frame.data[1] > 0;
|
||||||
|
|
||||||
|
PinState *adressedPin;
|
||||||
|
for (int i = 0; i <= PinCount - 1; i++)
|
||||||
|
if (PinPD2[i].meyPinId == meyPinId)
|
||||||
|
{
|
||||||
|
adressedPin = &PinPD2[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (adressedPin != NULL)
|
||||||
|
{
|
||||||
|
if (adressedPin->is_input == true)
|
||||||
|
{
|
||||||
|
pinMode(adressedPin->pin_id, OUTPUT);
|
||||||
|
adressedPin->is_input = false;
|
||||||
|
}
|
||||||
|
adressedPin->pin_state = state;
|
||||||
|
digitalWrite(adressedPin->pin_id, state);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
delay(20);
|
||||||
|
}
|
||||||
|
|
||||||
|
byte CircularShift(byte b)
|
||||||
|
{
|
||||||
|
return (b << 1) | (b >> 7 & 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t GetDeviceId(uint32_t canFrameId)
|
||||||
|
{
|
||||||
|
return canFrameId & 0xFFFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t CreateCanId(uint32_t commandId)
|
||||||
|
{
|
||||||
|
return ((commandId & 0xFFF) * 0x10000) | ( DeviceId[0] << 8) | (DeviceId[1]) | CAN_EFF_FLAG;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SendSerialPackage()
|
||||||
|
{
|
||||||
|
_frame.can_id = CreateCanId(0xFFFF);
|
||||||
|
_frame.can_dlc = 4;
|
||||||
|
|
||||||
|
_frame.data[0] = SoftwareVersionHigh;
|
||||||
|
_frame.data[1] = SoftwareVersionLow;
|
||||||
|
_frame.data[2] = HardwareVersionHigh;
|
||||||
|
_frame.data[3] = HardwareVersionLow;
|
||||||
|
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SendSwitchedTriggeredCanPackage(PinState * state)
|
||||||
|
{
|
||||||
|
_frame.can_id = CreateCanId(0x050);
|
||||||
|
_frame.can_dlc = 2;
|
||||||
|
_frame.data[0] = state->meyPinId;
|
||||||
|
_frame.data[1] = state->pin_state;
|
||||||
|
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SendDoTriggerSwitchCanPackage(uint32_t targetCanId, byte pinId, byte state)
|
||||||
|
{
|
||||||
|
_frame.can_id = CreateCanId(0x050);
|
||||||
|
_frame.can_dlc = 4;
|
||||||
|
_frame.data[0] = targetCanId & 0xFF;
|
||||||
|
_frame.data[1] = (targetCanId & 0xFF00) >> 8;
|
||||||
|
_frame.data[2] = pinId;
|
||||||
|
_frame.data[3] = state;
|
||||||
|
mcp2515.sendMessage(MCP2515::TXB1, &_frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user