Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Code_APP1 by
Diff: main.cpp
- Revision:
- 9:dc81d8ee4a97
- Parent:
- 8:6f2b7f9b0d0d
diff -r 6f2b7f9b0d0d -r dc81d8ee4a97 main.cpp
--- 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
