MotionLib  1.0.0
SamBuCa motion library
GrblGpio.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-16T13:47:28
21 ** Author: Sylvain Fargier <sylvain.fargier@cern.ch>
22 */
23 
24 #include "GrblGpio.hpp"
25 
26 #include <ccut/async.hpp>
27 
28 #include "GrblPlatform.hpp"
29 #include "util/grbl/GrblDeviceBase.hpp"
30 #include "util/grbl/GrblGenerator.hpp"
31 
32 using namespace logger;
33 using namespace smc;
34 using namespace smc::internal;
35 using namespace smc::units;
36 
38 
39 GrblGpio::GrblGpio(const std::string &uid,
40  const std::shared_ptr<GrblPlatform> &grbl) :
41  Gpio{uid},
42  m_grbl(grbl)
43 {}
44 
45 std::future<void> GrblGpio::setIoState(const units::io_port_t &state) const
46 {
47  std::shared_ptr<GrblPlatform> grbl(m_grbl.lock());
48  if (!grbl)
49  return ccut::make_future_error(
50  Exception(ErrorCode::Canceled, "platform stopped (deleted)"));
51 
52  try
53  {
54  uint8_t gpioPin = grbl->getGpio(uid());
55  return grbl->run<void>(
56  [gpioPin, state](ImmediateCmd &cmd, GrblPlatform::Context &ctx) {
57  grbl::ErrorCode rc;
58  if (!state)
59  rc = ctx.device->send(
60  grbl::gen::DigitalOut::off(gpioPin, true).value);
61  else
62  rc = ctx.device->send(
63  grbl::gen::DigitalOut::on(gpioPin, true).value);
64  ctx.checkReply(rc);
65  cmd.prom->get<void>().set_value();
66  });
67  }
68  catch (const std::exception &e)
69  {
70  return ccut::make_future_error(e);
71  }
72 }
73 
74 std::future<io_port_t> GrblGpio::getIoState() const
75 {
76  return ccut::make_future_error<io_port_t>(
77  Exception{ErrorCode::NotSupported,
78  std::string("getIoState not supported on: ") + uid()});
79 }
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
virtual std::future< units::io_port_t > getIoState() const override
Get current state of this GPIO.
Definition: GrblGpio.cpp:74
main motion-lib namespace
Definition: Client.cpp:30