temperature.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, 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 <stdexcept>
32 
33 #include <libphidget22/phidget22.h>
34 
35 #include "phidgets_api/phidget22.h"
37 
38 namespace phidgets {
39 
40 Temperature::Temperature(int32_t serial_number, int hub_port,
41  bool is_hub_port_device,
42  std::function<void(double)> temperature_handler)
43  : temperature_handler_(temperature_handler)
44 {
45  PhidgetReturnCode ret =
46  PhidgetTemperatureSensor_create(&temperature_handle_);
47  if (ret != EPHIDGET_OK)
48  {
49  throw Phidget22Error("Failed to create TemperatureSensor handle", ret);
50  }
51 
53  reinterpret_cast<PhidgetHandle>(temperature_handle_), serial_number,
54  hub_port, is_hub_port_device, 0);
55 
56  ret = PhidgetTemperatureSensor_setOnTemperatureChangeHandler(
58  if (ret != EPHIDGET_OK)
59  {
60  throw Phidget22Error("Failed to set change handler for Temperature",
61  ret);
62  }
63 }
64 
66 {
67  PhidgetHandle handle = reinterpret_cast<PhidgetHandle>(temperature_handle_);
68  helpers::closeAndDelete(&handle);
69 }
70 
72 {
73  PhidgetReturnCode ret = PhidgetTemperatureSensor_setThermocoupleType(
75  static_cast<PhidgetTemperatureSensor_ThermocoupleType>(type));
76  if (ret != EPHIDGET_OK)
77  {
78  throw Phidget22Error("Failed to set Temperature thermocouple type",
79  ret);
80  }
81 }
82 
84 {
85  double current_temperature;
86  PhidgetReturnCode ret = PhidgetTemperatureSensor_getTemperature(
87  temperature_handle_, &current_temperature);
88  if (ret != EPHIDGET_OK)
89  {
90  throw Phidget22Error("Failed to get temperature", ret);
91  }
92  return current_temperature;
93 }
94 
95 void Temperature::setDataInterval(uint32_t interval_ms) const
96 {
97  PhidgetReturnCode ret = PhidgetTemperatureSensor_setDataInterval(
98  temperature_handle_, interval_ms);
99  if (ret != EPHIDGET_OK)
100  {
101  throw Phidget22Error("Failed to set data interval", ret);
102  }
103 }
104 
105 void Temperature::temperatureChangeHandler(double temperature) const
106 {
107  temperature_handler_(temperature);
108 }
109 
111  PhidgetTemperatureSensorHandle /* temperature_handle */, void *ctx,
112  double temperature)
113 {
114  ((Temperature *)ctx)->temperatureChangeHandler(temperature);
115 }
116 
117 } // namespace phidgets
phidgets
Definition: accelerometer.h:39
temperature.h
phidgets::Temperature::Temperature
Temperature(int32_t serial_number, int hub_port, bool is_hub_port_device, std::function< void(double)> temperature_handler)
Definition: temperature.cpp:40
phidgets::Temperature
Definition: temperature.h:48
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::helpers::closeAndDelete
void closeAndDelete(PhidgetHandle *handle) noexcept
Definition: phidget22.cpp:93
phidgets::Temperature::setThermocoupleType
void setThermocoupleType(ThermocoupleType type)
Definition: temperature.cpp:71
phidget22.h
phidgets::Phidget22Error
Definition: phidget22.h:46
phidgets::Temperature::getTemperature
double getTemperature() const
Definition: temperature.cpp:83
phidgets::Temperature::TemperatureChangeHandler
static void TemperatureChangeHandler(PhidgetTemperatureSensorHandle temperature_handle, void *ctx, double temperature)
Definition: temperature.cpp:110
phidgets::Temperature::temperature_handler_
std::function< void(double)> temperature_handler_
Definition: temperature.h:68
phidgets::Temperature::setDataInterval
void setDataInterval(uint32_t interval_ms) const
Definition: temperature.cpp:95
phidgets::Temperature::~Temperature
~Temperature()
Definition: temperature.cpp:65
phidgets::Temperature::temperatureChangeHandler
void temperatureChangeHandler(double temperature) const
Definition: temperature.cpp:105
phidgets::Temperature::temperature_handle_
PhidgetTemperatureSensorHandle temperature_handle_
Definition: temperature.h:69
phidgets::ThermocoupleType
ThermocoupleType
Definition: temperature.h:41


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