eurorack/midi2cv/ui.cc

112 lines
2.2 KiB
C++
Raw Normal View History

2019-09-19 14:41:32 +00:00
#include "ui.h"
#include "drivers/display.h"
2019-10-28 17:56:37 +00:00
#include "midi2cv/drivers/encoder.h"
#include "part.h"
2019-09-19 14:41:32 +00:00
#include "stmlib/utils/random.h"
#include <u8g2.h>
2019-09-19 14:41:32 +00:00
using namespace stmlib;
#define HEADER_HEIGHT 16
const char part_names[4][2] = { "A", "B", "C", "D" };
2019-10-28 17:56:37 +00:00
void UI::Init()
2019-09-19 14:41:32 +00:00
{
2019-10-28 17:56:37 +00:00
input_queue.Init();
}
void UI::Poll()
{
encoder.Debounce();
int32_t increment = encoder.increment();
if (increment != 0) {
input_queue.AddEvent(CONTROL_ENCODER, 0, increment);
}
}
void UI::Draw() {
2019-10-22 19:54:33 +00:00
u8g2_ClearBuffer(display.u8g2());
2019-10-28 17:56:37 +00:00
switch (current_menu) {
case MENU_PART_1:
case MENU_PART_2:
case MENU_PART_3:
case MENU_PART_4:
DrawHeader();
DrawPartMenu(current_menu);
break;
default:
break;
}
2019-10-28 17:56:37 +00:00
2019-10-22 19:54:33 +00:00
display.Swap();
}
void UI::DrawHeader()
{
2019-10-22 19:54:33 +00:00
u8g2_SetFont(display.u8g2(), u8g2_font_6x12_tf);
for (int i = 0; i < PART_COUNT; i++) {
u8g2_SetFontMode(display.u8g2(), 1);
u8g2_SetDrawColor(display.u8g2(), 1);
if (current_menu == i)
2019-10-22 19:54:33 +00:00
u8g2_DrawBox(display.u8g2(), i * (DISPLAY_WIDTH / PART_COUNT), 0, (DISPLAY_WIDTH / PART_COUNT) - 1, HEADER_HEIGHT);
else
2019-10-22 19:54:33 +00:00
u8g2_DrawFrame(display.u8g2(), i * (DISPLAY_WIDTH / PART_COUNT), 0, (DISPLAY_WIDTH / PART_COUNT) - 1, HEADER_HEIGHT);
u8g2_SetDrawColor(display.u8g2(), 2);
2019-10-22 19:54:33 +00:00
u8g2_DrawStr(display.u8g2(), i * (DISPLAY_WIDTH / PART_COUNT) + 5, 2 + 10, part_names[i]);
u8g2_SetDrawColor(display.u8g2(), 1);
}
}
void UI::DrawPartMenu(Menu_t menu)
{
}
void UI::Flush()
{
display.Flush();
2019-09-19 14:41:32 +00:00
}
2019-10-28 17:56:37 +00:00
bool UI::DoEvents()
{
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) {
input_queue.Touch();
Draw();
}
return refresh_display;
}
void UI::OnClick()
{
}
void UI::OnLongClick()
{
}
void UI::OnIncrement(Event& e)
{
current_menu = (Menu_t) (((uint32_t)current_menu + e.data) % MENU_COUNT);
}