Éric Bisson / Mbed 2 deprecated S5info_APP4

Dependencies:   mbed-rtos mbed CRC16

Fork of S5info_APP2 by Éric Bisson

Files at this revision

API Documentation at this revision

Comitter:
ericbisson
Date:
Mon Mar 06 22:22:02 2017 +0000
Parent:
7:5501dbea5650
Child:
9:01be68364986
Commit message:
lis le message

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
uart.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Mar 06 19:18:30 2017 +0000
+++ b/main.cpp	Mon Mar 06 22:22:02 2017 +0000
@@ -1,22 +1,14 @@
 #include "mbed.h"
 #include "rtos.h"
-#include <string>
 #include "bit.h"
-using std::string;
+#include "uart.h"
 
-#define PREAMBULE 0b01010101
-const char START =      0b01111110;
-const char END =        0b01111110;
-const char FLAGS =      0x00;
-const char MAX_LENGTH = 80;
-const float HALF_PERIOD = 0.0005; // secondes
+const float HALF_PERIOD = 0.05; // secondes
 
 Serial pc(USBTX, USBRX, 9600);
 Serial uart(p13, p14, 9600);
 DigitalOut myled1(LED1);
-DigitalOut myled2(LED2);
-InterruptIn read_pin(p14);
-Thread* thread;
+Thread ThreadLecture;
 
 bool bIsHalfPeriod = false;
   
@@ -30,24 +22,29 @@
 
 void p14_interrupt()
 {    
+    // On turn off les interrupts de lecture une fois qu'on a détecter un message
+    uart.attach(NULL, uart.RxIrq);
+
     // On envoie le signal au thread de lecture
-    thread->signal_set(1);
-    
-    // On turn off les interrupts de lecture une fois qu'on a détecter un message
-    read_pin.disable_irq();
+    ThreadLecture.signal_set(1);
 };
 
 void read()
 {
     while(true) 
     {
-        thread->signal_wait(1);
+        // Attente passive d'un message entrant
+        uart.attach(&p14_interrupt, uart.RxIrq);
+        ThreadLecture.signal_wait(1);
         
-        // TODO: mettre ici le code pour la lecture de trame
-        myled2 = !myled2;
+        // Lis le message. Retourne une exception si il y a une erreur
+        vector<char> message = uart_read(uart, pc);
         
-        // Une fois fini, on réactive l'interrupt de lecture
-        read_pin.enable_irq();
+        if (!message.empty())
+        {
+            // Affiche le message à l'écran
+            pc.printf(&message[0], message.size());
+        }
     }
 };
 
@@ -56,7 +53,7 @@
     LPC_SC->PCONP |= bit16;                             //Power Control for Peripherals register: power up RIT clock
     LPC_SC->PCLKSEL1 |= (bit26 && bit27);               //Peripheral clock selection: divide clock by 8 (run RIT clock by 12.5MHz)
     LPC_RIT->RICOUNTER = 0;                             //set counter to zero
-    LPC_RIT->RICOMPVAL = 1000000000 * HALF_PERIOD;       //interrupt tick every HALF_PERIOD
+    LPC_RIT->RICOMPVAL = 250000000 * HALF_PERIOD;       //interrupt tick every HALF_PERIOD
     LPC_RIT->RICTRL |= bit1;                            // clear timer when counter reaches value
     LPC_RIT->RICTRL |= bit3;                            // enable timer
      
@@ -67,16 +64,12 @@
 
 int main() {
     rit_init();
-    read_pin.fall(p14_interrupt);
-    read_pin.enable_irq();
     
-    Thread ThreadLecture; // Thread pour pas avoir à rien faire dans l'interrupt
-    thread = &ThreadLecture;
-    thread->start(read);
+    ThreadLecture.start(read);
     
     while(true) {
         // TODO: Mettre ici le code pour l'input et l'envoie de message
-        uart.putc(0);
+        uart.printf("12345");
         wait_ms(100);
     }
 };
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uart.h	Mon Mar 06 22:22:02 2017 +0000
@@ -0,0 +1,36 @@
+#include <vector>
+#include "mbed.h"
+using std::vector;
+
+#define RETURN_EMPTYVECTOR(x) return vector<x>();
+const char PREAMBULE =  0b01010101;
+const char START =      0b01111110;
+const char END =        0b01111110;
+const char FLAGS =      0x00;
+const char MAX_LENGTH = 80;
+
+vector<char> uart_read(Serial& COM, Serial& pc)
+{
+    vector<char> result;
+
+    while (COM.readable())
+    {
+        result.push_back(COM.getc());
+        wait_ms(8); // 9600 bits/secondes, donc ~1ms par bit
+    }
+    if (result.size() > 7)
+    {
+        if (result[0] != PREAMBULE || 
+            result[1] != START ||
+            result[2] != result.size() - 7 ||
+            result.back() != END)
+        {
+            RETURN_EMPTYVECTOR(char);
+        }
+        
+        result.push_back('\0'); // end of string pour pouvoir l'afficher
+        
+        return vector<char>(&result[4], &result[result.size()-4]);
+    }
+    RETURN_EMPTYVECTOR(char);
+}
\ No newline at end of file