
#include <Arduino.h>
#include "pins_arduino.h"

#include "mik32_hal_timer16.h"
#include "mik32_hal_irq.h"
#include "wiring_LL.h" 

#include "Servo.h"


static servo_t servos[MAX_SERVOS];                         // static array of servo structures
static volatile int8_t currentServoIndex[_Nbr_16timers];   // index for the servo being pulsed for each timer (or -1 if refresh interval)
uint8_t servoCount = 0;                                    // the total number of attached servos

uint16_t Period = 0xFFFF;
uint16_t Compare = 0xFFFF;
uint8_t prescaler = TIMER16_PRESCALER_16;

#define usToTicks(_us)    ((clockCyclesPerMicrosecond() * _us) / ( (1 << prescaler) ))                 // converts microseconds to ticks (TIMERx_PRESCALER_16)
#define ticksToUs(_ticks) (((unsigned) _ticks * ( (1 << prescaler) )) / clockCyclesPerMicrosecond())   // converts from ticks back to microseconds

#define SERVO_MIN()       (MIN_PULSE_WIDTH - this->min * 4)      // minimum value in us for this servo
#define SERVO_MAX()       (MAX_PULSE_WIDTH - this->max * 4)      // maximum value in us for this servo

#define TRIM_DURATION     5   // compensation ticks to trim adjust for digitalWrite delays

// convenience macros
#define SERVO_INDEX_TO_TIMER(_servo_nbr)    ((timer16_Sequence_t)(_servo_nbr / SERVOS_PER_TIMER)) // returns the timer controlling this servo
#define SERVO_INDEX_TO_CHANNEL(_servo_nbr)  (_servo_nbr % SERVOS_PER_TIMER)                       // returns the index of the servo on this timer
#define SERVO_INDEX(_timer,_channel)        ((_timer*SERVOS_PER_TIMER) + _channel)                // macro to access servo index by timer and channel
#define SERVO(_timer,_channel)              (servos[SERVO_INDEX(_timer,_channel)])                // macro to access servo class by timer and channel

// timer handler
Timer16_HandleTypeDef htimer16;

void Timer16_2_Init()
{
  htimer16.Instance             = TIMER16_2;
  htimer16.Clock.Source         = TIMER16_SOURCE_INTERNAL_SYSTEM;
  // prescaler depends on required frequency
  htimer16.Clock.Prescaler      = prescaler; 
  htimer16.CountMode            = TIMER16_COUNTMODE_INTERNAL;
  htimer16.ActiveEdge           = TIMER16_ACTIVEEDGE_RISING;
  htimer16.Preload              = TIMER16_PRELOAD_AFTERWRITE;
  htimer16.Trigger.Source       = TIMER16_TRIGGER_TIM2_GPIO2_3; 
  // timer triggers by software
  htimer16.Trigger.ActiveEdge   = TIMER16_TRIGGER_ACTIVEEDGE_SOFTWARE;
  htimer16.Trigger.TimeOut      = TIMER16_TIMEOUT_DISABLE; 
  htimer16.Filter.ExternalClock = TIMER16_FILTER_NONE;
  htimer16.Filter.Trigger       = TIMER16_FILTER_NONE;
  htimer16.Waveform.Enable      = TIMER16_WAVEFORM_GENERATION_ENABLE;
  htimer16.Waveform.Polarity    = TIMER16_WAVEFORM_POLARITY_NONINVERTED;
  htimer16.EncoderMode          = TIMER16_ENCODER_DISABLE;
  HAL_Timer16_Init(&htimer16);
}


/************ static functions common to all instances ***********************/

// irq handler
extern "C" void __attribute__((optimize("O3"))) servo_interrupt_handler(void)
{
  if (TIM16_GET_ARRM_INT_STATUS(htimer16))
  {
    timer16_Sequence_t timer = _timer1;
    
    if( currentServoIndex[timer] < 0 )
      Compare = 0;
    else{
      if( SERVO_INDEX(timer, currentServoIndex[timer]) < servoCount && SERVO(timer, currentServoIndex[timer]).Pin.isActive == true )
        digitalWrite( SERVO(timer,currentServoIndex[timer]).Pin.nbr, LOW ); // pulse this channel low if activated
    }

    currentServoIndex[timer]++;    // increment to the next channel
    if( SERVO_INDEX(timer, currentServoIndex[timer]) < servoCount && currentServoIndex[timer] < SERVOS_PER_TIMER) {
      Period = Compare + SERVO(timer, currentServoIndex[timer]).ticks;
      if(SERVO(timer, currentServoIndex[timer]).Pin.isActive == true)     // check if activated
        digitalWrite( SERVO(timer, currentServoIndex[timer]).Pin.nbr, HIGH ); // it's an active channel so pulse it high
    }
    else {
      // finished all channels so wait for the refresh period to expire before starting over
      if( ((unsigned)Compare) + 4 < usToTicks(REFRESH_INTERVAL) )  // allow a few ticks to ensure the next OCR1A not missed
        Period = (unsigned int)usToTicks(REFRESH_INTERVAL);
      else
        Period = Compare + 4;  // at least REFRESH_INTERVAL has elapsed
      currentServoIndex[timer] = -1; // this will get incremented at the end of the refresh period to start again at the first channel
    }
    HAL_Timer16_Counter_Start_IT(&htimer16, Period); 
  }

  // reset timer interrupt flags
  TIM16_CLEAR_INT_MASK(htimer16, 0xFFFFFFFF);
}

// init isr
static void initISR(timer16_Sequence_t timer)
{
  if (timer == _timer1)
  {
    Timer16_2_Init();  
    HAL_EPIC_MaskLevelSet(HAL_EPIC_TIMER16_2_MASK);
    HAL_Timer16_Counter_Start_IT(&htimer16, 0xFFFF);  
  }
}

// deinit isr
static void finISR(timer16_Sequence_t timer)
{
  if (timer == _timer1)
  {
    TIM16_DISABLE(htimer16);
    TIM16_DISABLE_INT_BY_MASK(htimer16, TIMER16_IER_ARROKIE_M | TIMER16_IER_CMPOKIE_M | TIMER16_IER_ARRMIE_M | TIMER16_IER_CMPMIE_M);
    EPIC_LEVEL_CLEAR_BY_MASK(HAL_EPIC_TIMER16_1_MASK);
  }
}

// is timer active?
static bool isTimerActive(timer16_Sequence_t timer)
{
  // returns true if any servo is active on this timer
  for(uint8_t channel = 0; channel < SERVOS_PER_TIMER; channel++) {
    if(SERVO(timer, channel).Pin.isActive == true)
      return true;
  }
  return false;
}

/****************** end of static functions ******************************/

// --------------------------------------------------
Servo::Servo()
{
  if (servoCount < MAX_SERVOS) {
    this->servoIndex = servoCount++;                    // assign a servo index to this instance
    servos[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH);   // store default values
  } else {
    this->servoIndex = INVALID_SERVO;  // too many servos
  }
};
// --------------------------------------------------


// --------------------------------------------------
uint8_t Servo::attach(int pin)
{
  return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
}
// --------------------------------------------------


// --------------------------------------------------
uint8_t Servo::attach(int pin, int min, int max)
{
  timer16_Sequence_t timer;

  if (this->servoIndex < MAX_SERVOS) {
    pinMode(pin, OUTPUT);                                   // set servo pin to output
    servos[this->servoIndex].Pin.nbr = pin;
    // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128
    this->min  = (MIN_PULSE_WIDTH - min)/4; //resolution of min/max is 4 us
    this->max  = (MAX_PULSE_WIDTH - max)/4;
    // initialize the timer if it has not already been initialized
    timer = SERVO_INDEX_TO_TIMER(servoIndex);
    if (isTimerActive(timer) == false) {
      initISR(timer);
    }
    servos[this->servoIndex].Pin.isActive = true;  // this must be set after the check for isTimerActive
  }

  return this->servoIndex;
}
// --------------------------------------------------


// --------------------------------------------------
void Servo::detach()
{
  timer16_Sequence_t timer;

  servos[this->servoIndex].Pin.isActive = false;
  timer = SERVO_INDEX_TO_TIMER(servoIndex);
  if(isTimerActive(timer) == false) {
    finISR(timer);    
    pinMode(servos[this->servoIndex].Pin.nbr, INPUT); // deinit pin
  }
}
// --------------------------------------------------


// --------------------------------------------------
void Servo::write(int value)
{
  // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
  if (value < MIN_PULSE_WIDTH)
  {
    if (value < 0)
      value = 0;
    else if (value > 180)
      value = 180;

    value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX());
  }
  writeMicroseconds(value);
}
// --------------------------------------------------


// --------------------------------------------------
void Servo::writeMicroseconds(int value)
{
  // calculate and store the values for the given channel
  byte channel = this->servoIndex;
  if( (channel < MAX_SERVOS) )   // ensure channel is valid
  {
    if (value < SERVO_MIN())          // ensure pulse width is valid
      value = SERVO_MIN();
    else if (value > SERVO_MAX())
      value = SERVO_MAX();

    value = value - TRIM_DURATION;
    value = usToTicks(value);  // convert to ticks after compensating for interrupt overhead
    servos[channel].ticks = value;
  }
}
// --------------------------------------------------


// --------------------------------------------------
int Servo::read() // return the value as degrees
{
  return map(readMicroseconds()+1, SERVO_MIN(), SERVO_MAX(), 0, 180);
}
// --------------------------------------------------


// --------------------------------------------------
int Servo::readMicroseconds()
{
  unsigned int pulsewidth;
  if (this->servoIndex != INVALID_SERVO)
    pulsewidth = ticksToUs(servos[this->servoIndex].ticks)  + TRIM_DURATION;
  else
    pulsewidth  = 0;

  return pulsewidth;
}
// --------------------------------------------------


// --------------------------------------------------
bool Servo::attached()
{
  return servos[this->servoIndex].Pin.isActive;
}
// --------------------------------------------------

