app3

Dependencies:   mbed

Fork of Code_APP1 by Louis Beaulieu

Files at this revision

API Documentation at this revision

Comitter:
ericbisson
Date:
Tue Feb 14 04:11:07 2017 +0000
Parent:
8:6f2b7f9b0d0d
Commit message:
app3

Changed in this revision

Sensors/Accelerometer.cpp Show annotated file Show diff for this revision Revisions of this file
Sensors/Accelerometer.h Show annotated file Show diff for this revision Revisions of this file
Sensors/IO_p15.cpp Show annotated file Show diff for this revision Revisions of this file
Sensors/IO_p15.h Show annotated file Show diff for this revision Revisions of this file
config.h Show annotated file Show diff for this revision Revisions of this file
header.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
xbee.cpp Show annotated file Show diff for this revision Revisions of this file
xbee.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sensors/Accelerometer.cpp	Tue Feb 14 04:11:07 2017 +0000
@@ -0,0 +1,25 @@
+#include "Accelerometer.h"
+
+I2C accel(p28, p27);
+bool bIsInitialized = false;
+
+//--------------------------------------------------------------//
+// Fonction pour l'envoie de données de l'accéléromètre
+// Testé et fonctionnel
+//--------------------------------------------------------------//
+void func_accel(CArray* DATA_TO_SEND)
+{
+    if (!bIsInitialized)
+    {
+        char activation[2] = {0x2A, 0x03};
+        char fullScale[2] = {0x0E, 0x00};
+        accel.write(0x3A, activation, 2, true);
+        accel.write(0x3A, fullScale, 2, true);
+        bIsInitialized = true;
+    }
+    DATA_TO_SEND->_ptr = new char[3];
+    accel.write(0x3A, DATA_TO_SEND->_ptr, 1, true);
+    accel.read(0x3A, DATA_TO_SEND->_ptr, 3);
+    DATA_TO_SEND->size = 3;
+}
+//--------------------------------------------------------------//
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sensors/Accelerometer.h	Tue Feb 14 04:11:07 2017 +0000
@@ -0,0 +1,2 @@
+#include "xbee.h"
+void func_accel(CArray* DATA_TO_SEND);
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sensors/IO_p15.cpp	Tue Feb 14 04:11:07 2017 +0000
@@ -0,0 +1,20 @@
+#include "IO_p15.h"
+
+DigitalIn btn(p15);
+bool bLastState = false;
+
+//--------------------------------------------------------------//
+// Fonction pour l'envoie de données pour le bouton
+// Testé et fonctionnel
+//--------------------------------------------------------------//
+void func_button(CArray* DATA_TO_SEND)
+{
+    if (!bLastState == btn)
+    {
+        DATA_TO_SEND->_ptr = new char[1];
+        DATA_TO_SEND->_ptr[0] = btn;
+        DATA_TO_SEND->size = 1;
+    }
+    bLastState = btn;
+}
+//--------------------------------------------------------------//
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sensors/IO_p15.h	Tue Feb 14 04:11:07 2017 +0000
@@ -0,0 +1,2 @@
+#include "xbee.h"
+void func_button(CArray* DATA_TO_SEND);
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config.h	Tue Feb 14 04:11:07 2017 +0000
@@ -0,0 +1,2 @@
+const short PAN_ID = 0x0777;
+const float SENSOR_FREQUENCY_HZ = 10;
\ No newline at end of file
--- a/header.h	Tue Jan 17 02:09:22 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,40 +0,0 @@
-/********************************/
-// Configuration
-/********************************/
-#define IS_USING_SPI false
-#define MOVING_AVG_SIZE 10
-/********************************/
-#define PI 3.1416
-
-int* PINSEL0 = (int*)0x4002C000;
-int* U3FCR = (int*)0x4009C008;
-int* U3LCR = (int*)0x4009C00C;
-int* U3DLM = (int*)0x4009C004;
-int* U3DLL = (int*)0x4009C000;
-int* PCONP = (int*)0x400FC0C4;
-int* PCLKSEL1 = (int*)0x400FC1AC;
-int* U3TER = (int*)0x4009C030;
-int* U3THR = (int*)0x4009C000;
-int* U3LSR = (int*)0x4009C014;
-
-struct MovingAverage_t
-{
-    int buffer[MOVING_AVG_SIZE];
-    char cursor;
-    bool bFilled;
-};
-MovingAverage_t MovingAverage = {
-        {},
-        0,
-        false
-    };
-
-void write_to_7segment(char d1, char d2, char d3, char d4);
-int moyenne_mobile(int newData);
-void calculer_angle(char bufferAngle[], int accZ);
-void change_dots(char dot);
-void chip_select(bool bSet);
-void SetClockAndMode(int Speed, char Mode);
-void ResetCursor();
-unsigned char reverse(unsigned char b);
-void write_uart(unsigned char value);
\ No newline at end of file
--- a/main.cpp	Tue Jan 17 02:09:22 2017 +0000
+++ b/main.cpp	Tue Feb 14 04:11:07 2017 +0000
@@ -1,202 +1,98 @@
-#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 "Accelerometer.h"
+#include "IO_p15.h"
+#include "config.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);
+//--------------------------------------------------------------//
+// APP stuff : tableau de pointeur de fonctions
+//--------------------------------------------------------------//
+#define NB_FUNCTIONS 2
+void func_accel(CArray*);
+void func_button(CArray*);
+void (*functions[NB_FUNCTIONS])(CArray*) = {func_accel, func_button};
+//--------------------------------------------------------------//
 
 int main() {
-    int addrChip = 0x3A;
-    char buffer[3] = {0,0,0};
+    DigitalOut RESET(p8);
+    Serial XBee(p13, p14, 9600);
     
-    SetClockAndMode(250000, 0);
-    change_dots(0x02);
+    // 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_SEND;
     
-    //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);
+    // Addresse spéciale pour le coordinateur
+    for (int j = 0; j < 8; j++)
+        DATA_TO_SEND._64bit.bit[j] = 0;
+    DATA_TO_SEND._16bit.bit[0] = 0xFF;
+    DATA_TO_SEND._16bit.bit[1] = 0xFE;
+    
+    DATA_TO_SEND._FrameType = 0x10; // Transmit Request    
+    
+    // options pour l'envoie au coordinateur
+    DATA_TO_SEND.options = new char[2];
+    DATA_TO_SEND.options[0] = 0; // broadcast
+    DATA_TO_SEND.options[1] = 0; // other options
+    DATA_TO_SEND.opt_size = 2;
+    
+    bool IsInitialized = false;
+    char InitBytes = 0;
     
     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]);
+    {
+        if (IsInitialized)
+        {
+            for (char i = 0; i < NB_FUNCTIONS; i++)
+            {
+                DATA_TO_SEND._ptr = NULL;
+                
+                (*functions[i])(&DATA_TO_SEND);
+                
+                if (DATA_TO_SEND._ptr != NULL)
+                {
+                    led1 = !led1;
+                    send(&XBee, &DATA_TO_SEND);
+#ifdef __DEBUG__
+                    send(&pc, &DATA_TO_SEND); // debug only
+#endif
+                    delete DATA_TO_SEND._ptr;
+                }
+            }
+            wait_ms( 1000 / SENSOR_FREQUENCY_HZ);
+        }
+        if (XBee.readable())
+        {
+#ifdef __DEBUG__
+            pc.putc(XBee.getc());
+#endif
+            InitBytes++;
+            if (InitBytes == 6)
+            {
+                IsInitialized = true;
+                setPAN(&XBee, PAN_ID);
+#ifdef __DEBUG__
+                setPAN(&pc, PAN_ID);
+#endif
+            }
+        }
         
-        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
-    {
-        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;
-}
-
-// 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);
+#ifdef __DEBUG__
+        if (pc.readable())
+        {
+            XBee.putc(pc.getc());
+        }
+#endif
     }
-}
-
-// 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)
-        {
-            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
--- a/mbed.bld	Tue Jan 17 02:09:22 2017 +0000
+++ b/mbed.bld	Tue Feb 14 04:11:07 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/99b5ccf27215
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/176b8275d35d
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xbee.cpp	Tue Feb 14 04:11:07 2017 +0000
@@ -0,0 +1,200 @@
+#include "xbee.h"
+
+Timeout timeOut;
+PwmOut led_error(LED2);
+void LED_TurnOff()
+{
+    led_error = 0;
+}
+
+//--------------------------------------------------------------//
+// Fonction d'envoie de données pour ZigBee (Transmit request)
+// Testé et fonctionnel
+//--------------------------------------------------------------//
+void send(Serial* COM, CArray* message)
+{
+    short opt_size = 0;
+    COM->putc(0x7E); // Start delimiter
+    COM->putc(0x00); // length MSB is always 0
+    
+    if (message->_FrameType == 0x08) // Les commandes AT ne reçoivent pas d'addresses
+    {
+        COM->putc(6);
+    }
+    else
+    {
+        COM->putc(message->size + 12 + message->opt_size);
+    }
+
+    COM->putc(message->_FrameType);
+    COM->putc(0x01); // Frame ID
+    
+    if (message->_FrameType != 0x08) // Les commandes AT ne reçoivent pas d'addresses
+    {
+        COM->putc(message->_64bit.bit[0]);
+        COM->putc(message->_64bit.bit[1]);
+        COM->putc(message->_64bit.bit[2]);
+        COM->putc(message->_64bit.bit[3]);
+        COM->putc(message->_64bit.bit[4]);
+        COM->putc(message->_64bit.bit[5]);
+        COM->putc(message->_64bit.bit[6]);
+        COM->putc(message->_64bit.bit[7]);
+        COM->putc(message->_16bit.bit[0]);
+        COM->putc(message->_16bit.bit[1]);
+        
+        for (int i = 0; i < message->opt_size; i++)
+        {
+            opt_size += message->options[i];
+            COM->putc(message->options[i]);
+        }
+    }
+    
+    for (int j = 0; j < message->size; j++)
+    {
+        COM->putc(message->_ptr[j]);
+    }
+    COM->putc(calculate_checksum(message, 
+        message->_FrameType + 
+        0x01 +
+        opt_size +      
+        message->_16bit.bit[0] + 
+        message->_16bit.bit[1] + 
+        message->_64bit.bit[0] + 
+        message->_64bit.bit[1] + 
+        message->_64bit.bit[2] + 
+        message->_64bit.bit[3] + 
+        message->_64bit.bit[4] + 
+        message->_64bit.bit[5] + 
+        message->_64bit.bit[6] + 
+        message->_64bit.bit[7]
+    ));
+}
+//--------------------------------------------------------------//
+
+//--------------------------------------------------------------//
+// Fonction pour la réception de données (Packet Receive)
+// Testé et fonctionnel
+//--------------------------------------------------------------//
+void read(Serial* COM, CArray* returnValue)
+{
+    char BYTE_ID = 0;
+    char opts = 0;
+    bool bIsTrame = false;
+    while (true)
+    if (COM->readable())
+    {            
+        char value = COM->getc();
+        
+        if (bIsTrame)
+        {
+            switch (BYTE_ID)
+            {
+            case 1:
+                break;
+            case 2: // on ignore le 1, car c'est toujours 0x00
+                returnValue->size = value - 12;
+                break;
+            case 3:                  
+                if (value != 0x90)
+                    return;
+                returnValue->_FrameType = value;
+                break;
+            case 4:
+            case 5:
+            case 6:
+            case 7:
+            case 8:
+            case 9:
+            case 10:
+            case 11:
+                returnValue->_64bit.bit[BYTE_ID - 4] = value;
+                break;
+            case 12:
+            case 13: // <-- Addresse 16 bit de l'envoyeur ici
+                returnValue->_16bit.bit[BYTE_ID - 12] = value;
+                break;
+            case 14: // je gère pas les options pour là
+                opts = value;
+                break;
+            default:
+                if (BYTE_ID < 15 + returnValue->size)
+                {
+                    if (returnValue->_ptr == NULL)
+                        returnValue->_ptr = new char[returnValue->size];
+                        
+                    returnValue->_ptr[BYTE_ID - 15] = value;
+                    break;
+                }
+                // Finalement, le checksum. On vérifie que c'est bon
+                if (value != calculate_checksum(returnValue, 
+                        0x90 +
+                        opts + 
+                        returnValue->_16bit.bit[0] + 
+                        returnValue->_16bit.bit[1] + 
+                        returnValue->_64bit.bit[0] + 
+                        returnValue->_64bit.bit[1] + 
+                        returnValue->_64bit.bit[2] + 
+                        returnValue->_64bit.bit[3] + 
+                        returnValue->_64bit.bit[4] + 
+                        returnValue->_64bit.bit[5] + 
+                        returnValue->_64bit.bit[6] + 
+                        returnValue->_64bit.bit[7]
+                ))
+                {
+                    // Gestion d'erreur ici?
+                    led_error = 1;
+                    timeOut.attach(LED_TurnOff, 1);
+                }
+                return;
+            }
+        }
+        else if (value == 0x7E)
+            bIsTrame = true;
+        else
+            return;
+
+        //wait_ms(3); // petit sleep pour recevoir un nouveau byte
+        BYTE_ID++;
+    }
+}
+//--------------------------------------------------------------//
+
+//--------------------------------------------------------------//
+// Fonction de checksum pour le protocole ZigBee
+// Testé et fonctionnel
+//--------------------------------------------------------------//
+char calculate_checksum(CArray* message, short header_size)
+{
+    short sum = header_size;
+    if (message != NULL)
+    {
+        for (int i = 0; i < message->size; i++)
+        {
+            sum += message->_ptr[i];
+        }
+    }
+    return 0xFF - (sum & 0x00FF);
+}
+//--------------------------------------------------------------//
+
+void setPAN(Serial* COM, short pan_id)
+{
+    CArray DATA_TO_SEND;
+    for (int j = 0; j < 8; j++) // Je force tout à 0 pour les addresses
+        DATA_TO_SEND._64bit.bit[j] = 0;
+    DATA_TO_SEND._16bit.bit[0] = 0;
+    DATA_TO_SEND._16bit.bit[1] = 0;
+    DATA_TO_SEND.opt_size = 0;
+    
+    DATA_TO_SEND._FrameType = 0x08; // AT Command
+    DATA_TO_SEND.size = 4;
+    DATA_TO_SEND._ptr = new char[4];
+    DATA_TO_SEND._ptr[0] = 'I';
+    DATA_TO_SEND._ptr[1] = 'D';
+    DATA_TO_SEND._ptr[2] = pan_id >> 8;
+    DATA_TO_SEND._ptr[3] = pan_id & 0x00FF;
+    
+    send(COM, &DATA_TO_SEND);
+    
+    delete DATA_TO_SEND._ptr;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xbee.h	Tue Feb 14 04:11:07 2017 +0000
@@ -0,0 +1,30 @@
+#include "mbed.h"
+
+#ifndef XBEE_H
+#define XBEE_H
+
+struct Addresse_64Bit {
+    char bit[8];
+};
+
+struct Addresse_16Bit {
+    char bit[2];
+};
+
+struct CArray {
+    char* _ptr;
+    short size;
+    char _FrameType;
+    Addresse_64Bit _64bit;
+    Addresse_16Bit _16bit;
+    char* options;
+    char opt_size;
+};
+
+char calculate_checksum(CArray* message, short header_size);
+void send(Serial* COM, CArray* message);
+void read(Serial* COM, CArray* returnValue);
+void read(Serial* COM, CArray* returnValue);
+void setPAN(Serial* COM, short pan_id);
+
+#endif // XBEE_H
\ No newline at end of file