am7xxx: release the interface when needed in open_device()
[libam7xxx.git] / src / am7xxx.h
1 /* am7xxx - communication with AM7XXX based USB Pico Projectors and DPFs
2  *
3  * Copyright (C) 2012-2014  Antonio Ospite <ao2@ao2.it>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /**
20  * @file
21  * Public libam7xxx API.
22  */
23
24 #ifndef __AM7XXX_H
25 #define __AM7XXX_H
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31
32 /**
33  * @typedef am7xxx_context
34  *
35  * An opaque data type representing a context.
36  */
37 struct _am7xxx_context;
38 typedef struct _am7xxx_context am7xxx_context;
39
40 /**
41  * @typedef am7xxx_device
42  *
43  * An opaque data type representing an am7xxx device.
44  */
45 struct _am7xxx_device;
46 typedef struct _am7xxx_device am7xxx_device;
47
48 /**
49  * A struct describing device specific properties.
50  *
51  * A user program may want to inspect these before providing data to the
52  * device. For instance, when sending an image the user may want to rescale it
53  * to the device native width and height in order to be sure the image will be
54  * displayed in its entirety.
55  */
56 typedef struct {
57         unsigned int native_width;  /**< The device native width. */
58         unsigned int native_height; /**< The device native height. */
59 } am7xxx_device_info;
60
61 /**
62  * The verbosity level of logging messages.
63  *
64  * This can be set with am7xxx_set_log_level() and the level will be used
65  * internally by libam7xxx to adjust the granularity of the information
66  * exposed to the user about the internal library operations.
67  */
68 typedef enum {
69         AM7XXX_LOG_FATAL   = 0, /**< Fatal messages, the user application should stop if it gets one of this. */
70         AM7XXX_LOG_ERROR   = 1, /**< Error messages, typically they describe API functions failures. */
71         AM7XXX_LOG_WARNING = 2, /**< Warnings about conditions worth mentioning to the user. */
72         AM7XXX_LOG_INFO    = 3, /**< Informations about the device operations. */
73         AM7XXX_LOG_DEBUG   = 4, /**< Informations about the library internals. */
74         AM7XXX_LOG_TRACE   = 5, /**< Verbose informations about the communication with the hardware. */
75 } am7xxx_log_level;
76
77 /**
78  * The image formats accepted by the device.
79  */
80 typedef enum {
81         AM7XXX_IMAGE_FORMAT_JPEG = 1, /**< JPEG format. */
82         AM7XXX_IMAGE_FORMAT_NV12 = 2, /**< Raw YUV in the NV12 variant. */
83 } am7xxx_image_format;
84
85 /**
86  * The device power modes.
87  *
88  * An am7xxx device can operate in several power modes. A certain power mode
89  * may have effect on the display brightness or on the device power
90  * consumption.
91  *
92  * @note Most am7xxx devices come with a Y-shaped USB cable with a Master and
93  * a Slave connector, higher power modes may require that both connectors are
94  * plugged in to the host system for the device to work properly.
95  *
96  * @note At higher power modes some devices may use a fan to cool down the
97  * internal hardware components, and hence may be noisier in this case.
98  */
99 typedef enum {
100         AM7XXX_POWER_OFF    = 0, /**< Display is powered off, no image shown. */
101         AM7XXX_POWER_LOW    = 1, /**< Low power consumption but also low brightness. */
102         AM7XXX_POWER_MIDDLE = 2, /**< Middle level of brightness. This and upper modes need both the Master and Slave USB connectors plugged. */
103         AM7XXX_POWER_HIGH   = 3, /**< More brightness, but more power consumption. */
104         AM7XXX_POWER_TURBO  = 4, /**< Max brightness and power consumption. */
105 } am7xxx_power_mode;
106
107 /**
108  * The display zoom modes.
109  *
110  * An am7xxx device can display images using several zoom modes.
111  *
112  * @note Changing the zoom mode can change the aspect ratio of the displayed
113  * image.
114  *
115  * @note On the zoom test screen the version of the firmware running on the
116  * device is shown as well (e.g SPI_V21.0.0_2011.03.18).
117  *
118  * @note The Tele mode is available only on some PicoPix models, when using it
119  * the image is distorted like if a different lens was used, but the global
120  * aspect ratio of the image does not change.
121  */
122 typedef enum {
123         AM7XXX_ZOOM_ORIGINAL = 0, /**< Original Size, as retrieved via #am7xxx_device_info. */
124         AM7XXX_ZOOM_H        = 1, /**< Zoom 1: H Scale (changes aspect ratio). */
125         AM7XXX_ZOOM_H_V      = 2, /**< Zoom 2: H/V Scale (changes aspect ratio). */
126         AM7XXX_ZOOM_TEST     = 3, /**< Zoom test screen, the firmware version is shown as well. */
127         AM7XXX_ZOOM_TELE     = 4, /**< Zoom Tele: available on some PicoPix models. */
128 } am7xxx_zoom_mode;
129
130 /**
131  * Initialize the library context and data structures, and scan for devices.
132  *
133  * @param[out] ctx A pointer to the context the library will be used in.
134  *
135  * @return 0 on success, a negative value on error
136  */
137 int am7xxx_init(am7xxx_context **ctx);
138
139 /**
140  * Cleanup the library data structures and free the context.
141  *
142  * @param[in,out] ctx The context to free.
143  */
144 void am7xxx_shutdown(am7xxx_context *ctx);
145
146 /**
147  * Set verbosity level of log messages.
148  *
149  * @note The level is per-context.
150  *
151  * @note Messages of level AM7XXX_LOG_FATAL are always shown, regardless
152  * of the value of the log_level parameter.
153  *
154  * @param[in] ctx The context to set the log level for
155  * @param[in] log_level The verbosity level to use in the context (see @link am7xxx_log_level @endlink)
156  */
157 void am7xxx_set_log_level(am7xxx_context *ctx, am7xxx_log_level log_level);
158
159 /**
160  * Open an am7xxx_device according to a index.
161  *
162  * The semantics of the 'device_index' argument follows the order
163  * of the devices as found when scanning the bus at am7xxx_init() time.
164  *
165  * @note When the user tries to open a device already opened the function
166  * returns -EBUSY and the device is left open.
167  *
168  * @param[in] ctx The context to open the device in
169  * @param[out] dev A pointer to the structure representing the device to open
170  * @param[in] device_index The index of the device on the bus
171  *
172  * @return 0 on success, a negative value on error
173  */
174 int am7xxx_open_device(am7xxx_context *ctx,
175                        am7xxx_device **dev,
176                        unsigned int device_index);
177
178 /**
179  * Close an am7xxx_device.
180  *
181  * Close an am7xxx_device so that it becomes available for some other
182  * user/process to open it.
183  *
184  * @param[in] dev A pointer to the structure representing the device to close
185  *
186  * @return 0 on success, a negative value on error
187  */
188 int am7xxx_close_device(am7xxx_device *dev);
189
190 /**
191  * Get info about an am7xxx device.
192  *
193  * Get information about a device, in the form of a
194  * @link am7xxx_device_info @endlink structure.
195  *
196  * @param[in] dev A pointer to the structure representing the device to get info of
197  * @param[out] device_info A pointer to the structure where to store the device info (see @link am7xxx_device_info @endlink)
198  *
199  * @return 0 on success, a negative value on error
200  */
201 int am7xxx_get_device_info(am7xxx_device *dev,
202                            am7xxx_device_info *device_info);
203
204 /**
205  * Calculate the dimensions of an image to be shown on an am7xxx device.
206  *
207  * Before sending images bigger than the device native dimensions the user
208  * needs to rescale them, this utility function does the calculation in a way
209  * that the original image aspect ratio is preserved.
210  *
211  * @param[in] dev A pointer to the structure representing the device to get info of
212  * @param[in] upscale Whether to calculate scaled dimensions for images smaller than the native dimensions
213  * @param[in] original_width The width of the original image
214  * @param[in] original_height The height of the original image
215  * @param[out] scaled_width The width the rescaled image should have
216  * @param[out] scaled_height The height the rescaled image should have
217  *
218  * @return 0 on success, a negative value on error
219  */
220 int am7xxx_calc_scaled_image_dimensions(am7xxx_device *dev,
221                                         unsigned int upscale,
222                                         unsigned int original_width,
223                                         unsigned int original_height,
224                                         unsigned int *scaled_width,
225                                         unsigned int *scaled_height);
226 /**
227  * Send an image for display on an am7xxx device.
228  *
229  * This is the function that actually makes the device display something.
230  * Static pictures can be sent just once and the device will keep showing them
231  * until another image get sent or some command resets or turns off the display.
232  *
233  * @param[in] dev A pointer to the structure representing the device to get info of
234  * @param[in] format The format the image is in (see @link am7xxx_image_format @endlink enum)
235  * @param[in] width The width of the image
236  * @param[in] height The height of the image
237  * @param[in] image A buffer holding data in the format specified by the format parameter
238  * @param[in] image_size The size in bytes of the image buffer
239  *
240  * @return 0 on success, a negative value on error
241  */
242 int am7xxx_send_image(am7xxx_device *dev,
243                       am7xxx_image_format format,
244                       unsigned int width,
245                       unsigned int height,
246                       unsigned char *image,
247                       unsigned int image_size);
248
249 /**
250  * Queue transfer of an image for display on an am7xxx device and return immediately.
251  *
252  * This is the function that actually makes the device display something.
253  * Static pictures can be sent just once and the device will keep showing them
254  * until another image get sent or some command resets or turns off the display.
255  * 
256  * @note This _async() variant makes a copy of the image buffer, so the caller
257  * is free to reuse the buffer just after the function returns.
258  *
259  * @param[in] dev A pointer to the structure representing the device to get info of
260  * @param[in] format The format the image is in (see @link am7xxx_image_format @endlink enum)
261  * @param[in] width The width of the image
262  * @param[in] height The height of the image
263  * @param[in] image A buffer holding data in the format specified by the format parameter
264  * @param[in] image_size The size in bytes of the image buffer
265  *
266  * @return 0 on success, a negative value on error
267  */
268 int am7xxx_send_image_async(am7xxx_device *dev,
269                             am7xxx_image_format format,
270                             unsigned int width,
271                             unsigned int height,
272                             unsigned char *image,
273                             unsigned int image_size);
274
275 /**
276  * Set the power mode of an am7xxx device.
277  *
278  * @note When setting the mode to AM7XXX_POWER_OFF the display can't be turned
279  * on again by using only am7xxx_set_power_mode(), am7xxx_set_zoom_mode() has
280  * to be called first, the current guess is that the latter performs some
281  * other resets beside setting the zoom mode.
282  *
283  * @param[in] dev A pointer to the structure representing the device to set power mode to
284  * @param[in] power The power mode to put the device in (see #am7xxx_power_mode enum)
285  *
286  * @return 0 on success, a negative value on error
287  *
288  */
289 int am7xxx_set_power_mode(am7xxx_device *dev, am7xxx_power_mode power);
290
291 /**
292  * Set the zoom mode of an am7xxx device.
293  *
294  * @note When setting the mode to AM7XXX_ZOOM_TEST, the calling program might
295  * want to skip displaying actual images.
296  *
297  * @note It looks like that power mode and zoom mode are related somehow wrt.
298  * resetting the operational mode after AM7XXX_POWER_OFF, applications can
299  * restore the display properly using this combination:
300  *  - Off: power mode 0, zoom mode 3
301  *  - On: power mode != 0, zoom mode != 3
302  *
303  * @param[in] dev A pointer to the structure representing the device to set zoom mode to
304  * @param[in] zoom The zoom mode to put the device in (see #am7xxx_zoom_mode enum)
305  *
306  * @return 0 on success, a negative value on error
307  *
308  */
309 int am7xxx_set_zoom_mode(am7xxx_device *dev, am7xxx_zoom_mode zoom);
310
311 #ifdef __cplusplus
312 }
313 #endif
314
315 #endif /* __AM7XXX_H */