APP4 S5

Dependencies:   mbed-rtos mbed

Revision:
8:a878763b0ae3
Parent:
7:d25bcde7dcf1
Child:
9:081324663b8c
--- a/radio.cpp	Wed Oct 11 21:54:27 2017 +0000
+++ b/radio.cpp	Thu Oct 12 20:14:21 2017 +0000
@@ -8,7 +8,7 @@
 
 #define MESSAGE_BUFFER_SIZE 16
 
-#define MANCHESTER_SPEED_OUT 50
+#define MANCHESTER_SPEED_OUT 10
     
 #define INPUT_RADIO p18
 #define OUTPUT_RADIO p6
@@ -27,10 +27,15 @@
 
 DigitalOut output(OUTPUT_RADIO);
 
+#ifdef LED
 DigitalOut in_debug_led(LED4);
 DigitalOut out_debug_led(LED3);
 
 DigitalOut frame_end_led(LED2);
+DigitalOut frame_in_end_led(LED1);
+#endif
+
+Serial PC(USBTX, USBRX);
 
 // API functions
 void init_radio_system()
@@ -42,25 +47,154 @@
 
 // Private functions
 
-void in_rise()
+typedef enum {
+    in_attente = 0,
+    in_preambule,
+    in_data,
+    in_idle
+} receive_state_t;
+
+
+
+DigitalOut led4(LED4);
+DigitalOut led3(LED3);
+DigitalOut led2(LED2);
+DigitalOut led1(LED1);
+
+byte current_state = in_attente;
+byte current_byte_progress = 0;
+
+
+void stop_frame(void const *n);
+
+RtosTimer ticker_watch(stop_frame, osTimerPeriodic, NULL);
+Thread thread;
+
+void stop_frame(void const *n)
 {
-    in_debug_led = 1;   
+    //frame_in_end_led = 0;
+    current_state = in_attente;
+    current_byte_progress = 0;
+    ticker_watch.stop();
+}   
+
+volatile int t = 0;
+volatile int c = 0;
+    
+void radio_in()
+{    
+    
+    static Timer timer;   
+    static int t_half = 0;
+    
+    static byte current_byte = 0;
+    
+    //in_debug_led = !in_debug_led;   
+    
+    
+    switch (current_state)
+    {
+        case in_attente:
+        {    
+            //led4 = 0;
+            led3 = 0;
+            led2 = 0;
+            led1 = 0;        
+            //frame_in_end_led = 0;
+            if (input == 1)
+            {
+                led1 = 1;
+                timer.start();
+                current_state = in_preambule;   
+                current_byte_progress = 1;
+                //frame_in_end_led = 1;
+            }
+            break;
+        }
+        case in_preambule:
+        {
+            led2 = 1;
+            current_byte_progress++;
+            t = timer.read_ms();           
+            timer.reset();
+            if (current_byte_progress > 7)
+            {             
+                t_half = t / 2;
+                ticker_watch.start(t + t_half);
+                current_byte_progress = 0;
+                current_state = in_data;    
+                c = t;
+                thread.signal_set(0x1);
+            }
+            break;
+        }
+        case in_data:
+        {        
+            led3 = 1;   
+            if(timer.read_ms() > t + t_half)
+            {
+                led4 = 1;
+                //frame_in_end_led = 0;
+                current_state = in_attente;
+                current_byte_progress = 0;
+                
+                timer.stop();      
+                timer.reset();      
+            }
+            else if (timer.read_ms() > t_half)
+            {               
+                current_byte = (!input << (7 - current_byte_progress)) | current_byte;
+                current_byte_progress++ ;
+                //PC.putc(input);
+                // Display data 
+                //frame_in_end_led = !input;
+                ticker_watch.start(t + t_half);
+                timer.reset();      
+                
+                if (current_byte_progress > 7)
+                {                    
+                    c = current_byte;
+                    thread.signal_set(0x1);
+                    current_byte = 0;
+                    current_byte_progress = 0;
+                }
+            }
+            else
+            {
+                //ticker_watch.start(t + t_half);                
+            }
+            break;
+        }
+        case in_idle:
+        {
+            led4 = 1;
+            //frame_in_end_led = 0;
+            current_state = in_attente;
+            current_byte_progress = 0;
+            break;
+        }
+    }
 }
 
-void in_fall()
+void thread_putc()
 {
-    in_debug_led = 0;
+    while(1)
+    {
+        Thread::signal_wait(0x1);
+        PC.printf("0x%x\n\r", c);    
+    }   
 }
 
+
 void setup_radio_in()
 {
     in_message_in = 0;
     in_message_out = 0;
     
-    in_debug_led = 0;
+    thread.start(callback(thread_putc));
     
-    input.rise(&in_rise);
-    input.fall(&in_fall);   
+    input.rise(&radio_in);
+    input.fall(&radio_in);       
 }
 
 
@@ -69,8 +203,8 @@
     out_message_in = 0;
     out_message_out = 0;
         
-    out_debug_led = 0;    
-    frame_end_led = 1;
+    //out_debug_led = 0;    
+    //frame_end_led = 1;
     
     //////////////////////////////////////////////////////
     // Creation d'un message et insertion dans le buffer
@@ -104,14 +238,15 @@
     length,
     data,
     crc,
-    end
+    end,
+    idle
 } out_state_t;
 
 void radio_out(void const *args)
 {
     static byte current_byte_progress = 0;    
     static byte current_byte = 0;    
-    static byte current_state = 0;    
+    static byte out_current_state = 0;    
     static bool IsBitTransition = false;
     static byte next_value = 0;
     
@@ -126,16 +261,16 @@
     
     #define CHECK_NEXT_STATE if (current_byte_progress > 7)     \
                              {                                  \
-                                    current_state++;            \
+                                    out_current_state++;            \
                                     current_byte_progress = 0;  \
                              }
     
-    out_debug_led = !out_debug_led;
+    //out_debug_led = !out_debug_led;
     
     if (!IsBitTransition)
     {
         // Dependant du state, on progresse dans l'envoi du message
-        switch (current_state) 
+        switch (out_current_state) 
         {
             case preambule: // preambule
             {
@@ -171,7 +306,7 @@
                     if (current_byte >= message->length)
                     {
                         current_byte = 0;
-                        current_state++;
+                        out_current_state++;
                     }                 
                 }
                 //CHECK_NEXT_STATE
@@ -189,7 +324,7 @@
                 CHECK_NEXT_STATE
                 break; 
             }
-            default:
+            case idle:
             {
                 //current_state = 0;
                 current_byte = 0;
@@ -198,18 +333,18 @@
             }                                         
         }
         
-        if (next_value != output)
+        if (next_value != output && out_current_state != idle)
         {
             output = !output;
         }
         
-        if (current_state > end)
+        if (out_current_state > end)
         {
-            frame_end_led = 0;
-            //current_state = preambule;
+            //frame_end_led = 0;
+            //out_current_state = preambule;
         }
     }
-    else
+    else if (out_current_state != idle)
     {
         output = !output;
     }