diff --git a/Demos/Lin/LinDemoDynamic.cpp b/Demos/Lin/LinDemoDynamic.cpp index 9ba4f579c..083dfc675 100644 --- a/Demos/Lin/LinDemoDynamic.cpp +++ b/Demos/Lin/LinDemoDynamic.cpp @@ -396,8 +396,20 @@ try std::cout << "Creating participant '" << participantName << "' with registry " << registryUri << std::endl; auto participant = SilKit::CreateParticipant(participantConfiguration, participantName, registryUri); - auto* lifecycleService = participant->CreateLifecycleService({OperationMode::Coordinated}); - auto* timeSyncService = lifecycleService->CreateTimeSyncService(); + + ILifecycleService* lifecycleService{nullptr}; + ITimeSyncService* timeSyncService{nullptr}; + + if (runSync) + { + lifecycleService = participant->CreateLifecycleService({OperationMode::Coordinated}); + timeSyncService = lifecycleService->CreateTimeSyncService(); + } + else + { + lifecycleService = participant->CreateLifecycleService({OperationMode::Autonomous}); + } + auto* linController = participant->CreateLinController("LIN1", "LIN1"); // Set a Stop and Shutdown Handler @@ -409,8 +421,12 @@ try if (participantName == "LinMaster") { - lifecycleService->SetCommunicationReadyHandler( - [&participantName, linController]() { InitLinMaster(linController, participantName); }); + if (runSync) + { + lifecycleService->SetCommunicationReadyHandler( + [&participantName, linController]() { InitLinMaster(linController, participantName); }); + } + linController->AddFrameStatusHandler( [&master](ILinController* linController, const LinFrameStatusEvent& frameStatusEvent) { master.FrameStatusHandler(linController, frameStatusEvent); @@ -450,21 +466,25 @@ try std::thread workerThread; auto now = 0ms; - workerThread = std::thread{[&]() { - while (lifecycleService->State() == ParticipantState::ReadyToRun - || lifecycleService->State() == ParticipantState::Running) - { - master.DoAction(now); - now += 1ms; - std::this_thread::sleep_for(200ms); - } - if (!isStopRequested) - { - std::cout << "Press enter to end the process..." << std::endl; - } - }}; + lifecycleService->SetStartingHandler([&] { + workerThread = std::thread{[&]() { + while (lifecycleService->State() == ParticipantState::ReadyToRun + || lifecycleService->State() == ParticipantState::Running) + { + master.DoAction(now); + now += 1ms; + std::this_thread::sleep_for(200ms); + } + + if (!isStopRequested) + { + std::cout << "Press enter to end the process..." << std::endl; + } + }}; + }); lifecycleService->StartLifecycle(); + std::cout << "Press enter to leave the simulation..." << std::endl; std::cin.ignore(); @@ -485,8 +505,11 @@ try } else if (participantName == "LinSlave") { - lifecycleService->SetCommunicationReadyHandler( - [&participantName, linController]() { InitLinSlave(linController, participantName); }); + if (runSync) + { + lifecycleService->SetCommunicationReadyHandler( + [&participantName, linController]() { InitLinSlave(linController, participantName); }); + } linController->AddFrameStatusHandler( [&slave](ILinController* linController, const LinFrameStatusEvent& frameStatusEvent) { @@ -532,21 +555,25 @@ try std::thread workerThread; auto now = 0ms; - workerThread = std::thread{[&]() { - while (lifecycleService->State() == ParticipantState::ReadyToRun - || lifecycleService->State() == ParticipantState::Running) - { - slave.DoAction(now); - now += 1ms; - std::this_thread::sleep_for(200ms); - } - if (!isStopRequested) - { - std::cout << "Press enter to end the process..." << std::endl; - } - }}; + lifecycleService->SetStartingHandler([&] { + workerThread = std::thread{[&]() { + while (lifecycleService->State() == ParticipantState::ReadyToRun + || lifecycleService->State() == ParticipantState::Running) + { + slave.DoAction(now); + now += 1ms; + std::this_thread::sleep_for(200ms); + } + + if (!isStopRequested) + { + std::cout << "Press enter to end the process..." << std::endl; + } + }}; + }); lifecycleService->StartLifecycle(); + std::cout << "Press enter to leave the simulation..." << std::endl; std::cin.ignore();