pre commentaire

Dependencies:   EthernetInterface WebSocketClient mbed-rtos mbed

Fork of Code_APP3_C by Éric Bisson

Revision:
9:400cfcf4b06e
Parent:
8:6f2b7f9b0d0d
Child:
10:565271e4d52a
--- a/main.cpp	Tue Jan 17 02:09:22 2017 +0000
+++ b/main.cpp	Tue Feb 14 04:08:58 2017 +0000
@@ -1,202 +1,126 @@
-#include "mbed.h"
-#include "header.h"
-
-Serial pc(USBTX, USBRX);
-SPI spi(p11, p12, p13);
-DigitalOut cs(p14);
-I2C i2c(p28, p27);
-PwmOut led1(LED1);
+#include "xbee.h"
+#include "config.h"
+#include "EthernetInterface.h"
+#include "Websocket.h"
 
-void calculer_angle(char bufferAngle[], int accZ)
-{
-    float angle = abs(cos(static_cast<float>(moyenne_mobile(accZ)*90/64)*PI/180)*90);
-    int angleInt = (int)(angle*100);
-    
-    // reset buffer
-    bufferAngle[0] = '0';
-    bufferAngle[1] = '0';
-    bufferAngle[2] = '0';
-    bufferAngle[3] = '0';
-    
-    signed char pos = 3;
-    while (angleInt > 1 && pos >= 0)
-    {
-        bufferAngle[pos] = angleInt % 10;
-        angleInt = angleInt / 10;
-        pos--;
-    }
-}
-
-void uart_init()
-{
-    pc.printf("uart before\n");
-    
-    *PCONP   |=  0x02000000;            // Turn ON PCUART3
-    *PCLKSEL1 |= 0x00040000;            // Set PCLK_UART3 to 01 (CLK / 1)
-    *PINSEL0 &= ~0x00000003;            // Turn Off TxD3 P0.0
-    *PINSEL0 |=  0x00000002;            // Enable TxD3 P0.0
-    *U3FCR   |=  0x00000007;            // Reset Tx, Reset Rx, Enable FIFO
-    *U3LCR   |=  0x00000083;            // Enable DLAB, 8-bit char length
-    *U3DLM   |=  0x00000000;            // DLMSB ; Baud rate
-    *U3DLL   |=  0x00000060;            // DLLSB ; Baud rate
-    *U3LCR   &= ~0x00000080;            // Turn off DLAB
-    
-    pc.printf("uart init()\n");
-}
+//#define __DEBUG__
+//#ifdef __DEBUG__
+Serial pc(USBTX, USBRX, 9600);
+//#endif
+PwmOut led1(LED1);
+PwmOut led4(LED4);
 
 int main() {
-    int addrChip = 0x3A;
-    char buffer[3] = {0,0,0};
     
-    SetClockAndMode(250000, 0);
-    change_dots(0x02);
+    // Setup du port ethernet
+    EthernetInterface eth;
+    eth.init(mbedIp, mbedMask, mbedGateway);
+    eth.connect();
+    
+    DigitalOut RESET(p8);
+    Serial XBee(p13, p14, 9600);
     
-    //Activer l'accéléromètre pour lecture 8 bits
-    char activation[2] = {0x2A, 0x03};
-    char fullScale[2] = {0x0E, 0x00};
-    char resultat[4] = {'0','0','0','0'};
-    i2c.write(addrChip, activation, 2, true);
-    i2c.write(addrChip, fullScale, 2, true);
+    // Selon le lab, reset le Xbee
+    RESET = 0;
+    wait_ms(400);
+    RESET = 1;
+
+#ifdef __DEBUG__
+    pc.format(8, SerialBase::None, 1);
+#endif
+    XBee.format(8, SerialBase::None, 1);
+    
+    CArray DATA_TO_READ;
+    
+    bool IsInitialized = false;
+    bool bAddressSet = false;
+    char InitBytes = 0;
+    
+    Websocket ws(WEBSOCKET_URL);
+    ws.connect();
+    wait(1);
     
     while(1)
-    {   
-        //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]);
-        
-        chip_select(false);
-        write_to_7segment(resultat[0],resultat[1],resultat[2],resultat[3]);
-        chip_select(true);
-        wait(0.1);
-    }
-}
+    {
+#ifdef __DEBUG__
+        if (pc.readable())
+        {
+            XBee.putc(pc.getc());
+        }
+#endif
+    
+        if (IsInitialized)
+        {
+            DATA_TO_READ._ptr = NULL;
+#ifdef __DEBUG__
+            if (XBee.readable())
+            {
+                pc.putc(XBee.getc());
+            }
+#endif
+            read(&XBee, &DATA_TO_READ);
 
-void SetClockAndMode(int Speed, char Mode)
-{
-    if (IS_USING_SPI)
-    {
-        spi.format(8,Mode);
-        spi.frequency(Speed);
-    }
-    else
-    {
-        uart_init();
-    }
-}
-
-void write_uart(unsigned char value)
-{   pc.printf("%d - ", reverse(value));
-    *U3THR = reverse(value); // Data to send ; LSB first
-    *U3TER |= 0x80;          // Enable TXEn
-    //*U3TER &= ~0x80;         // Disable TXEn
-}
-
-// inverse bit order
-unsigned char reverse(unsigned char b) 
-{
-   b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
-   b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
-   b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
-   return b;
-}
+            if (DATA_TO_READ._ptr != NULL)
+            {
+                led1 = !led1;
+                
+                if (!bAddressSet)
+                {
+                    bAddressSet = true;
 
-// function to change displayed dots
-void change_dots(char dot)
-{
-    if (IS_USING_SPI)
-    {
-        spi.write(0x77);
-        spi.write(dot);
-    }
-    else
-    {
-        write_uart(0x77);
-        write_uart(dot);
-    }
-}
-
-void ResetCursor()
-{
-    if (IS_USING_SPI)
-    {
-        spi.write(0x79);
-        spi.write(0);
-    }
-    else
-    {
-        write_uart(0x79);
-        write_uart(0);
-    }
-}
-
-// 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
-    {
-        write_uart(d1);
-        write_uart(d2);
-        write_uart(d3);
-        write_uart(d4);
-        pc.printf("\n");
-    }
-}
-
-void chip_select(bool bSet)
-{
-    if (IS_USING_SPI)
-    {
-        if (!bSet)
+                    CArray DATA_TO_SEND;
+                    
+                    DATA_TO_SEND._64bit = DATA_TO_READ._64bit; // <-- Je récupère l'addresse au premier paquet. Inutile de faire plus pour l'app ...
+                    DATA_TO_SEND._16bit = DATA_TO_READ._16bit;
+                    
+                    DATA_TO_SEND._FrameType = 0x17; // Remote AT Request
+                    
+                    // options pour l'envoie au coordinateur
+                    DATA_TO_SEND.options = new char[1];
+                    DATA_TO_SEND.options[0] = 0x02;
+                    DATA_TO_SEND.opt_size = 1;
+                    
+                    DATA_TO_SEND._ptr = new char[3];
+                    DATA_TO_SEND._ptr[0] = 'L';
+                    DATA_TO_SEND._ptr[1] = 'T';
+                    DATA_TO_SEND._ptr[2] = 100; // x10ms donc 1000ms donc 1hz
+                    DATA_TO_SEND.size = 3;
+                    
+                    send(&XBee, &DATA_TO_SEND);
+#ifdef __DEBUG__
+                    send(&pc, &DATA_TO_SEND);
+#endif
+                    
+                    delete DATA_TO_SEND._ptr;
+                    delete DATA_TO_SEND.options;
+                }
+                
+#ifdef __DEBUG__
+                for (int i = 0; i < DATA_TO_READ.size; i++)
+                {
+                    pc.putc(DATA_TO_READ._ptr[i]); // debug ; send to pc
+                }
+#endif
+                ws.send(DATA_TO_READ._ptr);
+                wait_ms(5);
+                char* result;
+                ws.read(result);
+                
+                pc.printf(result);
+                
+                delete DATA_TO_READ._ptr;
+            }
+        }
+        else if (XBee.readable())
         {
-            wait_us(25);
-        }
-        cs = bSet;
-        if (bSet)
-        {
-            wait_us(25);
+#ifdef __DEBUG__
+            pc.putc(XBee.getc());
+#endif
+            InitBytes++;
+            if (InitBytes == 6)
+            {
+                IsInitialized = true;
+            }
         }
     }
-}
-
-// 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