uC/Pinball_Controller_V0/Core/Src/main.c
void start_solenoid_driver(void *argument)
{


	// convert argument to struct
	Solenoid_task_argument *Sta = (Solenoid_task_argument*)(argument);


	// save struct in local variables to prevent errors when it is reused
	volatile uint16_t *status_button = Sta->status_button;
	TIM_HandleTypeDef *htim = Sta->htimX;
	TIM_TypeDef *TIM = Sta->TIMX;
	unsigned timer_channel = Sta->timer_channel;

  uint16_t prev_status_button = *status_button;
  uint16_t duty = 0, output = 0;

  const float iir_const = 0.05;
  HAL_TIM_PWM_Start(htim, timer_channel);

  /* Infinite loop */
	for(;;)
	{



		// solenoid should be enalbed
		if(*status_button)
		{
			// calculate duty value: iir approach
			duty = duty * (1 - iir_const) + output * iir_const;


			// if initial hit: do nothing
			if(prev_status_button != *status_button)
			{
			  output = MAX_PWM;
			}
			// if duty cycle exceeds max. rated duty cycle
			else if(duty > MAX_DUTY_CYCLE && output > MAX_DUTY_CYCLE)
			{
			  output -= 1920;
			}
			// if duty cycle below max_duty cycle
			else if(duty < MAX_DUTY_CYCLE && output < MAX_DUTY_CYCLE)
			{
			  output = MAX_DUTY_CYCLE;
			}


			HAL_GPIO_WritePin(USER_LED_GPIO_Port, USER_LED_Pin, SET);






		}
		else
		{
			HAL_GPIO_WritePin(USER_LED_GPIO_Port, USER_LED_Pin, RESET);
			output = 0;
		}


		// write pwm signal:
		// to timer instance transfered in arguments
		TIM->CCR2 = output;

		prev_status_button = *status_button;

		osDelay(50);			// delay for 50 ms


	}
  /* USER CODE END 5 */
}
/**
  * @brief  Function implementing the solenoid_driver thread.
  * @param  argument: struct of Solenoid_task_argument, to transfer interface options like timer and status_buttonQ
  * @retval None
  */
@brief  Function implementing the solenoid_driver thread.
@param  argument: struct of Solenoid_task_argument, to transfer interface options like timer and status_buttonQ
@retval None
@brief Function implementing the solenoid_driver thread . @param argument : struct of Solenoid_task_argument to transfer interface options like timer and status_buttonQ @retval None
void start_solenoid_driver(void *argument)
{
	Solenoid_task_argument *Sta = (Solenoid_task_argument*)(argument);
	volatile uint16_t *status_button = Sta->status_button;
	TIM_HandleTypeDef *htim = Sta->htimX;
	TIM_TypeDef *TIM = Sta->TIMX;
	unsigned timer_channel = Sta->timer_channel;
  uint16_t prev_status_button = *status_button;
  uint16_t duty = 0, output = 0;
  const float iir_const = 0.05;
  HAL_TIM_PWM_Start(htim, timer_channel);
	for(;;)
	{
		if(*status_button)
		{
			duty = duty * (1 - iir_const) + output * iir_const;
			if(prev_status_button != *status_button)
			{
			  output = MAX_PWM;
			}
			else if(duty > MAX_DUTY_CYCLE && output > MAX_DUTY_CYCLE)
			{
			  output -= 1920;
			}
			else if(duty < MAX_DUTY_CYCLE && output < MAX_DUTY_CYCLE)
			{
			  output = MAX_DUTY_CYCLE;
			}
			HAL_GPIO_WritePin(USER_LED_GPIO_Port, USER_LED_Pin, SET);
		}
		else
		{
			HAL_GPIO_WritePin(USER_LED_GPIO_Port, USER_LED_Pin, RESET);
			output = 0;
		}
		TIM->CCR2 = output;
		prev_status_button = *status_button;
		osDelay(50);			
	}
}
void start_solenoid_driver ( void * argument ) { Solenoid_task_argument * Sta = ( Solenoid_task_argument * ) ( argument ) ; volatile uint16_t * status_button = Sta -> status_button ; TIM_HandleTypeDef * htim = Sta -> htimX ; TIM_TypeDef * TIM = Sta -> TIMX ; unsigned timer_channel = Sta -> timer_channel ; uint16_t prev_status_button = * status_button ; uint16_t duty = 0 , output = 0 ; const float iir_const = 0.05 ; HAL_TIM_PWM_Start ( htim , timer_channel ) ; for ( ; ; ) { if ( * status_button ) { duty = duty * ( 1 - iir_const ) + output * iir_const ; if ( prev_status_button != * status_button ) { output = MAX_PWM ; } else if ( duty > MAX_DUTY_CYCLE && output > MAX_DUTY_CYCLE ) { output -= 1920 ; } else if ( duty < MAX_DUTY_CYCLE && output < MAX_DUTY_CYCLE ) { output = MAX_DUTY_CYCLE ; } HAL_GPIO_WritePin ( USER_LED_GPIO_Port , USER_LED_Pin , SET ) ; } else { HAL_GPIO_WritePin ( USER_LED_GPIO_Port , USER_LED_Pin , RESET ) ; output = 0 ; } TIM -> CCR2 = output ; prev_status_button = * status_button ; osDelay ( 50 ) ; } }
@brief  Function implementing the solenoid_driver thread.
@brief Function implementing the solenoid_driver thread .
// convert argument to struct
 // save struct in local variables to prevent errors when it is reused
 /* Infinite loop */ // solenoid should be enalbed
 // calculate duty value: iir approach
 // if initial hit: do nothing
 // if duty cycle exceeds max. rated duty cycle
 // if duty cycle below max_duty cycle
 // write pwm signal:
 // to timer instance transfered in arguments
 // delay for 50 ms
 /* USER CODE END 5 */