Iniversity work

Dependencies:   mbed MMA8452

Revision:
1:0630912bb400
Parent:
0:ca687da7d225
Child:
2:8d0e48d38611
--- a/main.cpp	Wed Aug 30 13:28:26 2017 +0000
+++ b/main.cpp	Fri Sep 01 20:53:36 2017 +0000
@@ -1,19 +1,110 @@
-#include "mbed.h"
-
-DigitalOut myled(LED1);
 Serial pc(USBTX, USBRX);
-//Serial uart(p13,p14);
 SPI spiTest(p5, p6, p7);
 DigitalOut cs(p8);
 
+//Serial pc(USBTX,USBRX);
+PwmOut led1(LED1);
+PwmOut led2(LED2);
+PwmOut led3(LED3);
+
+ 
+MMA8452 acc(p9, p10, 100000);
+void calculateAngle();
+
 int main() {
-    //I2C
+   
+  while(1) {
+      calculateAngle();
+      wait(1);
+   }
+}
+
+void calculateAngle(){
+    double x, y, z;
+    float val = 180.0 / 3.1416;
+    acc.readXYZGravity(&x,&y,&z);
+    
+    led1 = abs(x);
+    led2 = abs(y);
+    led3 = abs(z);
+    float amplitude =sqrt(pow(x,2)+pow(y,2)+pow(z,2));
+    float crossProductVector = y-x;
+    float t = asin(crossProductVector/amplitude)*val; //was z before
+    float e = 0.5*acos(1-((pow(x,2)+pow(y,2))/((pow(x,2)+pow(y,2)+pow(z,2)))))*val;
+    pc.printf("x:%lf   y:%lf z:%lf\r\n",x,y,z);
+    pc.printf("angle: %lf autre: %lf \r\n",t,e);
+}
+/*
+void display(int number);
+
+int main() {
+    //https://developer.mbed.org/handbook/SerialPC
+    //https://developer.mbed.org/handbook/mbed-NXP-LPC1768-Getting-Started
+    //pc.printf("Echoes back to the screen anything you type\n");
+    int nombre = 0;
+    cs = 1;
+    spiTest.frequency(250000);
+    cs = 0;
+    spiTest.write(0x81);
+    //int t =  spiTest.write(nombre);
+    //pc.printf("Nombre = %X\r\n", t);
+    cs = 1;
     while(1) {
-
+        //uart.printf("%c",pc.getc());
+        wait(0.7);
+        nombre++;
+        display(nombre);
+        pc.printf("Nombre = %X\r\n", nombre);
+        if(nombre == 15){
+            return(0);
+        }
     }
 }
+void display(int number){
+    char c[4];
+    //Explication de la fonction sprintf https://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm
+    int test = sprintf(c,"%u", number);
+    //sprintf(c,"%ld", number);
+    cs = 0;
+    spiTest.write(0x76);
+    cs = 1;
+    pc.printf("c = %u\r\n", c[3] == '\0');
+    pc.printf("test = %d\r\n", test);
+    
+    switch(test) {
+       case 1:
+          cs = 0;
+          spiTest.write(0x7E);
+          cs = 1;
+          cs = 0;
+          spiTest.write(c[0]);
+          cs = 1;
+          break;
+       case 2:
+          cs = 0;
+          spiTest.write(0x7D|c[0]);
+          cs = 1;
+          cs = 0;
+          spiTest.write(0x7E|c[1]);
+          cs = 1;
+          break;
+        case 3:
+          break;
+    }
+    
+    int n = sizeof(c)/sizeof(c[0]);
+    pc.printf("n = %i\r\n", n);
+    /*for (int i=4; i>=0; i--){
+        cs = 0;
+        spiTest.write(c[i]);
+        cs = 1;
+    }*/
+    /*spiTest.write(c);
+    ;
+    spiTest.write(c[0]);
+}*/
 
-/*
+/* Ignore cette partie c'Est le lab
 int main() {
     //https://developer.mbed.org/handbook/SerialPC
     //https://developer.mbed.org/handbook/mbed-NXP-LPC1768-Getting-Started