mraa  1.9.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
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;
154  const char * pn = mraa_get_platform_name();
155  if (pn) ret_val = pn;
156  return ret_val;
157 }
158 
165 inline std::string
167 {
168  std::string ret_val;
169  const char* pv = mraa_get_platform_version(platform_offset);
170  if (pv) ret_val = pv;
171  return ret_val;
172 }
173 
179 inline unsigned int
181 {
182  return mraa_get_pin_count();
183 }
184 
191 inline int
193 {
194  return mraa_get_uart_count();
195 }
196 
203 inline int
205 {
206  return mraa_get_i2c_bus_count();
207 }
208 
215 inline int
216 getI2cBusId(int i2c_bus)
217 {
218  return mraa_get_i2c_bus_id(i2c_bus);
219 }
220 
228 inline std::string
229 getPinName(int pin)
230 {
231  std::string ret_val;
232  const char* pn = mraa_get_pin_name(pin);
233  if (pn) ret_val = pn;
234  return ret_val;
235 }
236 
244 inline int
245 getGpioLookup(std::string pin_name)
246 {
247  int index = mraa_gpio_lookup(pin_name.c_str());
248 
249  if (index < 0){
250  std::ostringstream oss;
251  oss << "Gpio name " << pin_name << " is not valid";
252  throw std::invalid_argument(oss.str());
253  }
254 
255  return index;
256 }
257 
265 inline int
266 getI2cLookup(std::string i2c_name)
267 {
268  int index = mraa_i2c_lookup(i2c_name.c_str());
269 
270  if (index < 0){
271  std::ostringstream oss;
272  oss << "i2c name " << i2c_name << " is not valid";
273  throw std::invalid_argument(oss.str());
274  }
275 
276  return index;
277 }
278 
286 inline int
287 getSpiLookup(std::string spi_name)
288 {
289  int index = mraa_spi_lookup(spi_name.c_str());
290 
291  if (index < 0){
292  std::ostringstream oss;
293  oss << "Spi name " << spi_name << " is not valid";
294  throw std::invalid_argument(oss.str());
295  }
296 
297  return index;
298 }
299 
307 inline int
308 getPwmLookup(std::string pwm_name)
309 {
310  int index = mraa_pwm_lookup(pwm_name.c_str());
311 
312  if (index < 0){
313  std::ostringstream oss;
314  oss << "PWM name " << pwm_name << " is not valid";
315  throw std::invalid_argument(oss.str());
316  }
317 
318  return index;
319 }
320 
328 inline int
329 getUartLookup(std::string uart_name)
330 {
331  int index = mraa_uart_lookup(uart_name.c_str());
332 
333  if (index < 0) {
334  std::ostringstream oss;
335  oss << "UART name " << uart_name << " is not valid";
336  throw std::invalid_argument(oss.str());
337  }
338 
339  return index;
340 }
341 
349 inline Result
350 setLogLevel(int level)
351 {
352  return (Result) mraa_set_log_level(level);
353 }
354 
360 inline bool
362 {
363  return static_cast<bool>(mraa_has_sub_platform());
364 }
365 
373 inline bool
374 isSubPlatformId(int pin_or_bus_id)
375 {
376  return static_cast<bool>(mraa_is_sub_platform_id(pin_or_bus_id));
377 }
378 
386 inline int
387 getSubPlatformId(int pin_or_bus_index)
388 {
389  return mraa_get_sub_platform_id(pin_or_bus_index);
390 }
391 
399 inline int
400 getSubPlatformIndex(int pin_or_bus_id)
401 {
402  return mraa_get_sub_platform_index(pin_or_bus_id);
403 }
404 
411 inline int
413 {
414  return mraa_get_default_i2c_bus(platform_offset);
415 }
416 
425 inline Result
426 addSubplatform(Platform subplatformtype, std::string dev)
427 {
428  return (Result) mraa_add_subplatform((mraa_platform_t) subplatformtype, dev.c_str());
429 }
430 
438 inline Result
439 removeSubplatform(Platform subplatformtype)
440 {
441  return (Result) mraa_remove_subplatform((mraa_platform_t) subplatformtype);
442 }
443 
455 template <class T>
456 inline T*
457 initIo(std::string desc)
458 {
459  return new T(mraa_init_io(desc.c_str()));
460 }
461 
470 inline Result
471 initJsonPlatform(std::string path)
472 {
473  return (Result) mraa_init_json_platform(path.c_str());
474 }
475 
476 }
Result
Definition: types.hpp:201
std::string getPlatformVersion(int platform_offset=MRAA_MAIN_PLATFORM_OFFSET)
Definition: common.hpp:166
int mraa_spi_lookup(const char *spi_name)
Result setLogLevel(int level)
Definition: common.hpp:350
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:471
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:308
Result addSubplatform(Platform subplatformtype, std::string dev)
Definition: common.hpp:426
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:204
int getUartCount()
Definition: common.hpp:192
mraa_boolean_t mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
int getI2cLookup(std::string i2c_name)
Definition: common.hpp:266
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:216
bool isSubPlatformId(int pin_or_bus_id)
Definition: common.hpp:374
int mraa_get_i2c_bus_count()
int getDefaultI2cBus(int platform_offset=MRAA_MAIN_PLATFORM_OFFSET)
Definition: common.hpp:412
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:457
unsigned int adcSupportedBits()
Definition: common.hpp:140
int mraa_set_priority(const int priority)
int getSpiLookup(std::string spi_name)
Definition: common.hpp:287
char * mraa_get_pin_name(int pin)
int mraa_get_uart_count()
unsigned int getPinCount()
Definition: common.hpp:180
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:245
int mraa_uart_lookup(const char *uart_name)
std::string getPlatformName()
Definition: common.hpp:151
mraa_result_t mraa_add_subplatform(mraa_platform_t subplatformtype, const char *dev)
int mraa_pwm_lookup(const char *pwm_name)
bool pinModeTest(int pin, Pinmodes mode)
Definition: common.hpp:118
bool hasSubPlatform()
Definition: common.hpp:361
int mraa_get_sub_platform_id(int pin_or_bus_index)
unsigned int mraa_adc_raw_bits()
Definition: aio.hpp:31
Result removeSubplatform(Platform subplatformtype)
Definition: common.hpp:439
mraa_result_t
Definition: types.h:208
void * mraa_init_io(const char *desc)
int setPriority(const int priority)
Definition: common.hpp:83
mraa_pinmodes_t
Definition: types.h:231
std::string getPinName(int pin)
Definition: common.hpp:229
#define MRAA_MAIN_PLATFORM_OFFSET
Definition: common.h:41
const char * mraa_get_platform_version(int platform_offset)
mraa_boolean_t mraa_has_sub_platform()
unsigned int adcRawBits()
Definition: common.hpp:129
Pinmodes
Definition: types.hpp:224
int getUartLookup(std::string uart_name)
Definition: common.hpp:329
void mraa_result_print(mraa_result_t result)
int getSubPlatformId(int pin_or_bus_index)
Definition: common.hpp:387
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:400
unsigned int mraa_get_pin_count()