* Routeur

Dependencies:   ConfigFile mbed

Files at this revision

API Documentation at this revision

Comitter:
benjaminroy
Date:
Sun Feb 12 03:58:07 2017 +0000
Parent:
2:aebf0ae12644
Commit message:
- Communication coordinateur-routeur fonctionne; - Tableau de pointeurs de fonciton

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Feb 12 02:26:40 2017 +0000
+++ b/main.cpp	Sun Feb 12 03:58:07 2017 +0000
@@ -2,10 +2,20 @@
 #include "ConfigFile.h"
 #include "mbed.h"
 
+#define MMA8452_CTRL_REG_1 0x2A
+#define MMA8452_ADDRESS 0x3A
+#define MMA8452_OUT_Z_MSB 0x05 // [7:0] are 8 MSBs of 12-bit sample
+#define MMA8552_OUT_Z_LSB 0x06 // [7:4] are 4 LSBs of 12-bit sample
+#define MMA8452_WHO_AM_I 0x0D
+
 DigitalOut reset(p8);
+DigitalIn btn(p15);
+I2C i2c(p28, p27); // SDA, SCL
 Serial xbee(p13, p14);  // tx, rx
 Serial pc(USBTX, USBRX); // tx, rx
+Ticker ticker;
 
+int16_t (*functionPointers[2])();
 uint16_t panId = 0;
 char panIdChar[5];
 
@@ -16,9 +26,6 @@
     printf("\n");
 }
 
-/*
-* Read a configuration file from a mbed.
-*/
 void readConfigFile() {
     LocalFileSystem local("local");
     ConfigFile cfg;
@@ -33,6 +40,43 @@
     }
 }
 
+int16_t readXYZAcceleration() {
+    char addr[1] = { MMA8452_OUT_Z_MSB };
+    char data[1] = { 0 };
+    char z[2] = { 0 };
+    char buff[6] = { 0 };
+    int16_t temp; 
+    
+    data[0] = MMA8452_WHO_AM_I;
+    i2c.write(MMA8452_ADDRESS, data, 1, true);
+    i2c.read(MMA8452_ADDRESS, buff, 1);
+    
+    if (buff[0] != 0x2A){
+        printf("Erreur de communication...");
+        return 1;
+    }
+
+    // Set actif mode on
+    buff[0] = 0;
+    data[0] = MMA8452_CTRL_REG_1;
+    i2c.write(MMA8452_ADDRESS, data, 1, true);
+    i2c.read(MMA8452_ADDRESS, &buff[0], 1);
+    buff[0] |= 0x05; 
+    i2c.write(MMA8452_ADDRESS, buff, 1);
+    
+    i2c.write(MMA8452_ADDRESS, addr, 1, true);
+    i2c.read(MMA8452_ADDRESS, &z[0], 2); // OUT_Z_MSB, and OUT_Z_LSB
+
+    // MSB + LSB
+    ((char*)&temp)[0] = z[1];
+    ((char*)&temp)[1] = z[0];
+    return (temp >> 4);
+}
+
+int16_t readDryContact() {
+    return btn;
+}
+
 uint8_t calculateChecksum(uint8_t frameSize, uint8_t* buffer) {
     uint32_t checksum = 0;
     uint8_t frameTypeIndex = 3;
@@ -48,13 +92,12 @@
     for (int i = 0; i < bufferSize;) {
         if (xbee.writeable()) {
             xbee.putc(buffer[i]);
-            wait(0.50);
             i++;
-        }   
+        }
     }
 }
 
-void setFrame() {
+void setFrame(bool dryContact, int16_t accelerations) {
     const uint8_t bufferSize = 22;
     uint8_t buffer[bufferSize] = { 0 };
     
@@ -85,9 +128,17 @@
     sendFrame(bufferSize, buffer);
 }
 
+void sendDataToCoordinator() {
+    bool dryContact = functionPointers[0] ();
+    int16_t XYZAccelerations = functionPointers[1]();
+        
+    printf("Contact sec: %d, Acceleration: %i\n", dryContact, XYZAccelerations); 
+    setFrame(dryContact, XYZAccelerations);
+}
+
 int main() {
     printf("Starting a router...\n");    
-
+    
     reset = 0;
     wait_ms(1);
     reset = 1;
@@ -95,10 +146,14 @@
     
     readConfigFile();
     
-    xbee.baud(9600); // Set baud rate
-    xbee.printf("ATID %i\r", panId); // Set the 64-bit PAN ID
+    xbee.baud(9600);                    // Set baud rate
+    xbee.printf("ATID %i\r", panId);    // Set the 64-bit PAN ID
     xbee.printf("ATWR \r");
     xbee.printf("ATCN \r");
     
-    setFrame();
+    functionPointers[0] = readDryContact;       // a. Un contact sec
+    functionPointers[1] = readXYZAcceleration;    // b. Un accéléromètre 3 axes
+    
+    ticker.attach(&sendDataToCoordinator, 1.0);
+    while (1) {}
 }