Projet_S5 / Mbed 2 deprecated Repo_Noeud_Mobile_refactor

Dependencies:   mbed-rtos mbed

Fork of Repo_Noeud_Mobile by Projet_S5

main.cpp

Committer:
groygirard
Date:
2015-03-26
Revision:
25:184915935d68
Parent:
15:dc18545822b3
Child:
27:0c0dfdf8d953

File content as of revision 25:184915935d68:

/* S5 Projet - Conception d'un systeme embarque reseaute
 * main.cpp
 *
 * @author Equipe de projet 2
 *
 */

// System libraries
#include "mbed.h"
#include "rtos.h"

// Proprietary libraries
#include "Cible.h"
#include "CountDown.h"
#include "FlexSensor.h"
#include "MMA8452Q.h"

#define GO 0x01

Serial m_pc(USBTX, USBRX);

enum GameMode { GUNNER, RPS, AirGuitar};

//PROTOTYPES DE FONCTION
void gunner(void const* args);
void rps(void const* args);
void airGuitar(void const* args);
void get_sensor_data(void const* args);
void analyze_sensor_data(void const* args);
void analyze_GUNNER();
void analyze_RPS();
void analyze_GUITAR();
void etat_de_jeu(void const *args);
void reception_coord(void const *args);
void timer2_init(void);

uint8_t play = 0;

FlexSensor index(DIGITAL, p15);       // flex sensor 1.
FlexSensor majeur(DIGITAL, p16);      // flex sensor 2.
FlexSensor annulaire(DIGITAL, p17);   // flex sensor 3.
CountDown countDown;
Accel accel;
flex_t flex_data;
FlexSensor flex;
RtosTimer *sync;
GameMode mode = RPS;


Thread *threads[3];
Thread* gunner_thread_ptr = NULL;       // Possiblement mettre dans un tableau
Thread* rps_thread_ptr = NULL;          // avec des position codees
Thread* airguitar_thread_ptr = NULL;    // dans des define. guillaume


Mail<flex_t, 32> mailbox_flex;

extern "C" void TIMER2_IRQHandler(void)
{
    /*Flag du decodage - s'execute a chaque fronts, descendants et montants*/
    if ((LPC_TIM2->IR & 0x20) == 0x20) {
        LPC_TIM2->IR |= 0x20;     // clear Timer2 interrupt register
        threads[2]->signal_set(0x02);
    }
}

int main(void const* args)
{
    // Initializing the accelerometer
    accel = Accel();
    accel.init_MMA8452();
    timer2_init();

    //RtosTimer timer(get_sensor_data, osTimerPeriodic, (void *)0);
    //sync = &timer;
    //sync->start(200);

    switch(mode) {
        case GUNNER:
            gunner_thread_ptr =  new Thread(gunner);
            break;
        case RPS:
            rps_thread_ptr =  new Thread(rps);
            break;
        case AirGuitar:
            airguitar_thread_ptr = new Thread(airGuitar);
            break;
        default:
            break;
    }

    //Thread thread0(reception_coord);
    //Thread thread1(etat_de_jeu);
    Thread thread2(analyze_sensor_data);
    // threads[0] = &thread0;
    // threads[1] = &thread1;
    threads[2] = rps_thread_ptr;

    while(true) {
    }
}

void timer2_init(void)
{
    LPC_PINCON->PINSEL0  |= 0xc00;      // set P0.5 to CAP2.1
    LPC_SC->PCONP    |= (1 << 22);      // Timer2 power on
    LPC_SC->PCLKSEL1 |= (1 << 12);      // Divide CCLK by 1 for Timer2
    LPC_TIM2->CCR    |= 0x30;           // set cap2.1 rising-edge/falling-edge and interrupt
    LPC_TIM2->TCR    |= (1 << 0);       // start Timer2
    LPC_TIM2->EMR     = 0x20;           //
    LPC_TIM2->IR     |= 0xFFFFFFFF;
    NVIC_EnableIRQ(TIMER2_IRQn);
}

void gunner(void const* args)
{
    // local variables
    Cible* cible = new Cible();
    countDown.run();

    while(true) {
        Thread::signal_wait(GO);
        cible->reset();
        int target = rand() % 3;

        cible->set(target);
        countDown.run();
    }
}

void rps(void const* args)
{
    // local variables
    analyze_RPS();
    uint8_t windup = 0;
    while(true) {
        Thread::signal_wait(0x02);
        windup++;
        m_pc.printf("Decide in : %d \n\r", windup);
        if(windup >= 3){
            windup = 0;
            Thread::wait(500);
            flex_data = flex.get_flex_values();
            flex_t *mail = mailbox_flex.alloc();
            mail = &flex_data;
            mailbox_flex.put(mail);
            // send data frame to the fixed mbed for analyze
        }
        accel.clear_TRANSIENT_INTERRUPT();

    }
}

void airGuitar(void const* args)
{
    // local variables

    while(true) {
        Thread::signal_wait(GO);
        // code...
    }
}


void get_sensor_data(void const* args)
{
    flex_data = flex.get_flex_values();
}

void analyze_GUNNER()
{
}

void analyze_RPS()
{
    accel.set_TRANSIENT_MODE(0x12, 0x05, 0x08);
}

void analyze_GUITAR()
{
}

/*void etat_de_jeu(void const *args)
{

    while(true) {
        Thread::signal_wait(0x1);
        m_pc.printf("Etat \r\n");
        switch(play) {
            case 0:
                accel.set_STANDBY_MODE();
                break;
            case 1:
                accel.set_TRANSIENT_MODE(0x12, 0x08, 0x05);
                break;
            default:
                break;
        }
    }
}*/

/*void reception_coord(void const *args)
{
    while(true) {
        if(play == 0) {
            play = 1;
            threads[1]->signal_set(0x1);
            Thread::wait(2000);
        } else {
            play = 0;
            Thread::wait(2000);
        }
    }
}*/

void analyze_sensor_data(void const* args)
{
    while (true) {
        // écriture de l'événement en sortie (port série)
        switch(mode) {
            case GUNNER:
                analyze_GUNNER();
                break;
            case RPS:
                //analyze_RPS();
                break;
            case AirGuitar:
                analyze_GUITAR();
                break;
            default:
                break;
        }
    }
}