#include "terasic_includes.h"
#include "vip.h"

void FrameReader_SetFrame0(alt_u32 VipBase, alt_u32 FrameBase, alt_u32 words, alt_u32 cycle, alt_u32 width, alt_u32 height, alt_u32 interlace){
    IOWR(VipBase, 4, FrameBase); // frame0 base address
    IOWR(VipBase, 5, words); // frame0 words
    IOWR(VipBase, 6, cycle); // frame0 single cylce color pattern
    IOWR(VipBase, 8, width); // frame0 width
    IOWR(VipBase, 9, height); // frame0 height
    IOWR(VipBase, 10, interlace); // frame0 interlaced
}

void FrameReader_SetFrame1(alt_u32 VipBase, alt_u32 FrameBase, alt_u32 words, alt_u32 cycle, alt_u32 width, alt_u32 height, alt_u32 interlace){
    IOWR(VipBase, 11, FrameBase); // frame0 base address
    IOWR(VipBase, 12, words); // frame0 words
    IOWR(VipBase, 13, cycle); // frame0 single cylce color pattern
    IOWR(VipBase, 15, width); // frame0 width
    IOWR(VipBase, 16, height); // frame0 height
    IOWR(VipBase, 17, interlace); // frame0 interlaced
}        

///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

bool Histogram_CaptureSync(alt_u32 VipBase, alt_u32 szHist[3][256]){
    int i = 0;
    alt_u32 done_flag;
    alt_u32 timeout;
    
    //start capture
    IOWR(VipBase, 0, 0x00);
    IOWR(VipBase, 0, 0x01);
    
    // wait done
    timeout = alt_nticks() + alt_ticks_per_second()/5; // timeout, 1/5 second
    do{
        done_flag = IORD(VipBase, 0);
    }while((done_flag != 0x01) && (alt_nticks() < timeout));
    
    // read histogram
    if (done_flag == 0x01){
        for(i=0;i<256*3;i++){
            szHist[i/256][i%256] = IORD(VipBase, i+1);
        }            
    }
    
    if (done_flag != 0x01)
        return FALSE;
    return TRUE;     
}

///////////////////////////////////////////////////////////////
// Pixel Picker
///////////////////////////////////////////////////////////////

#define PP_REG_CONTROL_STATUS      0
#define PP_REG_PIXEL_R             1
#define PP_REG_PIXEL_G             2
#define PP_REG_PIXEL_B             3
#define PP_REG_PIXEL_X             4
#define PP_REG_PIXEL_Y             5
#define PP_REG_FRAME_WIDTH         6
#define PP_REG_FRAME_HEIGHT        7

void PixelPicker_ConfigFrame(alt_u32 VipBase, alt_u32 width, alt_u32 height){
    IOWR(VipBase, PP_REG_FRAME_WIDTH, width);
    IOWR(VipBase, PP_REG_FRAME_HEIGHT, height);         
}

bool PixelPicker_CaptureSync(alt_u32 VipBase, alt_u32 x, alt_u32 y, alt_u32 szLevel[3]){
    int i = 0;
    alt_u32 done_flag;
    alt_u32 timeout;
    
    // set x/y
    IOWR(VipBase, PP_REG_PIXEL_X, x);
    IOWR(VipBase, PP_REG_PIXEL_Y, y);         
    
    //start capture
    IOWR(VipBase, PP_REG_CONTROL_STATUS, 0x00);
    IOWR(VipBase, PP_REG_CONTROL_STATUS, 0x01);
    
    // wait done
    timeout = alt_nticks() + alt_ticks_per_second()/10; // timeout, 1/10 second
    do{
        done_flag = IORD(VipBase, PP_REG_CONTROL_STATUS);
    }while((done_flag != 0x01) && (alt_nticks() < timeout));
    
    // read histogram
    if (done_flag == 0x01){
        szLevel[0] = IORD(VipBase, PP_REG_PIXEL_R);
        szLevel[1] = IORD(VipBase, PP_REG_PIXEL_G);
        szLevel[2] = IORD(VipBase, PP_REG_PIXEL_B);
    }
    
    if (done_flag != 0x01)
        return FALSE;
    return TRUE;     
    
}