summaryrefslogtreecommitdiff
path: root/peripheral/libmraa/api/mraa/gpio.hpp
blob: e2d7ad5379aeb8997e9b2f48d72693e0e29b43ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * Author: Brendan Le Foll <brendan.le.foll@intel.com>
 * Copyright (c) 2014 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#pragma once

#include "gpio.h"
#include "types.hpp"
#include <stdexcept>

#if defined(SWIGJAVASCRIPT)
#if NODE_MODULE_VERSION >= 0x000D
#include <uv.h>
#endif
#endif

namespace mraa
{

// These enums must match the enums in gpio.h

/**
 * Gpio Output modes
 */
typedef enum {
    MODE_STRONG = 0,   /**< Default. Strong High and Low */
    MODE_PULLUP = 1,   /**< Resistive High */
    MODE_PULLDOWN = 2, /**< Resistive Low */
    MODE_HIZ = 3       /**< High Z State */
} Mode;

/**
 * Gpio Direction options
 */
typedef enum {
    DIR_OUT = 0,      /**< Output. A Mode can also be set */
    DIR_IN = 1,       /**< Input */
    DIR_OUT_HIGH = 2, /**< Output. Init High */
    DIR_OUT_LOW = 3   /**< Output. Init Low */
} Dir;

/**
 * Gpio Edge types for interrupts
 */
typedef enum {
    EDGE_NONE = 0,   /**< No interrupt on Gpio */
    EDGE_BOTH = 1,   /**< Interrupt on rising & falling */
    EDGE_RISING = 2, /**< Interrupt on rising only */
    EDGE_FALLING = 3 /**< Interrupt on falling only */
} Edge;

/**
 * @brief API to General Purpose IO
 *
 * This file defines the gpio interface for libmraa
 *
 * @snippet Blink-IO.cpp Interesting
 */
class Gpio
{
  public:
    /**
     * Instantiates a Gpio object
     *
     * @param pin pin number to use
     * @param owner (optional) Set pin owner, default behaviour is to 'own'
     * the pin if we exported it. This means we will close it on destruct.
     * Otherwise it will get left open. This is only valid in sysfs use
     * cases
     * @param raw (optional) Raw pins will use gpiolibs pin numbering from
     * the kernel module. Note that you will not get any muxers set up for
     * you so this may not always work as expected.
     */
    Gpio(int pin, bool owner = true, bool raw = false)
    {
        if (raw) {
            m_gpio = mraa_gpio_init_raw(pin);
        } else {
            m_gpio = mraa_gpio_init(pin);
        }

        if (m_gpio == NULL) {
            throw std::invalid_argument("Invalid GPIO pin specified");
        }

        if (!owner) {
            mraa_gpio_owner(m_gpio, 0);
        }
    }
    /**
     * Gpio Constructor, takes a pointer to the GPIO context and initialises
     * the GPIO class
     *
     * @param void * to GPIO context
     */
    Gpio(void* gpio_context)
    {
        m_gpio = (mraa_gpio_context) gpio_context;
        if (m_gpio == NULL) {
            throw std::invalid_argument("Invalid GPIO context");
        }
    }
    /**
     * Gpio object destructor, this will only unexport the gpio if we where
     * the owner
     */
    ~Gpio()
    {
        mraa_gpio_close(m_gpio);
    }
    /**
     * Set the edge mode for ISR
     *
     * @param mode The edge mode to set
     * @return Result of operation
     */
    Result
    edge(Edge mode)
    {
        return (Result) mraa_gpio_edge_mode(m_gpio, (mraa_gpio_edge_t) mode);
    }
#if defined(SWIGPYTHON)
    Result
    isr(Edge mode, PyObject* pyfunc, PyObject* args)
    {
        return (Result) mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) mode, (void (*) (void*)) pyfunc, (void*) args);
    }
#elif defined(SWIGJAVASCRIPT)
    static void
    v8isr(uv_work_t* req, int status)
    {
#if NODE_MODULE_VERSION >= 0x000D
        v8::HandleScope scope(v8::Isolate::GetCurrent());
#endif
        mraa::Gpio* This = (mraa::Gpio*) req->data;
        int argc = 1;
        v8::Local<v8::Value> argv[] = { SWIGV8_INTEGER_NEW(-1) };
#if NODE_MODULE_VERSION >= 0x000D
        v8::Local<v8::Function> f = v8::Local<v8::Function>::New(v8::Isolate::GetCurrent(), This->m_v8isr);
        f->Call(SWIGV8_CURRENT_CONTEXT()->Global(), argc, argv);
#else
        This->m_v8isr->Call(SWIGV8_CURRENT_CONTEXT()->Global(), argc, argv);
#endif
        delete req;
    }

    static void
    nop(uv_work_t* req)
    {
        // Do nothing.
    }

    static void
    uvwork(void* ctx)
    {
        uv_work_t* req = new uv_work_t;
        req->data = ctx;
        uv_queue_work(uv_default_loop(), req, nop, v8isr);
    }

    Result
    isr(Edge mode, v8::Handle<v8::Function> func)
    {
#if NODE_MODULE_VERSION >= 0x000D
        m_v8isr.Reset(v8::Isolate::GetCurrent(), func);
#else
        m_v8isr = v8::Persistent<v8::Function>::New(func);
#endif
        return (Result) mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) mode, &uvwork, this);
    }
#elif defined(SWIGJAVA) || defined(JAVACALLBACK)
    Result
    isr(Edge mode, jobject runnable)
    {
        return (Result) mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) mode, mraa_java_isr_callback, runnable);
    }
#endif
    /**
     * Sets a callback to be called when pin value changes
     *
     * @param mode The edge mode to set
     * @param fptr Function pointer to function to be called when interrupt is
     * triggered
     * @param args Arguments passed to the interrupt handler (fptr)
     * @return Result of operation
     */
    Result
    isr(Edge mode, void (*fptr)(void*), void* args)
    {
        return (Result) mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) mode, fptr, args);
    }

    /**
     * Exits callback - this call will not kill the isr thread immediately
     * but only when it is out of it's critical section
     *
     * @return Result of operation
     */
    Result
    isrExit()
    {
#if defined(SWIGJAVASCRIPT)
#if NODE_MODULE_VERSION >= 0x000D
        m_v8isr.Reset();
#else
        m_v8isr.Dispose();
        m_v8isr.Clear();
#endif
#endif
        return (Result) mraa_gpio_isr_exit(m_gpio);
    }
    /**
     * Change Gpio mode
     *
     * @param mode The mode to change the gpio into
     * @return Result of operation
     */
    Result
    mode(Mode mode)
    {
        return (Result )mraa_gpio_mode(m_gpio, (mraa_gpio_mode_t) mode);
    }
    /**
     * Change Gpio direction
     *
     * @param dir The direction to change the gpio into
     * @return Result of operation
     */
    Result
    dir(Dir dir)
    {
        return (Result )mraa_gpio_dir(m_gpio, (mraa_gpio_dir_t) dir);
    }

    /**
     * Read Gpio direction
     *
     * @throw std::runtime_error in case of failure
     * @return Result of operation
     */
    Dir
    readDir()
    {
        mraa_gpio_dir_t dir;
        if (mraa_gpio_read_dir(m_gpio, &dir) != MRAA_SUCCESS) {
            throw std::runtime_error("Failed to read direction");
        }
        return (Dir) dir;
    }

    /**
     * Read value from Gpio
     *
     * @return Gpio value
     */
    int
    read()
    {
        return mraa_gpio_read(m_gpio);
    }
    /**
     * Write value to Gpio
     *
     * @param value Value to write to Gpio
     * @return Result of operation
     */
    Result
    write(int value)
    {
        return (Result) mraa_gpio_write(m_gpio, value);
    }
    /**
     * Enable use of mmap i/o if available.
     *
     * @param enable true to use mmap
     * @return Result of operation
     */
    Result
    useMmap(bool enable)
    {
        return (Result) mraa_gpio_use_mmaped(m_gpio, (mraa_boolean_t) enable);
    }
    /**
     * Get pin number of Gpio. If raw param is True will return the
     * number as used within sysfs. Invalid will return -1.
     *
     * @param raw (optional) get the raw gpio number.
     * @return Pin number
     */
    int
    getPin(bool raw = false)
    {
        if (raw) {
            return mraa_gpio_get_pin_raw(m_gpio);
        }
        return mraa_gpio_get_pin(m_gpio);
    }

  private:
    mraa_gpio_context m_gpio;
#if defined(SWIGJAVASCRIPT)
    v8::Persistent<v8::Function> m_v8isr;
#endif
};
}