You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
toaster-oven-bluepill/Core/Src/app_knob.c

93 lines
2.2 KiB

2 years ago
/**
* TODO file description
*/
#include <stdbool.h>
#include "main.h"
#include "app_knob.h"
#include "tim.h"
2 years ago
#include "app_gui.h"
#include "FreeRTOS.h"
#include "queue.h"
#include "timers.h"
extern osTimerId_t buttonPushTimerHandle;
extern osTimerId_t buttonReleaseTimerHandle;
2 years ago
static struct {
uint16_t wheel;
bool push;
} s_knob = {};
void app_knob_init()
{
/* Enable the rotary encoder */
LL_TIM_CC_EnableChannel(TIM_KNOB, LL_TIM_CHANNEL_CH1 | LL_TIM_CHANNEL_CH2);
LL_TIM_EnableCounter(TIM_KNOB);
2 years ago
// for the change interrupt
LL_TIM_EnableIT_CC1(TIM_KNOB);
LL_TIM_EnableIT_CC2(TIM_KNOB);
2 years ago
}
uint16_t app_knob_get_raw() {
return LL_TIM_GetCounter(TIM_KNOB);
2 years ago
}
2 years ago
static char buf[100];
void app_knob_turn_isr()
{
// TODO
uint16_t old_wheel = s_knob.wheel;
s_knob.wheel = LL_TIM_GetCounter(TIM_KNOB);
int16_t wheel_change = (int16_t)(s_knob.wheel - old_wheel);
BaseType_t yield = pdFALSE;
while (wheel_change > 0) {
wheel_change--;
enum GuiNotify ev = GUI_NOTIFY_KNOB_PLUS;
xQueueSendFromISR(guiEventQueHandle, &ev, &yield);
}
while (wheel_change < 0) {
wheel_change++;
enum GuiNotify ev = GUI_NOTIFY_KNOB_MINUS;
xQueueSendFromISR(guiEventQueHandle, &ev, &yield);
}
portYIELD_FROM_ISR(yield);
}
// TODO use EXTI for push
bool app_knob_pushed() {
return 0 == LL_GPIO_IsInputPinSet(KNOB_PUSH_GPIO_Port, KNOB_PUSH_Pin);
}
2 years ago
void app_knob_push_isr(bool push)
{
PUTCHAR(push ? '#' : '.');
BaseType_t yield = pdFALSE;
if (push) {
xTimerStopFromISR(buttonReleaseTimerHandle, &yield);
xTimerStopFromISR(buttonPushTimerHandle, &yield);
xTimerChangePeriodFromISR(buttonPushTimerHandle, pdMS_TO_TICKS(10), &yield);
} else {
xTimerStopFromISR(buttonPushTimerHandle, &yield);
xTimerStopFromISR(buttonReleaseTimerHandle, &yield);
xTimerChangePeriodFromISR(buttonReleaseTimerHandle, pdMS_TO_TICKS(10), &yield);
}
portYIELD_FROM_ISR(yield);
}
void app_push_debounce(void *argument) {
bool push = (bool) argument;
enum GuiNotify ev = push ? GUI_NOTIFY_KNOB_PRESS : GUI_NOTIFY_KNOB_RELEASE;
xQueueSend(guiEventQueHandle, &ev, pdMS_TO_TICKS(100));
}