// exper11a.c

#include "exper11a.h"

int CheckAnalog(int analog_channel);

enum
{
  MainMotor,
  LED1,
  LED2,
  LASTSLOT
};

void main(void)
{
  int x;
  double dc,oldfreq,newfreq;

  oldfreq = 1193180.0/65536.0;

  printf("oldfreq = %f calling set_up_new_timer for 1K Hz\n",oldfreq);

  set_up_new_timer(1000.0);

  newfreq = get_frequency();

  printf("new frequency = %fHz\n",newfreq);

  x = (int)InitializeAnalog();

  printf("init ana = %X\n",x);

  // make everthing an output
  set_up_ppi(Aout_CUout_Bout_CLout);

  x = TurnOnAnalog(0, // analog channel number
                   2, // 0 = no digital control, 1 = uni-directional, 2 = bi-directional
                   MainMotor, // output control array number
                   PA0, // port bit for forward control
                   PA3, // port bit for direction control
                   6); // current and last reading must differ by
                       // this much for PWM change to take place

  Blink(LED1,PA1, .03, .02);

  Blink(LED2,PA2, .3, .2);

  printf("Blinking LED1=PA1=%d .03,.02; LED2=PA2=%d .3,.2\n",PA1,PA2);
  printf("TurnOnAnalog(PWM=PA0=%d, Direction=PA3=%d) = %d\n",PA0,PA3,x);
  printf("Press any key to continue then any key to quit -- no Ctr-C!\n");

  getch();

  while(!kbhit())
  {
    if(!CheckAnalog(0))
    	break;
  }

  portaoff();

  // be sure to restore the timer!
  restore_old_timer();

  portaoff();
}

int CheckAnalog(int anachan)
{
  int SpeedValue,SetDirection;

  if(abs(AnalogChannel[anachan].current_value -
         AnalogChannel[anachan].last_value) > 
         AnalogChannel[anachan].NoiseBand)
    {
      AnalogChannel[anachan].last_value = 
        AnalogChannel[anachan].current_value;

      switch(AnalogChannel[anachan].controls_PWM)
      {
        case 1: // unidirectional

        UniPwmDuty(AnalogChannel[anachan].arraynumber,
                   AnalogChannel[anachan].PWM_channel,
                  (double)AnalogChannel[anachan].current_value/255.0);
        break;

        case 2: // bidirectional

        SpeedValue = AnalogChannel[anachan].current_value;

        // the low end of the values, from 0 to 127, are used for reverse
        // the high end values, from 128 to 255, are used for forward

        // for forward, the range is changed from 128 to 255 to 0 to 127

        // for reverse, the range is flipped from 0 to 127 to 127 to 0 so
        // that the low end of the trimmer produces maximum reverse speed

        // the process ends up with 0 to 127 for both forward and reverse,
        // with direction set by the SetDirection variable

        if(SpeedValue > 127) // forward
        {
          SpeedValue-=128; // modify to 0 to 127
          SetDirection = 1; // forward
        }

        else // reverse
        {
          SpeedValue = 127 - SpeedValue; // modify to 127 to 0
          SetDirection = 2; // reverse
        }

        printf("AnalogChannel[%d] changed, now = %d, direction = %d\n"
         ,anachan,AnalogChannel[anachan].last_value,SetDirection);

        printf("SpeedValue = %d /127 = %f\n"
        ,SpeedValue,(double)SpeedValue/127.0);

        printf("AnalogChannel[anachan=%d].PWM_channel = %d\n"
        ,anachan,AnalogChannel[anachan].PWM_channel);

        printf("AnalogChannel[%d].PWM_Direction_Channel = %d\n"
        ,anachan,AnalogChannel[anachan].PWM_Direction_Channel);

        return PwmAndDirectionDuty(AnalogChannel[anachan].arraynumber,
                  SetDirection,
                  AnalogChannel[anachan].PWM_channel,
                  AnalogChannel[anachan].PWM_Direction_Channel,
                  (double)SpeedValue/127.0,
                  (double)SpeedValue/127.0);

        break;

      } // end switch(AnalogChannel[anachan].controls_PWM)

      return 1;

    } // end if(abs(AnalogChannel[anachan].current_value...)
}

// end exper11a.c

