Projet S5 Info / Mbed 2 deprecated Projet_S5

Dependencies:   mbed PowerControl

Fork of Projet_S5 by Jonathan Tousignant

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /*!
00002  *  \file main.cpp
00003  *  \brief Main file and initialize interrupt function
00004  *  \author Equipe P02
00005  *  \version 1
00006  *  \date 02/04/2014
00007  */
00008  
00009 #include "accelerometer.h"
00010 #include "analyzer.h"
00011 #include "interrupt.h"
00012 #include <iostream>
00013 #include <fstream>
00014 #include "PowerControl/PowerControl.h"
00015 #include "PowerControl/EthernetPowerControl.h"
00016 
00017 DigitalOut led1(LED1);  // Debug led
00018 DigitalOut led2(LED2);  // Debug led
00019 DigitalOut led4(LED4);  // Debug led
00020 DigitalIn button(p30);  // Push button
00021 
00022 LocalFileSystem local("local"); // Open config file
00023 
00024 void *accelerometer = Accelerometer_C_new ();    // Accelerometer object in C
00025 void *analyzer = Analyzer_C_new();              // Analyzer object in C
00026 
00027 bool rebound = true;    // For rebound
00028 bool appuyer = false;   // For push bouton
00029 
00030 extern "C" void interruptCapture(void)
00031 {
00032     if(rebound && !appuyer)
00033     {
00034         LPC_TIM2->TC = 0;       // clear timer counter
00035         LPC_TIM2->PC = 0;       // clear prescale counter
00036         LPC_TIM2->PR = 0;       // clear prescale register
00037         LPC_TIM2->MCR |= 0x03;  // interrupt and reset control
00038         LPC_TIM2->CCR = 0;  //no capture interupt
00039         rebound = false;
00040     }
00041     else if(!rebound && !appuyer)
00042     {
00043         led1 = 1;
00044         LPC_TIM2->MCR = 0;
00045         LPC_TIM2->CCR = (0x06 << 0);  //falling edge
00046         
00047         LPC_TIM1->TC = 0;       // clear timer counter
00048         LPC_TIM1->PC = 0;       // clear prescale counter
00049         LPC_TIM1->PR = 0;       // clear prescale register
00050         LPC_TIM1->IR |= 0xFF;
00051         LPC_TIM1->TCR = 0x01;   //enable timer 3
00052     
00053         appuyer = true;
00054         NVIC_EnableIRQ(TIMER1_IRQn); // Enable TIMER1 interrupt
00055         
00056         signed char* values = Accelerometer_C_getAccelValue (accelerometer);
00057         
00058         // Analyze data
00059         Analyzer_C_setInitial (values, analyzer);
00060     }
00061     else
00062     {
00063         led1 = 0;
00064         LPC_TIM2->CCR = (5 << 0);  //rising edge
00065         
00066         LPC_TIM1->TC = 0;       // clear timer counter
00067         LPC_TIM1->PC = 0;       // clear prescale counter
00068         LPC_TIM1->PR = 0;       // clear prescale register
00069         LPC_TIM1->IR |= 0xFF;
00070         LPC_TIM1->TCR = 0;      //disable timer 3
00071         
00072         appuyer = false;
00073         rebound = true;
00074         NVIC_DisableIRQ(TIMER1_IRQn); // Disable TIMER1 interrupt
00075         
00076         Analyzer_C_checkMouvement (analyzer);    // Find mouvement
00077     }
00078     
00079     LPC_TIM2->IR |= 0xFF;   // Reset interrupt
00080 }
00081 
00082 extern "C" void interruptMatch(void)
00083 {
00084     if(button)
00085     {
00086         led4 = !led4;   // Debug led
00087         
00088         signed char* values = Accelerometer_C_getAccelValue (accelerometer); // Get value of accelerometer
00089         
00090         // Analyze data
00091         Analyzer_C_setMinMax (values, analyzer);
00092         
00093         delete values;
00094     }
00095     
00096     LPC_TIM1->IR |= 0xFF; // Reset interrupt
00097 }
00098 
00099 void initialize()
00100 {
00101     //Initialize the analyzer
00102     Analyzer_C_Initialize (analyzer);
00103 
00104     //  CAPTURE 
00105     //  Enable the pins on the device to use TIMER CAP2.0 on pin 30
00106     LPC_PINCON->PINSEL0 |= 3 << 8;     //set pin 30 capture 2.0
00107     LPC_SC->PCONP |= (1 << 22);        // Timer2 power on
00108     
00109       //LPC_TIM2->MCR |= 3;             //interrupt and reset control
00110     LPC_TIM2->TC = 0;                   // clear timer counter
00111     LPC_TIM2->PC = 0;                   // clear prescale counter
00112     LPC_TIM2->PR = 0;                   // clear prescale register
00113     LPC_TIM2->MR0 = SystemCoreClock / 100; //match pour rebound
00114     
00115     LPC_TIM2->IR |= 0xFF;               // Clear CR0 interrupt flag
00116     LPC_TIM2->CCR = (5 << 0);           // enable cap2.0 rising-edge capture; interrupt on cap2.0 p.496
00117     LPC_TIM2->TCR = (1 << 1);           // reset timer
00118     LPC_TIM2->TCR = 1;                  // enable timer
00119     
00120     NVIC_EnableIRQ(TIMER2_IRQn);        // Enable timer2 interrupt
00121     NVIC_SetVector(TIMER2_IRQn, uint32_t(interruptCapture));
00122     
00123     //  TIMER 3
00124     //LPC_SC->PCONP |= 1 << 23;           // TIMER3 power on
00125         
00126     LPC_TIM1->MCR = 3;                  // Interrupt and reset control
00127     LPC_TIM1->TC = 0;                   // clear timer counter
00128     LPC_TIM1->PC = 0;                   // clear prescale counter
00129     LPC_TIM1->PR = 0;                   // clear prescale register
00130     LPC_TIM1->MR0 = SystemCoreClock / 100;
00131         
00132     LPC_TIM1->IR |= 0xFF;               // Clear MR0 interrupt flag
00133     LPC_TIM1->TCR = (1 << 1);           // Reset TIMER1
00134     LPC_TIM1->TCR = 0;                  // Disable timer
00135     
00136     //NVIC_EnableIRQ(TIMER1_IRQn);      // Enable TIMER1 interrupt
00137     NVIC_SetVector(TIMER1_IRQn, uint32_t(interruptMatch));
00138 }
00139 
00140 int main()
00141 {
00142     string line;
00143     string filename = "/local/config.txt";
00144     
00145     PHY_PowerDown(); //power down ethernet interface 175mw
00146     
00147     ifstream myfile(filename.c_str());
00148     
00149     if (myfile.is_open())
00150     {
00151         while (getline(myfile,line))
00152         {
00153             if (line[0] != '#')
00154             {
00155                 // Find hand
00156                 if (line.find("Hand") != string::npos)
00157                 {
00158                     if (line.compare(5,6,"L") == 0) // If left hand
00159                         Analyzer_C_setHand (true, analyzer);
00160                     else
00161                         Analyzer_C_setHand (false, analyzer);
00162                 }
00163             }
00164         }
00165         
00166         myfile.close();
00167         
00168         initialize();
00169         
00170         while(true)
00171         {
00172            Sleep(); // Sleep main thread
00173         }
00174     }
00175     
00176     return 0;
00177 }