Pixy (cmucam5) Library HelloWorld Program

Dependencies:   Pixy mbed

Fork of TestPixy by FRC - Hackathon

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Pixy.h"
00003 
00004 Serial      pc          (PA_2, PA_3, 921600);                   // Serial link with PC
00005 PIXY        Pixy        (PA_0, PA_1, 230400);                   // The PIXY
00006 
00007 DigitalOut  led1        (PA_5);                                 // Led1 is for test purpose
00008 DigitalOut  led2        (PD_2);                                 // Led2 is for test purpose
00009 DigitalOut  disquette   (PA_12);                                // Baloon destructor command signal (must be set to 0 if not used)
00010 
00011 int main()
00012 {
00013     int                 nbCC, nbNM, cr;                         // Local treatment variables
00014     Byte                msg, brightness = 50;                   
00015     T_pixyCCBloc        CCBuf;                                  // Color code object buffer
00016     T_pixyNMBloc        NMBuf;                                  // Normal object buffer
00017 
00018     pc.printf ("\r\nHelloWorld\r\n");                           // display "HelloWorld"
00019     led1 = 1;                                                   // Switch ON Led1
00020     led2 = 0;                                                   // Switch OFF Led2
00021     disquette = 0;                                              // Baloon destructor set to OFF
00022 
00023     wait (5);                                                   // Wait for PIXY to boot up
00024     if (Pixy.checkPixy() == -1) {                               // If no pixy is transmitting (neither pixy not teached or not connected or else)
00025         pc.printf ("\rNo Pixy\n\r");                            // display error message            
00026         while(1);                                               // Hang
00027     } else {                                                    // If pixy is talking
00028         pc.printf ("\rRock'n'Roll\n\r");                        // Let fun begin
00029     }
00030     
00031     do {
00032         Pixy.setBrightness (brightness);                        // Set brightness  (note that 0 brightness is ajustable in PIXYMON)
00033         wait (0.2);                                             // Wait for brightness to establish
00034         cr = Pixy.detectedObject (&nbNM, &nbCC);                // Read the number of objects detected
00035         pc.printf ("\r\nBrightness = %3d => nbCC = %2d - nbNM = %2d : cr = %2d ? ", brightness, nbCC, nbNM, cr);
00036         msg = pc.getc();                                        // Wait for user to choose what to do
00037         pc.printf("%c\r\n", msg);                               // Echo
00038         if (msg=='a') brightness += 5;                          // if message is 'a' then increase brightness by 5
00039         if (msg=='q') brightness -= 5;                          // if message is 'q' then decrease brightness by 5
00040         if (msg=='z') brightness += 1;                          // if message is 'z' then increase brightness by 1
00041         if (msg=='s') brightness -= 1;                          // if message is 's' then decrease brightness by 1
00042     } while (msg != 'X');                                       // Une 'X' to quit
00043         
00044     while (1) {
00045         if (Pixy.checkNewImage()) {                                             //Wait for new image
00046             cr = Pixy.detectedObject (&nbNM, &nbCC);                            // Get detected object list and display it
00047             pc.printf ("\r\tnbCC = %2d - nbNM = %2d : cr = %2d\n", nbCC, nbNM, cr);
00048             while (nbCC > 0) {                                                  // while there are some CC Blocks to read
00049                 CCBuf = Pixy.getCCBloc ();                                      // Read them from FIFO and display them
00050                 nbCC--;
00051                 pc.printf ("\rCC %5d : x=%5d, y=%5d - w=%5d, h=%5d, a=%5d\n", CCBuf.signature, CCBuf.x, CCBuf.y, CCBuf.width, CCBuf.height, (short)CCBuf.angle);
00052             }
00053             while (nbNM > 0) {                                                  // while there are some NM Blocks to read
00054                 NMBuf = Pixy.getNMBloc ();                                      // Read them from FIFO and display them
00055                 nbNM--;
00056                 pc.printf ("\rNM %4x : x=%5d, y=%5d - w=%5d, h=%5d\n", NMBuf.signature, NMBuf.x, NMBuf.y, NMBuf.width, NMBuf.height);
00057             }
00058             led1 = !led1;                                       // Switch Led1
00059             led2 = !led2;                                       // Switch Led2
00060         }
00061     }
00062 }