classdef (StrictDefaults)VHTtoCAMERALINK_Adapter < matlab.System
    % VHTtoCAMERALINK_Adapter System object
    %
    % Converts Vision HDL Toolbox control signals to Camera Link signals. 
    %
    % [pix_out,F,L,D] = step(CLAdapter,pix_in,ctrl)
    %
    % Example
    % helpview (doc example)
    %
    %   Copyright 2015 The MathWorks, Inc.
    
    %#codegen
    
    
   
    properties (DiscreteState)
        vEndReg;
        hEndReg;
        % Output registers
        FOutReg;
        LOutReg;
        DOutReg;
        pixOutReg;
    end

    
    methods
        function obj = VHTtoCAMERALINK_Adapter(varargin)
            setProperties(obj,nargin,varargin{:});
        end
        
    end
        
    methods (Access=protected)
       
        function validateInputsImpl(obj,pixel,ctrl) 
            validatecontrolsignals(ctrl); 
        end % validateInputsImpl

        function resetImpl(obj)
            obj.FOutReg = false;
            obj.LOutReg = false;
            obj.DOutReg = false;
            obj.vEndReg = false;
            obj.hEndReg = false;
        end

        function setupImpl(obj,pixel,~) 
            obj.FOutReg = false;
            obj.LOutReg = false;
            obj.DOutReg = false;
            obj.pixOutReg = cast(0,'like',pixel); 
            obj.vEndReg = false;
            obj.hEndReg = false;
        end
        
        %% Main algorithm functionality
        function [pixelOut,F,L,D] = stepImpl(obj,pixel,ctrl)
            % Convert Vision HDL Toolbox control signals to Cameralink
            % F,L,D signals

            % send output from output registers
            F = obj.FOutReg;
            L = obj.LOutReg;
            D = obj.DOutReg;
            pixelOut = obj.pixOutReg;
            
            [hStart,hEnd,vStart,vEnd,valid] = pixelcontrolsignals(ctrl);
            
            % start/end of frame
            Fnew = (~obj.FOutReg && vStart) || (obj.FOutReg && ~obj.vEndReg); 
            % start/end of line
            Lnew = (~obj.LOutReg && hStart) || (obj.LOutReg && ~obj.hEndReg);
            
            % output register
            obj.FOutReg = Fnew;
            obj.LOutReg = Lnew;
            obj.DOutReg = valid;
            obj.pixOutReg(:) = pixel;
            % store vEnd and hEnd to check for transition
            obj.vEndReg = vEnd;
            obj.hEndReg = hEnd;
                
         end

        function N = getNumInputsImpl(obj) %#ok<MANU>
            % Specify number of inputs 
            N = 2;
        end 
        
        function N = getNumOutputsImpl(obj) %#ok<MANU>
            % Specify number of outputs
            N = 4;
        end
           
    end
end % VHTtoCAMERALINK_Adapter
