Mike Spadaru / physcom
Committer:
maspadaru
Date:
Tue Nov 24 14:26:05 2020 +0000
Revision:
10:b1bdc51e1c50
added Pixy2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maspadaru 10:b1bdc51e1c50 1 //
maspadaru 10:b1bdc51e1c50 2 // begin license header
maspadaru 10:b1bdc51e1c50 3 //
maspadaru 10:b1bdc51e1c50 4 // This file is part of Pixy CMUcam5 or "Pixy" for short
maspadaru 10:b1bdc51e1c50 5 //
maspadaru 10:b1bdc51e1c50 6 // All Pixy source code is provided under the terms of the
maspadaru 10:b1bdc51e1c50 7 // GNU General Public License v2 (http://www.gnu.org/licenses/gpl-2.0.html).
maspadaru 10:b1bdc51e1c50 8 // Those wishing to use Pixy source code, software and/or
maspadaru 10:b1bdc51e1c50 9 // technologies under different licensing terms should contact us at
maspadaru 10:b1bdc51e1c50 10 // cmucam@cs.cmu.edu. Such licensing terms are available for
maspadaru 10:b1bdc51e1c50 11 // all portions of the Pixy codebase presented here.
maspadaru 10:b1bdc51e1c50 12 //
maspadaru 10:b1bdc51e1c50 13 // end license header
maspadaru 10:b1bdc51e1c50 14 //
maspadaru 10:b1bdc51e1c50 15 // This file is for defining the Block struct and the Pixy template class version 2.
maspadaru 10:b1bdc51e1c50 16 // (TPixy2). TPixy takes a communication link as a template parameter so that
maspadaru 10:b1bdc51e1c50 17 // all communication modes (SPI, I2C and UART) can share the same code.
maspadaru 10:b1bdc51e1c50 18 //
maspadaru 10:b1bdc51e1c50 19
maspadaru 10:b1bdc51e1c50 20 #ifndef _PIXY2VIDEO_H
maspadaru 10:b1bdc51e1c50 21 #define _PIXY2VIDEO_H
maspadaru 10:b1bdc51e1c50 22
maspadaru 10:b1bdc51e1c50 23 #define VIDEO_REQUEST_GET_RGB 0x70
maspadaru 10:b1bdc51e1c50 24
maspadaru 10:b1bdc51e1c50 25 template <class LinkType>
maspadaru 10:b1bdc51e1c50 26 class TPixy2;
maspadaru 10:b1bdc51e1c50 27
maspadaru 10:b1bdc51e1c50 28 template <class LinkType>
maspadaru 10:b1bdc51e1c50 29 class Pixy2Video {
maspadaru 10:b1bdc51e1c50 30 public:
maspadaru 10:b1bdc51e1c50 31 Pixy2Video(TPixy2<LinkType>* pixy)
maspadaru 10:b1bdc51e1c50 32 {
maspadaru 10:b1bdc51e1c50 33 m_pixy = pixy;
maspadaru 10:b1bdc51e1c50 34 }
maspadaru 10:b1bdc51e1c50 35
maspadaru 10:b1bdc51e1c50 36 /**
maspadaru 10:b1bdc51e1c50 37 * This function gets the RGB value of a pixel in the camera frame.
maspadaru 10:b1bdc51e1c50 38 *
maspadaru 10:b1bdc51e1c50 39 * @param x x-position of the pixel. top-left is 0,0
maspadaru 10:b1bdc51e1c50 40 * @param y y-position of the pixel. top-left is 0,0
maspadaru 10:b1bdc51e1c50 41 * @param r uint8_t that will be populated with the red value
maspadaru 10:b1bdc51e1c50 42 * @param g uint8_t that will be populated with the gree value
maspadaru 10:b1bdc51e1c50 43 * @param b uint8_t that will be populated with the blue value
maspadaru 10:b1bdc51e1c50 44 * @returns an error value (<0) if it fails and PIXY_RESULT_OK if it succeeds
maspadaru 10:b1bdc51e1c50 45 */
maspadaru 10:b1bdc51e1c50 46 int8_t getRGB(uint16_t x, uint16_t y, uint8_t* r, uint8_t* g, uint8_t* b, bool saturate = true);
maspadaru 10:b1bdc51e1c50 47
maspadaru 10:b1bdc51e1c50 48 private:
maspadaru 10:b1bdc51e1c50 49 TPixy2<LinkType>* m_pixy;
maspadaru 10:b1bdc51e1c50 50 };
maspadaru 10:b1bdc51e1c50 51
maspadaru 10:b1bdc51e1c50 52 template <class LinkType>
maspadaru 10:b1bdc51e1c50 53 int8_t Pixy2Video<LinkType>::getRGB(uint16_t x, uint16_t y, uint8_t* r, uint8_t* g, uint8_t* b, bool saturate)
maspadaru 10:b1bdc51e1c50 54 {
maspadaru 10:b1bdc51e1c50 55 while (1) {
maspadaru 10:b1bdc51e1c50 56 *(int16_t*)(m_pixy->m_bufPayload + 0) = x;
maspadaru 10:b1bdc51e1c50 57 *(int16_t*)(m_pixy->m_bufPayload + 2) = y;
maspadaru 10:b1bdc51e1c50 58 *(m_pixy->m_bufPayload + 4) = saturate;
maspadaru 10:b1bdc51e1c50 59 m_pixy->m_length = 5;
maspadaru 10:b1bdc51e1c50 60 m_pixy->m_type = VIDEO_REQUEST_GET_RGB;
maspadaru 10:b1bdc51e1c50 61 m_pixy->sendPacket();
maspadaru 10:b1bdc51e1c50 62 if (m_pixy->recvPacket() == 0) {
maspadaru 10:b1bdc51e1c50 63 if (m_pixy->m_type == PIXY_TYPE_RESPONSE_RESULT && m_pixy->m_length == 4) {
maspadaru 10:b1bdc51e1c50 64 *b = *(m_pixy->m_buf + 0);
maspadaru 10:b1bdc51e1c50 65 *g = *(m_pixy->m_buf + 1);
maspadaru 10:b1bdc51e1c50 66 *r = *(m_pixy->m_buf + 2);
maspadaru 10:b1bdc51e1c50 67 return 0;
maspadaru 10:b1bdc51e1c50 68 }
maspadaru 10:b1bdc51e1c50 69 // deal with program changing
maspadaru 10:b1bdc51e1c50 70 else if (m_pixy->m_type == PIXY_TYPE_RESPONSE_ERROR && (int8_t)m_pixy->m_buf[0] == PIXY_RESULT_PROG_CHANGING) {
maspadaru 10:b1bdc51e1c50 71 wait_ms(500); // don't be a drag
maspadaru 10:b1bdc51e1c50 72 continue;
maspadaru 10:b1bdc51e1c50 73 }
maspadaru 10:b1bdc51e1c50 74 }
maspadaru 10:b1bdc51e1c50 75 return PIXY_RESULT_ERROR;
maspadaru 10:b1bdc51e1c50 76 }
maspadaru 10:b1bdc51e1c50 77 }
maspadaru 10:b1bdc51e1c50 78
maspadaru 10:b1bdc51e1c50 79 #endif
maspadaru 10:b1bdc51e1c50 80