%function [] = PostProc2(filenm)
close all
clear all

filenm = 'foo';



fileID = fopen(filenm, 'r');


R_idx = PacketDefinition;

ExpectedSampleRate = 100;  %Hz
ExpectedSamplePeriod = 1/ExpectedSampleRate;
DisplayTime = 60;  % Seconds

n_samples = DisplayTime/ExpectedSamplePeriod;

CircBuff = NaN(n_samples,length(fieldnames(R_idx)));

%CircBuff(1:end,R_idx.time) = (1:length(CircBuff))*ExpectedSamplePeriod-ExpectedSamplePeriod;
%CircBuff(1:end,R_idx.DataIDs) = 0;


n  = 0;


figure
ah1 = subplot(5,1,1);
ph_11 = plot(CircBuff(:,R_idx.time),CircBuff(:,R_idx.TargetTension));
hold all;
plot(CircBuff(:,R_idx.time),CircBuff(:,R_idx.MeasTension));
legend('Target Tension','Measured Tension');
ylabel('kg');
grid on;

while true
    % Read one line
    thisLine = fgetl(fileID);
    
    % Check if new data exists
    if ischar(thisLine)
        % Process the new data
        n = n+1;
        if(n >n_samples)
            n = 1;
        end
        disp(thisLine);

        sscanf(thisLine,"%d,%d,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%d,%d", ...
        CircBuff(n,R_idx.time), ...
        CircBuff(n,R_idx.TargetTension), ...
        CircBuff(n,R_idx.MeasTension), ...
        CircBuff(n,R_idx.TorqueCmd), ...
        CircBuff(n,R_idx.TorqueAct), ...
        CircBuff(n,R_idx.RPM_Cmd), ...
        CircBuff(n,R_idx.RPM_Act), ...
        CircBuff(n,R_idx.SD_TorqueAct), ...
        CircBuff(n,R_idx.SD_RPM_Act), ...
        CircBuff(n,R_idx.SD_Pos), ...
        CircBuff(n,R_idx.PID_FF), ...
        CircBuff(n,R_idx.PID_P), ...
        CircBuff(n,R_idx.PID_I), ...
        CircBuff(n,R_idx.PID_D), ...
        CircBuff(n,R_idx.PID_Tf), ...
        CircBuff(n,R_idx.cmd_min), ...
        CircBuff(n,R_idx.cmd_max), ...
        CircBuff(n,R_idx.SD_RPM_Cmd), ...
        CircBuff(n,R_idx.SWC_OilPressure), ...
        CircBuff(n,R_idx.TorqueMode_VFD_RPMLimit), ...
        CircBuff(n,R_idx.TorqueMode_PID_RPMLimit), ...
        CircBuff(n,R_idx.CraneIndoorMode), ...
        CircBuff(n,R_idx.OutHaulerActive)); 

        ph_11.XData = CircBuff(:,R_idx.time);
        ph_11.YData = CircBuff(:,R_idx.MeasTension);


    else
        % No new data, pause before checking again
        pause(0.001); 
        
        % Optionally re-check if file is still open or should stop
        % if stop_condition, break; end
    end
end

% 3. Close file (never reached in this infinite loop)
fclose(fileID);


% 
% 
% 
% 
% data = readtable(filenm);
% 
% t = data.Var1;
% t = t-t(1);
% 
% TargTension = data.Var2;
% MeasTension = data.Var3;
% TorqueCmd = data.Var4;
% TorqueAct = data.Var5;
% RPM_Cmd = data.Var6;
% RPM_Act= data.Var7;
% SD_TorqueAct = data.Var8;
% SD_RPM_Act = data.Var9;
% SD_Pos = data.Var10;
% PID_FF = data.Var11;
% PID_P = data.Var12;
% PID_I = data.Var13;
% PID_D = data.Var14;
% PID_Tf = data.Var15;
% cmd_min = data.Var16;
% cmd_max = data.Var17;
% SD_RPM_Cmd = data.Var18;
% SWC_OilPressure = data.Var19;
% SWC_OilPressureAlarmLimit = data.Var20;
% TorqueMode_VFD_RPMLimit = data.Var21;
% TorqueMode_PID_RPMLimit = data.Var22;
% CraneIndoorMode = data.Var23; 
% OutHaulerActive = data.Var24;
% 
% 
% 
% 
% num_plots = 5;
% figure('Position', [1 1 1280 599.3333]);
% ah1 = subplot(num_plots,1,1);
% plot(t,TargTension);
% hold all;
% plot(t,MeasTension,'-');
% legend('Target Tension','Measured Tension');
% ylabel('kg');
% grid on;
% 
% ah2 = subplot(num_plots,1,2);
% plot(t,TorqueCmd);
% hold all;
% plot(t,TorqueAct);
% %plot(t,cmd_min,'k--');
% %plot(t,cmd_max,'k--');
% legend('Cmd Torque','Actual Torque');
% grid on;
% 
% ah3 = subplot(num_plots,1,3);
% plot(t,RPM_Cmd);
% hold all;
% plot(t,RPM_Act);
% legend('Target RPM','Actual RPM');
% ylabel('RPM');
% grid on;
% 
% ah4 = subplot(num_plots,1,4);
% plot(t,PID_P);
% hold all;
% plot(t,PID_I);
% plot(t,PID_D);
% plot(t,PID_Tf);
% legend('P','I','D','Tf');
% grid on;
% 
% ah5 = subplot(num_plots,1,5);
% plot(t,PID_FF);
% legend('PID_FF)');
% grid on;
% 
% ah3 = subplot(num_plots,1,4);
% plot(t,RPM_Cmd);
% hold all;
% plot(t,RPM_Act);
% legend('Target RPM','Actual RPM');
% ylabel('RPM');
% grid on;
% 
% 
% linkaxes([ah1 ah2 ah3 ah4 ah5],'x');
% 
% 
% SD_TorqueAct = data.Var8;
% SD_RPM_Act = data.Var9;
% SD_Pos = data.Var10;
% 
% num_plots = 3;
% figure('Position', [1 1 1280 599.3333]);
% ah1 = subplot(num_plots,1,1);
% plot(t,SD_TorqueAct);
% legend('SD_TorqueAct');
% ylabel('%');
% grid on;
% 
% ah2 = subplot(num_plots,1,2);
% plot(t,SD_RPM_Act);
% hold all;
% plot(t,SD_RPM_Cmd);
% plot(t,RPM_Act);
% legend('SD_RPM_Act','SD_RPM_Cmd','TW Actual RPM');
% grid on;
% ylabel('RPM');
% 
% ah3 = subplot(num_plots,1,3);
% plot(t,SD_Pos);
% 
% legend('TSD_Pos');
% ylabel('%');
% grid on;
% 
% linkaxes([ah1 ah2 ah3],'x');
% 
% 
% 
% %end