/*
	 SM_DEMO1.C
	
    Sample program for the IAB239 (XP8800/Stepper Motor Expansion Board).

    This program does the following:
    
    (a) Finds all the available IAB239 boards connected to the PLCBUS.
    (b) Set the board(s) to detect an Origin low.
    (c) Set the board(s) to detect an EL+ low or 8000 counts.
    (d) Set the board(s) to detect an EL- low or 8000 counts.
*/

#if ((BOARD_TYPE==L_STAR) || (BOARD_TYPE==CPLC_BOARD))
#use	cplc.lib
#endif

main()
{
    int total_boards, i;
    int pulse_counter;

#if ( (BOARD_TYPE==L_STAR) + (BOARD_TYPE==CPLC_BOARD))
    uplc_init();
#endif

    reset_pbus();
    reset_pbus_wait();

    total_boards = sm_find_boards();
    
    if(total_boards == 0)
    {
        printf("No IAB239 connected !!!\n");
        printf("Cannot proceed with the Sample Program !!!\n");
        while(1) {
        	hitwd();
         runwatch();
        }
    }

    printf("Found the following boards:\n");
    for(i=0;i<total_boards;i++)
    {
        sm_board_reset(i);      // complete reset for all boards
        printf("Indx %d for Board address %x \n", i, sm_addr[i]);
    }
   
    for(i = 0;i < total_boards; i++)
    {
        printf("Demo Board Indx %d (%x)\n", i+1, sm_addr[i]);
        sm_board_reset(i);                  // complete reset for this board
        sm_drvoe(i,1);          // enable the driver
        sm_led(i,1);            // enable led for current board
        smc_setspeed(i,20,100); // set low speed to 20 pps and high speed to
                                // 100 pps
      
        smc_seek_origin(i, 0, 1);  // set to find origin, ccw rotation

        printf("Connect ORG to GND to simulate Origin Found.\n");
        while( !(smc_stat0(i) & 0x04)) runwatch(); // wait for ORG low
      
        smq_hardreset(i);           // reset counter
        smc_softreset(i);           // soft reset the PCL-AK
        smc_manual_move(i,0,1);     // manual move, ccw

        printf("Connect EL+ to GND to stop rotation.\n");
        while(1)
        {
        		hitwd();
            runwatch();
            if((smc_stat0(i) & 0x02))       // wait for EL+ low 
            {
                printf(">>>>>EL+ detected !!!\n");
                break;
            }   
            pulse_counter = smq_read16(i);
            if( abs(smq_read16(i)) > 8000)
            {
                if( pulse_counter > 0)   
                    printf(">>>>>encoder detected 8000 cw counts.\n");
                else                     
                    printf(">>>>>encoder detected 8000 ccw counts.\n");      
                break;
            }
        } 

        smq_hardreset(i);             // reset counter
        smc_softreset(i);             // soft reset the PCL-AK
        smc_manual_move(i,1,1);       // manual move, cw
        printf("Connect EL- to GND to stop rotation.\n");
        while(1)
        {
        		hitwd();
            runwatch();
            if((smc_stat0(i) & 0x01))  // wait for EL- low 
            {
                printf(">>>>>EL- detected !!!\n");
                break;
            }   
            
            pulse_counter = smq_read16(i);
            if( abs(smq_read16(i)) > 8000)
            {
                if( pulse_counter > 0)   
                    printf(">>>>>encoder detected 8000 cw counts.\n");
                else                     
                    printf(">>>>>encoder detected 8000 ccw counts.\n");      
                break;
            }
        }
        sm_board_reset(i);      // reset the board
    }
}

