constantCOLOR_CODES = 128constantSIG_ALL = 127constantSIG_7 = 64constantSIG_6 = 32constantSIG_5 = 16constantSIG_4 = 8constantSIG_3 = 4constantSIG_2 = 2constantSIG_1 = 1addcomment ▾Clear response buffer and reset index06310=arraypixybuf ▾elementbufIdx ▾00bufLen ▾=bufIdx ▾=repeatbufIdx ▾fromthru ▾bydofunctionclear_buffer run function “clear_buffer”Serial transmitASCII character ▾AEhexadecimalSerial transmitASCII character ▾C1hexadecimalSerial transmitASCII character ▾0EhexadecimalSerial transmitASCII character ▾00hexadecimalrun function “pixy_get_response”run function “extract_version”functionpixy_request_version run function “clear_buffer”Serial transmitASCII character ▾AEhexadecimalSerial transmitASCII character ▾C1hexadecimalSerial transmitASCII character ▾0ChexadecimalSerial transmitASCII character ▾01hexadecimalSerial transmitASCII character ▾00hexadecimalrun function “pixy_get_response”run function “extract_resolution”functionpixy_request_resolution run function “clear_buffer”maxBlocks ▾constrainfrom1(min) to3(max)Serial transmitASCII character ▾AEhexadecimalSerial transmitASCII character ▾C1hexadecimalSerial transmitASCII character ▾20hexadecimalSerial transmitASCII character ▾02hexadecimalSerial transmitASCII character ▾sigMap ▾Serial transmitASCII character ▾maxBlocks ▾run function “pixy_get_response”maxBlocks ▾=functionpixy_request_blocks run function “clear_buffer”Serial transmitASCII character ▾AEhexadecimalSerial transmitASCII character ▾C1hexadecimalSerial transmitASCII character ▾14hexadecimalSerial transmitASCII character ▾03hexadecimalSerial transmitASCII character ▾ledRed ▾Serial transmitASCII character ▾ledGreen ▾Serial transmitASCII character ▾ledBlue ▾run function “pixy_get_response”functionpixy_set_led run function “clear_buffer”Serial transmitASCII character ▾AEhexadecimalSerial transmitASCII character ▾C1hexadecimalSerial transmitASCII character ▾16hexadecimalSerial transmitASCII character ▾02hexadecimalSerial transmitASCII character ▾lampUpper ▾Serial transmitASCII character ▾lampLower ▾run function “pixy_get_response”functionpixy_set_lamps array initializepixybufwith64elementsaddcomment ▾Get response header0repeatx times ▾do6Serial receiveASCII character ▾store initem ▾bufIdx ▾item ▾+ ▾bufIdx ▾1bufIdx ▾=arraypixybuf ▾element=addcomment ▾Get size of payload3arraypixybuf ▾elementaddcomment ▾Get payloadrepeatx times ▾dopayloadBytes ▾Serial receiveASCII character ▾store initem ▾bufIdx ▾item ▾+ ▾bufIdx ▾1bufIdx ▾=arraypixybuf ▾element=payloadBytes ▾=bufIdx ▾=functionpixy_get_response Serial initialize RX5 ▾TX4 ▾baud19200 ▾modestandard ▾addcomment ▾Make sure that we have the correct packet2arraypixybuf ▾elementifdoelseitem ▾0Fhexadecimal≠ ▾Serial transmittextdecimal number“Error: ”item ▾0000build ▾=minor ▾=major ▾=hardware ▾=6run function “extract_word”item ▾8arraypixybuf ▾element9arraypixybuf ▾element10run function “extract_word”item ▾build ▾=bufIdx ▾=minor ▾=major ▾=hardware ▾=bufIdx ▾=item ▾=functionextract_version addcomment ▾Pixy2 Demo Code“Pixy2 Camera Demo”run function “show_version”run function “show_resolution”run function “rgb_led_demo”run function “lamp_demo”repeatforever ▾dorun function “show_signatures”1000pause (ms)Terminal new lineTerminal print textthen a new line ✓addcomment ▾Make sure that we have the correct packet2arraypixybuf ▾elementifdoelseitem ▾0Dhexadecimal≠ ▾Serial transmittextdecimal number“Error: ”item ▾00height ▾=width ▾=6run function “extract_word”item ▾8run function “extract_word”item ▾height ▾=bufIdx ▾=width ▾=bufIdx ▾=item ▾=functionextract_resolution addcomment ▾Extract values for block data indicated by block…addcomment ▾Reset block values00000addcomment ▾Make sure that we have the correct packet2arraypixybuf ▾elementitem ▾21hexadecimal= ▾÷ ▾3arraypixybuf ▾element14blockIdx ▾1≥ ▾blockIdx ▾blocksDetected ▾≤ ▾and ▾addcomment ▾Locate start of block values+ ▾× ▾− ▾blockIdx ▾1146run function “extract_word”item ▾run function “extract_word”item ▾run function “extract_word”item ▾run function “extract_word”item ▾run function “extract_word”item ▾bHeight ▾=bWidth ▾=bYpos ▾=bXpos ▾=bSignature ▾=bufIdx ▾=ifdoblocksDetected ▾=ifdoitem ▾=bHeight ▾=bWidth ▾=bYpos ▾=bXpos ▾=bSignature ▾=functionextract_block addcomment ▾Test Lamps“Upper lamp on”10run function “pixy_set_lamps”1000“Lower lamp on”01run function “pixy_set_lamps”1000“Lamps off”00run function “pixy_set_lamps”Terminal new linelampLower ▾=lampUpper ▾=Terminal print textthen a new line ✓pause (ms)lampLower ▾=lampUpper ▾=Terminal print textthen a new line ✓pause (ms)lampLower ▾=lampUpper ▾=Terminal print textthen a new line ✓functionlamp_demo addcomment ▾Test RGB LEDs“LED is Red”12800run function “pixy_set_led”1000“LED is Green”01280run function “pixy_set_led”1000“LED is Blue”00128run function “pixy_set_led”1000“LED off”000run function “pixy_set_led”Terminal new lineledBlue ▾=ledGreen ▾=ledRed ▾=Terminal print textthen a new line ✓pause (ms)ledBlue ▾=ledGreen ▾=ledRed ▾=Terminal print textthen a new line ✓pause (ms)ledBlue ▾=ledGreen ▾=ledRed ▾=Terminal print textthen a new line ✓pause (ms)ledBlue ▾=ledGreen ▾=ledRed ▾=Terminal print textthen a new line ✓functionrgb_led_demo addcomment ▾Extract 16-bit value from pixybufaddcomment ▾-- put LSB in bufIdx before callingaddcomment ▾-- bufIdx advanced by 2 after call bufIdx ▾arraypixybuf ▾element+ ▾bufIdx ▾1+ ▾item ▾× ▾bufIdx ▾arraypixybuf ▾element256+ ▾bufIdx ▾1addcomment ▾bufIdx points to LSB of next consecutive value i…bufIdx ▾=item ▾=bufIdx ▾=item ▾=functionextract_word run function “pixy_request_resolution”“X Pixels = ”width ▾“Y Pixels = ”height ▾Terminal new lineTerminal print numberasDecimal ▾then a new line ✓Terminal print textthen a new line ✓Terminal print numberasDecimal ▾then a new line ✓Terminal print textthen a new line ✓functionshow_resolution run function “pixy_request_version”“Hardware = ”hardware ▾“Firmware = ”major ▾“.”minor ▾“.”build ▾Terminal new lineTerminal print numberasDecimal ▾then a new line ✓Terminal print textthen a new line ✓Terminal print numberasDecimal ▾then a new line ✓Terminal print textthen a new line ✓Terminal print numberasDecimal ▾then a new line ✓Terminal print textthen a new line ✓Terminal print numberasDecimal ▾then a new line ✓Terminal print textthen a new line ✓functionshow_version addcomment ▾For Debuggingaddcomment ▾Display raw contents of receive bufferaddcomment ▾Header (6 bytes) + payload size3arraypixybuf ▾element+ ▾6payloadBytes ▾00addcomment ▾Display entire bufferrepeatx times ▾dobufLen ▾bufIdx ▾arraypixybuf ▾element+ ▾bufIdx ▾1addcomment ▾Display byte as 2-digit hex÷ ▾item ▾16% (remainder after division) ▾item ▾16“ ”addcomment ▾Display 8 bytes per line+ ▾lineCount ▾1lineCount ▾8= ▾0Terminal new linelineCount ▾=ifdolineCount ▾=Terminal print textthen a new line ✓Terminal print numberasHexadecimal ▾then a new line ✓Terminal print numberasHexadecimal ▾then a new line ✓bufIdx ▾=item ▾=Terminal new lineTerminal new linebufIdx ▾=lineCount ▾=bufLen ▾=payloadBytes ▾=functionshow_buffer addcomment ▾Monitor signatures 1, 2, and 3111binary3run function “pixy_request_blocks”÷ ▾3arraypixybuf ▾element14“Blocks detected = ”blocksDetected ▾blocksDetected ▾0> ▾1blocksDetected ▾1run function “extract_block”“-- Signature = ”bSignature ▾“-- X Position = ”bXpos ▾“-- Y Position = ”bYpos ▾“-- X Size = ”bWidth ▾“-- Y Size = ”bHeight ▾Terminal new lineTerminal print numberasDecimal ▾then a new line ✓Terminal print textthen a new line ✓Terminal print numberasDecimal ▾then a new line ✓Terminal print textthen a new line ✓Terminal print numberasDecimal ▾then a new line ✓Terminal print textthen a new line ✓Terminal print numberasDecimal ▾then a new line ✓Terminal print textthen a new line ✓Terminal print numberasDecimal ▾then a new line ✓Terminal print textthen a new line ✓repeatblockIdx ▾fromthru ▾bydoifdoTerminal new lineTerminal print numberasDecimal ▾then a new line ✓Terminal print textthen a new line ✓blocksDetected ▾=maxBlocks ▾=sigMap ▾=functionshow_signatures Parallax BlocklyProp ProjectUser: JonnyMacTitle: Pixy2 InterfaceProject ID: 125427Device: otherDescription: Demonstrate serial communications with the Pixy2 camera. Note that not all features are implemented, but there is enough to track a colored object that has been trained into the camera.
The API is explained here:
-- https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:porting_guide
Updated 18 JAN 2020 -- Added RGB LED and Lamp control demonstrations. Moved all other demo code to functions to make main block cleaner.bufIdxmaxBlocksitemsigMapbSignaturelampUpperledRedbufLenbXposlampLowerledGreenwidthhardwarepayloadBytesbYposledBlueblocksDetectedmajorheightbWidthlineCountminorbHeightbuildblockIdxSIG_11SIG_22SIG_34SIG_48SIG_516SIG_632SIG_764SIG_ALL127COLOR_CODES128clear_bufferCOMMENTClear response buffer and reset indexbufIdx= 0631pixybufbufIdx0bufIdx0bufLen0pixy_request_versionBYTEAEBYTEC1BYTE0EBYTE00pixy_request_resolutionBYTEAEBYTEC1BYTE0CBYTE01BYTE00pixy_request_blocksmaxBlocks13maxBlocksBYTEAEBYTEC1BYTE20BYTE02BYTEsigMapBYTEmaxBlockspixy_set_ledBYTEAEBYTEC1BYTE14BYTE03BYTEledRedBYTEledGreenBYTEledBluepixy_set_lampsBYTEAEBYTEC1BYTE16BYTE02BYTElampUpperBYTElampLowerpixybuf64pixy_get_responseCOMMENTGet response headerbufIdx0TIMES6BYTEitempixybufbufIdxitembufIdx + bufIdx1COMMENTGet size of payloadpayloadBytespixybuf3COMMENTGet payloadTIMESpayloadBytesBYTEitempixybufbufIdxitembufIdx + bufIdx15419200standardextract_versionCOMMENTMake sure that we have the correct packetitempixybuf2!=item0FError: itemhardware0major0minor0build0bufIdx6hardwareitemmajorpixybuf8minorpixybuf9bufIdx10builditemCOMMENTPixy2 Demo CodeTRUEPixy2 Camera DemoFOREVER1000extract_resolutionCOMMENTMake sure that we have the correct packetitempixybuf2!=item0DError: itemwidth0height0bufIdx6widthitembufIdx8heightitemextract_blockCOMMENTExtract values for block data indicated by blockIdxCOMMENTReset block valuesbSignature0bXpos0bYpos0bWidth0bHeight0COMMENTMake sure that we have the correct packetitempixybuf2==item21blocksDetected / pixybuf314 && >=blockIdx1<=blockIdxblocksDetectedCOMMENTLocate start of block valuesbufIdx + * - blockIdx1146bSignatureitembXpositembYpositembWidthitembHeightitemlamp_demoCOMMENTTest LampsTRUEUpper lamp onlampUpper1lampLower01000TRUELower lamp onlampUpper0lampLower11000TRUELamps offlampUpper0lampLower0rgb_led_demoCOMMENTTest RGB LEDsTRUELED is RedledRed128ledGreen0ledBlue01000TRUELED is GreenledRed0ledGreen128ledBlue01000TRUELED is BlueledRed0ledGreen0ledBlue1281000TRUELED offledRed0ledGreen0ledBlue0extract_wordCOMMENTExtract 16-bit value from pixybufCOMMENT-- put LSB in bufIdx before callingCOMMENT-- bufIdx advanced by 2 after call itempixybufbufIdxbufIdx + bufIdx1item + item * pixybufbufIdx256bufIdx + bufIdx1COMMENTbufIdx points to LSB of next consecutive value in buffershow_resolutionFALSEX Pixels = DECTRUEwidthFALSEY Pixels = DECTRUEheightshow_versionFALSEHardware = DECTRUEhardwareFALSEFirmware = DECFALSEmajorFALSE.DECFALSEminorFALSE.DECTRUEbuildshow_bufferCOMMENTFor DebuggingCOMMENTDisplay raw contents of receive bufferCOMMENTHeader (6 bytes) + payload sizepayloadBytespixybuf3bufLen + 6payloadByteslineCount0bufIdx0COMMENTDisplay entire bufferTIMESbufLenitempixybufbufIdxbufIdx + bufIdx1COMMENTDisplay byte as 2-digit hexHEXFALSE / item16HEXFALSE % item16FALSE COMMENTDisplay 8 bytes per linelineCount + lineCount1==lineCount8lineCount0show_signaturesCOMMENTMonitor signatures 1, 2, and 3sigMap111maxBlocks3blocksDetected / pixybuf314FALSEBlocks detected = DECTRUEblocksDetected>blocksDetected0blockIdx= 1blocksDetected1FALSE-- Signature = DECTRUEbSignatureFALSE-- X Position = DECTRUEbXposFALSE-- Y Position = DECTRUEbYposFALSE-- X Size = DECTRUEbWidthFALSE-- Y Size = DECTRUEbHeight000969951216