static int i2c_write(unsigned long addr, unsigned char c)
{
    mdelay(10);

    i2c_send_cmd(CMD_WRITE | CMD_TXVAL | addr);
    i2c_wait_irq(IRQ_M_TX_DATA);

    i2c_send_cmd(CMD_NOP | CMD_TXVAL | c);
    i2c_wait_irq(IRQ_M_TX_DATA);

    i2c_send_cmd(CMD_STOP);
    i2c_wait_irq(IRQ_M_CMD_ACK);

    return 0;
}
static int i2c_read(unsigned long addr)
{
    int value;

    mdelay(10);

    i2c_send_cmd(CMD_WRITE | CMD_TXVAL | addr);
    i2c_wait_irq(IRQ_M_TX_DATA);

    i2c_send_cmd(CMD_READ);
    value = i2c_wait_irq(IRQ_M_RX_DATA);

    i2c_send_cmd(CMD_STOP);
    i2c_wait_irq(IRQ_M_CMD_ACK);

    return value;
}
Example #3
0
File: main.c Project: dz3/CpE391
int16_t main(void)
{
    LED1_TRIS = 0; // LEDs may be 0 = on.
    LED2_TRIS = 0; // TRIS is 0 = 'output'.
    LED3_TRIS = 0;
    LED4_TRIS = 0;

#ifdef LED_TEST

    while(1)
    {
        LED1 = 0;
        LED2 = 0;
        LED3 = 0;
        LED4 = 0;
        __delay_ms(100);
        LED1 = 1;
        LED2 = 0;
        LED3 = 0;
        LED4 = 0;
        __delay_ms(100);
        LED1 = 1;
        LED2 = 1;
        LED3 = 0;
        LED4 = 0;
        __delay_ms(100);
        LED1 = 1;
        LED2 = 1;
        LED3 = 1;
        LED4 = 0;
        __delay_ms(100);
        LED1 = 1;
        LED2 = 1;
        LED3 = 1;
        LED4 = 1;
        __delay_ms(100);

    } // preventative, so only this test code is activated
#endif //LED_TEST
    int state = START;
    //int32_t elapsed_time = 0; // best way to keep track of time?
	long elapsed_time = 0; // best way to keep track of time?
                              //    go with the timers?

    /* Initialize all registers and peripherals */
    InitApp();

    LED1 = 1;
    LED2 = 1;
    LED3 = 1;
    LED4 = 1;

    /* TODO <INSERT USER APPLICATION CODE HERE> */
    bool gained_power = false;
    // true if have visited that source
    bool solar = false;
    bool wind  = false;
//	set_servo_absolute(90); // aim the motors due n/s
    // don't need the node, can conclude from the other two

#ifdef I2C_TEST
    // make a call to all motor controllers:
    //void i2c_send_cmd(unsigned short int addr, unsigned short int cmd, int dat);
    // send to all -> addr = 0x00
    // command     -> ??? something to get a response
    // data        -> ??? something to get a response
    LED1  = 0;
    i2c_send_cmd(I2C_MOTOR_123, I2C_VELOCITY, 0x7FFF);
    
    int i2c_check = i2c_recv_vel(I2C_MOTOR_1);
    LED2 = 0;
    if (i2c_check = 5)
    {
        LED4 = 0;
    }

    
#endif // I2C_TEST

    // TODO: get exact measurements from field - particularly bucket
    //        and starting position
    while(1)
    {
#ifdef SWITCHCASE
        // one method to do this:
        switch(state)
        {
            case START: // at the bucket, on North side - GO
                // wait .1 sec
                // check for the light to be on.
                // if the light is on, go to it - should be simple?
                if (read(LIGHT_PIN))
                {
                    state = START_TO_SOLAR;
                    // That's move North 2'ish,
                    // rotate 90 deg clockwise (actually, rotate so the proper
                    //     side with the solar panel is facing the light source)
                    //     and rotate the wheels so they can drive East/towards
                    //     the solar panels
                    // proceed forward 3.5'/until you run into the wall.
                }
                else // if the light isn't on, go to the electric node
                {
                    state = START_TO_NODE;
                    // orient wheels to drive West, 90 deg counter-clockwise
                    // move forward 3.5'ish,
                    // rotate so the claw side is facing the cap
                    // orient wheels to drive toward the North
                    // move forward 2'ish
                }

                break;
            case ATWIND:
                // if receiving power
                if (0 /* check the power pin*/)
                {
                    gained_power = true;
                    // wait...
                }
                // else //if done receiving power
                else
                {
                    if (!gained_power)
                    {
                        state = WIND_TO_NODE;
                        // move 1.5' East
                        // orient wheels North - 90 deg turn
                        // drive North 1.5'-6.5'
                        // orient wheels West
                        // drive West for 1.5'
                        // rotate body so claw is North
                        // orient wheels North
                        // drive North until stopped.
                    }
                    else
                    {
                        state = WIND_TO_FLAG;
                        // drive East 1.5-6.5'
                        // orient wheels N/S.
                        // drive North 1'
                        // orient wheels E/W
                        // drive East until near the wall - 1.5'ish
                        // orient claw South
                        // orient wheels N/S
                        // drive South until you hit the wall
                    }
                }
                break;
            case ATSOLAR:
                // if receiving power
                if (0 /* check the power pin*/)
                {
                    gained_power = true;
                    // wait...
                }
                // else //if done receiving power
                else
                {
                    if (!gained_power)
                    {
                        state = SOLAR_TO_NODE;
                        // drive East 1.5-6.5'
                        // orient wheels N/S
                        // drive South 1'
                        // orient wheels E/W
                        // drive East to the wall 1.5'ish
                        // orient claw North
                        // orient wheels N/S
                        // drive North until you hit the wall
                    }
                    else
                    {
                        state = SOLAR_TO_FLAG;
                        // drive East 1.5'
                        // orient wheels N/S
                        // drive South 1.5-6.5'
                        // orient wheels E/W
                        // drive East until you hit the wall
                        // orient claw South
                        // orient wheels N/S
                        // drive South until you hit the node/wall
                    }
                }
                break;
            case ATELECTRIC:
                // if receiving power
                if (0 /* check the power pin*/)
                {
                    gained_power = true;
                    // wait...
                }
                // else //if done receiving power
                else
                {
                    if (!gained_power)
                    {
                        state = NODE_TO_SOLAR; // TODO: electric -> wind or solar?
                        // drive South 1.5'
                        // orient wheels E/W
                        // drive East 1.5-6.5'
                        // orient wheels N/S
                        // drive North 1.5', to the wall
                        // orient solar panels East
                        // orient wheels E/W
                        // drive East until you hit the wall
                    }
                    else
                    {
                        state = NODE_TO_FLAG;
                        // drive South 1.5-6.5'
                        // orient wheels East
                        // drive East to the wall
                        // orient claw South
                        // orient wheels N/S
                        // drive until you hit the wall
                    }
                }
                break;
            case ATFLAG:
                // if dumping power
                    // wait
                // else //if done dumping power
                    // state = NEXT_STATE;
                // update elapsed time:
                if (elapsed_time < 2)
                {   // sounds like we've been at this just over a minute,
                    //  under two, so go to the 'second' source
                    if (solar)
                    {   // have visited the solar source, try wind next
                        state = FLAG_TO_WIND;
                        // drive North 1.5'
                        // orient wheels East
                        // drive 1.5-6.5' East
                        // orient wheels N/S
                        // drive South to the wall
                        // orient turbine West
                        // orient wheels West
                        // drive West to the wall
                    }
                    else if (wind)
                    {   // have visited the wind source, try solar next
                        state = FLAG_TO_SOLAR;
                        // drive North 1.5-6.5'
                        // orient wheels E/W
                        // drive West 1.5'
                        // orient wheels N/S
                        // drive North to the wall
                        // orient wheels E/W
                        // drive East until you hit the wall
                    }
                    else
                    {   // have visited the electric source, try solar next
                        state = FLAG_TO_SOLAR;
                        // drive North 1.5-6.5'
                        // orient wheels E/W
                        // drive West 1.5'
                        // orient wheels N/S
                        // drive North to the wall
                        // orient wheels E/W
                        // drive East until you hit the wall
                    }

                }
                else if (elapsed_time < 3)
                {   // sounds like we've been at this just over two minutes,
                    //  under three, so go to the 'third' source
                    if (!solar)
                    {   // have not visited the solar source, go there.
                        state = FLAG_TO_SOLAR;
                        // drive North 1.5-6.5'
                        // orient wheels E/W
                        // drive West 1.5'
                        // orient wheels N/S
                        // drive North to the wall
                        // orient wheels E/W
                        // drive East until you hit the wall
                    }
                    else if (!wind)
                    {   // have not visited the wind source, go there.
                        state = FLAG_TO_WIND;
                        // drive North 1.5'
                        // orient wheels East
                        // drive 1.5-6.5' East
                        // orient wheels N/S
                        // drive South to the wall
                        // orient turbine West
                        // orient wheels West
                        // drive West to the wall
                    }
                    else
                    {   // have not visited the electric source, go there.
                        state = FLAG_TO_NODE;
                        // go via the node, possibly grab some solar on the way...
                        // go North 6.5'
                        // orient wheels E/W
                        // drive West 7.5'ish
                        // orient claw North
                        // orient wheels N/S
                        // drive North until you hit the wall
                    }
                }
                break;
            case DONE:
                // rejoice
                break;
            default:
                // scream & shout
                // not supposed to be here
                // OR
                // use this as the wait-state: when in doubt, if the voltage
                //  is changing, wait. Otherwise, proceed to the next location
                break;
        }
        //----------------------------------
#elif CALCULATING
        // another method:

#endif

    }
}