Test

Dependencies:   mbed AccelSensor

main.cpp

Committer:
Alegrowin
Date:
2013-01-15
Revision:
4:3397631c2f65
Parent:
3:4e4e4d7f6ee1
Child:
5:e5fbf322c180

File content as of revision 4:3397631c2f65:

#include "mbed.h"
#include "AccelSensor.h"
#include <math.h>

const int addr = 0x9A;              // define the I2C Address for TC74-A0
const int Afficheur_addr = 0xE2;    // define the I2C Address for 4*7seg display
const int Accel_addr = 0x3A;        // define the I2C Address for the Accelerometer

//Defines for the serial output for debug
#define Output_Temperature true;

//Creating the desired objetcs
I2C i2c(p9, p10);        // sda, scl
Serial pc(USBTX, USBRX); // tx, rx
//Serial Damn(p28,p27);  // Used as shortcut for printing result on 7seg using printf function as it is not available in I2C
//AccelSensor acc(p9,p10);  //Should be commented as it was a part of the prototype and test, we may want to use our own function
//Mainly used to calibrate and start the device

void init(void);

int main()
{
    int temp = 0;
    int a,b,c;
    char cmd[7];
    int result[3];

    init();
    wait(1);           //Make sure system is fully initialized

    while(1) {
        pc.printf("\r\n\nStart reading the temperature of TC74 on I2C\r\n");


        //--------------Afficheur LCD ------------------------------
        /*
        cmd[0] = 0x7b;      //Segment 1
        cmd[1] = 0x7f;

        i2c.write(Afficheur_addr,cmd,2);

        wait(0.07);

        cmd[0] = 0x7c;      //Segment 2
        cmd[1] = 0x7f;

        i2c.write(Afficheur_addr,cmd,2);

        wait(0.07);

        cmd[0] = 0x7d;      //Segment 3
        cmd[1] = 0x7f;

        i2c.write(Afficheur_addr,cmd,2);

        wait(0.07);

        cmd[0] = 0x7e;      //Segment 4
        cmd[1] = 0x7f;

        i2c.write(Afficheur_addr,cmd,2);*/

        //------------------Accelerometre ---------------------------
        //char temporary[2];

        /* cmd[0] = 0x2A;                     //Command :: CTRL reg
         i2c.write(Accel_addr, cmd, 1);    //Issue required command to perform a write of the command
         i2c.read(Accel_addr, temporary,1);

         cmd[1] = temporary[0] |1;
         i2c.write(Accel_addr, cmd, 2);    //Issue required command to perform a write of the command

         pc.printf("ctrl Reg1 status :: %x\r\n\n", cmd[0]);

         */

        cmd[0] = 0x00;                     //Command :: Status
        i2c.write(Accel_addr, cmd, 1);    //Issue required command to perform a write of the command
        i2c.read(Accel_addr, cmd, 7);     //Read the Data from the device

        pc.printf("Values :: %d %d %d %d %d %d %d \r\n\n", cmd[0], cmd[1], cmd[2],cmd[3],cmd[4],cmd[5],cmd[6] );
        //Damn.printf("v");
        long double anglez, anglex,angley;
        bool negatif;
        //calcul angle Z
        if (cmd[5]<0x7F) {
            anglez = acos((cmd[5]/62.0))/3.1416*180;
            negatif=false;

        } else {
            cmd[5]=(cmd[5]-256)*-1;
            anglez = acos((cmd[5]/62.0))/3.1416*180;

        }

        //calcul angle Y
        if (cmd[3]<0x7F) {
            angley = asin((cmd[3]/62.0))/3.1416*180;

        } else {
            cmd[3]=(cmd[3]-256)*-1;
            angley = asin((cmd[3]/62.0))/3.1416*180;

        }
        // calcul angle X
        if (cmd[1]<0x7F) {
            anglex = asin((cmd[1]/62.0))/3.1416*180;

        } else {
            cmd[1]=(cmd[1]-256)*-1;
            anglex = asin((cmd[1]/62.0))/3.1416*180;

        }


        //if (anglez<45
        int b = anglez;

        pc.printf("TEST: X=%f      Y=%f      Z=%f  \r\n", anglex, angley, anglez );
        if (negatif==false)
            Damn.printf("  ");
        else
            Damn.printf(" -");

        Damn.printf("%2d", b );


        //-------------------Accel status form Imported Class --------
        acc.readData(result);
        pc.printf("X: %d   Y: %d    Z:    %d \r\n", result[0],result[1],result[2]);

        wait(2);
    }

}

void init(void)
{
    //Let's clear the I2C display

    i2c.write(Afficheur_addr,"v",1);

    acc.init();
    acc.active();
}

char get_Temperature(void)
{
    char cmd[1];
    //Méthode 1
    //Utilisation des fonctions bas niveau
    /*
        i2c.start();            // Start condition
        a = i2c.write(addr);    // Write Device Address
        b = i2c.write(0x00);    // Write READ command of TC74 (voir page 8 de la datasheet du TC74)

        i2c.start();            //Reissue start condition
        //Au lieu de faire Stop condition et Start de nouveau

        c= i2c.write(addr|1);   //Adresse du Device en mode Lecture
        temp = i2c.read(0);     //Lecture de la valeur du registre de température
        i2c.stop();             //Fermeture de la trame
    */
    //Méthode 2
    //Utilisation des fonctions haut niveau

    cmd[0] = 0x0;               //Command :: READ
    i2c.write(addr, cmd, 1);    //Issue required command to perform a write of the command
    i2c.read(addr, cmd, 1);     //Read the Data from the device

    //-----------------Print out section ----------------------
    //Display device Address and informations

    // pc.printf("Device with address 0x%x with\r\n", addr);

    //Prints out the result of Method 1
    //pc.printf("ACK1 :: %d\n\rACK2 :: %d\n\rACK3 :: %d\n\r", a,b,c); //ACK bits
    //pc.printf("Method 1 :: %d\n\r", temp);

    //Prints out the Data from Method 2
#if Output_Temperature
    pc.printf("Method 2 :: %d\r\n\n", cmd[0]);
#endif

    return cmd[0];
}