current_inputs.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 
39 #include "phidgets_api/phidget22.h"
40 
41 namespace phidgets {
42 
43 CurrentInputs::CurrentInputs(int32_t serial_number, int hub_port,
44  bool is_hub_port_device,
45  std::function<void(int, double)> input_handler)
46 {
47  PhidgetReturnCode ret;
48 
49  PhidgetCurrentInputHandle ai_handle;
50 
51  ret = PhidgetCurrentInput_create(&ai_handle);
52  if (ret != EPHIDGET_OK)
53  {
54  throw Phidget22Error(
55  "Failed to create CurrentInput handle for determining channel "
56  "count",
57  ret);
58  }
59 
60  PhidgetHandle handle = reinterpret_cast<PhidgetHandle>(ai_handle);
61 
62  helpers::openWaitForAttachment(handle, serial_number, hub_port,
63  is_hub_port_device, 0);
64 
65  ret = Phidget_getDeviceChannelCount(handle, PHIDCHCLASS_CURRENTINPUT,
66  &input_count_);
67 
68  helpers::closeAndDelete(&handle);
69 
70  if (ret != EPHIDGET_OK)
71  {
72  throw Phidget22Error("Failed to get CurrentInput device channel count",
73  ret);
74  }
75 
76  cis_.resize(input_count_);
77  for (uint32_t i = 0; i < input_count_; ++i)
78  {
79  cis_[i] = std::make_unique<CurrentInput>(
80  serial_number, hub_port, is_hub_port_device, i, input_handler);
81  }
82 }
83 
85 {
86 }
87 
88 uint32_t CurrentInputs::getInputCount() const noexcept
89 {
90  return input_count_;
91 }
92 
93 double CurrentInputs::getSensorValue(int index) const
94 {
95  return cis_.at(index)->getSensorValue();
96 }
97 
98 void CurrentInputs::setDataInterval(int index, uint32_t data_interval_ms) const
99 {
100  cis_.at(index)->setDataInterval(data_interval_ms);
101 }
102 
103 } // namespace phidgets
phidgets
Definition: accelerometer.h:39
phidgets::CurrentInputs::~CurrentInputs
~CurrentInputs()
Definition: current_inputs.cpp:84
phidgets::CurrentInputs::getInputCount
uint32_t getInputCount() const noexcept
Definition: current_inputs.cpp:88
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::CurrentInputs::cis_
std::vector< std::unique_ptr< CurrentInput > > cis_
Definition: current_inputs.h:61
phidgets::helpers::closeAndDelete
void closeAndDelete(PhidgetHandle *handle) noexcept
Definition: phidget22.cpp:93
phidgets::CurrentInputs::setDataInterval
void setDataInterval(int index, uint32_t data_interval_ms) const
Definition: current_inputs.cpp:98
phidget22.h
phidgets::Phidget22Error
Definition: phidget22.h:46
phidgets::CurrentInputs::CurrentInputs
CurrentInputs(int32_t serial_number, int hub_port, bool is_hub_port_device, std::function< void(int, double)> input_handler)
Definition: current_inputs.cpp:43
current_inputs.h
phidgets::CurrentInputs::input_count_
uint32_t input_count_
Definition: current_inputs.h:60
phidgets::CurrentInputs::getSensorValue
double getSensorValue(int index) const
Definition: current_inputs.cpp:93
current_input.h


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