Library for Pixy - CMUCAM5 smart camera

Dependents:   TestPixy FRC_2018 FRC2018_Bis 0hackton_08_06_18 ... more

Fork of Pixy by FRC - Hackathon

Pixy.h

Committer:
haarkon
Date:
2018-05-21
Revision:
1:183ebbbc9c2e
Parent:
0:6f78c735f07c
Child:
2:90355c600404

File content as of revision 1:183ebbbc9c2e:

/**
 * @author Hugues Angelis
 *
 * @section LICENSE
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @section DESCRIPTION
 *
 * CMUCAM 5 - Pixy
 *
 * Datasheet, FAQ and PC drivers :
 *
 * http://www.cmucam.org/projects/cmucam5
 */

#ifndef PIXY_H
#define PIXY_H

/**
 * Includes
 */
#include "mbed.h"

/**
 * Defines
 */
#define CC_BLOCSIZE         14
#define N_BLOCSIZE          12

#define N_BLOCCODE          0xAA55
#define CC_BLOCCODE         0xAA56

    /**
     * Types
     */
    typedef unsigned char   Byte;
    typedef unsigned short  Word;
    typedef unsigned long   lWord;
    
    typedef struct {
        Word    checksum;
        Word    signature;
        Word    x;
        Word    y;
        Word    width;
        Word    height;
        Word    angle;
    } T_pixyCCBloc;
    
    typedef struct {
        Word    checksum;
        Word    signature;
        Word    x;
        Word    y;
        Word    width;
        Word    height;
    } T_pixyNMBloc;
    
    typedef union {
        Byte            tab[14];
        T_pixyCCBloc    CCbloc;
    } T_pixyCCData;
    
    typedef union {
        Byte            tab[12];
        T_pixyNMBloc    NMbloc;
    } T_pixyNMData;

    typedef enum {none, begin, normal, colorCode, doubleZero} T_pixyState;
    
    typedef union {
        lWord   mot;
        Byte    tab[4];
    } T_tmpBuffer;
    
    typedef union {
        Word    mot;
        Byte    tab[2];
    } T_structBuffer;
    


/**
 * Pixy
 */
class PIXY {

protected :

    Serial*  _cmucam;

public :

    /**
     * Variables globales
     *
     * @note
     *  A FIFO is a circular buffer where data are stored in the order in witch
     *  they arrive. You need 2 variables to point both read and write registers.
     *  The library manage both pointers, but access to the FIFO is allowed from
     *  outside of the library, so you can manage your own read pointers.
     *  Write pointers are strictly private.   
     *
     * @section DESCRIPTION
     *  Pixy_CCFIFO is a FIFO where the Color Code objects are stored
     *  Pixy_NMFIFO is a FIFO where the Normal objects are stored
     *  Pixy_CCObjet is the number of CC object read from the CAM during the last frame
     *  Pixy_NMObjet is the number of normal object read from the CAM during the last frame
     */
    T_pixyCCData        Pixy_CCFIFO[20];    // FIFO des objets ColorCode
    T_pixyNMData        Pixy_NMFIFO[20];    // FIFO des objets Normaux
    Byte                Pixy_CCObjet;       // Nombre d'objets de type Color Code
    Byte                Pixy_NMObjet;       // Nombre d'objets Normaux
    Byte                Pixy_FirstCCObjet;  // Position dans la FIFO du premier objet de la trame suivante
    Byte                Pixy_FirstNMObjet;  // Position dans la FIFO du premier objet de la trame suivante
    
    
    /** Liste des Sémaphores
     *
     * @note
     *  Semaphore are global variable used to communicate from ISR with main routine
     *  All semaphores must be cleared by user.
     *
     * @section DESCRIPTION
     *  FlagPixy is automaticly set after each received image frame
     *  FlagPixyOverflow is automaticly set if any FIFO oveflows (more than 20 obj). 
     */
    int                 FlagPixy, FlagPixyOverflow;
    int                 Pixy_check;

    /**
     * Constructor.
     *
     * @param tx is the Mbed pin used as TX
     * @param rx is the Mbed pin used as RX
     * @param debit is the bitrate of the serial (max value is 230400 b/s)
     */
    PIXY(PinName tx, PinName rx, int debit =230400);

    /**
     * Return the number of objects (normal and ColorCode) that have been
     * received from the PIXY during the last frame
     *
     * @param nbNormal (passed by reference) number of normal object detected
     * @param nbCC (passed by reference) number of color code object detected
     * @return : 0 if sucessfull,
     *          -1 if no PIXY is talking,
     *          -2 if object(s) have been lost (from previous frame or from actual) 
     */
    int detectedObject (int* nbNormal, int* nbCC);

    /**
     * Reads the oldest Color Code object from the last frame received 
     *
     * @note : also manage the Color Code FIFO
     *
     * @return a T_PixyCCBloc (see .h for details)
     */
    T_pixyCCBloc getCCBloc (void); 
        
    /**
     * Reads the oldest Normal object from the last frame received 
     *
     * @note : also manage the Normal FIFO
     *
     * @return a T_PixyNMBloc (see .h for details)
     */
    T_pixyNMBloc getNMBloc (void); 
        
    /**
     * Set the Brightness of the Pixy
     *
     * @param   brightness level (between 0 and 255)
     * @return  0 if successfull, else -1
     */
    int setBrightness (Byte brightness); 

private :

    int Pixy_CCFrameIsNew, Pixy_NMFrameIsNew;

    /**
     * Fonction d'interruption de lecture sur la liaison série.
     */
    void getPixyByte();


};

#endif /* PIXY_H */