test

Dependencies:   mbed MMA8452Q

Fork of HelloWorld by Simon Ford

Files at this revision

API Documentation at this revision

Comitter:
kkalsi
Date:
Sun Oct 01 18:28:14 2017 +0000
Parent:
30:5881c661b0bb
Commit message:
transmit

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 5881c661b0bb -r bcfe51898e7c main.cpp
--- a/main.cpp	Sat Sep 30 18:43:25 2017 +0000
+++ b/main.cpp	Sun Oct 01 18:28:14 2017 +0000
@@ -6,10 +6,15 @@
 Serial xbee_routeur(p13,p14);
 Serial pc(USBTX, USBRX); // tx, rx
 DigitalOut rst1(p8); //Digital reset for the XBee, 200ns for reset
-MMA8452Q mma8452(p9,p10,0x1d);
-DigitalIn btn(p15);
+DigitalIn btn(p15); //Bouton read
+//MMA8452Q mma8452(p9,p10,0x1d);
 
 LocalFileSystem local("local");               // Create the local filesystem under the name "local"
+DigitalOut myled(LED1);
+Ticker sample;
+int currentStateButton;
+int previousStateButton;
+int validationStateButton;
 
 uint16_t panId;  // panId
 uint8_t panId_LSB;
@@ -20,31 +25,53 @@
 
 void seperatePanId(void);
 void ReadFile (void);
-void setPanId(void);
-void sauvegarder(void);
+void envoyer_tramme(char commande[], uint8_t data[], uint8_t length_MSB, uint8_t length_LSB, uint8_t API_frame_name);
+void transmisson(uint8_t data[], uint8_t length_MSB, uint8_t length_LSB, uint8_t API_frame_name);
+void initialisation_xbee(void);
+void readBtn(void);
 
 int main() { 
  
     ReadFile();// when you run first time without this line, the setup file is on created
-    
     wait(1);
-    
     seperatePanId(); // séparé msb et lsb du panId
     
+    /*
     pc.printf("panId lsb : %x\n\r",panId_LSB);
     pc.printf("panId msb : %x\n\r",panId_MSB);
     pc.printf("panId : %x\n\r",panId);
     pc.printf("url: %s\n\r",URL);
+    */
     
-    setPanId();     // set panId to Xbee
+    initialisation_xbee();
+    
+    char commande0[2] = {'I','D'};
+    uint8_t data0[2] = {panId_MSB, panId_LSB};
+    envoyer_tramme(commande0, data0, 0x00, 0x06, 0x09);
+    
+    char commande1[2] = {'S','C'};
+    uint8_t data1[2] = {0x09};
+    envoyer_tramme(commande1, data1, 0x00, 0x05, 0x09);
     
-    wait_ms(500);
+    /*
+    char commande2[2] = {'W','R'};
+    uint8_t data2[10] = {};
+    envoyer_tramme(commande2, data2, 0x00, 0x04, 0x09);
+    */
+    /*
+    char commande3[2] = {'A','C'};
+    uint8_t data3[2] = {};
+    envoyer_tramme(commande3, data3, 0x00, 0x04, 0x09);
+    */
     
-    sauvegarder();
+    sample.attach(&readBtn,0.05);
     
     while(1)
     {   
     
+    
+    
+    //myled = 1;
     if(xbee_routeur.readable())
     {
      pc.putc(xbee_routeur.getc());   
@@ -54,37 +81,117 @@
     {
      xbee_routeur.putc(pc.getc());   
     }
+
+    }
+}
+
+void readBtn(void)
+{
+    uint8_t data4[2];
+    currentStateButton = btn.read();  // lecture courante numérique button 
     
-    /*
-        xbee_routeur.putc(0x7E);                     // start delimiter
-        xbee_routeur.putc(0x00);                     // length 1/2
-        xbee_routeur.putc(0x10);                     // legth 2/2
-        xbee_routeur.putc(0x10);                     // Frame type
-        xbee_routeur.putc(0x01);                     // Frame id
-        xbee_routeur.putc(0x00);       // 64-bit adress (broadcast)  
-        xbee_routeur.putc(0x00);       // 64-bit adress (broadcast) 
-        xbee_routeur.putc(0x00);       // 64-bit adress (broadcast) 
-        xbee_routeur.putc(0x00);       // 64-bit adress (broadcast) 
-        xbee_routeur.putc(0x00);       // 64-bit adress (broadcast) 
-        xbee_routeur.putc(0x00);       // 64-bit adress (broadcast) 
-        xbee_routeur.putc(0x00);       // 64-bit adress (broadcast) 
-        xbee_routeur.putc(0x00);       // 64-bit adress (broadcast)   
-        xbee_routeur.putc(0xFF);                   // 16-bit adress (broadcast)
-        xbee_routeur.putc(0xFE);                   // 16-bit adress (broadcast) 
-        xbee_routeur.putc(0x00);                     // Broadcast radius
-        xbee_routeur.putc(0x00);                     // options
-        
-        xbee_routeur.putc(0x23);
-        xbee_routeur.putc(0x45);
- 
-        long sum = 0xFF - ((0x10 + 0x01 + 0xFF + 0xFF + 0xFE + 0x23 + 0x45 + 1) & 0xFF); // calcul du checksum
-        xbee_routeur.printf("%x \n", sum);
-        xbee_routeur.putc(sum);           // checksum
-        
-        wait(1);
-        */
+        if(previousStateButton != currentStateButton)   // détection de changement d'état pour bouton
+        {    
+            wait_ms(50);
+            
+            validationStateButton = btn.read();   // nouvelle lecture apres stabilisation button 
+            
+            if(currentStateButton == validationStateButton)  
+            {        
+                if(previousStateButton != currentStateButton)
+                {
+                    if(currentStateButton == 1)
+                    {
+                    data4[0] = 0x01;
+                    }
+                    else
+                    {
+                    data4[0] = 0x00;
+                    }
+                    transmisson(data4, 0x00, 0x0F, 0x10);
+                }
+            }
+        }
+        previousStateButton = validationStateButton; // mise a jour de l'état précédente button 1
+}
+
+void initialisation_xbee(void)
+{
+    xbee_routeur.baud(9600); //set baud rate
+    rst1 = 0;   // xbee reset
+    wait_ms(400);
+    rst1 = 1;
+    xbee_routeur.printf("+++");
+    wait_ms(500);
+}
+
+void transmisson(uint8_t data[], uint8_t length_MSB, uint8_t length_LSB, uint8_t API_frame_name)
+{
+    int sum = 0;
+    
+    xbee_routeur.putc(0x7E); // start delimiter
+    xbee_routeur.putc(length_MSB); // MSB length
+    xbee_routeur.putc(length_LSB); // LSB length
+    xbee_routeur.putc(API_frame_name); // API frame name 
+    xbee_routeur.putc(0x01); // Frame Id
+    xbee_routeur.putc(0x00); // 64-bit address (broadcast)  
+    xbee_routeur.putc(0x00); // 
+    xbee_routeur.putc(0x00); // 
+    xbee_routeur.putc(0x00); // 
+    xbee_routeur.putc(0x00); // 
+    xbee_routeur.putc(0x00); // 
+    xbee_routeur.putc(0x00); // 
+    xbee_routeur.putc(0x00); // 
+    xbee_routeur.putc(0xFF); // 16-bit address (broadcast)
+    xbee_routeur.putc(0xFE); // 16-bit address (broadcast)
+    xbee_routeur.putc(0x00); // Broadcast radius
+    xbee_routeur.putc(0x00); // Options
+    
+    uint16_t length = length_MSB;
+    length = (length << 8) + length_LSB;
+    //pc.printf("length : %x\n\r",length);
+    
+    for (int i = 0 ; i < length - 14; i++)
+    {
+        xbee_routeur.putc(data[i]); // API frame name 
+        //pc.printf("data %d : %x\n\r",i,data[i]);
+        sum = sum + data[i]; 
     }
     
+    sum = sum + API_frame_name + 0x01 + 0xFF +0xFE;
+    char checksum = (0xFF - (sum & 0xFF));
+    //pc.printf("checksum : %x\n\r",checksum);
+    xbee_routeur.putc(checksum); // checksum
+}
+
+
+void envoyer_tramme(char commande[], uint8_t data[], uint8_t length_MSB, uint8_t length_LSB, uint8_t API_frame_name)
+{
+    int sum = 0;
+    
+    xbee_routeur.putc(0x7E); // start delimiter
+    xbee_routeur.putc(length_MSB); // MSB length
+    xbee_routeur.putc(length_LSB); // LSB length
+    xbee_routeur.putc(API_frame_name); // API frame name 
+    xbee_routeur.putc(0x01); // Frame Id
+    xbee_routeur.putc(commande[0]); // Commande lettre 1
+    xbee_routeur.putc(commande[1]); // Commande lettre 2
+    
+    uint16_t length = length_MSB;
+    length = (length << 8) + length_LSB;
+    //pc.printf("length : %x\n\r",length);
+    
+    for (int i = 0 ; i < length - 4; i++)
+    {
+        xbee_routeur.putc(data[i]); // API frame name 
+        //pc.printf("data %d : %x\n\r",i,data[i]);
+        sum = sum + data[i]; 
+    }
+    
+    sum = sum + API_frame_name + 0x01 + commande[0]+ commande[1];
+    char checksum = (0xFF - (sum & 0xFF));
+    //pc.printf("checksum : %x\n\r",checksum);
+    xbee_routeur.putc(checksum); // checksum
 }
 
 void seperatePanId(void)
@@ -101,46 +208,33 @@
     fclose(set);
 }
 
-void sauvegarder(void)
-{
-    xbee_routeur.putc(0x7E); // start delimiter
-    xbee_routeur.putc(0x00); // MSB length
-    xbee_routeur.putc(0x04); // LSB length
-    xbee_routeur.putc(0x09); // AT COMMAND queue
-    xbee_routeur.putc(0x41); // Frame Id
-    xbee_routeur.putc(0x57); // 'W' Two ASCII characters that identify the AT Command (ID)
-    xbee_routeur.putc(0x52); // 'R'
-    
-    uint8_t checksum = (0xFF - ((0x09 + 0x41 + 0x57 + 0x52) & 0xFF));
-    pc.printf("checksum : %x\n\r",checksum);
-    xbee_routeur.putc(checksum); // checksum
-}
+
+
+
+
+
+
+
+
+
+
+
+
 
 
-void setPanId(void)
-{
-    xbee_routeur.baud(9600); //set baud rate
-    rst1 = 0;   // xbee reset
-    wait_ms(400);
-    rst1 = 1;
-    
-    xbee_routeur.putc(0x2B); // +
-    xbee_routeur.putc(0x2B); // +
-    xbee_routeur.putc(0x2B); // +
-    xbee_routeur.putc(0x7E); // start delimiter
-    xbee_routeur.putc(0x00); // MSB length
-    xbee_routeur.putc(0x06); // LSB length
-    xbee_routeur.putc(0x08); // AT command
-    xbee_routeur.putc(0x01); // Frame Id
-    xbee_routeur.putc(0x49); // 'I' Two ASCII characters that identify the AT Command (ID)
-    xbee_routeur.putc(0x44); // 'D'
-    xbee_routeur.putc(panId_MSB); // MSB du panId
-    xbee_routeur.putc(panId_LSB); // LSB du panId
-    
-    uint8_t checksum = (0xFF - ((0x08 + 0x01 + 0x49 + 0x44 + panId_MSB + panId_LSB) & 0xFF));
-    pc.printf("checksum : %x\n\r",checksum);
-    xbee_routeur.putc(checksum); // checksum
-}
+
+
+
+
+
+
+
+
+
+
+
+
+
 
 // SPI COMMUNICATION
 /*