I have an ArduinoCanUtils.h
header file and a ArduinoCanUtils.cpp
source file, organized in the following directory tree:
Documents
|---Arduino
| |---libraries
| |--- autowp-mcp2515
| | |--- other library files
| |
| |--- ArduinoCanUtils
| |--- ArduinoCanUtils.h
|--- ArduinoCanUtils.cpp
These files make up the ArduinoCanUtils library I'm trying to create. The contents of ArduinoCanUtils.h
are as follows:
#ifndef ArduinoCanUtils_h
#define ArduinoCanUtils_h
#include <Arduino.h>
#include <stdint.h>
#include <mcp2515.h>
class ArduinoCanUtils
{
public:
ArduinoCanUtils();
uint32_t createCanMsgCanId(uint8_t priorityLevel, uint8_t activityCode, uint8_t destinationAddress, uint8_t originAddress);
uint8_t getPriorityLevelFromCanMsgCanId(uint32_t canId);
uint8_t getActivityCodeFromCanMsgCanId(uint32_t canId);
uint8_t getDestinationAddressFromCanMsgCanId(uint32_t canId);
uint8_t getOriginAddressFromCanMsgCanId(uint32_t canId);
void setAllCanMsgDataToZero(struct can_frame &canMsg);
};
#endif
While the contents of ArduinoCanUtils.cpp
are as follows:
#include <Arduino.h>
#include <stdint.h>
#include <mcp2515.h>
#include "ArduinoCanUtils.h"
ArduinoCanUtils::ArduinoCanUtils(){}
uint32_t ArduinoCanUtils::createCanMsgCanId(uint8_t priorityLevel, uint8_t activityCode, uint8_t destinationAddress, uint8_t originAddress)
{
uint32_t canId = 0;
canId = canId | originAddress;
canId = canId | destinationAddress << 8;
canId = canId | ((uint32_t) activityCode) << 16;
for (int i = 0; i <= 2; i++){bitWrite(canId, 26 + i, bitRead(priorityLevel, i));}
bitWrite(canId, 31, 1);
return canId;
}
uint8_t ArduinoCanUtils::getPriorityLevelFromCanMsgCanId(uint32_t canId){
uint8_t priorityLevel = 0;
for (int i = 0; i <= 2; i++){bitWrite(priorityLevel, i, bitRead(canId, 26 + i));}
return priorityLevel;
}
uint8_t ArduinoCanUtils::getActivityCodeFromCanMsgCanId(uint32_t canId){
uint8_t activityCode = 0;
for (int i = 0; i <= 7; i++){bitWrite(activityCode, i, bitRead(canId, 16 + i));}
return activityCode;
}
uint8_t ArduinoCanUtils::getDestinationAddressFromCanMsgCanId(uint32_t canId){
uint8_t destinationAddress = 0;
for (int i = 0; i <= 7; i++){bitWrite(destinationAddress, i, bitRead(canId, 8 + i));}
return destinationAddress;
}
uint8_t ArduinoCanUtils::getOriginAddressFromCanMsgCanId(uint32_t canId){
uint8_t originAddress = 0;
for (int i = 0; i <= 7; i++){bitWrite(originAddress, i, bitRead(canId, i));}
return originAddress;
}
void ArduinoCanUtils::setAllCanMsgDataToZero(struct can_frame &canMsg){
for (int i = 0; i <= 7; i++){canMsg.data[i] = 0;}
}
As you can see, the function setAllCanMsgDataToZero
requires the type struct can_frame
for its argument. This type is defined over in the 3rd-party library autowp-mcp2515 (link: https://github.com/autowp/arduino-mcp2515.git), but when I try to use that function in the following test script:
#include <mcp2515.h>
#include <ArduinoCanUtils.h>
MCP2515 mcp2515Global(10);
struct can_frame canMsg = {.can_id = 0, .can_dlc = 8, .data = {0,0,0,0,0,0,0,0}};
ArduinoCanUtils canUtils;
void setup() {
// put your setup code here, to run once:
uint32_t a = canUtils.createCanMsgCanId(1, 1, 2, 1);
uint8_t b = canUtils.getActivityCodeFromCanMsgCanId(a);
canUtils.setAllCanMsgDataToZero(&canMsg);
}
void loop() {
// put your main code here, to run repeatedly:
}
I get the following error log after compiling (see image):
Interestingly, all other functions from my library seem to work fine, only that function poses a problem.
I made sure to include all necessary dependencies. Also, the other functions aside from setAllCanMsgDataToZero
seem to work fine. I verified that I'm declaring the struct can_fame correctly but I don't know why it keeps raising that error. help would be very much appreciated. This is my first time writing a library for Arduino and I tried searching the internet but found little help. I hope you can help me.
I wasn't as familiar with "pass by reference" as I thought. I should do canUtils.setAllCanMsgDataToZero(canMsg)
instead of canUtils.setAllCanMsgDataToZero(&canMsg)
, since in the second case I'm passing the pointer while the function just expects the reference.
It turns out this is not a dependency problem at all.