#include "./motorstate.h"
#include "./motor.h"
#include "./state.h"
#include "./settings.h"
#include "include/config.h"


void motorStateMoveTo(uint16_t height)
{

}


bool motorStateCheckTargetReached()
{
  switch (State.MoveDirection)
  {
    case Direction::Up:
      if (State.CurrentHeight >= State.MoveTarget - Config::HeightMeasurementDeltaStop)
      {
        if (State.CurrentHeight - State.MoveTarget <= Config::HeightMeasurementDeltaOnTarget)
          State.CurrentHeight = State.MoveTarget;

        motorStateStop();
        return true;
      }
      break;

    case Direction::Down:
      if (State.CurrentHeight <= State.MoveTarget + Config::HeightMeasurementDeltaStop)
      {
        if (State.MoveTarget - State.CurrentHeight <= Config::HeightMeasurementDeltaOnTarget)
          State.CurrentHeight = State.MoveTarget;

        motorStateStop();
        return true;
      }
      break;

    default:
      break;
  }

  return false;
}


bool motorStateCheckOverCurrent()
{
  if (motorIsOverCurrent())
  {
    motorStateStop();
    return true;
  }

  return false;
}


void motorStateStop()
{
  motorStop();
  State.MoveDirection = Direction::None;
}


void getDisplayHeight(char* buffer, uint16_t value)
{
  uint8_t displayValue = (value + Settings.Height.Offset) / 10;

  if (displayValue > 99)
    buffer[0] = '0' + ((displayValue / 100) % 10);
  else
    buffer[0] = '0';

  buffer[1] = '.';
  buffer[2] = '0' + ((displayValue / 10) % 10);
  buffer[3] = '0' + (displayValue % 10);
  buffer[4] = 'm';
}