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