WIP
This commit is contained in:
@@ -5,7 +5,7 @@ struct can_frame _frame;
|
||||
MCP2515 mcp2515(10);
|
||||
|
||||
bool flag = false;
|
||||
|
||||
byte DeviceId[2];
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
@@ -30,31 +30,18 @@ void setup() {
|
||||
int i = 0;
|
||||
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() {
|
||||
|
||||
int analogValue = analogRead(A3);
|
||||
if (analogValue > 756 && !debugState)
|
||||
{
|
||||
debugState = true;
|
||||
SendCanPackage(debugState);
|
||||
SendSwitchedTriggeredCanPackage(5, debugState);
|
||||
}
|
||||
if (analogValue < 300 && debugState)
|
||||
{
|
||||
debugState = false;
|
||||
SendCanPackage(debugState);
|
||||
SendSwitchedTriggeredCanPackage(5, debugState);
|
||||
|
||||
}
|
||||
|
||||
@@ -65,6 +52,14 @@ void loop() {
|
||||
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++)
|
||||
@@ -111,5 +106,65 @@ void loop() {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user