Maxime Chevalier / Mbed 2 deprecated SX1272Pump

Dependencies:   SX1272Lib mbed WakeUp

Fork of SX1272PingPong by Semtech

Files at this revision

API Documentation at this revision

Comitter:
chevamax
Date:
Fri Mar 03 13:42:44 2017 +0000
Parent:
15:79a78f997f18
Child:
17:cce0eada6d82
Commit message:
Ajout timer d'?coute d'ordre

Changed in this revision

WakeUp.lib 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WakeUp.lib	Fri Mar 03 13:42:44 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/Sissors/code/WakeUp/#49ca85e8822f
--- a/main.cpp	Tue Feb 28 13:40:13 2017 +0000
+++ b/main.cpp	Fri Mar 03 13:42:44 2017 +0000
@@ -5,12 +5,14 @@
 #include "trame.h"
 #include "ordre.h"
 #include "pompe.h"
+#include "WakeUp.h"
 //#include "niveau.h"
 
 /* Set this flag to '1' to display debug messages on the console */
 #define DEBUG_MESSAGE   1
 #define ID_DEVICE 0x24
 #define ID_STATION 0x55
+#define TEMPS_ECOUTE_ORDRE_SECONDE 20.0
 
 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
 #define USE_MODEM_LORA  1
@@ -101,6 +103,31 @@
 int16_t RssiValue = 0.0;
 int8_t SnrValue = 0.0;
 
+/*Ticker flipper;
+
+
+void doNothing(){
+    NULL;
+}*/
+
+void dormir(){
+    debug("dodo\r\n");
+    Radio.Sleep();
+    //flipper.attach(&doNothing, 20.0);
+    // critical section
+    WakeUp::set_ms(10000);
+    sleep();
+    
+    wait(5);
+}
+/*
+void flip(){
+    debug("flip\r\n");
+    dormir();
+}*/
+
+Timer ecouterOrdre;
+
 int main() 
 {
     uint8_t i;
@@ -181,6 +208,10 @@
     
     bool enAttente = false;
     
+    //The low-power oscillator can be quite inaccurate on some targets
+    //this function calibrates it against the main clock
+    WakeUp::calibrate();
+    
     //TODO
     // Distinguer deux temps. Il faut pouvoir s'endormir une heure, mais aussi écouter pendant 5 minutes
     Radio.Rx( RX_TIMEOUT_VALUE );
@@ -204,7 +235,9 @@
                     //Si message recu alors trouver ordre correspondant et faire traitement
                     if( trameRecue.getIdRecepteur() == (char) ID_DEVICE )
                     {
+                        debug("ID DEVICE OK\r\n");
                         if( trameRecue.getIdEmetteur() == (char) ID_STATION ){
+                            debug("ID STATION OK\r\n");
                             //Ordre pour nous
                             led = !led;
                             debug( "...Ordre recu \r\n" );
@@ -213,29 +246,65 @@
                             //TODO executer ordre
                             debug("Executer Ordre\r\n");
                             
-                            //On attend 5 minutes
+                            //On attend 10 secondes
                             debug("Dormir\r\n");
-                            wait(10);
+                            dormir();
+                            
+                            
                             enAttente = false;
                             Radio.Rx( RX_TIMEOUT_VALUE );
                         }else{
                             debug("autre station\r\n");
+                            Radio.Rx( RX_TIMEOUT_VALUE );
                         }
                     }
                     else // valid reception but neither a PING or a PONG message
                     {    // Set device as master ans start again
                         debug("Reception message pour quelqu'un d'autre\r\n");
-                        isMaster = true;
                         Radio.Rx( RX_TIMEOUT_VALUE ); //La on reattend pour 5 minutes à chaque foi que qq'un envoit un message c'est pas top....
                     }    
                 }
             }
-            else //enAttente ==false donc on envoit les donnees
-            {
-                //Recuperer les données capteurs
-                //dataPaquet.mettreAJourEtatPompe(pompe.etat());
-                //dataPaquet.mettreAJourNiveauCuve(cuve.getNiveauCuve());
-                debug("envoie1\r\n");
+            State = LOWPOWER;
+            break;
+        case TX:    
+            led = !led; 
+            debug( "Envoi des donnees...\r\n" );
+            debug("En attente ORDRE\r\n");
+            //flipper.attach(&dormir, 40.0); // the address of the function to be attached (flip) and the interval (2 seconds)
+            Radio.Rx( RX_TIMEOUT_VALUE );
+            State = LOWPOWER;
+            break;
+        case RX_TIMEOUT:           
+            //TODO executer ordre
+            /*debug("Pas Ordre\r\n");
+            
+            //On attend 10 secondes
+            debug("Dormir\r\n");
+            dormir();
+            
+            wait(5);
+            enAttente = false; */
+            Radio.Rx( RX_TIMEOUT_VALUE );  
+            State = LOWPOWER;
+            break;
+        case RX_ERROR:
+            //Erreur CRC, faire une demande de renvoi
+            //TODO
+            debug("Erreur CRC \r\n");
+            Radio.Rx( RX_TIMEOUT_VALUE );
+            
+            State = LOWPOWER;
+            break;
+        case TX_TIMEOUT:
+            Radio.Rx( RX_TIMEOUT_VALUE );
+            State = LOWPOWER;
+            break;
+        case LOWPOWER:
+            if(!enAttente){
+                //Corps de l'application
+                //On envoie les données
+                debug("envoie3\r\n");
                 dataPaquet.mettreAJourEtatPompe((char) 0);
                 dataPaquet.mettreAJourNiveauCuve((char) 0x1F);
                 dataPaquet.mettreAJourNiveauBatterie((char) 0xF); //TODO
@@ -251,75 +320,17 @@
                 // Send the data 
                 Radio.Send( Buffer, BufferSize );
                 enAttente = true;
-            }
-            State = LOWPOWER;
-            break;
-        case TX:    
-            led = !led; 
-            debug( "Envoi des donnees...\r\n" );
-            Radio.Rx( RX_TIMEOUT_VALUE );
-            State = LOWPOWER;
-            break;
-        case RX_TIMEOUT:
-            debug("envoie2\r\n");
-            dataPaquet.mettreAJourEtatPompe((char) 0);
-            dataPaquet.mettreAJourNiveauCuve((char) 0x1F);
-            dataPaquet.mettreAJourNiveauBatterie((char) 0xF); //TODO
-            
-            char * trame = dataPaquet.creerTrame();
-            debug( trame);
-            strcpy( ( char* ) Buffer, trame);
-            for (i = TAILLE_TRAME_DATA; i < BufferSize; i++ )
-            {
-                Buffer[i] = i - 4;
+                ecouterOrdre.start();
+            }else{
+                if(ecouterOrdre.read() > TEMPS_ECOUTE_ORDRE_SECONDE){
+                    debug("delaisAttente depasse. Dormir.\r\n");
+                    ecouterOrdre.stop();
+                    ecouterOrdre.reset();
+                    dormir();
+                    debug("fin dodo\r\n");
+                    enAttente = false;
+                }
             }
-            wait_ms( 10 );
-            // Send the data 
-            Radio.Send( Buffer, BufferSize );
-            enAttente = true;
-            //On attend 5 minutes
-            //debug("Dormir\r\n");
-            //wait(10);
-            //enAttente = false;
-            //Radio.Rx( RX_TIMEOUT_VALUE );   
-            State = LOWPOWER;
-            break;
-        case RX_ERROR:
-            //Erreur CRC, faire une demande de renvoi
-            //TODO
-            // We have received a Packet with a CRC error, send reply as if packet was correct
-            /*if( isMaster == true )
-            {
-                // Send the next PING frame
-                strcpy( ( char* )Buffer, ( char* )PingMsg );
-                for( i = 4; i < BufferSize; i++ )
-                {
-                    Buffer[i] = i - 4;
-                }
-                wait_ms( 10 );  
-                Radio.Send( Buffer, BufferSize );
-            }
-            else
-            {
-                // Send the next PONG frame
-                strcpy( ( char* )Buffer, ( char* )PongMsg );
-                for( i = 4; i < BufferSize; i++ )
-                {
-                    Buffer[i] = i - 4;
-                }
-                wait_ms( 10 );  
-                Radio.Send( Buffer, BufferSize );
-            }*/
-            debug("Erreur CRC \r\n");
-            Radio.Rx( RX_TIMEOUT_VALUE );
-            
-            State = LOWPOWER;
-            break;
-        case TX_TIMEOUT:
-            Radio.Rx( RX_TIMEOUT_VALUE );
-            State = LOWPOWER;
-            break;
-        case LOWPOWER:
             break;
         default:
             State = LOWPOWER;