
// 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,colorHome,iconHome[,iconColor,icon,speed,valueUp,valueDown]x6
            spstr strCommand.txt,tHeading.txt,"~",1

              // navigation icon left
              spstr strCommand.txt,tTmp.txt,"~",2 //type
              if(tTmp.txt=="delete"||tTmp.txt=="")
              {
                vis bPrev,0
              }else
              {
                // get internal name
                spstr strCommand.txt,nent1.txt,"~",3
                // change icon
                spstr strCommand.txt,bPrev.txt,"~",4
                // change icon color
                spstr strCommand.txt,tTmp.txt,"~",5
                covx tTmp.txt,sys0,0,0
                bPrev.pco=sys0
                vis bPrev,1
              }
              
              // navigation icon right
              spstr strCommand.txt,tTmp.txt,"~",8 //type
              if(tTmp.txt=="delete"||tTmp.txt=="")
              {
                vis bNext,0
              }else
              {
                // get internal name
                spstr strCommand.txt,nent2.txt,"~",9
                // change icon
                spstr strCommand.txt,bNext.txt,"~",10
                // change icon color
                spstr strCommand.txt,tTmp.txt,"~",11
                covx tTmp.txt,sys0,0,0
                bNext.pco=sys0
                vis bNext,1
              }

			  // icon color home
              spstr strCommand.txt,tTmp.txt,"~",14
              covx tTmp.txt,t1.pco,0,0
			        // icon home
              spstr strCommand.txt,t1.txt,"~",15
			        // text home
              spstr strCommand.txt,tHome.txt,"~",16


              // iconColor
              spstr strCommand.txt,tTmp.txt,"~",17
              covx tTmp.txt,t0Icon.pco,0,0
              // icon
              spstr strCommand.txt,t0Icon.txt,"~",18
              // speed
              spstr strCommand.txt,tTmp.txt,"~",19
              covx tTmp.txt,t0Speed.val,0,0
              // lower text
              spstr strCommand.txt,t0u.txt,"~",20

              if(t0Icon.txt!="")
              {
                vis t0Icon,1
                vis t0u,1
                vis h0,1
              }else
              {
                vis t0Icon,0
                vis t0u,0
                vis h0,0
              }


              // iconColor
              spstr strCommand.txt,tTmp.txt,"~",21
              covx tTmp.txt,t1Icon.pco,0,0
              // icon
              spstr strCommand.txt,t1Icon.txt,"~",22
              // speed
              spstr strCommand.txt,tTmp.txt,"~",23
              covx tTmp.txt,t1Speed.val,0,0
              // lower text
              spstr strCommand.txt,t1u.txt,"~",24

              if(t1Icon.txt!="")
              {
                vis t1Icon,1
                vis t1u,1
                vis h1,1
              }else
              {
                vis t1Icon,0
                vis t1u,0
                vis h1,0
              }


              // iconColor
              spstr strCommand.txt,tTmp.txt,"~",25
              covx tTmp.txt,t2Icon.pco,0,0
              // icon
              spstr strCommand.txt,t2Icon.txt,"~",26
              // speed
              spstr strCommand.txt,tTmp.txt,"~",27
              covx tTmp.txt,t2Speed.val,0,0
              // lower text
              spstr strCommand.txt,t2u.txt,"~",28

              if(t2Icon.txt!="")
              {
                vis t2Icon,1
                vis t2u,1
                vis h2,1
              }else
              {
                vis t2Icon,0
                vis t2u,0
                vis h2,0
              }


              // iconColor
              spstr strCommand.txt,tTmp.txt,"~",29
              covx tTmp.txt,t3Icon.pco,0,0
              // icon
              spstr strCommand.txt,t3Icon.txt,"~",30
              // speed
              spstr strCommand.txt,tTmp.txt,"~",31
              covx tTmp.txt,t3Speed.val,0,0
              // lower text
              spstr strCommand.txt,t3u.txt,"~",32

              if(t3Icon.txt!="")
              {
                vis t3Icon,1
                vis t3u,1
                vis h3,1
              }else
              {
                vis t3Icon,0
                vis t3u,0
                vis h3,0
              }


              // iconColor
              spstr strCommand.txt,tTmp.txt,"~",33
              covx tTmp.txt,t4Icon.pco,0,0
              // icon
              spstr strCommand.txt,t4Icon.txt,"~",34
              // speed
              spstr strCommand.txt,tTmp.txt,"~",35
              covx tTmp.txt,t4Speed.val,0,0
              // lower text
              spstr strCommand.txt,t4u.txt,"~",36

              if(t4Icon.txt!="")
              {
                vis t4Icon,1
                vis t4u,1
                vis h4,1
              }else
              {
                vis t4Icon,0
                vis t4u,0
                vis h4,0
              }


              // iconColor
              spstr strCommand.txt,tTmp.txt,"~",37
              covx tTmp.txt,t5Icon.pco,0,0
              // icon
              spstr strCommand.txt,t5Icon.txt,"~",38
              // speed
              spstr strCommand.txt,tTmp.txt,"~",39
              covx tTmp.txt,t5Speed.val,0,0
              // lower text
              spstr strCommand.txt,t5u.txt,"~",40

              if(t5Icon.txt!="")
              {
                vis t5Icon,1
                vis t5u,1
                vis h5,1
              }else
              {
                vis t5Icon,0
                vis t5u,0
                vis h5,0
              }


            }

            if(tInstruction.txt=="pageType")
            {
              sleepValue=0
              //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
              spstr strCommand.txt,pageIcons.tTmp3.txt,"~",4
              //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(tId.txt=="cardPower")
              {
                page cardPower
              }
              if(tId.txt=="cardChart")
              {
                page cardChart
              }
            }
            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
              // get value normal
              spstr strCommand.txt,tTmp.txt,"~",2
              covx tTmp.txt,dimValueNormal,0,0
              dim=dimValueNormal
              // get background color
              spstr strCommand.txt,tTmp.txt,"~",3
              if(tTmp.txt!="")
              {
                covx tTmp.txt,defaultBcoColor,0,0
              }
              // get font color
              spstr strCommand.txt,tTmp.txt,"~",4
              if(tTmp.txt!="")
              {
                covx tTmp.txt,defaultFontColor,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++
  }
}

