// Demo program "TX_Basis"
//
// Can be run under control of the ROBO TX Controller
// firmware in download (local) mode.
// Switches two lamps connected to the outputs O7 and O8.
// A summer ist connected to O5 and a Stwitch to I3.
//
// Disclaimer - Exclusion of Liability

#include "ROBO_TX_PRG.h"

static unsigned long cur_time;
static unsigned long prev_time;
static enum {summer, pause_summer, taster, on, pause_on, off, pause_off} stage;
static int wait;

/*-----------------------------------------------------------------------------
 * Function Name       : PrgInit
 *
 * This it the program initialization.
 * It is called once.
 *-----------------------------------------------------------------------------*/
void PrgInit
(
    TA * p_ta_array,  // pointer to the array of transfer areas
    int ta_count     // number of transfer areas in array 
)
{
    p_ta = &p_ta_array[TA_LOCAL];
    prev_time = 0;
    stage = summer;
    wait = 200;
}
/*-----------------------------------------------------------------------------
 * Function Name       : PrgTic
 *
 * This is the main function of this program.
 * It is called every tic (1 ms) realtime.
 *-----------------------------------------------------------------------------*/
int PrgTic
(
    TA * p_ta_array,    // pointer to the array of transfer areas
    int ta_count        // number of transfer areas in array 
)
{              // return code:
  int rc = 0x7FFF;// 0x7FFF - program should be further called by the firmware;
              //  0   - program should be normally stopped by the firmware;
              // any other value is considered by the firmware as an error code
              // and the program is stopped.
     cur_time = gettime();  // Get the current value of the system time
       
     switch (stage)
        {   case summer:
               txlamp(4,500);
               prev_time = cur_time; 
               stage++;
            return rc;
                
            case pause_summer:
               if (cur_time - prev_time >= wait)
                 { txlamp(4,0);
               stage++;}
            return rc;
             
            case taster:
              if (txinput(2))
                {  
                  stage++;
                }
            return rc;
                
            case on:
               txlamp(6,0); txlamp(7,500); 
               prev_time = cur_time;  
               stage++;
            return rc;
               
           case pause_on:
              if (cur_time - prev_time >= wait)
              { prev_time = cur_time;
               stage++;}
           return rc;
             
           case off:
              txlamp(6,500); txlamp(7,0); 
              prev_time = cur_time;
              stage++;
            return rc; 
                
            case pause_off:
               if (cur_time - prev_time >= wait)
               { stage=on;}
            return rc;  
                
            default:
            return rc;
        }
    return rc;
}
