#include "Servo.h"

// Constructor
Servo::Servo(int handle, int low, int high, int pwm) : I2cPWM(handle, low, high, pwm, 1)
{
  currentPer = 0;
  direction = cent;
} // I2cPwm

void Servo::center()
{
  currentPer = 0;
  direction = cent;
  I2cPWM::middle();
} // center

void Servo::right(int percent)
{
  I2cPWM::increase(percent);
  currentPer = percent;
  direction = rite;
} // right

void Servo::left(int percent)
{
  I2cPWM::decrease(percent);
  currentPer = percent;
  direction = lef;
} // left

int Servo::increase(int percent)
{
  int flag;

  switch(direction)
  {
    currentPer += percent;
    if(currentPer > 100)
    {
      currentPer = 100;
      flag = -1;
    }
    else flag = 0;

    case rite:
      right(currentPer);
      break;
    case cent:
      right(currentPer);
      break;
    case lef:
      left(currentPer);
      break;
  }
  return flag;
} // increase

int Servo::decrease(int percent)
{
  switch(direction)
  {
    currentPer -= percent;
    if(currentPer <= 0)
    {
      currentPer = 0;
      direction = cent;
      center();
      return -1;
    }

    case rite:
      right(currentPer);
      break;
    case cent:
      left(currentPer);
      break;
    case lef:
      left(currentPer);
      break;
  }
  return 0;
} // decrease
