MotionLib  1.0.0
SamBuCa motion library
GrblTrigger.cpp
1 /*
2 ** Copyright (C) 2025 CERN
3 **
4 ** This software is provided 'as-is', without any express or implied
5 ** warranty. In no event will the authors be held liable for any damages
6 ** arising from the use of this software.
7 **
8 ** Permission is granted to anyone to use this software for any purpose,
9 ** including commercial applications, and to alter it and redistribute it
10 ** freely, subject to the following restrictions:
11 **
12 ** 1. The origin of this software must not be misrepresented; you must not
13 ** claim that you wrote the original software. If you use this software
14 ** in a product, an acknowledgment in the product documentation would be
15 ** appreciated but is not required.
16 ** 2. Altered source versions must be plainly marked as such, and must not be
17 ** misrepresented as being the original software.
18 ** 3. This notice may not be removed or altered from any source distribution.
19 **
20 ** Created on: 2025-04-16T17:07:32
21 ** Author: Sylvain Fargier <sylvain.fargier@cern.ch>
22 */
23 
24 #include "GrblTrigger.hpp"
25 
26 #include <ccut/async.hpp>
27 
28 #include "GrblPlatform.hpp"
29 #include "device/Trigger.hpp"
30 #include "util/grbl/GrblDeviceBase.hpp"
31 #include "util/grbl/GrblGenerator.hpp"
32 
33 using namespace logger;
34 using namespace smc;
35 using namespace smc::internal;
36 
38 
39 GrblTrigger::GrblTrigger(const std::string &uid,
40  const std::shared_ptr<GrblPlatform> &grbl) :
41  Trigger{uid},
42  m_grbl{grbl}
43 {}
44 
45 std::future<void> GrblTrigger::arm() const
46 {
47  try
48  {
49  std::shared_ptr<GrblPlatform> grbl(m_grbl.lock());
50  if (!grbl)
51  throw Exception(ErrorCode::Canceled, "platform stopped (deleted)");
52 
53  uint8_t trigPin = grbl->getTrigger(uid());
54  return grbl->run<void>(
55  [trigPin](ImmediateCmd &cmd, GrblPlatform::Context &ctx) {
56  grbl::ErrorCode rc = ctx.device->send(
57  grbl::gen::DigitalOut::on(trigPin, true).value);
58  ctx.checkReply(rc);
59  cmd.prom->get<void>().set_value();
60  });
61  }
62  catch (const std::exception &e)
63  {
64  return ccut::make_future_error(e);
65  }
66 }
67 
68 std::future<void> GrblTrigger::disarm() const
69 {
70  try
71  {
72  std::shared_ptr<GrblPlatform> grbl(m_grbl.lock());
73  if (!grbl)
74  throw Exception(ErrorCode::Canceled, "platform stopped (deleted)");
75 
76  uint8_t trigPin = grbl->getTrigger(uid());
77  return grbl->run<void>(
78  [trigPin](ImmediateCmd &cmd, GrblPlatform::Context &ctx) {
79  grbl::ErrorCode rc = ctx.device->send(
80  grbl::gen::DigitalOut::off(trigPin, true).value);
81  ctx.checkReply(rc);
82  cmd.prom->get<void>().set_value();
83  });
84  }
85  catch (const std::exception &e)
86  {
87  return ccut::make_future_error(e);
88  }
89 }
90 
91 std::future<bool> GrblTrigger::isArmed() const
92 {
93  return ccut::make_future_error<bool>(Exception{
94  ErrorCode::InvalidArguments,
95  std::string("Cannot check if non-MGrbl trigger is armed: ") + uid()});
96 }
97 
98 std::future<void> GrblTrigger::trig() const
99 {
100  return disarm();
101 }
DeviceId uid() const
Get the address of device.
Definition: DeviceBase.cpp:76
Exception thrown by MotionController in case of issues with command.
Definition: Exception.hpp:61
std::future< bool > isArmed() const override
check if trigger is armed
Definition: GrblTrigger.cpp:91
std::future< void > trig() const override
soft-trigger the trigger
Definition: GrblTrigger.cpp:98
std::future< void > disarm() const override
disarm the trigger
Definition: GrblTrigger.cpp:68
main motion-lib namespace
Definition: Client.cpp:30