/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim1;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM1_Init(void);
/* USER CODE BEGIN 0 */
#define DIR_PIN GPIO_PIN_1
#define DIR_PORT GPIOA
#define STEP_PIN GPIO_PIN_2
#define STEP_PORT GPIOA
int stepDelay = 1000; // 1000us more delay means less speed
void microDelay (uint16_t delay)
{
  __HAL_TIM_SET_COUNTER(&htim1, 0);
  while (__HAL_TIM_GET_COUNTER(&htim1) < delay);
}
void motorMoveTo(double distance, int direction, int SMAX);
/* USER CODE END 0 */
int main(void)
{
  /* USER CODE BEGIN 1 */
  /* USER CODE END 1 */
  /* MCU Configuration--------------------------------------------------------*/
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  /* USER CODE BEGIN Init */
  /* USER CODE END Init */
  /* Configure the system clock */
  SystemClock_Config();
  /* USER CODE BEGIN SysInit */
  /* USER CODE END SysInit */
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM1_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_Base_Start(&htim1);
  /* USER CODE END 2 */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
    {
            motorMoveTo(50.50,1,3400);
            HAL_Delay(1000);
            motorMoveTo(50.50,0,3400);
            HAL_Delay(1000);
        /* USER CODE END WHILE */
      /* USER CODE BEGIN 3 */
    }
    /* USER CODE END 3 */
}
/* USER CODE BEGIN 4 */
void motorMoveTo(double distance, int direction, int SMAX) //(distância, direção,
tempo da largura do pulso em microssegundos (metade do tempo em nível alto, metade
em nível baixo))
{
int ACC = 15;
long SMIN = 20000;
int SPM = 800;
// digitalWrite(DIR, direction);
if (direction == 0)
    {
    HAL_GPIO_WritePin(DIR_PORT, DIR_PIN, GPIO_PIN_SET);
    }
    else
    {
    HAL_GPIO_WritePin(DIR_PORT, DIR_PIN, GPIO_PIN_RESET);
    }
 long accSpeed = SMIN;
 long steps = distance*SPM;
 long stepsToStop;
 int flag = 1;
 for(long i = 0; i < steps; i++){
     if(i < (steps)/2){
        if(accSpeed>SMAX){
         if(direction == 0 || direction == 1){
           HAL_GPIO_WritePin(STEP_PORT, STEP_PIN, GPIO_PIN_SET);
           microDelay(accSpeed/2);
           HAL_GPIO_WritePin(STEP_PORT, STEP_PIN, GPIO_PIN_RESET);
           microDelay(accSpeed/2);
         }
         else{
            microDelay(accSpeed);
    }
    accSpeed = accSpeed - ACC;
 }
 else
    {
         if(flag==1){
         stepsToStop = i;
         flag = 0;
         }
         if(direction == 0 || direction == 1){
           HAL_GPIO_WritePin(STEP_PORT, STEP_PIN, GPIO_PIN_SET);
           microDelay(SMAX/2);
           HAL_GPIO_WritePin(STEP_PORT, STEP_PIN, GPIO_PIN_RESET);
           microDelay(SMAX/2);
         }
         else{
              microDelay(SMAX);
         }
      }
   }
else
   {
      if(i < steps-stepsToStop){
         if(direction == 0 || direction == 1){
           HAL_GPIO_WritePin(STEP_PORT, STEP_PIN, GPIO_PIN_SET);
           microDelay(SMAX/2);
           HAL_GPIO_WritePin(STEP_PORT, STEP_PIN, GPIO_PIN_RESET);
           microDelay(SMAX/2);
         }
         else{
              microDelay(SMAX);
         }
      }
      else
      {
        if(direction == 0 || direction == 1){
             HAL_GPIO_WritePin(STEP_PORT, STEP_PIN, GPIO_PIN_SET);
             microDelay(accSpeed/2);
          HAL_GPIO_WritePin(STEP_PORT, STEP_PIN, GPIO_PIN_RESET);
          microDelay(accSpeed/2);
        }
        else{
             microDelay(accSpeed);
        }
        accSpeed = accSpeed + ACC;
      }
     }
 }
}
/* USER CODE END 4 */