Skip to content

Commit

Permalink
-- adapt LIN demo schedule to make testing consistent (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
VDanielEdwards authored and GitHub Enterprise committed Nov 29, 2023
1 parent 0ed2225 commit 0953fe5
Showing 1 changed file with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions Demos/Lin/LinDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,17 @@ class LinMaster
: controller{controller}
{
schedule = {
{15ms, [this](std::chrono::nanoseconds now) { SendFrame_16(now); }},
{5ms, [this](std::chrono::nanoseconds now) { SendFrame_17(now); }},
{5ms, [this](std::chrono::nanoseconds now) { SendFrame_18(now); }},
{5ms, [this](std::chrono::nanoseconds now) { SendFrame_19(now); }},
{5ms, [this](std::chrono::nanoseconds now) { SendFrame_34(now); }},
{5ms, [this](std::chrono::nanoseconds /*now*/) { GoToSleep(); }}
{10ms, [this](std::chrono::nanoseconds now) { SendFrame_16(now); }},
{20ms, [this](std::chrono::nanoseconds now) { SendFrame_17(now); }},
{10ms, [this](std::chrono::nanoseconds now) { SendFrame_18(now); }},
{10ms, [this](std::chrono::nanoseconds now) { SendFrame_19(now); }},
{10ms, [this](std::chrono::nanoseconds now) { SendFrame_34(now); }},
{10ms, [this](std::chrono::nanoseconds /*now*/) { GoToSleep(); }}
};
}

void DoAction(std::chrono::nanoseconds now)
{
if (controller->Status() != LinControllerStatus::Operational)
return;

schedule.ExecuteTask(now);
}

Expand All @@ -165,10 +162,9 @@ class LinMaster
frame.dataLength = 6;
frame.data = std::array<uint8_t, 8>{1, 6, 1, 6, 1, 6, 1, 6};

controller->SendFrame(frame, LinFrameResponseType::MasterResponse);
std::cout << "<< LIN Frame sent with ID=" << static_cast<uint16_t>(frame.id) << std::endl;
SendFrameIfOperational(frame, LinFrameResponseType::MasterResponse);
}

void SendFrame_17(std::chrono::nanoseconds /*now*/)
{
LinFrame frame;
Expand All @@ -177,8 +173,7 @@ class LinMaster
frame.dataLength = 6;
frame.data = std::array<uint8_t, 8>{1,7,1,7,1,7,1,7};

controller->SendFrame(frame, LinFrameResponseType::MasterResponse);
std::cout << "<< LIN Frame sent with ID=" << static_cast<uint16_t>(frame.id) << std::endl;
SendFrameIfOperational(frame, LinFrameResponseType::MasterResponse);
}

void SendFrame_18(std::chrono::nanoseconds /*now*/)
Expand All @@ -189,8 +184,7 @@ class LinMaster
frame.dataLength = 8;
frame.data = std::array<uint8_t, 8>{0};

controller->SendFrame(frame, LinFrameResponseType::MasterResponse);
std::cout << "<< LIN Frame sent with ID=" << static_cast<uint16_t>(frame.id) << std::endl;
SendFrameIfOperational(frame, LinFrameResponseType::MasterResponse);
}

void SendFrame_19(std::chrono::nanoseconds /*now*/)
Expand All @@ -201,8 +195,7 @@ class LinMaster
frame.dataLength = 8;
frame.data = std::array<uint8_t, 8>{0};

controller->SendFrame(frame, LinFrameResponseType::MasterResponse);
std::cout << "<< LIN Frame sent with ID=" << static_cast<uint16_t>(frame.id) << std::endl;
SendFrameIfOperational(frame, LinFrameResponseType::MasterResponse);
}

void SendFrame_34(std::chrono::nanoseconds /*now*/)
Expand All @@ -212,8 +205,7 @@ class LinMaster
frame.checksumModel = LinChecksumModel::Enhanced;
frame.dataLength = 6;

controller->SendFrame(frame, LinFrameResponseType::SlaveResponse);
std::cout << "<< LIN Frame Header sent for ID=" << static_cast<unsigned int>(frame.id) << std::endl;
SendFrameIfOperational(frame, LinFrameResponseType::SlaveResponse);
}

void GoToSleep()
Expand Down Expand Up @@ -248,6 +240,30 @@ class LinMaster
linController->WakeupInternal();
}

private:
void SendFrameIfOperational(const LinFrame& linFrame, LinFrameResponseType responseType)
{
const auto linId{static_cast<unsigned>(linFrame.id)};

if (controller->Status() != LinControllerStatus::Operational)
{
std::cout << "!! LIN Frame with ID=" << linId << " not sent, since the controller is not operational"
<< std::endl;
return;
}

controller->SendFrame(linFrame, responseType);

if (responseType == LinFrameResponseType::SlaveResponse)
{
std::cout << "<< LIN Frame Header sent for ID=" << linId << std::endl;
}
else
{
std::cout << "<< LIN Frame sent with ID=" << linId << std::endl;
}
}

private:
ILinController* controller{nullptr};
Schedule schedule;
Expand Down Expand Up @@ -293,8 +309,8 @@ class LinSlave
void GoToSleepHandler(ILinController* linController, const LinGoToSleepEvent& /*goToSleepEvent*/)
{
std::cout << "LIN Slave received go-to-sleep command; entering sleep mode." << std::endl;
// wakeup in 10 ms
timer.Set(now + 10ms,
// wakeup in 15 ms
timer.Set(now + 15ms,
[linController](std::chrono::nanoseconds tnow) {
std::cout << "<< Wakeup pulse @" << tnow << std::endl;
linController->Wakeup();
Expand Down Expand Up @@ -465,6 +481,8 @@ int main(int argc, char** argv) try
std::cout << "now=" << nowMs.count() << "ms" << std::endl;

master.DoAction(now);

std::this_thread::sleep_for(100ms);
},
1ms);

Expand Down

0 comments on commit 0953fe5

Please sign in to comment.