PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/host/libpixyusb/src/pixy.cpp

https://gitlab.com/vicidroiddev/pixy
C++ | 457 lines | 276 code | 86 blank | 95 comment | 49 complexity | 2e44466db514c7978d7f1ac2e6c1b838 MD5 | raw file
  1. #include <stdio.h>
  2. #include "pixy.h"
  3. #include "pixyinterpreter.hpp"
  4. PixyInterpreter interpreter;
  5. /**
  6. \mainpage libpixyusb-0.4 API Reference
  7. \section introduction Introduction
  8. libpixyusb is an open source library that allows you to communicate with
  9. Pixy over the USB protocol.
  10. This documentation is aimed at application developers wishing to send
  11. commands to Pixy or read sensor data from Pixy.
  12. \section library_features Library features
  13. - Read blocks with or without color codes
  14. - RGB LED control (color/intensity)
  15. - Auto white balance control
  16. - Auto exposure compensation control
  17. - Brightness control
  18. - Servo position control/query
  19. - Custom commands
  20. \section dependencies Dependencies
  21. Required to build:
  22. - <a href=http://www.cmake.org>cmake</a>
  23. Required for runtime:
  24. - <a href=http://www.libusb.org>libusb</a>
  25. - <a href=http://www.boost.org>libboost</a>
  26. \section getting_started Getting Started
  27. The libpixyusb API reference documentation can be found here:
  28. libpixyusb API Reference
  29. Some tutorials that use libpixyusb can be found here:
  30. <a href=http://cmucam.org/projects/cmucam5/wiki/Hooking_up_Pixy_to_a_Raspberry_Pi>Hooking up Pixy to a Raspberry Pi</a>
  31. <a href=http://cmucam.org/projects/cmucam5/wiki/Hooking_up_Pixy_to_a_Beaglebone_Black>Hooking up Pixy to a BeagleBone Black</a>
  32. \section getting_help Getting Help
  33. Tutorials, walkthroughs, and more are available on the Pixy wiki page:
  34. <a href=http://www.cmucam.org/projects/cmucam5/wiki>Pixy Developer Wiki Page</a>
  35. Our friendly developers and users might be able to answer your question on the forums:
  36. <a href=http://www.cmucam.org/projects/cmucam5/boards/9>Pixy Software Discussion Forum</a>
  37. <a href=http://www.cmucam.org/projects/cmucam5/boards/8>Pixy Hardware Discussion Forum</a>
  38. */
  39. // Pixy C API //
  40. extern "C"
  41. {
  42. static struct
  43. {
  44. int error;
  45. const char * text;
  46. } PIXY_ERROR_TABLE[] = {
  47. { 0, "Success" },
  48. { PIXY_ERROR_USB_IO, "USB Error: I/O" },
  49. { PIXY_ERROR_USB_BUSY, "USB Error: Busy" },
  50. { PIXY_ERROR_USB_NO_DEVICE, "USB Error: No device" },
  51. { PIXY_ERROR_USB_NOT_FOUND, "USB Error: Target not found" },
  52. { PIXY_ERROR_CHIRP, "Chirp Protocol Error" },
  53. { PIXY_ERROR_INVALID_COMMAND, "Pixy Error: Invalid command" },
  54. { 0, 0 }
  55. };
  56. static int pixy_initialized = false;
  57. int pixy_init()
  58. {
  59. int return_value;
  60. return_value = interpreter.init();
  61. if(return_value == 0)
  62. {
  63. pixy_initialized = true;
  64. }
  65. return return_value;
  66. }
  67. int pixy_get_blocks(uint16_t max_blocks, struct Block * blocks)
  68. {
  69. return interpreter.get_blocks(max_blocks, blocks);
  70. }
  71. int pixy_blocks_are_new()
  72. {
  73. return interpreter.blocks_are_new();
  74. }
  75. int pixy_command(const char *name, ...)
  76. {
  77. va_list arguments;
  78. int return_value;
  79. if(!pixy_initialized) return -1;
  80. va_start(arguments, name);
  81. return_value = interpreter.send_command(name, arguments);
  82. va_end(arguments);
  83. return return_value;
  84. }
  85. void pixy_close()
  86. {
  87. if(!pixy_initialized) return;
  88. interpreter.close();
  89. }
  90. void pixy_error(int error_code)
  91. {
  92. int index;
  93. // Convert pixy error code to string and display to stdout //
  94. index = 0;
  95. while(PIXY_ERROR_TABLE[index].text != 0) {
  96. if(PIXY_ERROR_TABLE[index].error == error_code) {
  97. printf("%s\n", PIXY_ERROR_TABLE[index].text);
  98. return;
  99. }
  100. index += 1;
  101. }
  102. printf("Undefined error: [%d]\n", error_code);
  103. }
  104. int pixy_led_set_RGB(uint8_t red, uint8_t green, uint8_t blue)
  105. {
  106. int chirp_response;
  107. int return_value;
  108. uint32_t RGB;
  109. // Pack the RGB value //
  110. RGB = blue + (green << 8) + (red << 16);
  111. return_value = pixy_command("led_set", INT32(RGB), END_OUT_ARGS, &chirp_response, END_IN_ARGS);
  112. if (return_value < 0) {
  113. // Error //
  114. return return_value;
  115. } else {
  116. // Success //
  117. return chirp_response;
  118. }
  119. }
  120. int pixy_led_set_max_current(uint32_t current)
  121. {
  122. int chirp_response;
  123. int return_value;
  124. return_value = pixy_command("led_setMaxCurrent", INT32(current), END_OUT_ARGS, &chirp_response, END_IN_ARGS);
  125. if (return_value < 0) {
  126. // Error //
  127. return return_value;
  128. } else {
  129. // Success //
  130. return chirp_response;
  131. }
  132. }
  133. int pixy_led_get_max_current()
  134. {
  135. int return_value;
  136. uint32_t chirp_response;
  137. return_value = pixy_command("led_getMaxCurrent", END_OUT_ARGS, &chirp_response, END_IN_ARGS);
  138. if (return_value < 0) {
  139. // Error //
  140. return return_value;
  141. } else {
  142. // Success //
  143. return chirp_response;
  144. }
  145. }
  146. int pixy_cam_set_auto_white_balance(uint8_t enable)
  147. {
  148. int return_value;
  149. uint32_t chirp_response;
  150. return_value = pixy_command("cam_setAWB", UINT8(enable), END_OUT_ARGS, &chirp_response, END_IN_ARGS);
  151. if (return_value < 0) {
  152. // Error //
  153. return return_value;
  154. } else {
  155. // Success //
  156. return chirp_response;
  157. }
  158. }
  159. int pixy_cam_get_auto_white_balance()
  160. {
  161. int return_value;
  162. uint32_t chirp_response;
  163. return_value = pixy_command("cam_getAWB", END_OUT_ARGS, &chirp_response, END_IN_ARGS);
  164. if (return_value < 0) {
  165. // Error //
  166. return return_value;
  167. } else {
  168. // Success //
  169. return chirp_response;
  170. }
  171. }
  172. uint32_t pixy_cam_get_white_balance_value()
  173. {
  174. int return_value;
  175. uint32_t chirp_response;
  176. return_value = pixy_command("cam_getWBV", END_OUT_ARGS, &chirp_response, END_IN_ARGS);
  177. if (return_value < 0) {
  178. // Error //
  179. return return_value;
  180. } else {
  181. // Success //
  182. return chirp_response;
  183. }
  184. }
  185. int pixy_cam_set_white_balance_value(uint8_t red, uint8_t green, uint8_t blue)
  186. {
  187. int return_value;
  188. uint32_t chirp_response;
  189. uint32_t white_balance;
  190. white_balance = green + (red << 8) + (blue << 16);
  191. return_value = pixy_command("cam_setAWB", UINT32(white_balance), END_OUT_ARGS, &chirp_response, END_IN_ARGS);
  192. if (return_value < 0) {
  193. // Error //
  194. return return_value;
  195. } else {
  196. // Success //
  197. return chirp_response;
  198. }
  199. }
  200. int pixy_cam_set_auto_exposure_compensation(uint8_t enable)
  201. {
  202. int return_value;
  203. uint32_t chirp_response;
  204. return_value = pixy_command("cam_setAEC", UINT8(enable), END_OUT_ARGS, &chirp_response, END_IN_ARGS);
  205. if (return_value < 0) {
  206. // Error //
  207. return return_value;
  208. } else {
  209. // Success //
  210. return chirp_response;
  211. }
  212. }
  213. int pixy_cam_get_auto_exposure_compensation()
  214. {
  215. int return_value;
  216. uint32_t chirp_response;
  217. return_value = pixy_command("cam_getAEC", END_OUT_ARGS, &chirp_response, END_IN_ARGS);
  218. if (return_value < 0) {
  219. // Error //
  220. return return_value;
  221. } else {
  222. // Success //
  223. return chirp_response;
  224. }
  225. }
  226. int pixy_cam_set_exposure_compensation(uint8_t gain, uint16_t compensation)
  227. {
  228. int return_value;
  229. uint32_t chirp_response;
  230. uint32_t exposure;
  231. exposure = gain + (compensation << 8);
  232. return_value = pixy_command("cam_setECV", UINT32(exposure), END_OUT_ARGS, &chirp_response, END_IN_ARGS);
  233. if (return_value < 0) {
  234. // Error //
  235. return return_value;
  236. } else {
  237. // Success //
  238. return chirp_response;
  239. }
  240. }
  241. int pixy_cam_get_exposure_compensation(uint8_t * gain, uint16_t * compensation)
  242. {
  243. uint32_t exposure;
  244. int return_value;
  245. return_value = pixy_command("cam_getECV", END_OUT_ARGS, &exposure, END_IN_ARGS);
  246. if (return_value < 0) {
  247. // Chirp error //
  248. return return_value;
  249. }
  250. if(gain == 0 || compensation == 0) {
  251. // Error: Null pointer //
  252. return PIXY_ERROR_INVALID_PARAMETER;
  253. }
  254. printf("exp:%08x\n", exposure);
  255. *gain = exposure & 0xFF;
  256. *compensation = 0xFFFF & (exposure >> 8);
  257. return 0;
  258. }
  259. int pixy_cam_set_brightness(uint8_t brightness)
  260. {
  261. int chirp_response;
  262. int return_value;
  263. return_value = pixy_command("cam_setBrightness", UINT8(brightness), END_OUT_ARGS, &chirp_response, END_IN_ARGS);
  264. if (return_value < 0) {
  265. // Error //
  266. return return_value;
  267. } else {
  268. // Success //
  269. return chirp_response;
  270. }
  271. }
  272. int pixy_cam_get_brightness()
  273. {
  274. int chirp_response;
  275. int return_value;
  276. return_value = pixy_command("cam_getBrightness", END_OUT_ARGS, &chirp_response, END_IN_ARGS);
  277. if (return_value < 0) {
  278. // Error //
  279. return return_value;
  280. } else {
  281. // Success //
  282. return chirp_response;
  283. }
  284. }
  285. int pixy_rcs_get_position(uint8_t channel)
  286. {
  287. int chirp_response;
  288. int return_value;
  289. return_value = pixy_command("rcs_getPos", UINT8(channel), END_OUT_ARGS, &chirp_response, END_IN_ARGS);
  290. if (return_value < 0) {
  291. // Error //
  292. return return_value;
  293. } else {
  294. // Success //
  295. return chirp_response;
  296. }
  297. }
  298. int pixy_rcs_set_position(uint8_t channel, uint16_t position)
  299. {
  300. int chirp_response;
  301. int return_value;
  302. return_value = pixy_command("rcs_setPos", UINT8(channel), INT16(position), END_OUT_ARGS, &chirp_response, END_IN_ARGS);
  303. if (return_value < 0) {
  304. // Error //
  305. return return_value;
  306. } else {
  307. // Success //
  308. return chirp_response;
  309. }
  310. }
  311. int pixy_rcs_set_frequency(uint16_t frequency)
  312. {
  313. int chirp_response;
  314. int return_value;
  315. return_value = pixy_command("rcs_setFreq", UINT16(frequency), END_OUT_ARGS, &chirp_response, END_IN_ARGS);
  316. if (return_value < 0) {
  317. // Error //
  318. return return_value;
  319. } else {
  320. // Success //
  321. return chirp_response;
  322. }
  323. }
  324. int pixy_get_firmware_version(uint16_t * major, uint16_t * minor, uint16_t * build)
  325. {
  326. uint16_t * pixy_version;
  327. uint32_t version_length;
  328. uint32_t response;
  329. uint16_t version[3];
  330. int return_value;
  331. int chirp_response;
  332. if(major == 0 || minor == 0 || build == 0) {
  333. // Error: Null pointer //
  334. return PIXY_ERROR_INVALID_PARAMETER;
  335. }
  336. return_value = pixy_command("version", END_OUT_ARGS, &response, &version_length, &pixy_version, END_IN_ARGS);
  337. if (return_value < 0) {
  338. // Error //
  339. return return_value;
  340. }
  341. memcpy((void *) version, pixy_version, 3 * sizeof(uint16_t));
  342. *major = version[0];
  343. *minor = version[1];
  344. *build = version[2];
  345. return 0;
  346. }
  347. }