floatpump-firmware/Core/Src/button_input.c

52 lines
1.2 KiB
C

//
// Created by robtor on 16.12.22.
//
#include "main.h"
#include "button_input.h"
bool INP_button_state;
int8_t last;
int8_t enc_delta;
int16_t rot_value;
int8_t val;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if(HAL_GPIO_ReadPin(RRSW_GPIO_Port, RRSW_Pin) == GPIO_PIN_SET) {
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
INP_button_state = false;
} else {
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
INP_button_state = true;
}
// int8_t new = 0;
//
// if (HAL_GPIO_ReadPin(RRCLK_GPIO_Port, RRCLK_Pin) == GPIO_PIN_RESET)
// new = 1;
//
// if (new != last) {
// if(HAL_GPIO_ReadPin(RRDT_GPIO_Port, RRDT_Pin) == GPIO_PIN_RESET) {
// val++;
// } else {
// val --;
// }
// }
//
// last = new;
int8_t new = 0;
if (HAL_GPIO_ReadPin(RRCLK_GPIO_Port, RRCLK_Pin) == GPIO_PIN_SET) new = 3;
if (HAL_GPIO_ReadPin(RRDT_GPIO_Port, RRDT_Pin) == GPIO_PIN_SET) new ^= 1;
int8_t diff = last - new;
if( diff & 1) {
last = new;
enc_delta += (diff & 2) -1;
}
val = enc_delta;
enc_delta = val & 3; val >> 2;
}