% flicker_lpt.m
% Use Cogent 2000 to synchronously flicker display
% at the maximum video update rate.
% Use inpout32/io32() extensions to update LPT1-pin2 output
% to synchronously follow display state (0=BLACK; 1=WHITE)

clear all;
config_io;  %install inpout32.dll I/O extensions
config_keyboard; %install C2K keyboard driver
config_display(1,2); %full screen mode at 800x600 resolution
start_cogent; %enter Cogent 2000 mode

global cogent;
%assure successful installation of inpout32.dll device driver
if ( cogent.io.status == 0 )
    disp('inpout32.dll successfully installed.')
else
    disp('inpout32.dll installation failed.'); stop_cogent; return
end

%initialize black and white screens
clearpict(1,0,0,0); %screen buffer #1 = BLACK
clearpict(2,1,1,1); %screen buffer #2 = WHITE
%initialize LPT1 output (pin-2 = signal, pin-25 = ground)
lpt1=hex2dec('378'); %LPT1 base address
outp(lpt1, 0); %pin-2 = inactive

%loop until ESC key pressed
keymap=getkeymap; clearkeys;
white = 1;  %screen phase
loop = 1;
while (loop > 0)
    %transition to black?
    if white==1
        drawpict(1);   %display black screen at next vertical retrace
        outp(lpt1, 0); %reset LPT1 pin-2 output
        white = 0;     %toggle screen state to non-white
    else
        drawpict(2);   %display white screen at next vertical retrace
        outp(lpt1, 1); %set LPT1 pin-2 output
        white = 1;     %toggle screen state to white
    end
    %scan keyboard and test for ESC key
    readkeys; [keys,times,nkeys]=getkeydown;
    if(nkeys > 0)
        if(keys(1)==keymap.Escape)
            loop = 0; %exit if ESC key detected
        end
    end 
end
outp(lpt1, 0); stop_cogent;    %reset LPT1 output and exit Cogent 2000
