function plotContextTiming(filename, Offset, RXType, CoreNum);

% Filename - .mat format file with record 35:271 data
% Offset   - optional offset in seconds we will remove from the 
%            x-axis (by making the x values small, you get more
%            resolution in the tool tip)
% RXtype   - 0 = BD940
%            1 = BD992
%            This impacts the colored bands, which indicate the
%            task types. The task IDs are a function of product type
% CoreNum   - Only relevant for the BD992 - core 0 or 1           
%
%
% Record 35: 271
% The data is arranged in two columns, task ID followed by the time we
% switch to that task. We want to create a plot where we get a line from
% the start of the task to when it ends, then a pen up, and move to the
% next task and repeat. To achieve this we'll simply create an array
% that is three times longer than the input. The array will have the
% following information:-
%
% 1:3:end   - the original data with no modification
% 2:3:end,1 - this is the task ID, copy the same ID from 1:3:end,1
% 2:3:end,2 - this is the time we switch from the task to the next task,
%             1:3:end,2 is when we started the current task. Simply copy
%             the time from the start of the next task which is the
%             input time tag offset by an element
% 3:3:end   - NaN which forces a pen up
%
% Copyright 2023 Trimble Inc

if(RXType == 0)
  CoreNum = 0; % Only one core for the BD940
  isBD940 = 1;
  isBD992 = 0;
else
  isBD940 = 0;
  isBD992 = 1;
end
%-------------------------------------------


if(isBD992)
  EthStart   = 112;
  EthStop    = 121;
  IOTX       =  20;
  PM         =  27;
  IMU        =  84;
  TitanStart = 128;
  TitanStop  = 156;
  if(isBD940)
    BANG
  end
end

if(isBD940)
  EthStart   = 121;
  EthStop    = 130;
  IOTX       =  19;
  PM         =  25;
  IMU        =  94;
  TitanStart = 137;
  TitanStop  = 165;
end

InputData = load_viewdat(filename);

i = find(InputData(:,3) == CoreNum);
Task = [InputData(i,1) InputData(i,2) InputData(i,4)];

% Overwrite the original data
InputData = zeros(length(Task),2);
InputData(:,1) = Task(:,3);
InputData(:,2) = Task(:,2);

% Now write three copies of the data to support pen-up/pen-down
len = length(InputData);
CPU(1:3:(len*3),:) = InputData;
CPU(3:3:(len*3),:) = NaN;
CPU(2:3:(len*3),1) = InputData(:,1);
CPU(2:3:((len-1)*3),2) = InputData(2:end,2);
 
% We only have the start time of the last task and not when it ended
% execution so drop the data point
lenOut = (len-1)*3;

Offset = Offset * 1000000; % Convert to microseconds

start = (InputData(1,2) - Offset) / 1e6;
stop  = (InputData(end,2)- Offset) / 1e6;

X = (CPU(1:lenOut,2) - Offset)./1000000;
Y = CPU(1:lenOut,1);

figure

% Highlight the Ethernet write tasks
patch([start, ... 
       start, ... 
       stop,  ...
       stop], ...
       [EthStart-0.5,EthStop+0.5,EthStop+0.5,EthStart-0.5], 'b', 'FaceAlpha', 0.1, 'LineStyle','none')

% Highlight IOTX      
patch([start, ... 
       start, ... 
       stop,  ...
       stop], ...
       [IOTX-0.5,IOTX+0.5,IOTX+0.5,IOTX-0.5], 'g', 'FaceAlpha', 0.1, 'LineStyle','none')

% Highlight PM
patch([start, ... 
       start, ... 
       stop,  ...
       stop], ...
       [PM-0.5,PM+0.5,PM+0.5,PM-0.5], 'c', 'FaceAlpha', 0.1, 'LineStyle','none')

% IMU
patch([start, ... 
       start, ... 
       stop,  ...
       stop], ...
       [IMU-0.5,IMU+0.5,IMU+0.5,IMU-0.5], 'm', 'FaceAlpha', 0.1, 'LineStyle','none')

% Highlight the Titan AINS tasks       
patch([start, ... 
       start, ... 
       stop,  ...
       stop], ...
       [TitanStart-0.5,TitanStop+0.5,TitanStop+0.5,TitanStart-0.5], 'r', 'FaceAlpha', 0.1, 'LineStyle','none')

hold on;

plot(X,Y,'-x');

if(isBD992)
  title(['Core: ',num2str(CoreNum)]);
end    
xlabel(['GPS Time - ',num2str(Offset/1e6,'%.0f'),' [s]']);
ylabel('Task ID')
grid;




