-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
620 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
#include "silkit/services/can/all.hpp" | ||
#include "silkit/services/can/string_utils.hpp" | ||
#include "silkit/services/logging/ILogger.hpp" | ||
|
||
using namespace SilKit::Services::Can; | ||
|
||
namespace std { | ||
namespace chrono { | ||
std::ostream& operator<<(std::ostream& out, nanoseconds timestamp) | ||
{ | ||
auto seconds = std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1, 1>>>(timestamp); | ||
out << seconds.count() << "s"; | ||
return out; | ||
} | ||
} // namespace chrono | ||
} // namespace std | ||
|
||
namespace CanBehavior { | ||
|
||
void FrameTransmitHandler(const CanFrameTransmitEvent& ack, ILogger* logger) | ||
{ | ||
std::stringstream buffer; | ||
buffer << ">> " << ack.status << " for CAN frame with timestamp=" << ack.timestamp | ||
<< " and userContext=" << ack.userContext; | ||
logger->Info(buffer.str()); | ||
} | ||
|
||
void FrameHandler(const CanFrameEvent& frameEvent, ILogger* logger) | ||
{ | ||
std::string payload(frameEvent.frame.dataField.begin(), frameEvent.frame.dataField.end()); | ||
std::stringstream buffer; | ||
buffer << ">> CAN frame: canId=" << frameEvent.frame.canId << " timestamp=" << frameEvent.timestamp << " \"" | ||
<< payload << "\""; | ||
logger->Info(buffer.str()); | ||
} | ||
|
||
void SendFrame(ICanController* controller, ILogger* logger) | ||
{ | ||
CanFrame canFrame{}; | ||
canFrame.canId = 3; | ||
canFrame.flags |= static_cast<CanFrameFlagMask>(CanFrameFlag::Fdf) // FD Format Indicator | ||
| static_cast<CanFrameFlagMask>(CanFrameFlag::Brs); // Bit Rate Switch (for FD Format only) | ||
|
||
static int msgId = 0; | ||
const auto currentMessageId = msgId++; | ||
|
||
std::stringstream payloadBuilder; | ||
payloadBuilder << "CAN " << (currentMessageId % 100); | ||
auto payloadStr = payloadBuilder.str(); | ||
|
||
std::vector<uint8_t> payloadBytes; | ||
payloadBytes.resize(payloadStr.size()); | ||
std::copy(payloadStr.begin(), payloadStr.end(), payloadBytes.begin()); | ||
|
||
canFrame.dataField = payloadBytes; | ||
canFrame.dlc = static_cast<uint16_t>(canFrame.dataField.size()); | ||
|
||
void* const userContext = reinterpret_cast<void*>(static_cast<intptr_t>(currentMessageId)); | ||
|
||
controller->SendFrame(std::move(canFrame), userContext); | ||
std::stringstream buffer; | ||
buffer << "<< CAN frame sent with userContext=" << userContext; | ||
logger->Info(buffer.str()); | ||
} | ||
|
||
} // namespace CanDemo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.