v2

Dependencies:   MMA8452 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Antoine Mercier-Nicol (MERA2411)
00002 // Vincent Bougie ()
00003 // Code noeud Routeur
00004 
00005 #include "mbed.h"
00006 #include "MMA8452.h"
00007 
00008 DigitalOut XBee_reset(p8);
00009 Serial serial_pc(USBTX,USBRX);
00010 Serial serial_XBee(p13,p14);
00011 
00012 //MMA8452 mma8452(p28,p27);
00013 I2C i2c(p28,p27);
00014 DigitalIn button(p30);
00015  
00016 char panid1;
00017 char panid2;
00018 float rate;
00019 
00020 char var_acc;
00021 
00022 LocalFileSystem local("local");               // Create the local filesystem under the name "local
00023  
00024 int main() {
00025     void ReadConfig();
00026     void setPANID();
00027     void init_accelero();
00028     void read_accelero();
00029     
00030     char var_button;
00031     
00032     
00033     ReadConfig();
00034     setPANID();
00035     init_accelero();
00036     
00037     //mma8452.init();                     // initialisation de la communication I2C entre le microcontrolleur et l'accelerometre
00038     //Acceleration acc;                   // Accleration structure declared in MMA8452 class
00039     
00040     wait(1);
00041     while (1) {
00042         read_accelero();
00043         //acc = mma8452.readValues();
00044         if(button) {
00045             var_button = 0x10;
00046         } else {
00047             var_button = 0x11;
00048         }
00049         
00050         serial_pc.printf("%x", var_acc);
00051         serial_pc.printf("%x", var_button);
00052         
00053         serial_XBee.putc(0x7E);                     // start delimiter
00054         serial_XBee.putc(0x00);                     // length 1/2
00055         serial_XBee.putc(0x10);                     // legth 2/2
00056         serial_XBee.putc(0x10);                     // Frame type
00057         serial_XBee.putc(0x01);                     // Frame id
00058         serial_XBee.putc(0x00);       // 64-bit adress (broadcast)  
00059         serial_XBee.putc(0x00);       // 64-bit adress (broadcast) 
00060         serial_XBee.putc(0x00);       // 64-bit adress (broadcast) 
00061         serial_XBee.putc(0x00);       // 64-bit adress (broadcast) 
00062         serial_XBee.putc(0x00);       // 64-bit adress (broadcast) 
00063         serial_XBee.putc(0x00);       // 64-bit adress (broadcast) 
00064         serial_XBee.putc(0x00);       // 64-bit adress (broadcast) 
00065         serial_XBee.putc(0x00);       // 64-bit adress (broadcast)   
00066         serial_XBee.putc(0xFF);                   // 16-bit adress (broadcast)
00067         serial_XBee.putc(0xFE);                   // 16-bit adress (broadcast) 
00068         serial_XBee.putc(0x00);                     // Broadcast radius
00069         serial_XBee.putc(0x00);                     // options
00070         
00071 //        serial_XBee.putc(acc.x);
00072 //        serial_XBee.putc(acc.y);
00073 //        serial_XBee.putc(acc.z);
00074         serial_XBee.putc(var_acc);
00075         serial_XBee.putc(var_button);
00076 
00077        
00078         long sum = 0xFF - ((0x10 + 0x01 + 0xFF + 0xFF + 0xFE + var_button + var_acc + 1) & 0xFF); // calcul du checksum
00079         serial_pc.printf("%x \n", sum);
00080         serial_XBee.putc(sum);           // checksum
00081         
00082         wait(1);
00083     }
00084 }
00085 
00086 void init_accelero(){
00087     i2c.frequency(100000);
00088     
00089     char cmd[2];
00090     cmd[0] = 0x2A;
00091     cmd[1] = 0x01;
00092     int address = 0x1D << 1;
00093     
00094     i2c.write(address, cmd, 2, false);    
00095 }
00096 
00097 void read_accelero(){
00098     char cmd[2];
00099     cmd[0] = 0x05;
00100     cmd[1] = 0x06;
00101     int address = 0x1D << 1;
00102     i2c.write(address, cmd, 1, true);
00103     i2c.read(address, cmd, 1);
00104     
00105     short value = cmd[0] << 8 | cmd[1];
00106     value = value >> 4;
00107     var_acc = value;
00108 }
00109 
00110 void ReadConfig(){
00111     FILE *set = fopen("/local/config.txt", "r");
00112     fscanf(set,"%f", &rate); // Rate
00113     fscanf(set,"%x", &panid1);   // PAN id 1/2
00114     fscanf(set,"%x", &panid2);   // PAN id 2/2
00115     fclose(set);
00116 }
00117 
00118 void setPANID(){
00119    serial_XBee.baud(9600);
00120     XBee_reset = 0;
00121     wait_ms(400);
00122     XBee_reset = 1;
00123         serial_pc.printf("%x", panid1);
00124         serial_pc.printf("%x", panid2);
00125         // Setting PAN ID
00126         serial_XBee.putc(0x7E);       // start delimiter
00127         serial_XBee.putc(0x00);       // length 1/2
00128         serial_XBee.putc(0x06);       // legth 2/2
00129         serial_XBee.putc(0x08);       // Frame type
00130         serial_XBee.putc(0x01);       // Frame id
00131         serial_XBee.putc(0x49);       // AT Command 1/2 
00132         serial_XBee.putc(0x44);       // AT Command 2/2
00133         serial_XBee.putc(panid1);       // Parameter (PAN ID) 1/2
00134         serial_XBee.putc(panid2);       // Parameter (PAN ID) 2/2
00135         
00136         long sum = (0x08 + 0x01 + 0x49 + 0x44 + panid1 + panid2); // calcul du checksum
00137         serial_pc.printf("%h", 0xFF - sum & 0xFF);
00138         serial_XBee.putc(0xFF - (sum & 0xFF));    // Checksum
00139 }