Skip to content

Commit a751c87

Browse files
committed
don't modify status: it doesn't contain the extra data
1 parent 9c83730 commit a751c87

File tree

5 files changed

+22
-35
lines changed

5 files changed

+22
-35
lines changed

src/viam/sdk/components/board.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,7 @@ API API::traits<Board>::api() {
2323
Board::status Board::from_proto(const viam::component::board::v1::Status& proto) {
2424
Board::status status;
2525
for (const auto& analog : proto.analogs()) {
26-
Board::analog_value result;
27-
result.value = analog.second;
28-
// The status does not contain the extra data to describe the accuracy of the reader. We
29-
// fill those in with 0's instead.
30-
result.min_range = 0.0;
31-
result.max_range = 0.0;
32-
result.step_size = 0.0;
33-
status.analog_reader_values.emplace(analog.first, result);
26+
status.analog_reader_values.emplace(analog.first, analog.second);
3427
}
3528
for (const auto& digital : proto.digital_interrupts()) {
3629
status.digital_interrupt_values.emplace(digital.first, digital.second);
@@ -57,7 +50,7 @@ Board::power_mode Board::from_proto(viam::component::board::v1::PowerMode proto)
5750
viam::component::board::v1::Status Board::to_proto(const status& status) {
5851
viam::component::board::v1::Status proto;
5952
for (const auto& analog : status.analog_reader_values) {
60-
proto.mutable_analogs()->insert({analog.first, analog.second.value});
53+
proto.mutable_analogs()->insert({analog.first, analog.second});
6154
}
6255

6356
for (const auto& digital : status.digital_interrupt_values) {
@@ -87,10 +80,5 @@ bool operator==(const Board::status& lhs, const Board::status& rhs) {
8780
lhs.digital_interrupt_values == rhs.digital_interrupt_values);
8881
}
8982

90-
bool operator==(const Board::analog_value& lhs, const Board::analog_value& rhs) {
91-
return (lhs.value == rhs.value && lhs.min_range == rhs.min_range &&
92-
lhs.max_range == rhs.max_range && lhs.step_size == rhs.step_size);
93-
}
94-
9583
} // namespace sdk
9684
} // namespace viam

src/viam/sdk/components/board.hpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ namespace sdk {
2525
/// specific board implementations. This class cannot be used on its own.
2626
class Board : public Component {
2727
public:
28-
/// @brief Represents the value received by the registered analog to digital converter (ADC).
29-
/// The range and conversion mechanism to voltage will vary depending on the specific ADC
30-
/// registered to the pin, though the min and max voltages and step_size can often help with
31-
/// this conversion. Consult your ADC's documentation and Viam's `Board` documentation for
32-
/// more details.
33-
struct analog_value {
34-
int32_t value;
28+
/// @brief Represents the raw value received by the registered analog to digital converter
29+
/// (ADC). The range and conversion mechanism to voltage will vary depending on the specific
30+
/// ADC registered to the pin.
31+
using analog_value = int32_t;
32+
33+
/// @brief Represents the response received when reading the registered analog to digital
34+
/// converter (ADC). The range and conversion mechanism to voltage will vary depending on the
35+
/// specific ADC registered to the pin, though the min and max voltages and step_size can often
36+
/// help with this conversion. Consult your ADC's documentation and Viam's `Board`
37+
/// documentation for more details.
38+
struct analog_response {
39+
analog_value value;
3540
float min_range; // Minimum possible voltage read by the analog reader
3641
float max_range; // Maximum possible voltage read by the analog reader
3742
float step_size; // Volts represented in each step in the value
@@ -166,16 +171,16 @@ class Board : public Component {
166171
/// @brief Reads off the current value of an analog reader on a board. Consult your ADC's docs
167172
/// or Viam's `Board` docs for more information.
168173
/// @param analog_reader_name analog reader to read from
169-
inline analog_value read_analog(const std::string& analog_reader_name) {
174+
inline analog_response read_analog(const std::string& analog_reader_name) {
170175
return read_analog(analog_reader_name, {});
171176
}
172177

173178
/// @brief Reads off the current value of an analog reader on a board. Consult your ADC's docs
174179
/// or Viam's `Board` docs for more information.
175180
/// @param analog_reader_name analog reader to read from
176181
/// @param extra Any additional arguments to the method
177-
virtual analog_value read_analog(const std::string& analog_reader_name,
178-
const AttributeMap& extra) = 0;
182+
virtual analog_response read_analog(const std::string& analog_reader_name,
183+
const AttributeMap& extra) = 0;
179184

180185
/// @brief Writes the value to the analog writer of the board.
181186
/// @param pin the pin to write to
@@ -267,7 +272,6 @@ struct API::traits<Board> {
267272
};
268273

269274
bool operator==(const Board::status& lhs, const Board::status& rhs);
270-
bool operator==(const Board::analog_value& lhs, const Board::analog_value& rhs);
271275

272276
} // namespace sdk
273277
} // namespace viam

src/viam/sdk/components/private/board_client.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ AttributeMap BoardClient::do_command(const AttributeMap& command) {
8585

8686
// TODO(RSDK-6048) update `client_wrapper` to allow for requests without a `mutable_name()` method,
8787
// then wrap here.
88-
Board::analog_value BoardClient::read_analog(const std::string& analog_reader_name,
88+
Board::analog_result BoardClient::read_analog(const std::string& analog_reader_name,
8989
const AttributeMap& extra) {
9090
viam::component::board::v1::ReadAnalogReaderRequest request;
9191
viam::component::board::v1::ReadAnalogReaderResponse response;
@@ -99,12 +99,7 @@ Board::analog_value BoardClient::read_analog(const std::string& analog_reader_na
9999
if (!status.ok()) {
100100
throw GRPCException(status);
101101
}
102-
Board::analog_value result = {};
103-
result.value = response.value();
104-
result.min_range = response.min_range();
105-
result.max_range = response.max_range();
106-
result.step_size = response.step_size();
107-
return result;
102+
return {response.value(), response.min_range(), response.max_range, response.step_size()};
108103
}
109104

110105
void BoardClient::write_analog(const std::string& pin, int value, const AttributeMap& extra) {

src/viam/sdk/components/private/board_client.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class BoardClient : public Board {
3434
void set_pwm_frequency(const std::string& pin,
3535
uint64_t frequency_hz,
3636
const AttributeMap& extra) override;
37-
analog_value read_analog(const std::string& analog_reader_name,
38-
const AttributeMap& extra) override;
37+
analog_result read_analog(const std::string& analog_reader_name,
38+
const AttributeMap& extra) override;
3939
void write_analog(const std::string& pin, int value, const AttributeMap& extra) override;
4040
digital_value read_digital_interrupt(const std::string& digital_interrupt_name,
4141
const AttributeMap& extra) override;

src/viam/sdk/components/private/board_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ ::grpc::Status BoardServer::ReadAnalogReader(
103103
extra = struct_to_map(request->extra());
104104
}
105105

106-
const Board::analog_value result = board->read_analog(request->analog_reader_name(), extra);
106+
const Board::analog_response result = board->read_analog(request->analog_reader_name(), extra);
107107
response->set_value(result.value);
108108
response->set_min_range(result.min_range);
109109
response->set_max_range(result.max_range);

0 commit comments

Comments
 (0)