Viam C++ SDK current
Loading...
Searching...
No Matches
board.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <chrono>
7#include <string>
8#include <unordered_map>
9
10#include <viam/sdk/common/proto_value.hpp>
11#include <viam/sdk/common/utils.hpp>
12#include <viam/sdk/config/resource.hpp>
13
14namespace viam {
15namespace sdk {
16
24class Board : public Component {
25 public:
29 using analog_value = int32_t;
30
37 analog_value value;
38 float min_range; // Minimum possible voltage read by the analog reader
39 float max_range; // Maximum possible voltage read by the analog reader
40 float step_size; // Volts represented in each step in the value
41 };
42
47 using digital_value = int64_t;
48
52 struct status {
53 std::unordered_map<std::string, analog_value> analog_reader_values;
54 std::unordered_map<std::string, digital_value> digital_interrupt_values;
55 };
56
59 struct Tick {
61 std::string pin_name;
62
64 std::chrono::nanoseconds time;
65
67 bool high;
68 };
69
73 enum class power_mode : uint8_t { normal = 0, offline_deep = 1 };
74
75 API api() const override;
76
80 inline bool get_gpio(const std::string& pin) {
81 return get_gpio(pin, {});
82 }
83
88 virtual bool get_gpio(const std::string& pin, const ProtoStruct& extra) = 0;
89
92 inline void set_gpio(const std::string& pin, bool high) {
93 return set_gpio(pin, high, {});
94 }
95
99 virtual void set_gpio(const std::string& pin, bool high, const ProtoStruct& extra) = 0;
100
104 inline double get_pwm_duty_cycle(const std::string& pin) {
105 return get_pwm_duty_cycle(pin, {});
106 }
107
112 virtual double get_pwm_duty_cycle(const std::string& pin, const ProtoStruct& extra) = 0;
113
117 inline void set_pwm_duty_cycle(const std::string& pin, double duty_cycle_pct) {
118 return set_pwm_duty_cycle(pin, duty_cycle_pct, {});
119 }
120
125 virtual void set_pwm_duty_cycle(const std::string& pin,
126 double duty_cycle_pct,
127 const ProtoStruct& extra) = 0;
128
131 inline uint64_t get_pwm_frequency(const std::string& pin) {
132 return get_pwm_frequency(pin, {});
133 }
134
138 virtual uint64_t get_pwm_frequency(const std::string& pin, const ProtoStruct& extra) = 0;
139
144 inline void set_pwm_frequency(const std::string& pin, uint64_t frequency_hz) {
145 return set_pwm_frequency(pin, frequency_hz, {});
146 }
147
153 virtual void set_pwm_frequency(const std::string& pin,
154 uint64_t frequency_hz,
155 const ProtoStruct& extra) = 0;
156
160 inline analog_response read_analog(const std::string& analog_reader_name) {
161 return read_analog(analog_reader_name, {});
162 }
163
168 virtual analog_response read_analog(const std::string& analog_reader_name,
169 const ProtoStruct& extra) = 0;
170
174 inline void write_analog(const std::string& pin, int value) {
175 return write_analog(pin, value, {});
176 }
177
182 virtual void write_analog(const std::string& pin, int value, const ProtoStruct& extra) = 0;
183
187 inline digital_value read_digital_interrupt(const std::string& digital_interrupt_name) {
188 return read_digital_interrupt(digital_interrupt_name, {});
189 }
190
195 virtual digital_value read_digital_interrupt(const std::string& digital_interrupt_name,
196 const ProtoStruct& extra) = 0;
197
203 inline void stream_ticks(std::vector<std::string> const& digital_interrupt_names,
204 std::function<bool(Tick&& tick)> const& tick_handler) {
205 return stream_ticks(digital_interrupt_names, tick_handler, {});
206 }
207
212 virtual void stream_ticks(std::vector<std::string> const& digital_interrupt_names,
213 std::function<bool(Tick&& tick)> const& tick_handler,
214 const ProtoStruct& extra) = 0;
215
221 const boost::optional<std::chrono::microseconds>& duration = {}) {
222 return set_power_mode(power_mode, {}, duration);
223 }
224
230 virtual void set_power_mode(
232 const ProtoStruct& extra,
233 const boost::optional<std::chrono::microseconds>& duration = {}) = 0;
234
238 virtual ProtoStruct do_command(const ProtoStruct& command) = 0;
239
242 inline std::vector<GeometryConfig> get_geometries() {
243 return get_geometries({});
244 }
245
249 virtual std::vector<GeometryConfig> get_geometries(const ProtoStruct& extra) = 0;
250
251 protected:
252 explicit Board(std::string name);
253};
254
255template <>
257 static API api();
258};
259
260bool operator==(const Board::status& lhs, const Board::status& rhs);
261
262} // namespace sdk
263} // namespace viam
Extends APIType to additionally define a resource's subtype (e.g., camera).
Definition resource_api.hpp:33
Represents a physical board with gpio pins, digital interrupts, and analog voltage reading.
Definition board.hpp:24
virtual digital_value read_digital_interrupt(const std::string &digital_interrupt_name, const ProtoStruct &extra)=0
Returns the current value of the interrupt which is based on the type of interrupt....
virtual std::vector< GeometryConfig > get_geometries(const ProtoStruct &extra)=0
Returns GeometryConfigs associated with the calling board.
power_mode
Power mode of the board The effect of these power modes depends on your physical board.
Definition board.hpp:73
virtual void set_pwm_frequency(const std::string &pin, uint64_t frequency_hz, const ProtoStruct &extra)=0
Sets the given pin on a board to the given PWM frequency. 0 will use the board's default PWM frequenc...
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
virtual ProtoStruct do_command(const ProtoStruct &command)=0
Send/receive arbitrary commands to the resource.
virtual uint64_t get_pwm_frequency(const std::string &pin, const ProtoStruct &extra)=0
Gets the PWM frequency of the given pin on a board.
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
virtual bool get_gpio(const std::string &pin, const ProtoStruct &extra)=0
Gets the high/low state of the given pin on a board.
uint64_t get_pwm_frequency(const std::string &pin)
Gets the PWM frequency of the given pin on a board.
Definition board.hpp:131
virtual analog_response read_analog(const std::string &analog_reader_name, const ProtoStruct &extra)=0
Reads off the current value of an analog reader on a board. Consult your ADC's docs or Viam's Board d...
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
virtual void set_pwm_duty_cycle(const std::string &pin, double duty_cycle_pct, const ProtoStruct &extra)=0
Sets the given pin of a board to the given duty cycle.
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
int32_t analog_value
Represents the raw value received by the registered analog to digital converter (ADC)....
Definition board.hpp:29
API api() const override
Returns the API associated with a particular resource.
virtual void write_analog(const std::string &pin, int value, const ProtoStruct &extra)=0
Writes the value to the analog writer of the board.
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
virtual double get_pwm_duty_cycle(const std::string &pin, const ProtoStruct &extra)=0
Gets the duty cycle of the given pin on a board.
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
virtual void stream_ticks(std::vector< std::string > const &digital_interrupt_names, std::function< bool(Tick &&tick)> const &tick_handler, const ProtoStruct &extra)=0
Returns a stream of digital interrupt ticks.
virtual void set_gpio(const std::string &pin, bool high, const ProtoStruct &extra)=0
Set the gpio high/low state of the given pin on a board.
virtual void set_power_mode(power_mode power_mode, const ProtoStruct &extra, const boost::optional< std::chrono::microseconds > &duration={})=0
Sets the power consumption mode of the board to the requested setting for the given duration.
double get_pwm_duty_cycle(const std::string &pin)
Gets the duty cycle of the given pin on a board.
Definition board.hpp:104
Definition component.hpp:10
virtual std::string name() const
Return the resource's name.
A board's digital interrupt.
Definition resource_api.hpp:50
Definition board.hpp:59
std::chrono::nanoseconds time
time in nanoseconds the tick occured. This does not represent an absolute time.
Definition board.hpp:64
std::string pin_name
name of the digital interrupt pin.
Definition board.hpp:61
bool high
bool high or low.
Definition board.hpp:67
Represents the response received when reading the registered analog to digital converter (ADC)....
Definition board.hpp:36
This contains all of the values for all of the registered analog readers and digital interrupts that ...
Definition board.hpp:52