/****************************************************************************/
/* Copyright 2010 MBARI.                                                    */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
#include "dig_io.h"
#include "p24Fxxxx.h"
#include "sys_defs.h"

static int dummyBit;

#define TOTAL_OUT_BITS  16
#define TOTAL_IN_BITS  16

/* init registers */
void digInit()
{
    /* Note: the AD1PCFGL and TRIS registers have to be set on 
    ADC pins if you want to use them as digital I/O pins */    

    /* set outputs here */
    OUT_0_TRIS = 0;
    OUT_1_TRIS = 0;
    OUT_2_TRIS = 0;
    OUT_3_TRIS = 0;
    OUT_4_TRIS = 0;
    OUT_5_TRIS = 0;
    OUT_6_TRIS = 0;
    OUT_7_TRIS = 0;
               
    OUT_8_TRIS = 0;
    OUT_9_TRIS = 0;
    OUT_10_TRIS = 0;
    OUT_11_TRIS = 0;
    OUT_12_TRIS = 0;
    OUT_13_TRIS = 0;
    OUT_14_TRIS = 0;
    OUT_15_TRIS = 0;
               
    OUT_16_TRIS = 0;
    OUT_17_TRIS = 0;
    OUT_18_TRIS = 0;
    OUT_19_TRIS = 0;
    OUT_20_TRIS = 0;
    OUT_21_TRIS = 0;
    OUT_22_TRIS = 0;
    OUT_23_TRIS = 0;
               
    OUT_24_TRIS = 0;
    OUT_25_TRIS = 0;
    OUT_26_TRIS = 0;
    OUT_27_TRIS = 0;
    OUT_28_TRIS = 0;
    OUT_29_TRIS = 0;
    OUT_30_TRIS = 0;
    OUT_31_TRIS = 0;
    
    /* put out bits in default state here */
    Nop();
    OUT_BIT_0 = 0;
    Nop();
    OUT_BIT_1 = 0;    
    Nop();
    OUT_BIT_2 = 0;
    Nop();
    OUT_BIT_3 = 0;
    Nop();
    OUT_BIT_4 = 0;
    Nop();
    OUT_BIT_5 = 0;
    Nop();
    OUT_BIT_6 = 0;
    Nop();
    OUT_BIT_7 = 0;


    Nop();
    OUT_BIT_8 = 0;
    Nop();
    OUT_BIT_9 = 0;
    Nop();
    OUT_BIT_10 = 0;
    Nop();
    OUT_BIT_11 = 0;
    Nop();
    OUT_BIT_12 = 0;
    Nop();
    OUT_BIT_13 = 0;
    Nop();
    OUT_BIT_14 = 0;
    Nop();
    OUT_BIT_15 = 0;
}

/* power bit API */
void digSetOutBit(int bit, int state)
{
    /* state is non zero, set it to one */
    if ( state )
        state = 1;
    
    Nop();

    switch (bit)
    {
        case 0: OUT_BIT_0 = state; break;
        case 1: OUT_BIT_1 = state; break;
        case 2: OUT_BIT_2 = state; break;
        case 3: OUT_BIT_3 = state; break;
        case 4: OUT_BIT_4 = state; break;
        case 5: OUT_BIT_5 = state; break;
        case 6: OUT_BIT_6 = state; break;
        case 7: OUT_BIT_7 = state; break;
        
        case 8: OUT_BIT_8 = state; break;
        case 9: OUT_BIT_9 = state; break;
        case 10: OUT_BIT_10 = state; break;
        case 11: OUT_BIT_11 = state; break;
        case 12: OUT_BIT_12 = state; break;
        case 13: OUT_BIT_13 = state; break;
        case 14: OUT_BIT_14 = state; break;
        case 15: OUT_BIT_15 = state; break;
        default: return;
    }
}

int digGetOutBit(int bit)
{
    Nop();

    /* set dummyBit to 0 so undefined bits always read back as 0 */
    dummyBit = 0;
    
    switch (bit)
    {
        case 0: return (int)OUT_BIT_0;
        case 1: return (int)OUT_BIT_1;
        case 2: return (int)OUT_BIT_2;
        case 3: return (int)OUT_BIT_3;
        case 4: return (int)OUT_BIT_4;
        case 5: return (int)OUT_BIT_5;
        case 6: return (int)OUT_BIT_6;
        case 7: return (int)OUT_BIT_7;

        case 8: return (int)OUT_BIT_8;
        case 9: return (int)OUT_BIT_9;
        case 10: return (int)OUT_BIT_10;
        case 11: return (int)OUT_BIT_11;
        case 12: return (int)OUT_BIT_12;
        case 13: return (int)OUT_BIT_13;
        case 14: return (int)OUT_BIT_14;
        case 15: return (int)OUT_BIT_15;

        default: return 0;
    }
}

void digSetOutPort(unsigned int val)
{
    int i;
    unsigned int bit = 0x0001;
     
    for (i = 0; i < TOTAL_OUT_BITS; ++i)
    {
        digSetOutBit(i, (val & bit));
        bit <<= 1;
    }
}

unsigned int digGetOutPort(void)
{
    int i;
    unsigned int bit = 0x0001;
    unsigned int val = 0;

    for (i = 0; i < TOTAL_OUT_BITS; ++i)
    {
        if ( digGetOutBit(i) )
            val += bit;
        
        bit <<= 1;
    }

    return val;
}

/* digital input API */
int digGetInBit(int bit)
{
    Nop();

    /* set dummyBit to 0 so undefined bits always read back as 0 */
    dummyBit = 0;
    
    switch (bit)
    {
        case 0: return (int)IN_BIT_0;
        case 1: return (int)IN_BIT_1;
        case 2: return (int)IN_BIT_2;
        case 3: return (int)IN_BIT_3;
        case 4: return (int)IN_BIT_4;
        case 5: return (int)IN_BIT_5;
        case 6: return (int)IN_BIT_6;
        case 7: return (int)IN_BIT_7;

        case 8: return (int)IN_BIT_8;
        case 9: return (int)IN_BIT_9;
        case 10: return (int)IN_BIT_10;
        case 11: return (int)IN_BIT_11;
        case 12: return (int)IN_BIT_12;
        case 13: return (int)IN_BIT_13;
        case 14: return (int)IN_BIT_14;
        case 15: return (int)IN_BIT_15;

        default: return 0;
    }
}

unsigned int digGetInPort(void)
{
    int i;
    unsigned int bit = 0x0001;
    unsigned int val = 0;

    for (i = 0; i < TOTAL_IN_BITS; ++i)
    {
        if ( digGetInBit(i) )
            val += bit;
        
        bit <<= 1;
    }

    return val;
}

