
// data available
if(usize>1)
{
  bufferPos=0
  while(bufferPos<usize)
  {
    // check for 0x55 0xBB - Command Init Secuence
    if(u[bufferPos]==187&&u[bufferPos-1]==85)
    {
      //remove garbage at the start of the buffer if there's any to free buffer for command
      if(u[bufferPos]!=1)
      {
        udelete bufferPos-1
      }
      //instruction is now aligned with buffer, because we deleted garbage before instrcution
      //get length after init sequence (check if there are more than to bytes in buffer)
      if(3<usize)
      {
        // check if serial buffer has reached the announced length
        ucopy payloadLength,2,2,0
        // we are only checking payload length so we have to skip first 3 bytes (init+payload length) (-1 because of < instead of <=)
        payloadLength+=3
        // payload length does also not contain crc, so we are adding another 2 bytes for crc
        payloadLength+=2
        if(payloadLength<usize)
        {
          // calculate crc
          crcrest 1,0xFFFF
          // u[2] contains payload legth at 3rd pos in buffer, we are calculating crc from 3rd pos with number of bytes from payload length
          //crcputu 3,u[2]
          // u[2] cotnains payload length, we are calculating a crc over the whole message, so we have to add 3 to the length from u[2]
          crcputu 0,payloadLength-1
          // get recived crc to be able to compare it
          ucopy recvCrc,payloadLength-1,2,0
          // compare crc with recived value
          if(crcval==recvCrc)
          {
            // crc is okay
            // here is the location where acual code should be
            // write command to variable strCommand
            ucopy strCommand.txt,4,payloadLength-5,0
            // write instruction to tInstuction (debug output, but used as variable here, ui elements will be disabled by default)
            spstr strCommand.txt,tInstruction.txt,"~",0

            if(tInstruction.txt=="entityUpd")
            {
              // command format: entityUpd,heading,navigation,[,type,internalName,iconId,iconColor,displayName,optionalValue]x6
              spstr strCommand.txt,tHeading.txt,"~",1

              // navigation icons
              spstr strCommand.txt,tId.txt,"~",2
              spstr tId.txt,tTmp.txt,"|",0
              if(tTmp.txt=="0")
              {
                vis bPrev,0
                tsw mSwipePrev,0
                tsw mSwipeUp,0
              }
              if(tTmp.txt=="1")
              {
                vis bPrev,1
                tsw mSwipePrev,1
                tsw mSwipeUp,0
                bPrev.txt=""
              }
              if(tTmp.txt=="2")
              {
                vis bPrev,1
                tsw mSwipePrev,0
                tsw mSwipeUp,1
                bPrev.txt=""
              }       
              spstr tId.txt,tTmp.txt,"|",1
              if(tTmp.txt=="0")
              {
                vis bNext,0
                tsw mSwipeNext,0
              }
              if(tTmp.txt=="1")
              {
                vis bNext,1
                tsw mSwipeNext,1
                bNext.txt=""
              }


              // get Type
              spstr strCommand.txt,type1.txt,"~",3
              // get internal name
              spstr strCommand.txt,entn1.txt,"~",4
              if(type1.txt=="delete"||type1.txt=="")
              {
                vis tEntity1,0
                vis bEntity1,0
              }else
              {
                // change icon
                spstr strCommand.txt,bEntity1.txt,"~",5
                vis bEntity1,1
                // change icon color
                spstr strCommand.txt,tTmp.txt,"~",6
                covx tTmp.txt,sys0,0,0
                bEntity1.pco=sys0
                // set name
                spstr strCommand.txt,tEntity1.txt,"~",7
                vis tEntity1,1
              }


              // get Type
              spstr strCommand.txt,type2.txt,"~",9
              // get internal name
              spstr strCommand.txt,entn2.txt,"~",10
              if(type2.txt=="delete"||type2.txt=="")
              {
                vis tEntity2,0
                vis bEntity2,0
              }else
              {
                // change icon
                spstr strCommand.txt,bEntity2.txt,"~",11
                vis bEntity2,1
                // change icon color
                spstr strCommand.txt,tTmp.txt,"~",12
                covx tTmp.txt,sys0,0,0
                bEntity2.pco=sys0
                // set name
                spstr strCommand.txt,tEntity2.txt,"~",13
                vis tEntity2,1
              }


              // get Type
              spstr strCommand.txt,type3.txt,"~",15
              // get internal name
              spstr strCommand.txt,entn3.txt,"~",16
              if(type3.txt=="delete"||type3.txt=="")
              {
                vis tEntity3,0
                vis bEntity3,0
              }else
              {
                // change icon
                spstr strCommand.txt,bEntity3.txt,"~",17
                vis bEntity3,1
                // change icon color
                spstr strCommand.txt,tTmp.txt,"~",18
                covx tTmp.txt,sys0,0,0
                bEntity3.pco=sys0
                // set name
                spstr strCommand.txt,tEntity3.txt,"~",19
                vis tEntity3,1
              }


              // get Type
              spstr strCommand.txt,type4.txt,"~",21
              // get internal name
              spstr strCommand.txt,entn4.txt,"~",22
              if(type4.txt=="delete"||type4.txt=="")
              {
                vis tEntity4,0
                vis bEntity4,0
              }else
              {
                // change icon
                spstr strCommand.txt,bEntity4.txt,"~",23
                vis bEntity4,1
                // change icon color
                spstr strCommand.txt,tTmp.txt,"~",24
                covx tTmp.txt,sys0,0,0
                bEntity4.pco=sys0
                // set name
                spstr strCommand.txt,tEntity4.txt,"~",25
                vis tEntity4,1
              }


              // get Type
              spstr strCommand.txt,type5.txt,"~",27
              // get internal name
              spstr strCommand.txt,entn5.txt,"~",28
              if(type5.txt=="delete"||type5.txt=="")
              {
                vis tEntity5,0
                vis bEntity5,0
              }else
              {
                // change icon
                spstr strCommand.txt,bEntity5.txt,"~",29
                vis bEntity5,1
                // change icon color
                spstr strCommand.txt,tTmp.txt,"~",30
                covx tTmp.txt,sys0,0,0
                bEntity5.pco=sys0
                // set name
                spstr strCommand.txt,tEntity5.txt,"~",31
                vis tEntity5,1
              }


              // get Type
              spstr strCommand.txt,type6.txt,"~",33
              // get internal name
              spstr strCommand.txt,entn6.txt,"~",34
              if(type6.txt=="delete"||type6.txt=="")
              {
                vis tEntity6,0
                vis bEntity6,0
              }else
              {
                // change icon
                spstr strCommand.txt,bEntity6.txt,"~",35
                vis bEntity6,1
                // change icon color
                spstr strCommand.txt,tTmp.txt,"~",36
                covx tTmp.txt,sys0,0,0
                bEntity6.pco=sys0
                // set name
                spstr strCommand.txt,tEntity6.txt,"~",37
                vis tEntity6,1
              }


            }

            if(tInstruction.txt=="pageType")
            {
              //command format pageType,specialPageName
              //write name of speical page to tId
              spstr strCommand.txt,tId.txt,"~",1
              //save second arg if there's one
              spstr strCommand.txt,tTmp.txt,"~",2
              //save third arg if there's one
              spstr strCommand.txt,pageIcons.tTmp2.txt,"~",3
              //we are going to exit this page with this command, so we have to clear the buffer, so we are not getting into a stupid loop ...
              udelete payloadLength-1
              bufferPos=0
              if(tId.txt=="pageStartup")
              {
                page pageStartup
              }
              if(tId.txt=="screensaver")
              {
                page screensaver
              }
              if(tId.txt=="cardEntities")
              {
                page cardEntities
              }
              if(tId.txt=="cardGrid")
              {
                page cardGrid
              }
              if(tId.txt=="popupLight")
              {
                pageIcons.tTmp1.txt=tTmp.txt
                page popupLight
              }
              if(tId.txt=="popupShutter")
              {
                pageIcons.tTmp1.txt=tTmp.txt
                page popupShutter
              }
              if(tId.txt=="popupNotify")
              {
                page popupNotify
              }
              if(tId.txt=="cardThermo")
              {
                page cardThermo
              }
              if(tId.txt=="cardMedia")
              {
                page cardMedia
              }
              if(tId.txt=="cardAlarm")
              {
                page cardAlarm
              }
              if(tId.txt=="cardQR")
              {
                page cardQR
              }
            }
            if(tInstruction.txt=="time")
            {
              // get set time to global variable
              spstr strCommand.txt,pageIcons.vaTime.txt,"~",1
            }
            if(tInstruction.txt=="date")
            {
              // get set date to global variable
              spstr strCommand.txt,pageIcons.vaDate.txt,"~",1
            }
            if(tInstruction.txt=="dimmode")
            {
              // get value
              spstr strCommand.txt,tTmp.txt,"~",1
              covx tTmp.txt,dimValue,0,0
            }
            if(tInstruction.txt=="timeout")
            {
              //set timeout to global var
              spstr strCommand.txt,tTmp.txt,"~",1
              covx tTmp.txt,sleepTimeout,0,0
            }
            // end of user code
            udelete payloadLength-1
            bufferPos=0
          }
        }
      }
    }
    // next character
    bufferPos++
  }
}

