Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Repo_Noeud_Mobile by
main.cpp
- Committer:
- llarose
- Date:
- 2015-04-11
- Revision:
- 48:bc531288c220
- Parent:
- 47:f372ca93d6c1
- Child:
- 51:299408ceee3a
File content as of revision 48:bc531288c220:
/* 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"
#include "Structure.h"
#include "Xbee.h"
#define GO 0x01
#define ACTIVATE_ACCEL 0
#define ACTIVATE_FLEX 1
Serial m_pc(USBTX, USBRX);
LocalFileSystem local("local"); // file system for config.txt
//PROTOTYPES DE FONCTION
void gunner(void const* args);
void rps(void const* args);
void airGuitar(void const* args);
void flex(void const* args);
void test(void const* args);
void analyze_sensor_data(void const* args);
void configure_GUNNER();
void configure_RPS();
void configure_GUITAR();
void ReadConfig();
void timer2_init(void);
void Send_Data_To_Fixe(void const* args);
uint8_t play = 0;
FlexSensor flexSensors(p18, p19, p20, p17); // flex sensor 1.
Accel accel;
flex_t flex_data;
RtosTimer *sync;
#if ACTIVATE_ACCEL
GameMode_e mode = RPS;
#else
GameMode_e mode = RPS;
#endif
short PanId;
char GantID;
Xbee xbee;
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
Thread* flex_thread_ptr = NULL; // Lecture des entrées analogiques
Thread* Thread_Send_Data_To_Fixe = NULL;// Thread pour envoyer les données des sensors au fixe
Thread* test_thread_ptr = NULL;
Mail<Mobile_Vers_Fixe, 32> mailbox_Mobile_Vers_Fixe;
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[mode]->signal_set(0x02);
}
}
int main(void const* args)
{
m_pc.printf("\r\n==== PROGRAM START MOBILE ====\r\n");
// Initializing the accelerometer
ReadConfig(); //read config file
m_pc.printf("PANID %x\r\n", PanId);
xbee = Xbee(PanId, p13, p14); //set PAN ID
#if ACTIVATE_ACCEL
accel = Accel();
accel.init_MMA8452();
timer2_init();
#endif
switch(mode) {
case GUNNER:
gunner_thread_ptr = new Thread(gunner);
flex_thread_ptr = new Thread(flex);
m_pc.printf("gunner mode started\r\n");
break;
case RPS:
#if ACTIVATE_FLEX
flex_thread_ptr = new Thread(flex);
#endif
//rps_thread_ptr = new Thread(rps);
m_pc.printf("rps mode started\r\n");
break;
case AirGuitar:
airguitar_thread_ptr = new Thread(airGuitar);
break;
case TEST:
test_thread_ptr = new Thread(test);
default:
break;
}
threads[GUNNER] = gunner_thread_ptr;
threads[RPS] = rps_thread_ptr;
threads[AirGuitar] = airguitar_thread_ptr;
Thread_Send_Data_To_Fixe = new Thread(Send_Data_To_Fixe);
while(true) {
}
}
void timer2_init(void)
{
LPC_PINCON->PINSEL0 |= 0xc00; // set P0.5 to CAP2.1
LPC_PINCON->PINMODE0 |= 0xc00; // pull-down enable
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 flex(void const* args)
{
flexSensors.Run();
}
void GetGameMode(void const* args)
{
while (true) {
// attente et lecture d'un événement digital
osEvent evtD = xbee.mailbox_TypeDeJeu.get();
if (evtD.status == osEventMail) {
Fixe_Vers_Mobile *mail = (Fixe_Vers_Mobile*)evtD.value.p;
mode = mail->game;
// Envoyer la structure
xbee.mailbox_TypeDeJeu.free(mail);
}
}
}
void gunner(void const* args)
{
// local variables
while(true) {
Thread::signal_wait(0x02);
flex_data = flexSensors.get_flex_values();
while(flex_data.index != 0) {
wait(10);
flex_data = flexSensors.get_flex_values();
}
Mobile_Vers_Fixe *mail = mailbox_Mobile_Vers_Fixe.alloc();
mail->flexSensor = flex_data;
mail->gants = GantID;
mail->accelData.x = 0x00;
mail->accelData.y = 0x00;
mail->accelData.z = 0x01;
mailbox_Mobile_Vers_Fixe.put(mail);
}
}
void configure_GUNNER()
{
accel.set_TRANSIENT_MODE(0x18, 0x05, 0x08); // z plan transient motion detection
}
void rps(void const* args)
{
// local variables
m_pc.printf("RPS Start! \n\r");
configure_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 = flexSensors.get_flex_values();
Mobile_Vers_Fixe *mail = mailbox_Mobile_Vers_Fixe.alloc();
mail->flexSensor = flex_data;
mail->gants = GantID; // Gauche ou Droit
mail->accelData.x = 0x01;
mail->accelData.y = 0x00;
mail->accelData.z = 0x00;
mailbox_Mobile_Vers_Fixe.put(mail);
// send data frame to the fixed mbed for analyze
}
accel.clear_TRANSIENT_INTERRUPT();
}
}
void test(void const* args)
{
m_pc.printf("TEST Thread\r\n");
flex_t flex_data = {0,1,0};
Mobile_Vers_Fixe *mail = mailbox_Mobile_Vers_Fixe.alloc();
mail->flexSensor = flex_data;
mail->gants = GantID;
mail->accelData.x = 0x01;
mail->accelData.y = 0x00;
mail->accelData.z = 0x00;
while(1)
{
m_pc.printf("send\r\n");
mailbox_Mobile_Vers_Fixe.put(mail);
wait(1);
}
}
void Send_Data_To_Fixe(void const* args)
{
while (true) {
// attente et lecture d'un événement digital
osEvent evtD = mailbox_Mobile_Vers_Fixe.get();
if (evtD.status == osEventMail) {
Mobile_Vers_Fixe *mail = (Mobile_Vers_Fixe*)evtD.value.p;
// écriture de l'événement en sortie (port série)
m_pc.printf("mail trouve");
// Envoyer la structure
xbee.EnvoyerStructure(mail);
mailbox_Mobile_Vers_Fixe.free(mail);
}
}
}
void configure_RPS()
{
accel.set_TRANSIENT_MODE(0x12, 0x05, 0x08);
}
void airGuitar(void const* args)
{
// local variables
while(true) {
Thread::signal_wait(GO);
// code...
}
}
void configure_GUITAR()
{
}
//read config file
void ReadConfig()
{
FILE* file = fopen("/local/config.txt","r");
if (file != NULL) {
char buffer[2];
fscanf(file, "%x", &buffer); //panID = 2 char
PanId = buffer[1] << 8 | buffer[0]; //set PAN ID global variable
fscanf(file, "%x", &buffer);
GantID = buffer[0];
fclose(file); //close file
} else { //if file is not found
m_pc.printf("ERROR AT CONFIG FILE \r\n");
}
}
