Leon Wehmeier / Mbed OS fiasco_max32630

Dependencies:   SoftSerial MAX14690 Buffer

Fork of rtos_threading_with_callback by mbed_example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers linkLayer.cpp Source File

linkLayer.cpp

00001 #include "linkLayer.h"
00002 #include "global.h"
00003 
00004 void LinkLayerEncoder::setPacket(linkPacket* p)
00005 {
00006     encoderIndex = 0;
00007     lp = p;
00008     oneCntr = 0;
00009     hasPacket = 1;
00010     preambleBits=8;
00011     frameTypeBits=8;
00012 }
00013 bool LinkLayerEncoder::getNext()
00014 {
00015     if(!hasPacket)//no active packet to send, get next
00016         setPacket(txQueue.getNext());//rtos semaphore; wait until new data available
00017     if(preambleBits-->0)//send preamble
00018     {
00019         return preambleBits<7;//send 1111 1110
00020     }
00021     if(frameTypeBits-->0)//send frametype
00022     {
00023         unsigned bitIndex = 7-frameTypeBits;
00024         
00025         bool real = (bool)(lp->frameType&(1<<bitIndex));
00026         bool returnVal;
00027         if(real)
00028         {
00029             if((oneCntr ++)<6)
00030                         returnVal=1;
00031             else
00032             {   
00033                 returnVal=0; // insert stuffing zero
00034                 frameTypeBits++;//need to transmit one bit next 
00035                 oneCntr=0;
00036             }
00037         }
00038         else
00039         {
00040             if(oneCntr==6)
00041             {
00042                 returnVal=0; // insert stuffing zero
00043                 frameTypeBits++;//need to transmit one bit next 
00044                 oneCntr=0;
00045             }
00046             else
00047             {
00048                 oneCntr=0;
00049                 returnVal=0;
00050             }
00051         }
00052         return returnVal;
00053     }
00054     unsigned byteIndex = encoderIndex / 8;
00055     unsigned bitIndex = encoderIndex % 8;
00056     encoderIndex++;
00057     
00058     bool real = (bool)(lp->payload[byteIndex]&(1<<bitIndex));
00059     bool returnVal;
00060     if(real)
00061     {
00062         if((oneCntr ++)<6)
00063                     returnVal=1;
00064         else
00065         {   
00066             returnVal=0; // insert stuffing zero
00067             encoderIndex--;//need to transmit one bit next 
00068             oneCntr=0;
00069         }
00070     }
00071     else
00072     {
00073         if(oneCntr==6)
00074         {
00075             returnVal=0; // insert stuffing zero
00076             encoderIndex--;//need to transmit one bit next 
00077             oneCntr=0;
00078         }
00079         else
00080         {
00081             oneCntr=0;
00082             returnVal=0;
00083         }
00084     }
00085     
00086     
00087     byteIndex = encoderIndex / 8;
00088     if(byteIndex >= lp->payloadSz)
00089     {
00090         hasPacket=0;
00091         delete lp->payload;
00092         delete lp;
00093     }
00094 
00095     return returnVal;
00096 }