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_i2c_bus_count();
191 }
192 
199 inline int
200 getI2cBusId(int i2c_bus)
201 {
202  return mraa_get_i2c_bus_id(i2c_bus);
203 }
204 
212 inline std::string
213 getPinName(int pin)
214 {
215  std::string ret_val(mraa_get_pin_name(pin));
216  return ret_val;
217 }
218 
226 inline int
227 getGpioLookup(std::string pin_name)
228 {
229  int index = mraa_gpio_lookup(pin_name.c_str());
230 
231  if (index < 0){
232  std::ostringstream oss;
233  oss << "Gpio name " << pin_name << " is not valid";
234  throw std::invalid_argument(oss.str());
235  }
236 
237  return index;
238 }
239 
247 inline int
248 getI2cLookup(std::string i2c_name)
249 {
250  int index = mraa_i2c_lookup(i2c_name.c_str());
251 
252  if (index < 0){
253  std::ostringstream oss;
254  oss << "i2c name " << i2c_name << " is not valid";
255  throw std::invalid_argument(oss.str());
256  }
257 
258  return index;
259 }
260 
268 inline int
269 getSpiLookup(std::string spi_name)
270 {
271  int index = mraa_spi_lookup(spi_name.c_str());
272 
273  if (index < 0){
274  std::ostringstream oss;
275  oss << "Spi name " << spi_name << " is not valid";
276  throw std::invalid_argument(oss.str());
277  }
278 
279  return index;
280 }
281 
289 inline int
290 getPwmLookup(std::string pwm_name)
291 {
292  int index = mraa_pwm_lookup(pwm_name.c_str());
293 
294  if (index < 0){
295  std::ostringstream oss;
296  oss << "PWM name " << pwm_name << " is not valid";
297  throw std::invalid_argument(oss.str());
298  }
299 
300  return index;
301 }
302 
310 inline int
311 getUartLookup(std::string uart_name)
312 {
313  int index = mraa_uart_lookup(uart_name.c_str());
314 
315  if (index < 0) {
316  std::ostringstream oss;
317  oss << "UART name " << uart_name << " is not valid";
318  throw std::invalid_argument(oss.str());
319  }
320 
321  return index;
322 }
323 
331 inline Result
332 setLogLevel(int level)
333 {
334  return (Result) mraa_set_log_level(level);
335 }
336 
342 inline bool
344 {
345  return static_cast<bool>(mraa_has_sub_platform());
346 }
347 
355 inline bool
356 isSubPlatformId(int pin_or_bus_id)
357 {
358  return static_cast<bool>(mraa_is_sub_platform_id(pin_or_bus_id));
359 }
360 
368 inline int
369 getSubPlatformId(int pin_or_bus_index)
370 {
371  return mraa_get_sub_platform_id(pin_or_bus_index);
372 }
373 
381 inline int
382 getSubPlatformIndex(int pin_or_bus_id)
383 {
384  return mraa_get_sub_platform_index(pin_or_bus_id);
385 }
386 
393 inline int
394 getDefaultI2cBus(int platform_offset=MRAA_MAIN_PLATFORM_OFFSET)
395 {
396  return mraa_get_default_i2c_bus(platform_offset);
397 }
398 
407 inline Result
408 addSubplatform(Platform subplatformtype, std::string uart_dev)
409 {
410  return (Result) mraa_add_subplatform((mraa_platform_t) subplatformtype, uart_dev.c_str());
411 }
412 
413 inline Result
414 removeSubplatform(Platform subplatformtype)
415 {
416  return (Result) mraa_remove_subplatform((mraa_platform_t) subplatformtype);
417 }
418 
430 template <class T>
431 inline T*
432 initIo(std::string desc)
433 {
434  return new T(mraa_init_io(desc.c_str()));
435 }
436 
445 inline Result
446 initJsonPlatform(std::string path)
447 {
448  return (Result) mraa_init_json_platform(path.c_str());
449 }
450 
451 }
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:332
Result addSubplatform(Platform subplatformtype, std::string uart_dev)
Definition: common.hpp:408
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:446
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:290
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:188
mraa_boolean_t mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
int getI2cLookup(std::string i2c_name)
Definition: common.hpp:248
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:200
bool isSubPlatformId(int pin_or_bus_id)
Definition: common.hpp:356
int mraa_get_i2c_bus_count()
int getDefaultI2cBus(int platform_offset=MRAA_MAIN_PLATFORM_OFFSET)
Definition: common.hpp:394
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:432
unsigned int adcSupportedBits()
Definition: common.hpp:140
int mraa_set_priority(const int priority)
int getSpiLookup(std::string spi_name)
Definition: common.hpp:269
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:227
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:343
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:213
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)
unsigned int adcRawBits()
Definition: common.hpp:129
Pinmodes
Definition: types.hpp:222
int getUartLookup(std::string uart_name)
Definition: common.hpp:311
void mraa_result_print(mraa_result_t result)
int getSubPlatformId(int pin_or_bus_index)
Definition: common.hpp:369
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:382
unsigned int mraa_get_pin_count()