swig: add unexport() calls to be used by destructors in object api
[contrib/mraa.git] / src / maa.i
1 %{
2     #include "maa.h"
3     #include "gpio.h"
4     #include "pwm.h"
5     #include "i2c.h"
6     #include "spi.h"
7     #include "aio.h"
8 %}
9
10 %rename(get_version) maa_get_version();
11 const char * maa_get_version();
12 %rename(init) maa_init();
13 int maa_init();
14
15 #### GPIO ####
16
17 typedef enum {
18     MAA_GPIO_STRONG     = 0, /**< Default. Strong high and low */
19     MAA_GPIO_PULLUP     = 1, /**< Resistive High */
20     MAA_GPIO_PULLDOWN   = 2, /**< Resistive Low */
21     MAA_GPIO_HIZ        = 3  /**< High Z State */
22 } gpio_mode_t;
23
24 typedef enum {
25     MAA_GPIO_OUT    = 0, /**< Output. A Mode can also be set */
26     MAA_GPIO_IN     = 1  /**< Input. */
27 } gpio_dir_t;
28
29 %nodefault maa_gpio_context;
30 %rename(Gpio) maa_gpio_context;
31 %ignore value_fp;
32
33 %feature("autodoc") maa_gpio_context "
34 Create a Gpio object and export it. Depending on your board the correct GPIO
35 value will be used. If raw is true then the pin that will be initialised will
36 be the hardcoded pin value in the kernel. Please see your board IO
37 documentation to understand exactly what will happen.
38
39 Parameters:
40         * pin: pin number read from the board, i.e IO3 is 3
41         * raw: set to True to use real pin value from the kernel";
42 typedef struct {
43     /*@{*/
44     int pin; /**< the pin number, as known to the os. */
45     FILE *value_fp; /**< the file pointer to the value of the gpio */
46     /*@}*/
47 } maa_gpio_context;
48
49 %extend maa_gpio_context {
50   maa_gpio_context(int pin, int raw=0)
51   {
52     if (raw)
53       return maa_gpio_init_raw(pin);
54     return maa_gpio_init(pin);
55   }
56   ~maa_gpio_context()
57   {
58     maa_gpio_unexport($self);
59   }
60   %feature("autodoc") write "
61   Write a value to a GPIO pin
62
63   Parameters:
64         * value: value to write to GPIO";
65   int write(int value)
66   {
67     return maa_gpio_write($self, value);
68   }
69   %feature("autodoc") dir "
70   Set the gpio direction
71
72   Parameters:
73         * dir: GPIO direction";
74   int dir(gpio_dir_t dir)
75   {
76     return maa_gpio_dir($self, dir);
77   }
78   %feature("autodoc") read "
79   Read the value of a GPIO
80
81   Returns:
82         * value: GPIO value";
83   int read()
84   {
85     return maa_gpio_read($self);
86   }
87   %feature("autodoc") mode "
88   Set the GPIO mode
89
90   Parameters:
91         * mode: GPIO mode to set";
92   int mode(gpio_mode_t mode)
93   {
94     return maa_gpio_mode($self, mode);
95   }
96 }
97
98 #### i2c ####
99
100 %nodefault maa_i2c_context;
101 %rename(I2c) maa_i2c_context;
102
103 %ignore fh;
104 typedef struct {
105     /*@{*/
106     int hz; /**< frequency of communication */
107     int fh; /**< the file handle to the /dev/i2c-* device */
108     int addr; /**< the address of the i2c slave */
109     maa_gpio_context gpio;
110     /*@}*/
111 } maa_i2c_context;
112
113 %nodefault maa_i2c_context;
114 %extend maa_i2c_context {
115   maa_i2c_context()
116   {
117     return maa_i2c_init();
118   }
119   ~maa_i2c_context()
120   {
121   }
122   int frequency(int hz)
123   {
124     return maa_i2c_frequency($self, hz);
125   }
126   int read(char *data, int length)
127   {
128     return maa_i2c_read($self, data, length);
129   }
130   int read()
131   {
132     return maa_i2c_read_byte($self);
133   }
134   int write(char *data, int length)
135   {
136     return maa_i2c_write($self, data, length);
137   }
138   int write(int data)
139   {
140     return maa_i2c_write_byte($self, data);
141   }
142 }
143
144 #### PWM ####
145
146 %rename(Pwm) maa_pwm_context;
147
148 %ignore duty_fp;
149 typedef struct {
150     /*@{*/
151     int pin; /**< the pin number, as known to the os. */
152     int chipid; /**< the chip id, which the pwm resides */
153     FILE *duty_fp; /**< File pointer to duty file */
154     /*@}*/
155 } maa_pwm_context;
156
157 %nodefault maa_pwm_context;
158 %extend maa_pwm_context {
159   maa_pwm_context(int pin)
160   {
161     return maa_pwm_init(pin);
162   }
163   maa_pwm_context(int chipid, int pin)
164   {
165     return maa_pwm_init_raw(chipid, pin);
166   }
167   ~maa_pwm_context()
168   {
169     maa_pwm_unexport($self);
170   }
171   int write(float percentage)
172   {
173     return maa_pwm_write($self, percentage);
174   }
175   int read()
176   {
177     return maa_pwm_read($self);
178   }
179   int period(float seconds)
180   {
181     return maa_pwm_period($self, seconds);
182   }
183   int period_ms(int ms)
184   {
185     return maa_pwm_period_ms($self, ms);
186   }
187   int period_us(int us)
188   {
189     return maa_pwm_period_us($self, us);
190   }
191   int pulsewidth(float seconds)
192   {
193     return maa_pwm_pulsewidth($self, seconds);
194   }
195   int pulsewidth_ms(int ms)
196   {
197     return maa_pwm_pulsewidth($self, ms);
198   }
199   int pulsewidth_us(int us)
200   {
201     return maa_pwm_pulsewidth($self, us);
202   }
203   int enable(int enable)
204   {
205     return maa_pwm_enable($self, enable);
206   }
207 }
208
209 #### SPI ####
210
211 %rename(Spi) maa_spi_context;
212
213 %ignore spifd;
214 typedef struct {
215     /*@{*/
216     int spifd; /**< File descriptor to SPI Device */
217     /*@}*/
218 } maa_spi_context;
219
220 %nodefault maa_spi_context;
221 %extend maa_spi_context {
222   maa_spi_context()
223   {
224     return maa_spi_init();
225   }
226   ~maa_spi_context()
227   {
228   }
229   int mode(unsigned short mode)
230   {
231     return maa_spi_mode($self, mode);
232   }
233   int frequency(int hz)
234   {
235     return maa_spi_frequency($self, hz);
236   }
237   unsigned int write(unsigned int data)
238   {
239     return maa_spi_write($self, data);
240   }
241 }
242
243 #### AIO ####
244
245 %rename(Aio) maa_aio_context;
246
247 %ignore adc_in_fp;
248 typedef struct {
249     unsigned int channel;
250     FILE *adc_in_fp;
251 } maa_aio_context;
252
253 %nodefault maa_aio_context;
254 %extend maa_aio_context {
255   maa_aio_context(unsigned int aio_channel)
256   {
257     return maa_aio_init(aio_channel);
258   }
259   ~maa_aio_context()
260   {
261   }
262   unsigned int read()
263   {
264     return maa_aio_read_u16($self);
265   }
266 }