eurorack/midi2cv/ui.cc

103 lines
2 KiB
C++
Raw Normal View History

2019-09-19 14:41:32 +00:00
#include "ui.h"
#include "ui/main_menu.h"
2019-09-19 14:41:32 +00:00
#include "drivers/display.h"
2019-10-28 17:56:37 +00:00
#include "midi2cv/drivers/encoder.h"
#include "midi2cv/menu/menu.h"
#include "midi2cv/menu/menu_items.h"
#include "part.h"
2019-09-19 14:41:32 +00:00
#include "stmlib/utils/random.h"
#include <u8g2.h>
#include <array>
2019-09-19 14:41:32 +00:00
using namespace stmlib;
2020-02-21 00:39:20 +00:00
const uint32_t kEncoderLongPressTime = 600;
UI::UI(Part** part_pointers) : main_menu(part_pointers)
2019-09-19 14:41:32 +00:00
{
2020-02-23 13:28:00 +00:00
this->input_queue.Init();
2019-10-28 17:56:37 +00:00
}
void UI::Poll()
{
encoder.Debounce();
2020-02-21 00:39:20 +00:00
if (encoder.just_pressed()) {
encoder_press_time_ = system_clock.milliseconds();
encoder_long_press_event_sent_ = false;
}
if (!encoder_long_press_event_sent_) {
if (encoder.pressed()) {
uint32_t duration = system_clock.milliseconds() - encoder_press_time_;
if (duration >= kEncoderLongPressTime && !encoder_long_press_event_sent_) {
input_queue.AddEvent(CONTROL_ENCODER_LONG_CLICK, 0, 0);
encoder_long_press_event_sent_ = true;
}
}
if (encoder.released()) {
input_queue.AddEvent(CONTROL_ENCODER_CLICK, 0, 0);
}
}
2019-10-28 17:56:37 +00:00
int32_t increment = encoder.increment();
if (increment != 0) {
input_queue.AddEvent(CONTROL_ENCODER, 0, increment);
}
}
void UI::Draw()
{
2020-02-23 10:15:27 +00:00
display.u8g2()->clearBuffer();
2019-10-28 17:56:37 +00:00
main_menu.render(display.u8g2(), 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT);
2019-10-28 17:56:37 +00:00
2019-10-22 19:54:33 +00:00
display.Swap();
}
void UI::Flush()
{
display.Flush();
2019-09-19 14:41:32 +00:00
}
2019-10-28 17:56:37 +00:00
void UI::DoEvents()
2019-10-28 17:56:37 +00:00
{
bool refresh_display = false;
while (input_queue.available()) {
Event e = input_queue.PullEvent();
if (e.control_type == CONTROL_ENCODER_CLICK) {
OnClick();
} else if (e.control_type == CONTROL_ENCODER_LONG_CLICK) {
OnLongClick();
} else if (e.control_type == CONTROL_ENCODER) {
OnIncrement(e);
}
refresh_display = true;
}
if (input_queue.idle_time() > 1000) {
refresh_display = true;
}
if (refresh_display) {
2019-10-28 17:56:37 +00:00
input_queue.Touch();
Draw();
}
}
void UI::OnClick()
{
main_menu.enter();
2019-10-28 17:56:37 +00:00
}
void UI::OnLongClick()
{
main_menu.back();
2019-10-28 17:56:37 +00:00
}
void UI::OnIncrement(Event& e)
{
if (e.data > 0)
main_menu.down();
else
main_menu.up();
2019-10-28 17:56:37 +00:00
}