Projet S5 Lecture de l'accelerometre avec interruption

Dependencies:   mbed PowerControl

Fork of Projet_S5 by Jonathan Tousignant

Files at this revision

API Documentation at this revision

Comitter:
joGenie
Date:
Thu Apr 10 18:39:16 2014 +0000
Parent:
11:9c0786fc06b4
Child:
13:d25fe10677e8
Commit message:
Value send in Short; Comment on class mouvement

Changed in this revision

analyzer.cpp Show annotated file Show diff for this revision Revisions of this file
analyzer.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mouvement.cpp Show annotated file Show diff for this revision Revisions of this file
mouvement.h Show annotated file Show diff for this revision Revisions of this file
trame.cpp Show annotated file Show diff for this revision Revisions of this file
trame.h Show annotated file Show diff for this revision Revisions of this file
--- a/analyzer.cpp	Tue Apr 08 13:46:22 2014 +0000
+++ b/analyzer.cpp	Thu Apr 10 18:39:16 2014 +0000
@@ -19,40 +19,33 @@
 
 void Analyzer::initialize()
 {
-    string pan("ACE");
-    
     DigitalOut reset(p8);
     reset = 0;
     wait(0.4);
     reset = 1;
     
-    /*trame = new Trame(p13, p14);
-    trame->sendATCommand("ID", pan.c_str(), pan.length());// Set le PANID selon le fichier de config
-    trame->sendATCommand("WR", 0, 0);    // Écrit la valeur du PANID en mémoire
-    trame->sendATCommand("AC", 0, 0);    // Applique les changements effectués*/
-    
-    //wait(1);
+    trame = new Trame(p13, p14);
     
-    allMouvement[0x0002] = new Mouvement(0x0002, 0x0002, "Z vertical, vers le ciel");
-    allMouvement[0x0003] = new Mouvement(0x0003, 0x0003, "Z vertical, vers le ciel INVERSE");
-    allMouvement[0x0004] = new Mouvement(0x0004, 0x0004, "Z vertical, vers le sol");
-    allMouvement[0x0005] = new Mouvement(0x0005, 0x0005, "Z vertical, vers le sol INVERSE");
-    allMouvement[0x0006] = new Mouvement(0x0006, 0x0006, "Z horizontal");
-    allMouvement[0x0007] = new Mouvement(0x0007, 0x0007, "Z horizontal INVERSE");
+    allMouvement[0x0002] = new Mouvement(0x0002, 0x0002, "Z vertical, vers le ciel", hand);
+    allMouvement[0x0003] = new Mouvement(0x0003, 0x0003, "Z vertical, vers le ciel INVERSE", hand);
+    allMouvement[0x0004] = new Mouvement(0x0004, 0x0004, "Z vertical, vers le sol", hand);
+    allMouvement[0x0005] = new Mouvement(0x0005, 0x0005, "Z vertical, vers le sol INVERSE", hand);
+    allMouvement[0x0006] = new Mouvement(0x0006, 0x0006, "Z horizontal", hand);
+    allMouvement[0x0007] = new Mouvement(0x0007, 0x0007, "Z horizontal INVERSE", hand);
 
-    allMouvement[0x0020] = new Mouvement(0x0020, 0x0020, "X vertical, vers le ciel");
-    allMouvement[0x0030] = new Mouvement(0x0030, 0x0030, "X vertical, vers le ciel INVERSE");
-    allMouvement[0x0040] = new Mouvement(0x0040, 0x0040, "X vertical, vers le sol");
-    allMouvement[0x0050] = new Mouvement(0x0050, 0x0050, "X vertical, vers le sol INVERSE");
-    allMouvement[0x0060] = new Mouvement(0x0060, 0x0060, "X horizontal");
-    allMouvement[0x0070] = new Mouvement(0x0070, 0x0070, "X horizontal INVERSE");
+    allMouvement[0x0020] = new Mouvement(0x0020, 0x0020, "X vertical, vers le ciel", hand);
+    allMouvement[0x0030] = new Mouvement(0x0030, 0x0030, "X vertical, vers le ciel INVERSE", hand);
+    allMouvement[0x0040] = new Mouvement(0x0040, 0x0040, "X vertical, vers le sol", hand);
+    allMouvement[0x0050] = new Mouvement(0x0050, 0x0050, "X vertical, vers le sol INVERSE", hand);
+    allMouvement[0x0060] = new Mouvement(0x0060, 0x0060, "X horizontal", hand);
+    allMouvement[0x0070] = new Mouvement(0x0070, 0x0070, "X horizontal INVERSE", hand);
 
-    allMouvement[0x0200] = new Mouvement(0x0200, 0x0200, "Y vertical, vers le ciel");
-    allMouvement[0x0300] = new Mouvement(0x0300, 0x0300, "Y vertical, vers le ciel INVERSE");
-    allMouvement[0x0400] = new Mouvement(0x0400, 0x0400, "Y vertical, vers le sol");
-    allMouvement[0x0500] = new Mouvement(0x0500, 0x0500, "Y vertical, vers le sol INVERSE");
-    allMouvement[0x0600] = new Mouvement(0x0600, 0x0600, "Y horizontal");
-    allMouvement[0x0700] = new Mouvement(0x0700, 0x0700, "Y horizontal INVERSE");
+    allMouvement[0x0200] = new Mouvement(0x0200, 0x0200, "Y vertical, vers le ciel", hand);
+    allMouvement[0x0300] = new Mouvement(0x0300, 0x0300, "Y vertical, vers le ciel INVERSE", hand);
+    allMouvement[0x0400] = new Mouvement(0x0400, 0x0400, "Y vertical, vers le sol", hand);
+    allMouvement[0x0500] = new Mouvement(0x0500, 0x0500, "Y vertical, vers le sol INVERSE", hand);
+    allMouvement[0x0600] = new Mouvement(0x0600, 0x0600, "Y horizontal", hand);
+    allMouvement[0x0700] = new Mouvement(0x0700, 0x0700, "Y horizontal INVERSE", hand);
 }
  
 void Analyzer::setMinMax(signed char* values)
@@ -150,8 +143,11 @@
     // pc.printf("\n\rInit: %i, min: %i, max: %i, sens: %s", x.initial, x.min, x.max, x.sens ? "acceleration":"deceleration");
     // pc.printf("\n\rInit: %i, min: %i, max: %i, sens: %s", y.initial, y.min, y.max, y.sens ? "acceleration":"deceleration");
     // pc.printf("\n\rInit: %i, min: %i, max: %i, sens: %s", z.initial, z.min, z.max, z.sens ? "acceleration":"deceleration");
+    
+    if(it != allMouvement.end())
+        it->second->sendData(trame);
 }
- 
+
 void Analyzer::setInitial(signed char* init)
 {
     x.setInitial(init[0]);
@@ -193,3 +189,9 @@
     Analyzer *an = (Analyzer*)analyzer;
     an->checkMouvement();
 }
+
+extern "C" void Analyzer_C_setHand(bool h, void *analyzer)
+{
+    Analyzer *an = (Analyzer*)analyzer;
+    an->setHand(h);
+}
--- a/analyzer.h	Tue Apr 08 13:46:22 2014 +0000
+++ b/analyzer.h	Thu Apr 10 18:39:16 2014 +0000
@@ -67,6 +67,8 @@
      */
     void checkMouvement();
     
+    void setHand(bool l) { hand = l; }
+    
 private:
 
     /*! 
@@ -134,8 +136,10 @@
     Data x; /*!< Data for the axe x */
     Data y; /*!< Data for the axe y */
     Data z; /*!< Data for the axe z */
+    bool hand; /*!< Char to determine the hand true = left, false = right*/
+    
     map<int, Mouvement*> allMouvement; /*!< map that containts all movement */
-    Trame *trame;
+    Trame *trame; /*!< Object Trame for send information */
 };
  
 // Define function in C for interruption
@@ -149,7 +153,8 @@
 extern void Analyzer_C_Initialize(void *analyzer); /*! Call Initialize \see initialize() */
 extern void Analyzer_C_setMinMax(signed char* values, void *analyzer); /*! Call setMinMax \see setMinMax(signed char*) */
 extern void Analyzer_C_setInitial(signed char* value, void *analyzer); /*! Call setInitial \see setInitial(signed char*) */
-extern void Analyzer_C_checkMouvement(void *analyzer); /*! Call setInitial \see checkMouvement() */
+extern void Analyzer_C_checkMouvement(void *analyzer); /*! Call checkMouvement \see checkMouvement() */
+extern void Analyzer_C_setHand(bool h, void *analyzer); /*! Call setHand \see setHand(bool left) */
  
 #ifdef __cplusplus
 }
--- a/main.cpp	Tue Apr 08 13:46:22 2014 +0000
+++ b/main.cpp	Thu Apr 10 18:39:16 2014 +0000
@@ -1,6 +1,8 @@
 #include "accelerometer.h"
 #include "analyzer.h"
 #include "interrupt.h"
+#include <iostream>
+#include <fstream>
 
 Serial pc(USBTX, USBRX);
 DigitalOut led1(LED1);
@@ -8,8 +10,11 @@
 DigitalOut led4(LED4);
 DigitalIn button(p30);
 
+LocalFileSystem local("local");
+
 void *accelerometer = Accelerometer_C_new();
 void *analyzer = Analyzer_C_new();
+
 bool rebound = true;
 bool appuyer = false;
 
@@ -30,14 +35,14 @@
         LPC_TIM2->MCR = 0;
         LPC_TIM2->CCR = (0x06 << 0);  //falling edge
         
-        LPC_TIM3->TC = 0;       // clear timer counter
-        LPC_TIM3->PC = 0;       // clear prescale counter
-        LPC_TIM3->PR = 0;       // clear prescale register
-        LPC_TIM3->IR |= 0xFF;
-        LPC_TIM3->TCR = 0x01;   //enable timer 3
+        LPC_TIM1->TC = 0;       // clear timer counter
+        LPC_TIM1->PC = 0;       // clear prescale counter
+        LPC_TIM1->PR = 0;       // clear prescale register
+        LPC_TIM1->IR |= 0xFF;
+        LPC_TIM1->TCR = 0x01;   //enable timer 3
     
         appuyer = true;
-        NVIC_EnableIRQ(TIMER3_IRQn); // Enable timer3 interrupt
+        NVIC_EnableIRQ(TIMER1_IRQn); // Enable TIMER1 interrupt
         
         signed char* values = Accelerometer_C_getAccelValue(accelerometer);
         
@@ -49,15 +54,15 @@
         led1 = 0;
         LPC_TIM2->CCR = (5 << 0);  //rising edge
         
-        LPC_TIM3->TC = 0;       // clear timer counter
-        LPC_TIM3->PC = 0;       // clear prescale counter
-        LPC_TIM3->PR = 0;       // clear prescale register
-        LPC_TIM3->IR |= 0xFF;
-        LPC_TIM3->TCR = 0;      //disable timer 3
+        LPC_TIM1->TC = 0;       // clear timer counter
+        LPC_TIM1->PC = 0;       // clear prescale counter
+        LPC_TIM1->PR = 0;       // clear prescale register
+        LPC_TIM1->IR |= 0xFF;
+        LPC_TIM1->TCR = 0;      //disable timer 3
         
         appuyer = false;
         rebound = true;
-        NVIC_DisableIRQ(TIMER3_IRQn); // Disable timer3 interrupt
+        NVIC_DisableIRQ(TIMER1_IRQn); // Disable TIMER1 interrupt
         
         Analyzer_C_checkMouvement(analyzer);
     }
@@ -83,7 +88,7 @@
         delete values;
     }
     
-    LPC_TIM3->IR |= 0xFF; // Reset timer  
+    LPC_TIM1->IR |= 0xFF; // Reset timer  
 }
 
 void initialize()
@@ -111,29 +116,55 @@
     NVIC_SetVector(TIMER2_IRQn, uint32_t(interruptCapture));
     
     //  TIMER 3
-    LPC_SC->PCONP |= 1 << 23;           // Timer3 power on
+    //LPC_SC->PCONP |= 1 << 23;           // TIMER3 power on
         
-    LPC_TIM3->MCR = 3;                  // Interrupt and reset control
-    LPC_TIM3->TC = 0;                   // clear timer counter
-    LPC_TIM3->PC = 0;                   // clear prescale counter
-    LPC_TIM3->PR = 0;                   // clear prescale register
-    LPC_TIM3->MR0 = SystemCoreClock / 20;
+    LPC_TIM1->MCR = 3;                  // Interrupt and reset control
+    LPC_TIM1->TC = 0;                   // clear timer counter
+    LPC_TIM1->PC = 0;                   // clear prescale counter
+    LPC_TIM1->PR = 0;                   // clear prescale register
+    LPC_TIM1->MR0 = SystemCoreClock / 100;
         
-    LPC_TIM3->IR |= 0xFF;               // Clear MR0 interrupt flag
-    LPC_TIM3->TCR = (1 << 1);           // Reset Timer3
-    LPC_TIM3->TCR = 0;                  // Disable timer
+    LPC_TIM1->IR |= 0xFF;               // Clear MR0 interrupt flag
+    LPC_TIM1->TCR = (1 << 1);           // Reset TIMER1
+    LPC_TIM1->TCR = 0;                  // Disable timer
     
-    //NVIC_EnableIRQ(TIMER3_IRQn);      // Enable timer3 interrupt
-    NVIC_SetVector(TIMER3_IRQn, uint32_t(interruptMatch));
+    //NVIC_EnableIRQ(TIMER1_IRQn);      // Enable TIMER1 interrupt
+    NVIC_SetVector(TIMER1_IRQn, uint32_t(interruptMatch));
 }
 
 int main()
 {
-    initialize();
+    string line;
+    string filename = "/local/config.txt";
     
-    pc.printf("\n\rReady");
+    ifstream myfile(filename.c_str());
     
-    while(true)
+    if (myfile.is_open())
     {
+        while (getline(myfile,line))
+        {
+            if (line[0] != '#')
+            {
+                // Find hand
+                if (line.find("Hand") != string::npos)
+                {
+                    if (line.compare(5,6,"L") == 0)
+                        Analyzer_C_setHand(true, analyzer);
+                    else
+                        Analyzer_C_setHand(false, analyzer);
+                }
+            }
+        }
+        
+        myfile.close();
+        
+        initialize();
+        pc.printf("\n\rReady");
+        
+        while(true)
+        {
+        }
     }
+    
+    return 0;
 }
--- a/mouvement.cpp	Tue Apr 08 13:46:22 2014 +0000
+++ b/mouvement.cpp	Thu Apr 10 18:39:16 2014 +0000
@@ -1,9 +1,10 @@
 #include "mouvement.h"
 
-Mouvement::Mouvement(int code, int code_envoie, string definition) :
+Mouvement::Mouvement(short code, short code_envoie, string definition, bool h) :
     _code(code),
     _code_envoie(code_envoie),
-    _definition(definition)
+    _definition(definition),
+    hand(h)
 {
 }
 
@@ -11,10 +12,15 @@
 {
     char code[4];
     
-    code[0] = (_code_envoie >> 24) & 0xFF;
-    code[1] = (_code_envoie >> 16) & 0xFF;
-    code[2] = (_code_envoie >> 8) & 0xFF;
-    code[3] = _code_envoie & 0xFF;
+    code[0] = hand ? 0x1 : 0x0;
+    code[1] = (_code_envoie >> 8) & 0xF;
+    code[2] = (_code_envoie >> 4) & 0xF;
+    code[3] = _code_envoie & 0xF;
     
-    trame->sendTransmitRequest(0, code, sizeof(int));   
+    Serial pc(USBTX,USBRX);
+    pc.printf("\n\rdefinition : %s",_definition.c_str());
+    
+    const char destination[8] = {0};
+    
+    trame->sendTransmitRequest(destination, code, sizeof(int));   
 }
--- a/mouvement.h	Tue Apr 08 13:46:22 2014 +0000
+++ b/mouvement.h	Thu Apr 10 18:39:16 2014 +0000
@@ -1,3 +1,11 @@
+/*!
+ *  \file mouvement.cpp
+ *  \brief Class that send mouvement and contain a definition
+ *  \author Equipe P02
+ *  \version 1
+ *  \date 9/04/2014
+ */
+ 
 #ifndef MOUVEMENT_H
 #define MOUVEMENT_H
 
@@ -6,17 +14,39 @@
 
 using namespace std;
 
+/*! 
+ *  \class Mouvement mouvement "mouvement.h"
+ *  \brief Class define a mouvement and send a code in xbee
+ */
 class Mouvement
 {
 public:
-    Mouvement(int code, int code_envoie, string definition);
+    /*!
+     *  \brief Constructor
+     *  \param code Code to find mouvement
+     *  \param code_envoie Code to send in xbee
+     *  \param definition Definition of the mouvement
+     *  \param h The hand true = left false = right
+     */
+    Mouvement(short code, short code_envoie, string definition, bool h);
     
+    /*!
+     * \brief Get definition
+     * \return _definition
+     */
+    string definition() const { return _definition; }
+    
+    /*!
+     *  \brief Construct data in char* and send this
+     *  \param trame Trame the send in xbee
+     */
     void sendData(Trame *trame);
     
 private:
-    int _code;
-    int _code_envoie;
-    string _definition;
+    short _code; /*!< Code that define mouvement */
+    short _code_envoie; /*!< Code that send in xbee */
+    string _definition; /*!< Definition of the mouvement */
+    bool hand; /*!< The hand use */
 };
 
 #endif
--- a/trame.cpp	Tue Apr 08 13:46:22 2014 +0000
+++ b/trame.cpp	Thu Apr 10 18:39:16 2014 +0000
@@ -6,30 +6,6 @@
 {
 }
 
-void Trame::sendATCommand(const char* command, const char* data, int length)
-{
-    int tmp = length + 4;   // length (grandeur du data à envoyer) 4 (API(1), FrameID(1), AT Command(2))
-    char* trm = new char[tmp + 4];  // 4 (Delimiter(1), Length(2), Checksum(1))
-    
-    trm[0] = 0x7E;      // Delimiter
-    trm[1] = static_cast<char>((tmp >> 8) & 0xFF);   // MSB de length
-    trm[2] = static_cast<char>(tmp & 0xFF); // LSB length
-    trm[3] = 0x09;      // Type
-    trm[4] = 0x01;      // API
-    trm[5] = command[0];
-    trm[6] = command[1];
-    for (int i = 0; i < length; i++)
-        trm[7+i] = data[i]; //Data
-    trm[7+length] = crc8(trm, tmp + 3);
-    
-    Serial xbee(tx, rx);
-    for (int i = 0; i < tmp + 4; i++)
-        xbee.putc(trm[i]); //Transmition des donnees
-        
-    wait(0.01);
-    delete trm;
-}
-
 void Trame::sendTransmitRequest(const char* destination, const char* data, int length)
 {
     int tmp = length + 14;  // length (grandeur du data à envoyer) 14 (API(1), FrameID(1), MAC(8), 16bit(2), Radius(1), Options(1))
@@ -52,6 +28,12 @@
         
     trm[17 + length] = crc8(trm, tmp + 3);
     
+    Serial pc(USBTX, USBRX);
+    
+    pc.printf("\n\rtrame: ");
+    for (int i = 0; i < tmp + 4; i++)
+        pc.printf("%X ", trm[i]);
+    
     Serial xbee(tx, rx);
     for (int i = 0; i < tmp + 4; i++)
         xbee.putc(trm[i]);
--- a/trame.h	Tue Apr 08 13:46:22 2014 +0000
+++ b/trame.h	Thu Apr 10 18:39:16 2014 +0000
@@ -26,15 +26,7 @@
      *  \param _tx PinName for tx
      *  \param _rx PinName for rx
      */
-    Trame(PinName _tx, PinName _rx);
-    
-    /*!
-     *  \brief Send a At Command to change configuration
-     *  \param command Name of the command
-     *  \param data Data to modify
-     *  \param lenght Lenght of the data
-     */
-    void sendATCommand(const char* command, const char* data, int length);   
+    Trame(PinName _tx, PinName _rx);  
     
     /*!
      *  \brief Send a transmit request