//
// Created by robtor on 16.12.22.
//
#include "main.h"

extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;

bool rot_button = false;
uint16_t rot_counter = 0;
uint16_t minute_counter = 0;

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
    static uint16_t rot_state = 0;
    static uint16_t btn_state = 0;

    if(htim == &htim2) {

        rot_state =
                (rot_state << 1) | (HAL_GPIO_ReadPin(RRCLK_GPIO_Port, RRCLK_Pin) == GPIO_PIN_SET ? 0x1 : 0x0) | 0xe000;

        btn_state =
                (btn_state << 1) | (HAL_GPIO_ReadPin(RRSW_GPIO_Port, RRSW_Pin) == GPIO_PIN_SET ? 0x1 : 0x0) | 0xe000;

        if (rot_state == 0xf000) {
            rot_state = 0x0000;
            if (HAL_GPIO_ReadPin(RRDT_GPIO_Port, RRDT_Pin) == GPIO_PIN_SET) {
                rot_counter++;
            } else {
                rot_counter--;
            }
        }

        if (btn_state == 0xf000) {
            rot_button = true;
        }

    } else if (htim == &htim3) {
        minute_counter++;
    }
}