forked from smaccm/camera_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixy.cpp
341 lines (255 loc) · 8.03 KB
/
pixy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <stdio.h>
#include "pixy.h"
#include "smaccminterpreter.hpp"
SmaccmInterpreter interpreter;
// Pixy C API //
extern "C"
{
static struct
{
int error;
const char * text;
} PIXY_ERROR_TABLE[] = {
{ 0, "Success" },
{ PIXY_ERROR_USB_IO, "USB Error: I/O" },
{ PIXY_ERROR_USB_BUSY, "USB Error: Busy" },
{ PIXY_ERROR_USB_NO_DEVICE, "USB Error: No device" },
{ PIXY_ERROR_USB_NOT_FOUND, "USB Error: Target not found" },
{ PIXY_ERROR_CHIRP, "Chirp Protocol Error" },
{ PIXY_ERROR_INVALID_COMMAND, "Pixy Error: Invalid command" },
{ 0, 0 }
};
static int pixy_initialized = false;
int pixy_init()
{
int return_value;
return_value = interpreter.init();
if(return_value == 0)
{
pixy_initialized = true;
}
return return_value;
}
int pixy_get_blocks(uint16_t max_blocks, struct Block * blocks)
{
return interpreter.get_blocks(max_blocks, blocks);
}
int pixy_command(const char *name, ...)
{
va_list arguments;
int return_value;
if(!pixy_initialized) return -1;
va_start(arguments, name);
return_value = interpreter.send_command(name, arguments);
va_end(arguments);
return return_value;
}
void pixy_close()
{
if(!pixy_initialized) return;
interpreter.close();
}
void pixy_error(int error_code)
{
int index;
// Convert pixy error code to string and display to stdout //
index = 0;
while(PIXY_ERROR_TABLE[index].text != 0) {
if(PIXY_ERROR_TABLE[index].error == error_code) {
printf("%s\n", PIXY_ERROR_TABLE[index].text);
return;
}
index += 1;
}
printf("Undefined error: [%d]\n", error_code);
}
int pixy_led_set_RGB(uint8_t red, uint8_t green, uint8_t blue)
{
int chirp_response;
int return_value;
uint32_t RGB;
// Pack the RGB value //
RGB = blue + (green << 8) + (red << 16);
return_value = pixy_command("led_set", CRP_INT32, RGB, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
return return_value;
}
int pixy_led_set_max_current(uint32_t current)
{
int chirp_response;
int return_value;
return_value = pixy_command("led_setMaxCurrent", CRP_INT32, current, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
return return_value;
}
int pixy_led_get_max_current()
{
int return_value;
uint32_t chirp_response;
return_value = pixy_command("led_getMaxCurrent", END_OUT_ARGS, &chirp_response, END_IN_ARGS);
if (return_value < 0) {
// Error //
return return_value;
} else {
// Success //
return chirp_response;
}
}
int pixy_cam_set_auto_white_balance(uint8_t enable)
{
int return_value;
uint32_t chirp_response;
return_value = pixy_command("cam_setAWB", CRP_UINT8, enable, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
return return_value;
}
int pixy_cam_get_auto_white_balance()
{
int return_value;
uint32_t chirp_response;
return_value = pixy_command("cam_getAWB", END_OUT_ARGS, &chirp_response, END_IN_ARGS);
if (return_value < 0) {
// Error //
return return_value;
} else {
// Success //
return chirp_response;
}
}
uint32_t pixy_cam_get_white_balance_value()
{
int return_value;
uint32_t chirp_response;
return_value = pixy_command("cam_getWBV", END_OUT_ARGS, &chirp_response, END_IN_ARGS);
return chirp_response;
}
int pixy_cam_set_white_balance_value(uint8_t red, uint8_t green, uint8_t blue)
{
int return_value;
uint32_t chirp_response;
uint32_t white_balance;
white_balance = green + (red << 8) + (blue << 16);
return_value = pixy_command("cam_setAWB", CRP_UINT32, white_balance, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
return return_value;
}
int pixy_cam_set_auto_exposure_compensation(uint8_t enable)
{
int return_value;
uint32_t chirp_response;
return_value = pixy_command("cam_setAEC", CRP_UINT8, enable, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
return return_value;
}
int pixy_cam_get_auto_exposure_compensation()
{
int return_value;
uint32_t chirp_response;
return_value = pixy_command("cam_getAEC", END_OUT_ARGS, &chirp_response, END_IN_ARGS);
if (return_value < 0) {
// Error //
return return_value;
} else {
// Success //
return chirp_response;
}
}
int pixy_cam_set_exposure_compensation(uint8_t gain, uint16_t compensation)
{
int return_value;
uint32_t chirp_response;
uint32_t exposure;
exposure = gain + (compensation << 8);
return_value = pixy_command("cam_setECV", CRP_UINT32, exposure, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
return return_value;
}
int pixy_cam_get_exposure_compensation(uint8_t * gain, uint16_t * compensation)
{
uint32_t exposure;
int return_value;
return_value = pixy_command("cam_getECV", END_OUT_ARGS, &exposure, END_IN_ARGS);
if (return_value < 0) {
// Chirp error //
return return_value;
}
if(gain == 0 || compensation == 0) {
// Error: Null pointer //
return PIXY_ERROR_INVALID_PARAMETER;
}
printf("exp:%08x\n", exposure);
*gain = exposure & 0xFF;
*compensation = 0xFFFF & (exposure >> 8);
return 0;
}
int pixy_cam_set_brightness(uint8_t brightness)
{
int chirp_response;
int return_value;
return_value = pixy_command("cam_setBrightness", CRP_INT8, brightness, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
return return_value;
}
int pixy_cam_get_brightness()
{
int chirp_response;
int return_value;
return_value = pixy_command("cam_getBrightness", END_OUT_ARGS, &chirp_response, END_IN_ARGS);
if (return_value < 0) {
// Error //
return return_value;
} else {
// Success //
return chirp_response;
}
}
int pixy_rcs_get_position(uint8_t channel)
{
int chirp_response;
int return_value;
return_value = pixy_command("rcs_getPos", CRP_INT8, channel, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
if (return_value < 0) {
// Error //
return return_value;
} else {
// Success //
return chirp_response;
}
}
int pixy_rcs_set_position(uint8_t channel, uint16_t position)
{
int chirp_response;
int return_value;
if(channel > 1 || position > 999) {
return PIXY_ERROR_INVALID_PARAMETER;
}
return_value = pixy_command("rcs_setPos", CRP_INT8, channel, CRP_INT16, position, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
return return_value;
}
int pixy_rcs_set_frequency(uint16_t frequency)
{
int chirp_response;
int return_value;
if(frequency < 20 || frequency > 300) {
return PIXY_ERROR_INVALID_PARAMETER;
}
return_value = pixy_command("rcs_setFreq", CRP_INT16, frequency, END_OUT_ARGS, &chirp_response, END_IN_ARGS);
return return_value;
}
int pixy_get_firmware_version(uint16_t * major, uint16_t * minor, uint16_t * build)
{
uint16_t * pixy_version;
uint32_t version_length;
uint32_t response;
uint16_t version[3];
int return_value;
int chirp_response;
if(major == 0 || minor == 0 || build == 0) {
// Error: Null pointer //
return PIXY_ERROR_INVALID_PARAMETER;
}
return_value = pixy_command("version", END_OUT_ARGS, &response, &version_length, &pixy_version, END_IN_ARGS);
if (return_value < 0) {
// Error //
return return_value;
}
memcpy((void *) version, pixy_version, 3 * sizeof(uint16_t));
*major = version[0];
*minor = version[1];
*build = version[2];
return 0;
}
}