APP3 / Mbed 2 deprecated APP4

Dependencies:   mbed mbed-rtos

Revision:
2:1665cd4c922c
Parent:
1:8b93b2102ac5
Child:
3:7b1110501ef9
diff -r 8b93b2102ac5 -r 1665cd4c922c main.cpp
--- a/main.cpp	Sat Mar 04 18:40:17 2017 +0000
+++ b/main.cpp	Mon Mar 06 08:00:37 2017 +0000
@@ -1,8 +1,81 @@
 #include "mbed.h"
-#define clock_max 96000000
+#include "rtos.h"
+#define CLOCK_MAX 96000000
+
 
 DigitalOut myled(LED1);
 
+//Mail<char (*) [256], 16> messageBox;
+Mail<char, 92 * 4> trameBox;
+
+bool ManchesterDecodeBit(bool* data, bool* out)
+{
+    if (data[0] == 0 && data[1] == 1)
+    {
+        out[0] = 0;
+    }
+    else if (data[0] == 1 &&  data[1] == 0)
+    {
+        out[0] = 1;
+    }
+    else
+    {
+        return false;
+    }
+    return true;
+}
+
+bool ManchesterDecodeByte(bool* data, char* out)
+{
+    bool Valid = true;
+    bool tmp = true;
+    for (int i = 0; i < 8; i++)
+    {
+        Valid = ManchesterDecodeBit(data, &tmp);
+        data += 2;
+        if (tmp)
+        {
+            *out = *out | (0x1 << (7-i));
+        }
+        else
+        {
+            *out = *out & ~(0x1 << (7-i));
+        }
+        if (!Valid)
+        {
+            return false;
+        }
+    }
+    return true;
+}
+
+void ManchesterEncode(bool data, bool* out)
+{
+    if (data == 0)
+    {
+        out[0] = 0;
+        out[1] = 1;
+    }
+    else
+    {
+        out[0] = 1;
+        out[1] = 0;
+    }
+}
+
+void ManchesterEncode(char data, bool* out)
+{
+    for (int i = 0; i < 8; i++)
+    {
+        ManchesterEncode((bool) ((data >> (7 - i)) & 1), &(out[2*i]));
+    }
+}
+
+void CreateAndSendTrame()
+{
+    
+}
+
 extern "C" void TIMER2_IRQHandler (void)
 {
 if((LPC_TIM2->IR & 0x01) == 0x01)   // if MR0 interrupt, proceed
@@ -16,7 +89,7 @@
     LPC_PINCON->PINSEL0 |=3<<12;            //P0.6 = MAT2.0  // p8
     LPC_SC->PCLKSEL1 |=1<<12;               //pclk timer2 = cclk 
     LPC_SC->PCONP |=1<<22;                  //timer2 power on
-    LPC_TIM2->MR0 = clock_max / 2;          // 1 sec period
+    LPC_TIM2->MR0 = CLOCK_MAX / 2;          // 1 sec period
     LPC_TIM2->MCR = 3;                      //interrupt and reset control
                                             //3 = Interrupt & reset timer2 on match
                                             //1 = Interrupt only, no reset of timer0
@@ -24,10 +97,11 @@
     NVIC_EnableIRQ(TIMER2_IRQn);            //enable timer2 interrupt
     LPC_TIM2->TCR = 1;                      //enable Timer2
 }
-
-int mainRaph()
+void TestManchester();
+void mainRaph()
 {
     init_clk();
+    TestManchester();
 }
 
 void tick()
@@ -37,4 +111,148 @@
 int main() {
     Ticker ticker;
     ticker.attach(&tick, 0.5);
+    wait(2);
+    mainRaph();
 }
+
+
+
+//--------------------------------TEST---------------------------------------------------//
+
+void TestManchester()
+{
+    printf("TestManchester - Begin\r\n");
+
+    // ----------------------------ENCODE-----------------------------------------------//
+    bool Valid = true;
+    bool boolData0 = (char) 0b0;
+    bool boolExpected0[] = {0,1};
+    bool boolAns0[2];
+    
+    ManchesterEncode(boolData0, (bool *) boolAns0);
+    
+    int i = 0;
+    Valid = true;
+    while (Valid && i < 2)
+    {
+        Valid = !(boolExpected0[i] ^ boolAns0[i]);
+        i++;
+    }
+    
+    if (!Valid)
+    {
+        printf("TestManchester - encode - bool0 - Failed\r\n");
+        printf("Expected: [%d, %d]. Received: [%d, %d].\r\n\n", boolExpected0[0], boolExpected0[1], boolAns0[0], boolAns0[1]);
+    }
+        
+    bool boolData1 = (char) 0b1;
+    bool boolExpected1[] = {1,0};
+    bool boolAns1[2];
+    ManchesterEncode(boolData1, (bool*) boolAns1);
+    
+    i = 0;
+    Valid = true;
+    while (Valid && i < 2)
+    {
+        Valid = !(boolExpected1[i] ^ boolAns1[i]);
+        i++;
+    }
+    if (!Valid)
+    {
+        printf("TestManchester - encode - boo11 - Failed\r\n");
+        printf("Expected: [%d, %d]. Received: [%d, %d].\r\n\n", boolExpected1[0], boolExpected1[1], boolAns1[0], boolAns1[1]);
+    }
+    
+    char charData = (char) 0b01010101;
+    bool charExpected[] = {0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0};
+    bool charAns[16];
+    ManchesterEncode(charData, (bool*) charAns);
+    
+    i = 0;
+    Valid = true;
+    while (Valid && i < 16)
+    {
+        Valid = !(charExpected[i] ^ charAns[i]);
+        i++;
+    }
+    if (!Valid)
+    {
+        printf("TestManchester - encode - char - Failed\r\n");
+        printf("Expected: [%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d]\r\nReceived: [%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d].\r\n\n", 
+        charExpected[0], charExpected[1], charExpected[2], charExpected[3],
+        charExpected[4], charExpected[5], charExpected[6], charExpected[7],
+        charExpected[8], charExpected[9], charExpected[10], charExpected[11],
+        charExpected[12], charExpected[13], charExpected[14], charExpected[15], 
+        charAns[0], charAns[1], charAns[2], charAns[3],
+        charAns[4], charAns[5], charAns[6], charAns[7],
+        charAns[8], charAns[9], charAns[10], charAns[11],
+        charAns[12], charAns[13], charAns[14], charAns[15]);
+    }
+    
+    // ----------------------------DECODE-----------------------------------------------//
+    Valid = true;
+    bool boolDecodeData0[] = {0,1};
+    bool boolDecodeExpected0 = 0b0;
+    bool boolDecodeAns0;
+    bool Success = true;
+    Success = ManchesterDecodeBit((bool *)boolDecodeData0, &boolDecodeAns0);
+    
+    if (!Success)
+    {
+        printf("TestManchester - decode - bool0 - Invalid format\r\n");
+    }
+    
+    if (boolDecodeExpected0 != boolDecodeAns0)
+    {
+        printf("TestManchester - decode - bool0 - Failed\r\n");
+        printf("Expected: %d. Received: %d.\r\n\n", boolDecodeExpected0,  boolDecodeAns0);
+    } 
+   
+    bool boolDecodeData1[] = {1,0};
+    bool boolDecodeExpected1 = (char) 0b1;
+    bool boolDecodeAns1;
+    Success = ManchesterDecodeBit((bool *)boolDecodeData1, &boolDecodeAns1);
+    
+    if (!Success)
+    {
+        printf("TestManchester - decode - bool1 - Invalid format\r\n");
+    }
+    if (boolDecodeExpected1 != boolDecodeAns1)
+    {
+        printf("TestManchester - decode - boo11 - Failed\r\n");
+        printf("Expected: %d. Received: %d\r\n\n", boolDecodeExpected1, boolDecodeAns1);
+    }
+    
+    bool boolDataInv0[] = {0,0};
+    bool boolDataInv1[] = {0,0};
+    bool boolAnsInv;
+    Success = ManchesterDecodeBit((bool *)boolDataInv0, (bool*) boolAnsInv);
+    if (Success)
+    {
+        printf("TestManchester - decode - boolInv0 - Invalid format not caught\r\n");
+    }
+    Success = ManchesterDecodeBit((bool *)boolDataInv1, (bool*) boolAnsInv);
+    if (Success)
+    {
+        printf("TestManchester - decode - boolInv1 - Invalid format not caught\r\n");
+    }
+    
+    
+    bool charEncodeData[] = {0,1,0,1,1,0,0,1,1,0,0,1,1,0,0,1};
+    char charEncodeExpected = 42;
+    char charEncodeAns;
+    Valid = ManchesterDecodeByte(charEncodeData, &charEncodeAns);
+    
+    if (!Valid)
+    {
+        printf("Function detected error in format\r\n");   
+    }
+    else if (charEncodeExpected != charEncodeAns)
+    {
+        printf("TestManchester - decode - char - Failed\r\n");
+        printf("Expected: %d\t\tReceived: %d\r\n", 
+        charEncodeExpected, charEncodeAns);
+    }
+    
+    printf("TestManchester - End\r\n");
+}
\ No newline at end of file