python: fix examples and add maa_init() call in swig
[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_close($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     maa_i2c_stop($self);
122   }
123   int frequency(int hz)
124   {
125     return maa_i2c_frequency($self, hz);
126   }
127   int read(char *data, int length)
128   {
129     return maa_i2c_read($self, data, length);
130   }
131   int read()
132   {
133     return maa_i2c_read_byte($self);
134   }
135   int write(char *data, int length)
136   {
137     return maa_i2c_write($self, data, length);
138   }
139   int write(int data)
140   {
141     return maa_i2c_write_byte($self, data);
142   }
143 }
144
145 #### PWM ####
146
147 %rename(Pwm) maa_pwm_context;
148
149 %ignore duty_fp;
150 typedef struct {
151     /*@{*/
152     int pin; /**< the pin number, as known to the os. */
153     int chipid; /**< the chip id, which the pwm resides */
154     FILE *duty_fp; /**< File pointer to duty file */
155     /*@}*/
156 } maa_pwm_context;
157
158 %nodefault maa_pwm_context;
159 %extend maa_pwm_context {
160   maa_pwm_context(int pin)
161   {
162     return maa_pwm_init(pin);
163   }
164   maa_pwm_context(int chipid, int pin)
165   {
166     return maa_pwm_init_raw(chipid, pin);
167   }
168   ~maa_pwm_context()
169   {
170     maa_pwm_close($self);
171   }
172   int write(float percentage)
173   {
174     return maa_pwm_write($self, percentage);
175   }
176   int read()
177   {
178     return maa_pwm_read($self);
179   }
180   int period(float seconds)
181   {
182     return maa_pwm_period($self, seconds);
183   }
184   int period_ms(int ms)
185   {
186     return maa_pwm_period_ms($self, ms);
187   }
188   int period_us(int us)
189   {
190     return maa_pwm_period_us($self, us);
191   }
192   int pulsewidth(float seconds)
193   {
194     return maa_pwm_pulsewidth($self, seconds);
195   }
196   int pulsewidth_ms(int ms)
197   {
198     return maa_pwm_pulsewidth($self, ms);
199   }
200   int pulsewidth_us(int us)
201   {
202     return maa_pwm_pulsewidth($self, us);
203   }
204   int enable(int enable)
205   {
206     return maa_pwm_enable($self, enable);
207   }
208 }
209
210 #### SPI ####
211
212 %rename(Spi) maa_spi_context;
213
214 %ignore spifd;
215 typedef struct {
216     /*@{*/
217     int spifd; /**< File descriptor to SPI Device */
218     /*@}*/
219 } maa_spi_context;
220
221 %nodefault maa_spi_context;
222 %extend maa_spi_context {
223   maa_spi_context()
224   {
225     return maa_spi_init();
226   }
227   ~maa_spi_context()
228   {
229     maa_spi_stop($self);
230   }
231   int mode(unsigned short mode)
232   {
233     return maa_spi_mode($self, mode);
234   }
235   int frequency(int hz)
236   {
237     return maa_spi_frequency($self, hz);
238   }
239   unsigned int write(unsigned int data)
240   {
241     return maa_spi_write($self, data);
242   }
243 }
244
245 #### AIO ####
246
247 %rename(Aio) maa_aio_context;
248
249 %ignore adc_in_fp;
250 typedef struct {
251     unsigned int channel;
252     FILE *adc_in_fp;
253 } maa_aio_context;
254
255 %nodefault maa_aio_context;
256 %extend maa_aio_context {
257   maa_aio_context(unsigned int aio_channel)
258   {
259     return maa_aio_init(aio_channel);
260   }
261   ~maa_aio_context()
262   {
263     maa_aio_close($self);
264   }
265   unsigned int read()
266   {
267     return maa_aio_read_u16($self);
268   }
269 }