diff --git a/midi2cv/drivers/display.cc b/midi2cv/drivers/display.cc index 641e876..01c2e25 100644 --- a/midi2cv/drivers/display.cc +++ b/midi2cv/drivers/display.cc @@ -2,65 +2,19 @@ #include "gpio.h" #include "spi_mode.h" #include "stmlib/system/system_clock.h" -#include "stmlib/utils/random.h" +#include #include #include using namespace stmlib; -const uint16_t kPinEnable = GPIO_Pin_2; -const uint16_t kPinReset = GPIO_Pin_0; -const uint16_t kPinDataCommand = GPIO_Pin_9; +static const uint16_t kPinEnable = GPIO_Pin_2; +static const uint16_t kPinReset = GPIO_Pin_0; +static const uint16_t kPinDataCommand = GPIO_Pin_9; -u8g2_t u8g2_; -uint8_t* default_buf; -uint8_t second_buf[1024]; -uint8_t* output_buf; - -u8g2_t* Display::u8g2() -{ - return &u8g2_; -} - -void Display::Init() -{ - // init SS/CS/RST GPIO - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); - - GPIO_InitTypeDef gpio_init; - gpio_init.GPIO_Mode = GPIO_Mode_OUT; - gpio_init.GPIO_OType = GPIO_OType_PP; - gpio_init.GPIO_Speed = GPIO_Speed_50MHz; - gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; - gpio_init.GPIO_Pin = kPinEnable | kPinReset | kPinDataCommand; - GPIO_Init(GPIOB, &gpio_init); - - GPIO_WriteBit(GPIOB, kPinEnable, Bit_SET); - - // init AF GPIO - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); - - gpio_init.GPIO_Mode = GPIO_Mode_AF; - gpio_init.GPIO_OType = GPIO_OType_PP; - gpio_init.GPIO_Speed = GPIO_Speed_50MHz; - gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; - gpio_init.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_14 | GPIO_Pin_15; - GPIO_Init(GPIOB, &gpio_init); - GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_5); - GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_5); - GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_5); - - // init SPI - RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); - SPI_I2S_DeInit(SPI2); - // Initialize SPI - InitSPI(SPI_MODE_DISPLAY); - GPIO_WriteBit(GPIOB, kPinReset, Bit_RESET); - asm("nop"); - - GPIO_WriteBit(GPIOB, kPinReset, Bit_SET); - InitGLib(); -} +static uint8_t* default_buf; +static uint8_t second_buf[1024]; +static uint8_t* output_buf; uint8_t u8x8_stm32_gpio_and_delay(U8X8_UNUSED u8x8_t* u8x8, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, @@ -105,30 +59,84 @@ uint8_t u8x8_byte_4wire_hw_spi(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, return 1; } +class U8G2_SH1106_128x64_NONAME_F_SPI : public U8G2 { + public: + U8G2_SH1106_128x64_NONAME_F_SPI() + : U8G2() + { + u8g2_Setup_sh1106_128x64_noname_f(&this->u8g2, U8G2_R0, u8x8_byte_4wire_hw_spi, u8x8_stm32_gpio_and_delay); + output_buf = default_buf = this->u8g2.tile_buf_ptr; + /*u8g2_InitDisplay(&u8g2_); + u8g2_SetContrast(&u8g2_, 255); + u8g2_SetPowerSave(&u8g2_, 0);*/ + } +}; + +U8G2_SH1106_128x64_NONAME_F_SPI u8g2_; + +U8G2* Display::u8g2() +{ + return &u8g2_; +} + +void Display::Init() +{ + // init SS/CS/RST GPIO + RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); + + GPIO_InitTypeDef gpio_init; + gpio_init.GPIO_Mode = GPIO_Mode_OUT; + gpio_init.GPIO_OType = GPIO_OType_PP; + gpio_init.GPIO_Speed = GPIO_Speed_50MHz; + gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; + gpio_init.GPIO_Pin = kPinEnable | kPinReset | kPinDataCommand; + GPIO_Init(GPIOB, &gpio_init); + + GPIO_WriteBit(GPIOB, kPinEnable, Bit_SET); + + // init AF GPIO + RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); + + gpio_init.GPIO_Mode = GPIO_Mode_AF; + gpio_init.GPIO_OType = GPIO_OType_PP; + gpio_init.GPIO_Speed = GPIO_Speed_50MHz; + gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; + gpio_init.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_14 | GPIO_Pin_15; + GPIO_Init(GPIOB, &gpio_init); + GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_5); + GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_5); + GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_5); + + // init SPI + RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); + SPI_I2S_DeInit(SPI2); + // Initialize SPI + InitSPI(SPI_MODE_DISPLAY); + GPIO_WriteBit(GPIOB, kPinReset, Bit_RESET); + asm("nop"); + + GPIO_WriteBit(GPIOB, kPinReset, Bit_SET); + InitGLib(); +} + void Display::InitGLib() { - u8g2_Setup_sh1106_128x64_noname_f(&u8g2_, U8G2_R0, u8x8_byte_4wire_hw_spi, u8x8_stm32_gpio_and_delay); - output_buf = default_buf = u8g2_.tile_buf_ptr; - u8g2_InitDisplay(&u8g2_); - u8g2_SetContrast(&u8g2_, 255); - u8g2_SetPowerSave(&u8g2_, 0); - - Random::Seed(42); + u8g2_.begin(); } void Display::Flush() { - uint8_t* cache = u8g2_.tile_buf_ptr; - u8g2_.tile_buf_ptr = output_buf; - u8g2_SendBuffer(&u8g2_); - u8g2_.tile_buf_ptr = cache; + uint8_t* cache = u8g2_.getU8g2()->tile_buf_ptr; + u8g2_.getU8g2()->tile_buf_ptr = output_buf; + u8g2_.sendBuffer(); + u8g2_.getU8g2()->tile_buf_ptr = cache; } void Display::Swap() { - output_buf = u8g2_.tile_buf_ptr; + output_buf = u8g2_.getU8g2()->tile_buf_ptr; if (output_buf == default_buf) - u8g2_.tile_buf_ptr = second_buf; + u8g2_.getU8g2()->tile_buf_ptr = second_buf; else - u8g2_.tile_buf_ptr = default_buf; + u8g2_.getU8g2()->tile_buf_ptr = default_buf; } diff --git a/midi2cv/drivers/display.h b/midi2cv/drivers/display.h index e19f419..30694d4 100644 --- a/midi2cv/drivers/display.h +++ b/midi2cv/drivers/display.h @@ -3,7 +3,7 @@ #include "stmlib/stmlib.h" #include -#include +#include #define DISPLAY_WIDTH 128 #define DISPLAY_HEIGHT 64 @@ -14,7 +14,7 @@ public: ~Display() {} void Init(); - u8g2_t* u8g2(); + U8G2* u8g2(); void Flush(); void Swap(); diff --git a/midi2cv/menu/menu.cc b/midi2cv/menu/menu.cc index ff812f4..62b14e1 100644 --- a/midi2cv/menu/menu.cc +++ b/midi2cv/menu/menu.cc @@ -4,7 +4,7 @@ const int kMenuItemHeight = 12; -void Menu::render(u8g2_t* u8g2_, uint8_t xStart, uint8_t yStart, uint8_t width, uint8_t height) +void Menu::render(U8G2* u8g2_, uint8_t xStart, uint8_t yStart, uint8_t width, uint8_t height) { this->width = width; this->height = height; @@ -12,7 +12,7 @@ void Menu::render(u8g2_t* u8g2_, uint8_t xStart, uint8_t yStart, uint8_t width, uint8_t maxVisibleItems = height / kMenuItemHeight; uint8_t itemsToRender = std::min(maxVisibleItems, uint8_t(this->itemCount - currentScrollStart)); - u8g2_SetFont(u8g2_, u8g2_font_6x10_tf); + u8g2_->setFont(u8g2_font_6x10_tf); for (uint8_t i = 0; i < itemsToRender; i++) { bool selected = this->selectedItem == (i + this->currentScrollStart); bool editing = this->currentEditingItem == (i + this->currentScrollStart); @@ -20,19 +20,19 @@ void Menu::render(u8g2_t* u8g2_, uint8_t xStart, uint8_t yStart, uint8_t width, AbstractMenuItem* item = this->items[i + this->currentScrollStart]; - u8g2_SetDrawColor(u8g2_, selected ? 1 : 0); + u8g2_->setDrawColor(selected ? 1 : 0); if (editing) { - u8g2_DrawFrame(u8g2_, xStart, yPosition, width, kMenuItemHeight); + u8g2_->drawFrame(xStart, yPosition, width, kMenuItemHeight); } else if (selected) { - u8g2_DrawBox(u8g2_, xStart, yPosition, width, kMenuItemHeight); + u8g2_->drawBox(xStart, yPosition, width, kMenuItemHeight); } - u8g2_SetDrawColor(u8g2_, editing || !selected ? 1 : 0); - u8g2_DrawStr(u8g2_, xStart + 2, yPosition + kMenuItemHeight - 3, item->get_label()); + u8g2_->setDrawColor(editing || !selected ? 1 : 0); + u8g2_->drawStr(xStart + 2, yPosition + kMenuItemHeight - 3, item->get_label()); - uint8_t valueStringWidth = u8g2_GetStrWidth(u8g2_, item->get_string_representation()); - u8g2_DrawStr(u8g2_, xStart + width - valueStringWidth - 2, yPosition + kMenuItemHeight - 3, item->get_string_representation()); + uint8_t valueStringWidth = u8g2_->getStrWidth(item->get_string_representation()); + u8g2_->drawStr(xStart + width - valueStringWidth - 2, yPosition + kMenuItemHeight - 3, item->get_string_representation()); } } diff --git a/midi2cv/menu/menu.h b/midi2cv/menu/menu.h index 6f0738c..fd18be6 100644 --- a/midi2cv/menu/menu.h +++ b/midi2cv/menu/menu.h @@ -2,7 +2,7 @@ #include "menu_items.h" -#include +#include #define MAXIMUM_MENU_ITEM_COUNT 16 @@ -25,7 +25,7 @@ class Menu { void up(); void down(); - bool back(); // returns true true if nothing happened here and the action can be delegated to up + bool back(); // returns true true if nothing happened here and the action can be delegated to up bool enter(); // returns true if it wants to give up control void add_item(AbstractMenuItem* item) @@ -35,5 +35,5 @@ class Menu { } } - void render(u8g2_t* u8g2_, uint8_t x, uint8_t y, uint8_t width, uint8_t height); + void render(U8G2* u8g2_, uint8_t x, uint8_t y, uint8_t width, uint8_t height); }; diff --git a/midi2cv/ui.cc b/midi2cv/ui.cc index afc3c32..7ef8675 100644 --- a/midi2cv/ui.cc +++ b/midi2cv/ui.cc @@ -49,7 +49,7 @@ void UI::Poll() void UI::Draw() { - u8g2_ClearBuffer(display.u8g2()); + display.u8g2()->clearBuffer(); mainMenu.render(display.u8g2(), 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT); diff --git a/midi2cv/ui/main_menu.cc b/midi2cv/ui/main_menu.cc index 0aa0dd4..52eb9bf 100644 --- a/midi2cv/ui/main_menu.cc +++ b/midi2cv/ui/main_menu.cc @@ -43,30 +43,30 @@ void MainMenu::down() } } -void MainMenu::render(u8g2_t* u8g2, int x, int y, int width, int height) +void MainMenu::render(U8G2* u8g2, int x, int y, int width, int height) { - u8g2_SetFont(u8g2, /*u8g2_font_5x8_tf*/ u8g2_font_pcsenior_8u); + u8g2->setFont(/*u8g2_font_5x8_tf*/ u8g2_font_pcsenior_8u); for (int i = 0; i < PART_COUNT; i++) { - u8g2_SetFontMode(u8g2, 1); - u8g2_SetDrawColor(u8g2, 1); + u8g2->setFontMode(1); + u8g2->setDrawColor(1); if (this->selectedPart == i) { if (this->activePartMenu == i) { - u8g2_DrawBox(u8g2, x + 1 + i * (width / PART_COUNT), y + 1, (width / PART_COUNT) - 3, kHeaderHeight - 2); + u8g2->drawBox(x + 1 + i * (width / PART_COUNT), y + 1, (width / PART_COUNT) - 3, kHeaderHeight - 2); } else { - u8g2_DrawFrame(u8g2, x + 1 + i * (width / PART_COUNT), y + 1, (width / PART_COUNT) - 3, kHeaderHeight - 2); - u8g2_DrawFrame(u8g2, x + i * (width / PART_COUNT), y, (width / PART_COUNT) - 1, kHeaderHeight); + u8g2->drawFrame(x + 1 + i * (width / PART_COUNT), y + 1, (width / PART_COUNT) - 3, kHeaderHeight - 2); + u8g2->drawFrame(x + i * (width / PART_COUNT), y, (width / PART_COUNT) - 1, kHeaderHeight); } } else { - u8g2_DrawFrame(u8g2, x + 1 + i * (width / PART_COUNT), y + 1, (width / PART_COUNT) - 3, kHeaderHeight - 2); + u8g2->drawFrame(x + 1 + i * (width / PART_COUNT), y + 1, (width / PART_COUNT) - 3, kHeaderHeight - 2); } - u8g2_SetDrawColor(u8g2, 2); - u8g2_DrawStr(u8g2, x + i * (width / PART_COUNT) + 5, y + 9, kPartNames[i]); - u8g2_SetDrawColor(u8g2, 1); + u8g2->setDrawColor(2); + u8g2->drawStr(x + i * (width / PART_COUNT) + 5, y + 9, kPartNames[i]); + u8g2->setDrawColor(1); } - u8g2_DrawHLine(u8g2, 0, kHeaderHeight + 1, width); + u8g2->drawHLine(0, kHeaderHeight + 1, width); this->partMenus[this->selectedPart].render(u8g2, x, y + kHeaderHeight + 3, width, height - kHeaderHeight - 3); } diff --git a/midi2cv/ui/main_menu.h b/midi2cv/ui/main_menu.h index adc7c21..319a6d5 100644 --- a/midi2cv/ui/main_menu.h +++ b/midi2cv/ui/main_menu.h @@ -2,7 +2,7 @@ #include "../config.h" #include "part_menu.h" -#include +#include class MainMenu { public: @@ -11,7 +11,7 @@ class MainMenu { void up(); void down(); - void render(u8g2_t* u8g2, int x, int y, int width, int height); + void render(U8G2* u8g2, int x, int y, int width, int height); MainMenu() : activePartMenu(0) diff --git a/midi2cv/ui/part_menu.cc b/midi2cv/ui/part_menu.cc index cfcf1fb..5b0b204 100644 --- a/midi2cv/ui/part_menu.cc +++ b/midi2cv/ui/part_menu.cc @@ -54,7 +54,7 @@ bool PartMenu::enter() return false; } -void PartMenu::render(u8g2_t* u8g2, int x, int y, int width, int height) +void PartMenu::render(U8G2* u8g2, int x, int y, int width, int height) { menu.render(u8g2, x, y, width, height); } diff --git a/midi2cv/ui/part_menu.h b/midi2cv/ui/part_menu.h index a7115bc..a5c1709 100644 --- a/midi2cv/ui/part_menu.h +++ b/midi2cv/ui/part_menu.h @@ -2,7 +2,7 @@ #include "../menu/menu.h" #include "../menu/menu_items.h" -#include +#include class PartMenu { public: @@ -13,7 +13,7 @@ class PartMenu { void up(); void down(); - void render(u8g2_t* u8g2, int x, int y, int width, int height); + void render(U8G2* u8g2, int x, int y, int width, int height); private: Menu menu; diff --git a/stmlib b/stmlib index 0f1dc19..99e1ad5 160000 --- a/stmlib +++ b/stmlib @@ -1 +1 @@ -Subproject commit 0f1dc19f23d34b6f282558794bc089a147015d4f +Subproject commit 99e1ad5fb7b241e13ec780c00ef1c3fd0200fdac