/*
 * inputs.c
 *
 *  Created on: Oct 29, 2022
 *      Author: mcfly
 */
#include "inputs.h"
#include "st7789.h"

#define BTNPRT				GPIOC
#define ENCBTNPRT			BGIOB
#define ENCBTNPRTMASK	0b11001000
#define SCRBTNPRT			BGIOD
#define SCRBTNPRTMASK	0b1110000

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
	static uint16_t btnbuff = 0xFFFF;
	static uint16_t btnprev = 0xFFFF;
	uint8_t	i;
	uint16_t pin;
	GPIO_TypeDef *port;

	//check if the interrupt comes from TIM6
	if(htim->Instance == TIM6) {
		btnbuff = BTNPRT->IDR;
		for (i=5; i>=2; i--) {
			if ( !((btnbuff>>i) & 1) && ((btnprev>>i) & 1) ) {
				switch (i) {
					case 5:
						port = CH1_EN_GPIO_Port;
						pin  = CH1_EN_Pin;
						break;
					case 4:
						port = CH2_EN_GPIO_Port;
						pin  = CH2_EN_Pin;
						break;
					case 3:
						port = PWM1_EN_GPIO_Port;
						pin  = PWM1_EN_Pin;
						break;
					case 2:
						port = PWM2_EN_GPIO_Port;
						pin  = PWM2_EN_Pin;
						break;
				}
				HAL_GPIO_TogglePin(port, pin);
			}
		}
		btnprev = btnbuff;
	}

}

