app3

Dependencies:   EthernetInterface WebSocketClient mbed-rtos mbed

Fork of Code_APP1 by Louis Beaulieu

Revision:
5:1a60144f7163
Parent:
4:ffd7cef72270
Child:
6:2ec2a74c807f
diff -r ffd7cef72270 -r 1a60144f7163 main.cpp
--- a/main.cpp	Mon Jan 16 19:19:27 2017 +0000
+++ b/main.cpp	Mon Jan 16 20:52:12 2017 +0000
@@ -1,4 +1,5 @@
 #include "mbed.h"
+#include "header.h"
 
 #define PI 3.1416
 
@@ -7,13 +8,10 @@
 DigitalOut cs(p14);
 I2C i2c(p28, p27);
 PwmOut led1(LED1);
-int addrChip = 0x3A;
-char buffer[3];
 
 void calculer_angle(char bufferAngle[], int accZ)
 {
-    float angle = abs(cos(static_cast<float>(accZ*90/64)*PI/180)*90);
-    pc.printf("%f\n", angle);
+    float angle = abs(cos(static_cast<float>(moyenne_mobile(accZ)*90/64)*PI/180)*90);
     int angleInt = (int)(angle*100);
     snprintf(bufferAngle, 5, "%d", angleInt);
     
@@ -23,44 +21,139 @@
         bufferAngle[2] = bufferAngle[1];
         bufferAngle[1] = bufferAngle[0];
         bufferAngle[0] = '0';
-    }    
-    pc.printf("%c", bufferAngle[0]);
-    pc.printf("%c", bufferAngle[1]);
-    pc.printf("%c", bufferAngle[2]);
-    pc.printf("%c", bufferAngle[3]);
-    pc.printf("\n");
-    
+    }
 }
 
 int main() {
-    spi.format(8,0);
-    spi.frequency(250000);
+    int addrChip = 0x3A;
+    char buffer[3] = {0,0,0};
+    
+    SetClockAndMode(250000, 0);
+    change_dots(0x02);
+    
+    //Activer l'accéléromètre pour lecture 8 bits
+    char activation[2] = {0x2A, 0x03};
+    char fullScale[2] = {0x0E, 0x00};
+    char resultat[5] = {0};
+    i2c.write(addrChip, activation, 2, true);
+    i2c.write(addrChip, fullScale, 2, true);
+    
     while(1)
-    {
-        //Activer l'accéléromètre pour lecture 8 bits
-        char activation[2] = {0x2A, 0x03};
-        char fullScale[2] = {0x0E, 0x00};
-        char resultat[5] = {0};
-        i2c.write(addrChip, activation, 2, true);
-        i2c.write(addrChip, fullScale, 2, true);
-        
+    {   
         //Aller lire les valeurs d'accélération
         buffer[0] = 0x01;
         i2c.write(addrChip, buffer, 1, true);
         i2c.read(addrChip, buffer, 3);
         calculer_angle(resultat, buffer[2]);
         
-        cs = 0;
-        wait_us(25);
+        chip_select(false);
+        write_to_7segment(resultat[0],resultat[1],resultat[2],resultat[3]);
+        chip_select(true);
+        wait(0.1);
+    }
+}
+
+void SetClockAndMode(int Speed, char Mode)
+{
+    if (IS_USING_SPI)
+    {
+        spi.format(8,Mode);
+        spi.frequency(Speed);
+    }
+    else
+    {
+        // TODO
+    }
+}
+
+// function to change displayed dots
+void change_dots(char dot)
+{
+    if (IS_USING_SPI)
+    {
         spi.write(0x77);
-        spi.write(0b00000010);
-        wait_us(100);
-        spi.write(resultat[0]);
-        spi.write(resultat[1]);
-        spi.write(resultat[2]);
-        spi.write(resultat[3]);
-        wait_us(25);
-        cs = 1;
-        wait(0.25);
+        spi.write(dot);
+    }
+    else
+    {
+        // TODO
+    }
+}
+
+void ResetCursor()
+{
+    if (IS_USING_SPI)
+    {
+        spi.write(0x79);
+        spi.write(0);
+    }
+    else
+    {
+        // TODO
     }
 }
+
+// function used to write numbers to all four digits
+void write_to_7segment(char d1, char d2, char d3, char d4)
+{
+    ResetCursor();
+    if (IS_USING_SPI)
+    {
+        spi.write(d1);
+        spi.write(d2);
+        spi.write(d3);
+        spi.write(d4);
+    }
+    else
+    {
+        // TODO
+    }
+}
+
+void chip_select(bool bSet)
+{
+    if (IS_USING_SPI)
+    {
+        if (!bSet)
+        {
+            wait_us(25);
+        }
+        cs = bSet;
+        if (bSet)
+        {
+            wait_us(25);
+        }
+    }
+}
+
+// function used to calculate and return the new value of a moving average
+int moyenne_mobile(int newData)
+{
+    int sum = 0;
+    MovingAverage.buffer[MovingAverage.cursor] = newData;
+    MovingAverage.cursor++;
+    if (MovingAverage.cursor >= MOVING_AVG_SIZE)
+    {
+        MovingAverage.cursor = 0;
+        MovingAverage.bFilled = true;
+    }
+    
+    if (MovingAverage.bFilled)
+    {
+        for (int i = 0; i < MOVING_AVG_SIZE; i++)
+        {
+            sum += MovingAverage.buffer[i];
+        }
+        sum = sum / MOVING_AVG_SIZE;
+    }
+    else
+    {
+        for (int i = 0; i < MovingAverage.cursor; i++)
+        {
+            sum += MovingAverage.buffer[i];
+        }
+        sum = sum / MovingAverage.cursor;
+    }
+    
+    return sum;
+}
\ No newline at end of file