mraa  1.7.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
 All Data Structures Namespaces Files Functions Typedefs Enumerations Enumerator Macros Pages
common.hpp
Go to the documentation of this file.
1 /*
2  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #pragma once
26 
27 #include "common.h"
28 #include "types.hpp"
29 #include <string>
30 #include <sstream>
31 #include <stdexcept>
32 
36 namespace mraa
37 {
38 
55 inline Result
57 {
58  return (Result) mraa_init();
59 }
60 
66 inline std::string
68 {
69  std::string ret = mraa_get_version();
70  return ret;
71 }
72 
82 inline int
83 setPriority(const int priority)
84 {
85  return mraa_set_priority(priority);
86 }
87 
93 inline Platform
95 {
97 }
98 
104 inline void
106 {
108 }
109 
117 inline bool
118 pinModeTest(int pin, Pinmodes mode)
119 {
120  return (bool) mraa_pin_mode_test(pin, (mraa_pinmodes_t) mode);
121 }
122 
128 inline unsigned int
130 {
131  return mraa_adc_raw_bits();
132 }
133 
139 inline unsigned int
141 {
142  return mraa_adc_supported_bits();
143 }
144 
150 inline std::string
152 {
153  std::string ret_val(mraa_get_platform_name());
154  return ret_val;
155 }
156 
163 inline std::string
164 getPlatformVersion(int platform_offset=MRAA_MAIN_PLATFORM_OFFSET)
165 {
166  std::string ret_val(mraa_get_platform_version(platform_offset));
167  return ret_val;
168 }
169 
175 inline unsigned int
177 {
178  return mraa_get_pin_count();
179 }
180 
187 inline int
189 {
190  return mraa_get_uart_count();
191 }
192 
199 inline int
201 {
202  return mraa_get_i2c_bus_count();
203 }
204 
211 inline int
212 getI2cBusId(int i2c_bus)
213 {
214  return mraa_get_i2c_bus_id(i2c_bus);
215 }
216 
224 inline std::string
225 getPinName(int pin)
226 {
227  std::string ret_val(mraa_get_pin_name(pin));
228  return ret_val;
229 }
230 
238 inline int
239 getGpioLookup(std::string pin_name)
240 {
241  int index = mraa_gpio_lookup(pin_name.c_str());
242 
243  if (index < 0){
244  std::ostringstream oss;
245  oss << "Gpio name " << pin_name << " is not valid";
246  throw std::invalid_argument(oss.str());
247  }
248 
249  return index;
250 }
251 
259 inline int
260 getI2cLookup(std::string i2c_name)
261 {
262  int index = mraa_i2c_lookup(i2c_name.c_str());
263 
264  if (index < 0){
265  std::ostringstream oss;
266  oss << "i2c name " << i2c_name << " is not valid";
267  throw std::invalid_argument(oss.str());
268  }
269 
270  return index;
271 }
272 
280 inline int
281 getSpiLookup(std::string spi_name)
282 {
283  int index = mraa_spi_lookup(spi_name.c_str());
284 
285  if (index < 0){
286  std::ostringstream oss;
287  oss << "Spi name " << spi_name << " is not valid";
288  throw std::invalid_argument(oss.str());
289  }
290 
291  return index;
292 }
293 
301 inline int
302 getPwmLookup(std::string pwm_name)
303 {
304  int index = mraa_pwm_lookup(pwm_name.c_str());
305 
306  if (index < 0){
307  std::ostringstream oss;
308  oss << "PWM name " << pwm_name << " is not valid";
309  throw std::invalid_argument(oss.str());
310  }
311 
312  return index;
313 }
314 
322 inline int
323 getUartLookup(std::string uart_name)
324 {
325  int index = mraa_uart_lookup(uart_name.c_str());
326 
327  if (index < 0) {
328  std::ostringstream oss;
329  oss << "UART name " << uart_name << " is not valid";
330  throw std::invalid_argument(oss.str());
331  }
332 
333  return index;
334 }
335 
343 inline Result
344 setLogLevel(int level)
345 {
346  return (Result) mraa_set_log_level(level);
347 }
348 
354 inline bool
356 {
357  return static_cast<bool>(mraa_has_sub_platform());
358 }
359 
367 inline bool
368 isSubPlatformId(int pin_or_bus_id)
369 {
370  return static_cast<bool>(mraa_is_sub_platform_id(pin_or_bus_id));
371 }
372 
380 inline int
381 getSubPlatformId(int pin_or_bus_index)
382 {
383  return mraa_get_sub_platform_id(pin_or_bus_index);
384 }
385 
393 inline int
394 getSubPlatformIndex(int pin_or_bus_id)
395 {
396  return mraa_get_sub_platform_index(pin_or_bus_id);
397 }
398 
405 inline int
406 getDefaultI2cBus(int platform_offset=MRAA_MAIN_PLATFORM_OFFSET)
407 {
408  return mraa_get_default_i2c_bus(platform_offset);
409 }
410 
419 inline Result
420 addSubplatform(Platform subplatformtype, std::string uart_dev)
421 {
422  return (Result) mraa_add_subplatform((mraa_platform_t) subplatformtype, uart_dev.c_str());
423 }
424 
425 inline Result
426 removeSubplatform(Platform subplatformtype)
427 {
428  return (Result) mraa_remove_subplatform((mraa_platform_t) subplatformtype);
429 }
430 
442 template <class T>
443 inline T*
444 initIo(std::string desc)
445 {
446  return new T(mraa_init_io(desc.c_str()));
447 }
448 
457 inline Result
458 initJsonPlatform(std::string path)
459 {
460  return (Result) mraa_init_json_platform(path.c_str());
461 }
462 
463 }
Result
Definition: types.hpp:199
std::string getPlatformVersion(int platform_offset=MRAA_MAIN_PLATFORM_OFFSET)
Definition: common.hpp:164
int mraa_spi_lookup(const char *spi_name)
Result setLogLevel(int level)
Definition: common.hpp:344
Result addSubplatform(Platform subplatformtype, std::string uart_dev)
Definition: common.hpp:420
int mraa_i2c_lookup(const char *i2c_name)
mraa_result_t mraa_remove_subplatform(mraa_platform_t subplatformtype)
int mraa_gpio_lookup(const char *pin_name)
const char * mraa_get_version()
Result initJsonPlatform(std::string path)
Definition: common.hpp:458
Result init()
Definition: common.hpp:56
std::string getVersion()
Definition: common.hpp:67
Platform getPlatformType()
Definition: common.hpp:94
int getPwmLookup(std::string pwm_name)
Definition: common.hpp:302
const char * mraa_get_platform_name()
mraa_platform_t
Definition: types.h:39
mraa_platform_t mraa_get_platform_type()
void printError(Result result)
Definition: common.hpp:105
int getI2cBusCount()
Definition: common.hpp:200
int getUartCount()
Definition: common.hpp:188
mraa_boolean_t mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
int getI2cLookup(std::string i2c_name)
Definition: common.hpp:260
int mraa_get_sub_platform_index(int pin_or_bus_id)
unsigned int mraa_adc_supported_bits()
int getI2cBusId(int i2c_bus)
Definition: common.hpp:212
bool isSubPlatformId(int pin_or_bus_id)
Definition: common.hpp:368
int mraa_get_i2c_bus_count()
int getDefaultI2cBus(int platform_offset=MRAA_MAIN_PLATFORM_OFFSET)
Definition: common.hpp:406
int mraa_get_i2c_bus_id(int i2c_bus)
mraa_boolean_t mraa_is_sub_platform_id(int pin_or_bus_id)
T * initIo(std::string desc)
Definition: common.hpp:444
unsigned int adcSupportedBits()
Definition: common.hpp:140
int mraa_set_priority(const int priority)
int getSpiLookup(std::string spi_name)
Definition: common.hpp:281
char * mraa_get_pin_name(int pin)
unsigned int getPinCount()
Definition: common.hpp:176
Platform
Definition: types.hpp:40
mraa_result_t mraa_init_json_platform(const char *path)
mraa_result_t mraa_init() __attribute__((constructor))
int getGpioLookup(std::string pin_name)
Definition: common.hpp:239
int mraa_uart_lookup(const char *uart_name)
std::string getPlatformName()
Definition: common.hpp:151
int mraa_pwm_lookup(const char *pwm_name)
bool pinModeTest(int pin, Pinmodes mode)
Definition: common.hpp:118
bool hasSubPlatform()
Definition: common.hpp:355
int mraa_get_sub_platform_id(int pin_or_bus_index)
unsigned int mraa_adc_raw_bits()
mraa_result_t
Definition: types.h:205
void * mraa_init_io(const char *desc)
int setPriority(const int priority)
Definition: common.hpp:83
mraa_pinmodes_t
Definition: types.h:228
std::string getPinName(int pin)
Definition: common.hpp:225
const char * mraa_get_platform_version(int platform_offset)
mraa_boolean_t mraa_has_sub_platform()
mraa_result_t mraa_add_subplatform(mraa_platform_t subplatformtype, const char *uart_dev)
int mraa_get_uart_count(void)
unsigned int adcRawBits()
Definition: common.hpp:129
Pinmodes
Definition: types.hpp:222
int getUartLookup(std::string uart_name)
Definition: common.hpp:323
void mraa_result_print(mraa_result_t result)
int getSubPlatformId(int pin_or_bus_index)
Definition: common.hpp:381
mraa_result_t mraa_set_log_level(int level)
int mraa_get_default_i2c_bus(uint8_t platform_offset)
int getSubPlatformIndex(int pin_or_bus_id)
Definition: common.hpp:394
unsigned int mraa_get_pin_count()