current_input.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2025, Open Source Robotics Foundation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <functional>
31 #include <memory>
32 #include <stdexcept>
33 #include <string>
34 
35 #include <libphidget22/phidget22.h>
36 
38 #include "phidgets_api/phidget22.h"
39 
40 namespace phidgets {
41 
42 CurrentInput::CurrentInput(int32_t serial_number, int hub_port,
43  bool is_hub_port_device, int channel,
44  std::function<void(int, double)> input_handler)
45  : channel_(channel), input_handler_(input_handler)
46 {
47  PhidgetReturnCode ret = PhidgetCurrentInput_create(&ci_handle_);
48  if (ret != EPHIDGET_OK)
49  {
50  throw Phidget22Error(
51  "Failed to create CurrentInput handle for channel " +
52  std::to_string(channel),
53  ret);
54  }
55 
56  helpers::openWaitForAttachment(reinterpret_cast<PhidgetHandle>(ci_handle_),
57  serial_number, hub_port, is_hub_port_device,
58  channel);
59 
60  ret = PhidgetCurrentInput_setOnCurrentChangeHandler(
62  if (ret != EPHIDGET_OK)
63  {
64  throw Phidget22Error(
65  "Failed to set change handler for CurrentInput channel " +
66  std::to_string(channel),
67  ret);
68  }
69 }
70 
72 {
73  PhidgetHandle handle = reinterpret_cast<PhidgetHandle>(ci_handle_);
74  helpers::closeAndDelete(&handle);
75 }
76 
78 {
79  double sensor_value;
80  PhidgetReturnCode ret =
81  PhidgetCurrentInput_getCurrent(ci_handle_, &sensor_value);
82  if (ret != EPHIDGET_OK)
83  {
84  throw Phidget22Error("Failed to get current sensor value", ret);
85  }
86 
87  return sensor_value;
88 }
89 
90 void CurrentInput::setDataInterval(uint32_t data_interval_ms) const
91 {
92  PhidgetReturnCode ret =
93  PhidgetCurrentInput_setDataInterval(ci_handle_, data_interval_ms);
94  if (ret != EPHIDGET_OK)
95  {
96  throw Phidget22Error("Failed to set current data interval", ret);
97  }
98 }
99 
100 void CurrentInput::currentChangeHandler(double sensorValue) const
101 {
102  input_handler_(channel_, sensorValue);
103 }
104 
106  PhidgetCurrentInputHandle /* input_handle */, void *ctx, double sensorValue)
107 {
108  ((CurrentInput *)ctx)->currentChangeHandler(sensorValue);
109 }
110 
111 } // namespace phidgets
phidgets
Definition: accelerometer.h:39
phidgets::CurrentInput::~CurrentInput
~CurrentInput()
Definition: current_input.cpp:71
phidgets::CurrentInput::input_handler_
std::function< void(int, double)> input_handler_
Definition: current_input.h:60
phidgets::helpers::openWaitForAttachment
void openWaitForAttachment(PhidgetHandle handle, int32_t serial_number, int hub_port, bool is_hub_port_device, int channel)
Definition: phidget22.cpp:57
phidgets::CurrentInput::currentChangeHandler
void currentChangeHandler(double sensorValue) const
Definition: current_input.cpp:100
phidgets::helpers::closeAndDelete
void closeAndDelete(PhidgetHandle *handle) noexcept
Definition: phidget22.cpp:93
phidgets::CurrentInput::CurrentChangeHandler
static void CurrentChangeHandler(PhidgetCurrentInputHandle input_handle, void *ctx, double sensorValue)
Definition: current_input.cpp:105
phidgets::CurrentInput::channel_
int channel_
Definition: current_input.h:59
phidget22.h
phidgets::CurrentInput::CurrentInput
CurrentInput(int32_t serial_number, int hub_port, bool is_hub_port_device, int channel, std::function< void(int, double)> input_handler)
Definition: current_input.cpp:42
phidgets::Phidget22Error
Definition: phidget22.h:46
phidgets::CurrentInput::ci_handle_
PhidgetCurrentInputHandle ci_handle_
Definition: current_input.h:61
phidgets::CurrentInput::setDataInterval
void setDataInterval(uint32_t data_interval_ms) const
Definition: current_input.cpp:90
phidgets::CurrentInput
Definition: current_input.h:41
phidgets::CurrentInput::getSensorValue
double getSensorValue() const
Definition: current_input.cpp:77
current_input.h


phidgets_api
Author(s): Tully Foote, Ivan Dryanovski
autogenerated on Sun May 11 2025 02:20:27