function [hStartO,hEndO,vStartO,vEndO,validO, outputData, Unloading, BlankCountEn, Running] = DataReadControllerRow(hStart,hEnd,vStart,vEnd,valid,...
    lineStartV, lineAverage, AllEndOfLine, BlankCount, frameReset)
%#codegen

persistent FSMState;
persistent InBetween;

S0 = fi(0,0,2,0);
S1 = fi(1,0,2,0);
S2 = fi(2,0,2,0);



if isempty(FSMState)
    FSMState = fi(0,0,2,0);
    InBetween = false;
end





switch FSMState
    
    case S0 %% Loading
        outputData = fi(1,0,1,0);
        hStartO = (hStart);
        hEndO = (hEnd);
        vStartO = (vStart);
        vEndO = (vEnd);
        validO = (valid);

        Unloading = fi(0,0,1,0);
        InBetween = false;
        BlankCountEn = fi(0,0,1,0);
        Running = fi(1,0,1,0);

        if vStart
            FSMState(:) = S1;
        else
            FSMState(:) = S0;
        end
        
        
    case S1 %% Running
        outputData = fi(1,0,1,0);
        Unloading = fi(0,0,1,0);
        BlankCountEn = fi(0,0,1,0);
        Running = fi(1,0,1,0);
        
        if frameReset
              FSMState(:) = S0;
            InBetween = false;
        hStartO = (hStart);
        hEndO = (hEnd);
        vStartO = (vStart);
        vEndO = vEnd;
        validO = (valid);      
        elseif vEnd
            FSMState(:) = S0;
            InBetween = false;
        hStartO = (hStart);
        hEndO = (hEnd);
        vStartO = (vStart);
        vEndO = vEnd;
        validO = (valid);
                BlankCountEn = fi(1,0,1,0);
        else
            FSMState(:) = S1;
        hStartO = (hStart);
        hEndO = (hEnd);
        vStartO = (vStart);
        vEndO = (vEnd);
        validO = (valid);
            InBetween = false;
        end
        
        
  
        

        
    otherwise
        hStartO = hStart;
        hEndO = hEnd;
        vStartO = vStart;
        vEndO = vEnd;
        validO = valid;
        outputData = fi(1,0,1,0);
        Unloading = fi(0,0,1,0);
        BlankCountEn = fi(0,0,1,0);
        InBetween = false;
        Running = fi(0,0,1,0);
end
