clang-format: run clang-format on C/C++ code
[contrib/mraa.git] / api / mraa / common.hpp
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 <string>
29
30 /**
31  * @namespace mraa namespace
32  */
33 namespace mraa
34 {
35
36 /**
37  * @file
38  * @brief API to common functions of MRAA
39  *
40  * This file defines the interface for libmraa common functions
41  */
42
43 /**
44  * Initialise MRAA
45  *
46  * Detects running platform and attempts to use included pinmap, this is run on
47  * module/library init/load but is handy to rerun to check board initialised
48  * correctly. MRAA_SUCCESS inidicates correct (first time) initialisation
49  * whilst MRAA_ERROR_PLATFORM_ALREADY_INITIALISED indicates the board is
50  * already initialised correctly
51  *
52  * @return Result of operation
53  */
54 inline mraa_result_t
55 init()
56 {
57     return mraa_init();
58 }
59
60 /**
61  * Get libmraa version.
62  *
63  * @return libmraa version (e.g. v0.4.0-20-gb408207)
64  */
65 inline std::string
66 getVersion()
67 {
68     std::string ret = mraa_get_version();
69     return ret;
70 }
71
72 /**
73  * This function attempts to set the mraa process to a given priority and the
74  * scheduler to SCHED_RR. Highest * priority is typically 99 and minimum is 0.
75  * This function * will set to MAX if * priority is > MAX. Function will return
76  * -1 on failure.
77  *
78  * @param priority Value from typically 0 to 99
79  * @return The priority value set
80  */
81 inline int
82 setPriority(const unsigned int priority)
83 {
84     return mraa_set_priority(priority);
85 }
86
87 /**
88  * Get platform type, board must be initialised.
89  *
90  * @return mraa_platform_t Platform type enum
91  */
92 inline mraa_platform_t
93 getPlatformType()
94 {
95     return mraa_get_platform_type();
96 }
97
98 /**
99  * Print a textual representation of the mraa_result_t
100  *
101  * @param result the result to print
102  */
103 inline void
104 printError(mraa_result_t result)
105 {
106     mraa_result_print(result);
107 }
108
109 /**
110  * Checks if a pin is able to use the passed in mode.
111  *
112  * @param pin Physical Pin to be checked.
113  * @param mode the mode to be tested.
114  * @return boolean if the mode is supported, 0=false.
115  */
116 inline bool
117 pinModeTest(int pin, mraa_pinmodes_t mode)
118 {
119     return (bool) mraa_pin_mode_test(pin, mode);
120 }
121
122 /**
123  * Check the board's bit size when reading the value
124  *
125  * @return raw bits being read from kernel module. Zero if no ADC
126  */
127 inline unsigned int
128 adcRawBits()
129 {
130     return mraa_adc_raw_bits();
131 }
132
133 /**
134  * Return value that the raw value should be shifted to. Zero if no ADC
135  *
136  * @return return actual bit size the adc value should be understood as.
137  */
138 inline unsigned int
139 adcSupportedBits()
140 {
141     return mraa_adc_supported_bits();
142 }
143
144 /**
145  * Return Platform Name. Returns NULL if no platform inited.
146  *
147  * @return platform name
148  */
149 inline std::string
150 getPlatformName()
151 {
152     std::string ret_val(mraa_get_platform_name());
153     return ret_val;
154 }
155
156 /**
157  * Return count of physical pins on the running platform
158  *
159  * @return uint of physical pins.
160  */
161 inline unsigned int
162 getPinCount()
163 {
164     return mraa_get_pin_count();
165 }
166
167 /**
168 * Get name of pin, board must be initialised.
169 *
170 * @param pin number
171 *
172 * @return char* of pin name
173 */
174 inline std::string
175 getPinName(int pin)
176 {
177     std::string ret_val(mraa_get_pin_name(pin));
178     return ret_val;
179 }
180
181 /**
182  * Sets the log level to use from 0-7 where 7 is very verbose. These are the
183  * syslog log levels, see syslog(3) for more information on the levels.
184  *
185  * @param level
186  * @return Result of operation
187  */
188 inline mraa_result_t
189 setLogLevel(int level)
190 {
191     return mraa_set_log_level(level);
192 }
193 }