Pixy: A fast, easy to use, vision sensor
 All Classes Files Functions Variables Macros Pages
pixy.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include "pixy.h"
3 #include "pixyinterpreter.hpp"
4 
5 PixyInterpreter interpreter;
6 
66 // Pixy C API //
67 
68 extern "C"
69 {
70  static struct
71  {
72  int error;
73  const char * text;
74  } PIXY_ERROR_TABLE[] = {
75  { 0, "Success" },
76  { PIXY_ERROR_USB_IO, "USB Error: I/O" },
77  { PIXY_ERROR_USB_BUSY, "USB Error: Busy" },
78  { PIXY_ERROR_USB_NO_DEVICE, "USB Error: No device" },
79  { PIXY_ERROR_USB_NOT_FOUND, "USB Error: Target not found" },
80  { PIXY_ERROR_CHIRP, "Chirp Protocol Error" },
81  { PIXY_ERROR_INVALID_COMMAND, "Pixy Error: Invalid command" },
82  { 0, 0 }
83  };
84 
85  static int pixy_initialized = false;
86 
87  int pixy_init()
88  {
89  int return_value;
90 
91  return_value = interpreter.init();
92 
93  if(return_value == 0)
94  {
95  pixy_initialized = true;
96  }
97 
98  return return_value;
99  }
100 
101  int pixy_get_blocks(uint16_t max_blocks, struct Block * blocks)
102  {
103  return interpreter.get_blocks(max_blocks, blocks);
104  }
105 
107  {
108  return interpreter.blocks_are_new();
109  }
110 
111  int pixy_command(const char *name, ...)
112  {
113  va_list arguments;
114  int return_value;
115 
116  if(!pixy_initialized) return -1;
117 
118  va_start(arguments, name);
119  return_value = interpreter.send_command(name, arguments);
120  va_end(arguments);
121 
122  return return_value;
123  }
124 
125  void pixy_close()
126  {
127  if(!pixy_initialized) return;
128 
129  interpreter.close();
130  }
131 
132  void pixy_error(int error_code)
133  {
134  int index;
135 
136  // Convert pixy error code to string and display to stdout //
137 
138  index = 0;
139 
140  while(PIXY_ERROR_TABLE[index].text != 0) {
141 
142  if(PIXY_ERROR_TABLE[index].error == error_code) {
143  printf("%s\n", PIXY_ERROR_TABLE[index].text);
144  return;
145  }
146 
147  index += 1;
148  }
149 
150  printf("Undefined error: [%d]\n", error_code);
151  }
152 
153  int pixy_led_set_RGB(uint8_t red, uint8_t green, uint8_t blue)
154  {
155  int chirp_response;
156  int return_value;
157  uint32_t RGB;
158 
159  // Pack the RGB value //
160  RGB = blue + (green << 8) + (red << 16);
161 
162  return_value = pixy_command("led_set", CRP_INT32, RGB, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
163 
164  return return_value;
165  }
166 
167  int pixy_led_set_max_current(uint32_t current)
168  {
169  int chirp_response;
170  int return_value;
171 
172  return_value = pixy_command("led_setMaxCurrent", CRP_INT32, current, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
173 
174  return return_value;
175  }
176 
178  {
179  int return_value;
180  uint32_t chirp_response;
181 
182  return_value = pixy_command("led_getMaxCurrent", END_OUT_ARGS, &chirp_response, END_IN_ARGS);
183 
184  if (return_value < 0) {
185  // Error //
186  return return_value;
187  } else {
188  // Success //
189  return chirp_response;
190  }
191  }
192 
194  {
195  int return_value;
196  uint32_t chirp_response;
197 
198  return_value = pixy_command("cam_setAWB", CRP_UINT8, enable, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
199 
200  return return_value;
201  }
202 
204  {
205  int return_value;
206  uint32_t chirp_response;
207 
208  return_value = pixy_command("cam_getAWB", END_OUT_ARGS, &chirp_response, END_IN_ARGS);
209 
210  if (return_value < 0) {
211  // Error //
212  return return_value;
213  } else {
214  // Success //
215  return chirp_response;
216  }
217  }
218 
220  {
221  int return_value;
222  uint32_t chirp_response;
223 
224  return_value = pixy_command("cam_getWBV", END_OUT_ARGS, &chirp_response, END_IN_ARGS);
225 
226  return chirp_response;
227  }
228 
229  int pixy_cam_set_white_balance_value(uint8_t red, uint8_t green, uint8_t blue)
230  {
231  int return_value;
232  uint32_t chirp_response;
233  uint32_t white_balance;
234 
235  white_balance = green + (red << 8) + (blue << 16);
236 
237  return_value = pixy_command("cam_setAWB", CRP_UINT32, white_balance, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
238 
239  return return_value;
240  }
241 
243  {
244  int return_value;
245  uint32_t chirp_response;
246 
247  return_value = pixy_command("cam_setAEC", CRP_UINT8, enable, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
248 
249  return return_value;
250 }
251 
253  {
254  int return_value;
255  uint32_t chirp_response;
256 
257  return_value = pixy_command("cam_getAEC", END_OUT_ARGS, &chirp_response, END_IN_ARGS);
258 
259  if (return_value < 0) {
260  // Error //
261  return return_value;
262  } else {
263  // Success //
264  return chirp_response;
265  }
266  }
267 
268  int pixy_cam_set_exposure_compensation(uint8_t gain, uint16_t compensation)
269  {
270  int return_value;
271  uint32_t chirp_response;
272  uint32_t exposure;
273 
274  exposure = gain + (compensation << 8);
275 
276  return_value = pixy_command("cam_setECV", CRP_UINT32, exposure, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
277 
278  return return_value;
279  }
280 
281  int pixy_cam_get_exposure_compensation(uint8_t * gain, uint16_t * compensation)
282  {
283  uint32_t exposure;
284  int return_value;
285 
286  return_value = pixy_command("cam_getECV", END_OUT_ARGS, &exposure, END_IN_ARGS);
287 
288  if (return_value < 0) {
289  // Chirp error //
290  return return_value;
291  }
292 
293  if(gain == 0 || compensation == 0) {
294  // Error: Null pointer //
295  return PIXY_ERROR_INVALID_PARAMETER;
296  }
297 
298  printf("exp:%08x\n", exposure);
299 
300  *gain = exposure & 0xFF;
301  *compensation = 0xFFFF & (exposure >> 8);
302 
303  return 0;
304  }
305 
306  int pixy_cam_set_brightness(uint8_t brightness)
307  {
308  int chirp_response;
309  int return_value;
310 
311  return_value = pixy_command("cam_setBrightness", CRP_INT8, brightness, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
312 
313  return return_value;
314  }
315 
317  {
318  int chirp_response;
319  int return_value;
320 
321  return_value = pixy_command("cam_getBrightness", END_OUT_ARGS, &chirp_response, END_IN_ARGS);
322 
323  if (return_value < 0) {
324  // Error //
325  return return_value;
326  } else {
327  // Success //
328  return chirp_response;
329  }
330  }
331 
332  int pixy_rcs_get_position(uint8_t channel)
333  {
334  int chirp_response;
335  int return_value;
336 
337  return_value = pixy_command("rcs_getPos", CRP_INT8, channel, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
338 
339  if (return_value < 0) {
340  // Error //
341  return return_value;
342  } else {
343  // Success //
344  return chirp_response;
345  }
346  }
347 
348  int pixy_rcs_set_position(uint8_t channel, uint16_t position)
349  {
350  int chirp_response;
351  int return_value;
352 
353  if(channel > 1 || position > 999) {
354  return PIXY_ERROR_INVALID_PARAMETER;
355  }
356 
357  return_value = pixy_command("rcs_setPos", CRP_INT8, channel, CRP_INT16, position, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
358 
359  return return_value;
360  }
361 
362  int pixy_rcs_set_frequency(uint16_t frequency)
363  {
364  int chirp_response;
365  int return_value;
366 
367  if(frequency < 20 || frequency > 300) {
368  return PIXY_ERROR_INVALID_PARAMETER;
369  }
370 
371  return_value = pixy_command("rcs_setFreq", CRP_INT16, frequency, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
372 
373  return return_value;
374  }
375 
376  int pixy_get_firmware_version(uint16_t * major, uint16_t * minor, uint16_t * build)
377  {
378  uint16_t * pixy_version;
379  uint32_t version_length;
380  uint32_t response;
381  uint16_t version[3];
382  int return_value;
383  int chirp_response;
384 
385  if(major == 0 || minor == 0 || build == 0) {
386  // Error: Null pointer //
387  return PIXY_ERROR_INVALID_PARAMETER;
388  }
389 
390  return_value = pixy_command("version", END_OUT_ARGS, &response, &version_length, &pixy_version, END_IN_ARGS);
391 
392  if (return_value < 0) {
393  // Error //
394  return return_value;
395  }
396 
397  memcpy((void *) version, pixy_version, 3 * sizeof(uint16_t));
398 
399  *major = version[0];
400  *minor = version[1];
401  *build = version[2];
402 
403  return 0;
404  }
405 }
int pixy_rcs_set_frequency(uint16_t frequency)
Set pixy servo pulse width modulation (PWM) frequency.
Definition: pixy.cpp:362
int pixy_cam_get_exposure_compensation(uint8_t *gain, uint16_t *compensation)
Get pixy camera exposure compensation.
Definition: pixy.cpp:281
int pixy_get_blocks(uint16_t max_blocks, struct Block *blocks)
Copies up to 'max_blocks' number of Blocks to the address pointed to by 'blocks'. ...
Definition: pixy.cpp:101
void pixy_close()
Terminates connection with Pixy.
Definition: pixy.cpp:125
int error
Definition: pixy.cpp:72
int pixy_rcs_get_position(uint8_t channel)
Get pixy servo axis position.
Definition: pixy.cpp:332
int pixy_cam_get_auto_white_balance()
Get pixy camera auto white balance setting.
Definition: pixy.cpp:203
void pixy_error(int error_code)
Send description of pixy error to stdout.
Definition: pixy.cpp:132
int pixy_cam_set_white_balance_value(uint8_t red, uint8_t green, uint8_t blue)
Set pixy camera white balance.
Definition: pixy.cpp:229
int pixy_cam_set_auto_white_balance(uint8_t enable)
Enable or disable pixy camera auto white balance.
Definition: pixy.cpp:193
int pixy_led_set_max_current(uint32_t current)
Set pixy LED maximum current.
Definition: pixy.cpp:167
int pixy_led_get_max_current()
Get pixy LED maximum current.
Definition: pixy.cpp:177
int pixy_init()
Creates a connection with Pixy and listens for Pixy messages.
Definition: pixy.cpp:87
const char * text
Definition: pixy.cpp:73
PixyInterpreter interpreter
Definition: pixy.cpp:5
int pixy_blocks_are_new()
Indicates when new block data from Pixy is received.
Definition: pixy.cpp:106
int pixy_rcs_set_position(uint8_t channel, uint16_t position)
Set pixy servo axis position.
Definition: pixy.cpp:348
int pixy_get_firmware_version(uint16_t *major, uint16_t *minor, uint16_t *build)
Get pixy firmware version.
Definition: pixy.cpp:376
int pixy_cam_get_brightness()
Get pixy camera brightness.
Definition: pixy.cpp:316
int pixy_cam_set_brightness(uint8_t brightness)
Set pixy camera brightness.
Definition: pixy.cpp:306
int pixy_cam_set_exposure_compensation(uint8_t gain, uint16_t compensation)
Set pixy camera exposure compensation.
Definition: pixy.cpp:268
int pixy_led_set_RGB(uint8_t red, uint8_t green, uint8_t blue)
Set color of pixy LED.
Definition: pixy.cpp:153
Definition: pixy.h:33
int pixy_cam_set_auto_exposure_compensation(uint8_t enable)
Enable or disable pixy camera auto exposure compensation.
Definition: pixy.cpp:242
int pixy_cam_get_auto_exposure_compensation()
Get pixy camera auto exposure compensation setting.
Definition: pixy.cpp:252
int pixy_command(const char *name,...)
Send a command to Pixy.
Definition: pixy.cpp:111
uint32_t pixy_cam_get_white_balance_value()
Get pixy camera white balance()
Definition: pixy.cpp:219