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
31 /**
32 * @namespace mraa namespace
33 */
34 namespace mraa
35 {
36
37 /**
38 * @file
39 * @brief API to common functions of MRAA
40 *
41 * This file defines the interface for libmraa common functions
42 */
43
44 /**
45 * Initialise MRAA
46 *
47 * Detects running platform and attempts to use included pinmap, this is run on
48 * module/library init/load but is handy to rerun to check board initialised
49 * correctly. MRAA_SUCCESS inidicates correct (first time) initialisation
50 * whilst MRAA_ERROR_PLATFORM_ALREADY_INITIALISED indicates the board is
51 * already initialised correctly
52 *
53 * @return Result of operation
54 */
55 inline Result
init()56 init()
57 {
58 return (Result) mraa_init();
59 }
60
61 /**
62 * Get libmraa version.
63 *
64 * @return libmraa version (e.g. v0.4.0-20-gb408207)
65 */
66 inline std::string
getVersion()67 getVersion()
68 {
69 std::string ret = mraa_get_version();
70 return ret;
71 }
72
73 /**
74 * This function attempts to set the mraa process to a given priority and the
75 * scheduler to SCHED_RR. Highest * priority is typically 99 and minimum is 0.
76 * This function * will set to MAX if * priority is > MAX. Function will return
77 * -1 on failure.
78 *
79 * @param priority Value from typically 0 to 99
80 * @return The priority value set
81 */
82 inline int
setPriority(const unsigned int priority)83 setPriority(const unsigned int priority)
84 {
85 return mraa_set_priority(priority);
86 }
87
88 /**
89 * Get platform type, board must be initialised.
90 *
91 * @return mraa::platform Platform type enum
92 */
93 inline Platform
getPlatformType()94 getPlatformType()
95 {
96 return (Platform) mraa_get_platform_type();
97 }
98
99 /**
100 * Print a textual representation of the mraa::Result
101 *
102 * @param Result the Result to print
103 */
104 inline void
printError(Result result)105 printError(Result result)
106 {
107 mraa_result_print((mraa_result_t) result);
108 }
109
110 /**
111 * Checks if a pin is able to use the passed in mode.
112 *
113 * @param pin Physical Pin to be checked.
114 * @param mode the mode to be tested.
115 * @return boolean if the mode is supported, 0=false.
116 */
117 inline bool
pinModeTest(int pin,Pinmodes mode)118 pinModeTest(int pin, Pinmodes mode)
119 {
120 return (bool) mraa_pin_mode_test(pin, (mraa_pinmodes_t) mode);
121 }
122
123 /**
124 * Check the board's bit size when reading the value
125 *
126 * @return raw bits being read from kernel module. Zero if no ADC
127 */
128 inline unsigned int
adcRawBits()129 adcRawBits()
130 {
131 return mraa_adc_raw_bits();
132 }
133
134 /**
135 * Return value that the raw value should be shifted to. Zero if no ADC
136 *
137 * @return return actual bit size the adc value should be understood as.
138 */
139 inline unsigned int
adcSupportedBits()140 adcSupportedBits()
141 {
142 return mraa_adc_supported_bits();
143 }
144
145 /**
146 * Return Platform Name. Returns NULL if no platform inited.
147 *
148 * @return platform name
149 */
150 inline std::string
getPlatformName()151 getPlatformName()
152 {
153 std::string ret_val(mraa_get_platform_name());
154 return ret_val;
155 }
156
157 /**
158 * Return platform versioning info. Returns NULL if no info present.
159 *
160 * @param optional subplatform identifier
161 * @return platform versioning info
162 */
163 inline std::string
getPlatformVersion(int platform_offset=MRAA_MAIN_PLATFORM_OFFSET)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
170 /**
171 * Return count of physical pins on the running platform
172 *
173 * @return uint of physical pins.
174 */
175 inline unsigned int
getPinCount()176 getPinCount()
177 {
178 return mraa_get_pin_count();
179 }
180
181 /**
182 * Get platform usable I2C bus count, board must be initialised.
183 *
184 * @return number f usable I2C bus count on the current platform. Function will
185 * return -1 on failure
186 */
187 inline int
getI2cBusCount()188 getI2cBusCount()
189 {
190 return mraa_get_i2c_bus_count();
191 }
192
193 /**
194 * Get I2C adapter number in sysfs.
195 *
196 * @param i2c_bus the logical I2C bus number
197 * @return I2C adapter number in sysfs. Function will return -1 on failure
198 */
199 inline int
getI2cBusId(unsigned int i2c_bus)200 getI2cBusId(unsigned int i2c_bus)
201 {
202 return mraa_get_i2c_bus_id(i2c_bus);
203 }
204
205 /**
206 * Get name of pin, board must be initialised.
207 *
208 * @param pin number
209 *
210 * @return char* of pin name
211 */
212 inline std::string
getPinName(int pin)213 getPinName(int pin)
214 {
215 std::string ret_val(mraa_get_pin_name(pin));
216 return ret_val;
217 }
218
219 /**
220 * Sets the log level to use from 0-7 where 7 is very verbose. These are the
221 * syslog log levels, see syslog(3) for more information on the levels.
222 *
223 * @param level
224 * @return Result of operation
225 */
226 inline Result
setLogLevel(int level)227 setLogLevel(int level)
228 {
229 return (Result) mraa_set_log_level(level);
230 }
231
232 /**
233 * Detect presence of sub platform.
234 *
235 * @return bool true if sub platform is present and initialized, false otherwise
236 */
237 inline bool
hasSubPlatform()238 hasSubPlatform()
239 {
240 return static_cast<bool>(mraa_has_sub_platform());
241 }
242
243
244
245 /**
246 * Check if pin or bus id includes sub platform mask.
247 *
248 * @param int pin or bus number
249 *
250 * @return mraa_boolean_t 1 if pin or bus is for sub platform, 0 otherwise
251 */
252 inline bool
isSubPlatformId(int pin_or_bus_id)253 isSubPlatformId(int pin_or_bus_id)
254 {
255 return static_cast<bool>(mraa_is_sub_platform_id(pin_or_bus_id));
256 }
257
258 /**
259 * Convert pin or bus index to corresponding sub platform id.
260 *
261 * @param int pin or bus index
262 *
263 * @return int sub platform pin or bus number
264 */
265 inline int
getSubPlatformId(int pin_or_bus_index)266 getSubPlatformId(int pin_or_bus_index)
267 {
268 return mraa_get_sub_platform_id(pin_or_bus_index);
269 }
270
271 /**
272 * Convert pin or bus sub platform id to index.
273 *
274 * @param int sub platform pin or bus id
275 *
276 * @return int pin or bus index
277 */
278 inline int
getSubPlatformIndex(int pin_or_bus_id)279 getSubPlatformIndex(int pin_or_bus_id)
280 {
281 return mraa_get_sub_platform_index(pin_or_bus_id);
282 }
283 }
284