TP3 - Réseau terrain de supervision : CAN

Dependencies:   mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
candybibby
Date:
Fri Jun 02 16:20:34 2017 +0000
Parent:
2:468ee5a130a1
Commit message:
Commit final

Changed in this revision

mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
speaker.cpp Show annotated file Show diff for this revision Revisions of this file
test_main.cpp Show diff for this revision Revisions of this file
diff -r 468ee5a130a1 -r 478e83703442 mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Fri Jun 02 16:20:34 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed-rtos/#58563e6cba1e
diff -r 468ee5a130a1 -r 478e83703442 speaker.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/speaker.cpp	Fri Jun 02 16:20:34 2017 +0000
@@ -0,0 +1,105 @@
+#include "mbed.h"
+#include "rtos.h"
+ 
+#define SIG_RX_CAN  0x01
+ 
+ 
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+
+ 
+Thread threadA;
+Thread threadB;
+ 
+ 
+CAN CanPort(p30, p29);
+CANMessage  MessageRx; 
+CANMessage  MessageTx;
+
+PwmOut Speaker(p21);
+ 
+unsigned int    Id;
+ 
+void canReader(void)
+{ 
+    if (CanPort.read(MessageRx))
+    {
+        led1 = !led1;
+            threadA.signal_set(SIG_RX_CAN);
+    }  
+} 
+ 
+ 
+ // THREAD DE RECEPTION
+void thA() 
+{
+    while(true)
+    {
+        // le thread attend un signe du canReader()
+        Thread::signal_wait(SIG_RX_CAN);    
+        led2 = !led2;
+
+        // si l'ID correspond à notre protocole applicatif ( 0X111 pour le speaker)
+        // alors on traite la trame, sinon on ne fait rien
+        if (MessageRx.id == 0x111){
+
+            // la lecture du badge est autorisée (data reçue : 0x01)
+            if (MessageRx.data[0] == 0x01){
+                led1 = !led1;
+                // on fait 2 bips
+                Speaker.write(0.3);    
+                wait(0.1);
+                Speaker.write(0.3);    
+                wait(0.1);
+
+            }
+            else if (MessageRx.data[0] == 0x00){
+                // dans ce cas là, l'accès est refusée (data reçue : 0x00)
+                led1 =  !led1;
+                // on fait 6 petits bips 
+                for (int i = 0; i<6 ; i++){
+                    Speaker.write(0.1);    
+                    wait(0.1);
+                }
+            }
+        }
+            
+    }
+}
+ 
+// THREAD D'ENVOI
+/*void thB() 
+{
+    while (true) 
+    {
+    led1 = !led1;   
+    printf("TIC3s\n");
+    MessageTx.id=Id;
+    CanPort.write(MessageTx);
+    
+    if (Id < 0x3FF) Id++;
+    else Id = 0x000;
+    
+    wait(3);   
+    }
+}
+ */
+ 
+ 
+int main() 
+{
+    CanPort.frequency(20000);
+    
+    CanPort.attach(canReader,CAN::RxIrq);
+        
+    threadA.start(thA);
+    
+    led1 = 0;
+    printf("Start OK\n");
+    
+    while (true) 
+        {
+    
+        }
+}
+ 
diff -r 468ee5a130a1 -r 478e83703442 test_main.cpp
--- a/test_main.cpp	Tue May 23 08:09:24 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,94 +0,0 @@
-#include "mbed.h"
-
-
-#define LA_440      2273
-
-DigitalOut myled(LED1);
-
-PwmOut Speaker(p21); 
-
- 
-CAN CanPort(p30, p29);
- 
-CANMessage  MessageRx; 
-CANMessage  MessageTx;
-
-
-unsigned int    Id;
-
-
-
-void canReader(void)
-{ 
-printf("Coucou\n");
-    if (CanPort.read(MessageRx))
-    {
-            myled = !myled;
-            if ( MessageRx.id == 0x111)
-            {
-                switch (MessageRx.data[0]){
-                case 0x01 :
-                    Speaker.write(0.2);    
-                    wait(0.1);
-                    Speaker.write(0.2);    
-                    wait(0.1); 
-                    break;
-                     
-                case 0x11 :
-                    Speaker.write(1);    
-                    wait(0.1); 
-                    break;
-                    
-                default :
-                    Speaker.write(0.1);    
-                    wait(0.1);
-                    Speaker.write(0.1);    
-                    wait(0.1); 
-                    Speaker.write(0.1);    
-                    wait(0.1); 
-                    Speaker.write(0.1);    
-                    wait(0.1); 
-                    Speaker.write(0.1);    
-                    wait(0.1); 
-                    Speaker.write(0.1);    
-                    wait(0.1); 
-                
-                }
-                myled = !myled;
-            }    
-    } 
-} 
- 
- 
-
-
-int main() 
-{
-
-
-    CanPort.frequency(20000);
-    
-    MessageRx.len=1;
-
-    //MessageTx.format = CANStandard;
-    //MessageTx.format = CANExtended;
-    
-    //MessageTx.type = CANData;
-    //MessageTx.type = CANRemote;
-    
-    CanPort.attach(canReader,CAN::RxIrq);
-        
-    //threadA.start(thA);
-    //threadB.start(thB);
-    
-    myled = 0;
-    printf("Start OK\n");
-
-Speaker.period_us(LA_440);  
-Speaker.write(0);
-
-    while(1) 
-    {
-      
-    }
-}