1 #include <twr_module_encoder.h>
2 #include <twr_scheduler.h>
5 #include <twr_system.h>
19 } _twr_module_encoder;
21 static void _twr_module_encoder_task(
void *param);
23 static void _twr_module_encoder_exti_handler(
twr_exti_line_t line,
void *param);
27 static bool _twr_module_encoder_present_test(
void);
31 memset(&_twr_module_encoder, 0,
sizeof(_twr_module_encoder));
55 if (_twr_module_encoder.initialized)
65 _twr_module_encoder.event_handler = event_handler;
67 _twr_module_encoder.event_param = event_param;
72 return &_twr_module_encoder.button;
77 return _twr_module_encoder.increment_shadow;
82 if (_twr_module_encoder.initialized)
87 return _twr_module_encoder_present_test();
90 static void _twr_module_encoder_task(
void *param)
94 if (_twr_module_encoder.initialized)
99 _twr_module_encoder.increment_shadow = _twr_module_encoder.increment;
100 _twr_module_encoder.increment = 0;
105 if (_twr_module_encoder.increment_shadow != 0)
107 if (_twr_module_encoder.event_handler != NULL)
115 if (_twr_module_encoder_present_test())
129 _twr_module_encoder.initialized =
true;
133 if (_twr_module_encoder.event_handler != NULL)
141 static void _twr_module_encoder_exti_handler(
twr_exti_line_t line,
void *param)
147 _twr_module_encoder.samples <<= 2;
150 _twr_module_encoder.samples &= 0xf;
152 if (_twr_module_encoder.samples == 0x7)
154 _twr_module_encoder.increment--;
158 else if (_twr_module_encoder.samples == 0xd)
160 _twr_module_encoder.increment++;
171 if (_twr_module_encoder.event_handler == NULL)
219 static bool _twr_module_encoder_present_test(
void)
221 twr_system_pll_enable();
231 twr_system_pll_disable();
void twr_exti_register(twr_exti_line_t line, twr_exti_edge_t edge, void(*callback)(twr_exti_line_t, void *), void *param)
Enable EXTI line interrupt and register callback function.
void twr_exti_unregister(twr_exti_line_t line)
Disable EXTI line interrupt.
twr_exti_line_t
EXTI lines.
@ TWR_EXTI_LINE_P5
EXTI line P5.
@ TWR_EXTI_LINE_P4
EXTI line P4.
@ TWR_EXTI_EDGE_RISING_AND_FALLING
EXTI line is configured to both rising and falling edge sensitivity.
void twr_gpio_set_output(twr_gpio_channel_t channel, int state)
Set output state for GPIO channel.
void twr_gpio_set_pull(twr_gpio_channel_t channel, twr_gpio_pull_t pull)
Set pull-up/pull-down configuration for GPIO channel.
void twr_gpio_init(twr_gpio_channel_t channel)
Initialize GPIO channel.
twr_gpio_channel_t
GPIO channels.
int twr_gpio_get_input(twr_gpio_channel_t channel)
Get input state for GPIO channel.
void twr_gpio_set_mode(twr_gpio_channel_t channel, twr_gpio_mode_t mode)
Set mode of operation for GPIO channel.
@ TWR_GPIO_MODE_INPUT
GPIO channel operates as input.
@ TWR_GPIO_MODE_OUTPUT
GPIO channel operates as output.
@ TWR_GPIO_MODE_ANALOG
GPIO channel operates in analog mode.
@ TWR_GPIO_P5
GPIO channel P5, A5, DAC1.
@ TWR_GPIO_BUTTON
GPIO channel BUTTON.
@ TWR_GPIO_P4
GPIO channel P4, A4, DAC0.
@ TWR_GPIO_PULL_NONE
GPIO channel has no pull-up/pull-down.
@ TWR_GPIO_PULL_DOWN
GPIO channel has pull-down.
void twr_irq_disable(void)
Disable interrupt requests globally (call can be nested)
void twr_irq_enable(void)
Enable interrupt requests globally (call can be nested)
void twr_module_encoder_set_event_handler(void(*event_handler)(twr_module_encoder_event_t, void *), void *event_param)
Set callback function.
bool twr_module_encoder_is_present(void)
Get Encoder Module is pressent, can use without twr_module_encoder_init.
twr_button_t * twr_module_encoder_get_button_instance(void)
Get encoder button instance.
void twr_module_encoder_init(void)
Initialize Encoder Module.
void twr_module_encoder_deinit(void)
Deinitialize Encoder Module.
int twr_module_encoder_get_increment(void)
Read encoder delta increment (can be positive or negative number)
twr_module_encoder_event_t
Callback events.
@ TWR_MODULE_ENCODER_EVENT_CLICK
Event button clicked (pressed and released within certain time)
@ TWR_MODULE_ENCODER_EVENT_PRESS
Event button pressed.
@ TWR_MODULE_ENCODER_EVENT_HOLD
Event button hold (pressed for longer time)
@ TWR_MODULE_ENCODER_EVENT_ERROR
Event error (module is not present)
@ TWR_MODULE_ENCODER_EVENT_ROTATION
Event rotation.
@ TWR_MODULE_ENCODER_EVENT_RELEASE
Event button released.
void twr_scheduler_unregister(twr_scheduler_task_id_t task_id)
Unregister specified task.
size_t twr_scheduler_task_id_t
Task ID assigned by scheduler.
void twr_scheduler_plan_now(twr_scheduler_task_id_t task_id)
Schedule specified task for immediate execution.
twr_scheduler_task_id_t twr_scheduler_register(void(*task)(void *), void *param, twr_tick_t tick)
Register task in scheduler.
void twr_timer_init(void)
Initialize timer.
void twr_timer_delay(uint16_t microseconds)
Relative delay.
void twr_timer_stop(void)
Stop timer.
void twr_timer_start(void)
Start timer.