Theo Le Paih / Mbed 2 deprecated Lib_Pixy2

Dependencies:   mbed

main.cpp

Committer:
theolp
Date:
2019-11-21
Revision:
25:2a2fcb9e4775
Parent:
23:d6bbc4ec1f22
Child:
26:a56cc5d715e9

File content as of revision 25:2a2fcb9e4775:

#include "pixy2.h"

/*      _DEBUG_     */
int sommeDeControle,sommeRecue;

Serial pc(USBTX, USBRX, 9600);
/*                  */

typedef enum {IDLE, SND, CHECKSUM_ERROR, TIMEOUT, OK} T_state;

// FLAGS
char FLAG_BP = 0;

// Prototypes
void appui_BP(void);


int main(void)
{
    PIXY2               maPixy(PC_12, PD_2);     // PC_12 : UART5_TX --- PD_2 : UART5_RX
    
    T_pixy2Resolution*     testResolution;
    T_pixy2ErrorCode    rep;
    
    // Initialisations
    InterruptIn BP(USER_BUTTON);
    BP.rise(&appui_BP);
    
    Timer temps;
    temps.start();
    
    T_state etat = IDLE;
    
    while(1) {

        switch (etat) {
            case IDLE :
                if (FLAG_BP) {
                    pc.printf("Envoi...\n\r");
                    etat = SND;
                    FLAG_BP = 0;
                    temps.reset();
                }
                break;

            case SND :
                rep = maPixy.pixy2_getResolution(&testResolution);
                //pc.printf("%d\n\r",rep);                      // Affichage des erreurs renvoyées
                if (rep == PIXY2_BAD_CHECKSUM) {
                    etat = CHECKSUM_ERROR;   
                }
                if (temps.read()>0.5f) {
                    etat = TIMEOUT;
                }
                if (rep == PIXY2_OK) {
                    etat = OK;
                }
                break;
            
            case CHECKSUM_ERROR :
                pc.printf("Erreur de CheckSum...\n\r");
                pc.printf("CheckSum Recu    : %d\n\r", sommeRecue);
                pc.printf("CheckSum Calcule : %d\n\r", sommeDeControle); 
                etat = IDLE;
                break;
            
            case TIMEOUT :
                pc.printf("Erreur Timeout\n\r");
                etat = IDLE;
                break;

            case OK :
                pc.printf("Pixy frame width  : %d\n\r", testResolution->pixFrameWidth);
                pc.printf("Pixy frame height : %d\n\r", testResolution->pixFrameHeight);
                etat = IDLE;
                break;

        }
    }
}

// FONCTIONS
void appui_BP(void)
{
    FLAG_BP = 1;
}