Viam C++ SDK current
Loading...
Searching...
No Matches
board_client.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <viam/api/component/board/v1/board.grpc.pb.h>
7
10#include <viam/sdk/config/resource.hpp>
12#include <viam/sdk/rpc/dial.hpp>
13
14namespace viam {
15namespace sdk {
16namespace impl {
17
21class BoardClient : public Board {
22 public:
23 using interface_type = Board;
24 BoardClient(std::string name, const ViamChannel& channel);
25
26 const ViamChannel& channel() const {
27 return *channel_;
28 }
29 ProtoStruct do_command(const ProtoStruct& command) override;
30 void set_gpio(const std::string& pin, bool high, const ProtoStruct& extra) override;
31 bool get_gpio(const std::string& pin, const ProtoStruct& extra) override;
32 double get_pwm_duty_cycle(const std::string& pin, const ProtoStruct& extra) override;
33 void set_pwm_duty_cycle(const std::string& pin,
34 double duty_cycle_pct,
35 const ProtoStruct& extra) override;
36 uint64_t get_pwm_frequency(const std::string& pin, const ProtoStruct& extra) override;
37 void set_pwm_frequency(const std::string& pin,
38 uint64_t frequency_hz,
39 const ProtoStruct& extra) override;
40 analog_response read_analog(const std::string& analog_reader_name,
41 const ProtoStruct& extra) override;
42 void write_analog(const std::string& pin, int value, const ProtoStruct& extra) override;
43 digital_value read_digital_interrupt(const std::string& digital_interrupt_name,
44 const ProtoStruct& extra) override;
46 const ProtoStruct& extra,
47 const boost::optional<std::chrono::microseconds>& duration) override;
48 std::vector<GeometryConfig> get_geometries(const ProtoStruct& extra) override;
49
50 void stream_ticks(std::vector<std::string> const& digital_interrupt_names,
51 std::function<bool(Tick&& tick)> const& tick_handler,
52 const ProtoStruct& extra) override;
53
54 // the `extra` param is frequently unnecessary but needs to be supported. Ideally, we'd
55 // like to live in a world where implementers of derived classes don't need to go out of
56 // their way to support two versions of a method (an `extra` version and a non-`extra`
57 // version), and users don't need to pass an unnecessary parameters to all method calls.
58 //
59 // To do this, we define in the parent resource class a non-virtual version of the methods
60 // that calls the virtual method and passes a `nullptr` by default in place of the `extra`
61 // param. In order to access these versions of the methods within the client code, however,
62 // we need to include these `using` lines.
64 using Board::get_gpio;
69 using Board::set_gpio;
75
76 private:
77 using StubType = viam::component::board::v1::BoardService::StubInterface;
78 std::unique_ptr<StubType> stub_;
79 const ViamChannel* channel_;
80};
81
82} // namespace impl
83} // namespace sdk
84} // namespace viam
Defines a Board component.
Implements a gRPC server for the Board component.
Represents a physical board with gpio pins, digital interrupts, and analog voltage reading.
Definition board.hpp:24
power_mode
Power mode of the board The effect of these power modes depends on your physical board.
Definition board.hpp:73
std::vector< GeometryConfig > get_geometries()
Returns GeometryConfigs associated with the calling board.
Definition board.hpp:242
void set_power_mode(power_mode power_mode, const boost::optional< std::chrono::microseconds > &duration={})
Sets the power consumption mode of the board to the requested setting for the given duration.
Definition board.hpp:220
analog_response read_analog(const std::string &analog_reader_name)
Reads off the current value of an analog reader on a board. Consult your ADC's docs or Viam's Board d...
Definition board.hpp:160
void set_gpio(const std::string &pin, bool high)
Set the gpio high/low state of the given pin on a board.
Definition board.hpp:92
uint64_t get_pwm_frequency(const std::string &pin)
Gets the PWM frequency of the given pin on a board.
Definition board.hpp:131
int64_t digital_value
Depending on the type of digital interrupt, this can have different meanings. If a basic (default) in...
Definition board.hpp:47
digital_value read_digital_interrupt(const std::string &digital_interrupt_name)
Returns the current value of the interrupt which is based on the type of interrupt....
Definition board.hpp:187
void set_pwm_duty_cycle(const std::string &pin, double duty_cycle_pct)
Sets the given pin of a board to the given duty cycle.
Definition board.hpp:117
void write_analog(const std::string &pin, int value)
Writes the value to the analog writer of the board.
Definition board.hpp:174
void stream_ticks(std::vector< std::string > const &digital_interrupt_names, std::function< bool(Tick &&tick)> const &tick_handler)
Returns a stream of digital interrupt ticks.
Definition board.hpp:203
void set_pwm_frequency(const std::string &pin, uint64_t frequency_hz)
Sets the given pin on a board to the given PWM frequency. 0 will use the board's default PWM frequenc...
Definition board.hpp:144
bool get_gpio(const std::string &pin)
Gets the high/low state of the given pin on a board.
Definition board.hpp:80
double get_pwm_duty_cycle(const std::string &pin)
Gets the duty cycle of the given pin on a board.
Definition board.hpp:104
virtual std::string name() const
Return the resource's name.
Definition dial.hpp:25
gRPC client implementation of a Board component.
Definition board_client.hpp:21
void set_pwm_duty_cycle(const std::string &pin, double duty_cycle_pct, const ProtoStruct &extra) override
Sets the given pin of a board to the given duty cycle.
ProtoStruct do_command(const ProtoStruct &command) override
Send/receive arbitrary commands to the resource.
void write_analog(const std::string &pin, int value, const ProtoStruct &extra) override
Writes the value to the analog writer of the board.
double get_pwm_duty_cycle(const std::string &pin, const ProtoStruct &extra) override
Gets the duty cycle of the given pin on a board.
void set_pwm_frequency(const std::string &pin, uint64_t frequency_hz, const ProtoStruct &extra) override
Sets the given pin on a board to the given PWM frequency. 0 will use the board's default PWM frequenc...
digital_value read_digital_interrupt(const std::string &digital_interrupt_name, const ProtoStruct &extra) override
Returns the current value of the interrupt which is based on the type of interrupt....
std::vector< GeometryConfig > get_geometries(const ProtoStruct &extra) override
Returns GeometryConfigs associated with the calling board.
void set_power_mode(power_mode power_mode, const ProtoStruct &extra, const boost::optional< std::chrono::microseconds > &duration) override
Sets the power consumption mode of the board to the requested setting for the given duration.
void stream_ticks(std::vector< std::string > const &digital_interrupt_names, std::function< bool(Tick &&tick)> const &tick_handler, const ProtoStruct &extra) override
Returns a stream of digital interrupt ticks.
bool get_gpio(const std::string &pin, const ProtoStruct &extra) override
Gets the high/low state of the given pin on a board.
void set_gpio(const std::string &pin, bool high, const ProtoStruct &extra) override
Set the gpio high/low state of the given pin on a board.
uint64_t get_pwm_frequency(const std::string &pin, const ProtoStruct &extra) override
Gets the PWM frequency of the given pin on a board.
analog_response read_analog(const std::string &analog_reader_name, const ProtoStruct &extra) override
Reads off the current value of an analog reader on a board. Consult your ADC's docs or Viam's Board d...
gRPC client implementation for a robot.
A board's digital interrupt.
Definition board.hpp:59
Represents the response received when reading the registered analog to digital converter (ADC)....
Definition board.hpp:36