8845a146c3fb5806d1b69c24bc6f1098ed5a8b36
[contrib/mraa.git] / src / maa.i
1 %{
2     #include "maa.h"
3     #include "gpio.h"
4     #include "pwm.hpp"
5     #include "i2c.hpp"
6     #include "spi.h"
7     #include "aio.hpp"
8 %}
9
10 %init %{
11     //Adding maa_init() to the module initialisation process
12     maa_init();
13 %}
14
15 %rename(get_version) maa_get_version();
16 const char * maa_get_version();
17
18 #### GPIO ####
19
20 typedef enum {
21     MAA_GPIO_STRONG     = 0, /**< Default. Strong high and low */
22     MAA_GPIO_PULLUP     = 1, /**< Resistive High */
23     MAA_GPIO_PULLDOWN   = 2, /**< Resistive Low */
24     MAA_GPIO_HIZ        = 3  /**< High Z State */
25 } gpio_mode_t;
26
27 typedef enum {
28     MAA_GPIO_OUT    = 0, /**< Output. A Mode can also be set */
29     MAA_GPIO_IN     = 1  /**< Input. */
30 } gpio_dir_t;
31
32 %nodefault maa_gpio_context;
33 %rename(Gpio) maa_gpio_context;
34 %ignore value_fp;
35
36 %feature("autodoc") maa_gpio_context "
37 Create a Gpio object and export it. Depending on your board the correct GPIO
38 value will be used. If raw is true then the pin that will be initialised will
39 be the hardcoded pin value in the kernel. Please see your board IO
40 documentation to understand exactly what will happen.
41
42 Parameters:
43         * pin: pin number read from the board, i.e IO3 is 3
44         * raw: set to True to use real pin value from the kernel";
45 typedef struct {
46     /*@{*/
47     int pin; /**< the pin number, as known to the os. */
48     FILE *value_fp; /**< the file pointer to the value of the gpio */
49 #if defined(SWIGPYTHON)
50     PyObject *isr; /**< the interupt service request */
51 #endif
52     pthread_t thread_id; /**< the isr handler thread id */
53     int isr_value_fp; /**< the isr file pointer on the value */
54     /*@}*/
55 } maa_gpio_context;
56
57 %typemap(check) PyObject *pyfunc {
58   if (!PyCallable_Check($1))
59     SWIG_exception(SWIG_TypeError,"Expected function.");
60 }
61
62 %extend maa_gpio_context {
63   maa_gpio_context(int pin, int raw=0)
64   {
65     if (raw)
66       return maa_gpio_init_raw(pin);
67     return maa_gpio_init(pin);
68   }
69   ~maa_gpio_context()
70   {
71     maa_gpio_unexport($self);
72   }
73   %feature("autodoc") write "
74   Write a value to a GPIO pin
75
76   Parameters:
77         * value: value to write to GPIO";
78   int write(int value)
79   {
80     return maa_gpio_write($self, value);
81   }
82   %feature("autodoc") dir "
83   Set the gpio direction
84
85   Parameters:
86         * dir: GPIO direction";
87   int dir(gpio_dir_t dir)
88   {
89     return maa_gpio_dir($self, dir);
90   }
91   %feature("autodoc") read "
92   Read the value of a GPIO
93
94   Returns:
95         * value: GPIO value";
96   int read()
97   {
98     return maa_gpio_read($self);
99   }
100   %feature("autodoc") mode "
101   Set the GPIO mode
102
103   Parameters:
104         * mode: GPIO mode to set";
105   int mode(gpio_mode_t mode)
106   {
107     return maa_gpio_mode($self, mode);
108   }
109 #if defined(SWIGPYTHON)
110   //set python method as the isr function
111   int set_isr(PyObject *pyfunc)
112   {
113     Py_INCREF(pyfunc);
114     // do a nasty cast to get away from the warnings
115     maa_gpio_isr(self, MAA_GPIO_EDGE_BOTH, (void (*) ()) pyfunc);
116     return 0;
117   }
118 #else
119   %ignore maa_gpio_isr;
120 #endif
121   int isr_exit()
122   {
123     maa_gpio_isr_exit(self);
124   }
125 }
126
127 #### i2c ####
128
129 %include "i2c.hpp"
130
131 #### PWM ####
132
133 %include "pwm.hpp"
134
135 #### SPI ####
136
137 %rename(Spi) maa_spi_context;
138
139 %ignore spifd;
140 typedef struct {
141     /*@{*/
142     int spifd; /**< File descriptor to SPI Device */
143     /*@}*/
144 } maa_spi_context;
145
146 %nodefault maa_spi_context;
147 %extend maa_spi_context {
148   maa_spi_context()
149   {
150     return maa_spi_init();
151   }
152   ~maa_spi_context()
153   {
154   }
155   int mode(unsigned short mode)
156   {
157     return maa_spi_mode($self, mode);
158   }
159   int frequency(int hz)
160   {
161     return maa_spi_frequency($self, hz);
162   }
163   unsigned int write(unsigned int data)
164   {
165     return maa_spi_write($self, data);
166   }
167 }
168
169 #### AIO ####
170
171 %include "aio.hpp"