Switch to C++ interface of U8G2.

This commit is contained in:
Jan-Henrik 2020-02-23 11:15:27 +01:00
parent cb2dec87b9
commit cc015fbdc8
10 changed files with 108 additions and 100 deletions

View file

@ -2,65 +2,19 @@
#include "gpio.h" #include "gpio.h"
#include "spi_mode.h" #include "spi_mode.h"
#include "stmlib/system/system_clock.h" #include "stmlib/system/system_clock.h"
#include "stmlib/utils/random.h" #include <U8g2lib.h>
#include <stm32f37x_conf.h> #include <stm32f37x_conf.h>
#include <u8g2.h> #include <u8g2.h>
using namespace stmlib; using namespace stmlib;
const uint16_t kPinEnable = GPIO_Pin_2; static const uint16_t kPinEnable = GPIO_Pin_2;
const uint16_t kPinReset = GPIO_Pin_0; static const uint16_t kPinReset = GPIO_Pin_0;
const uint16_t kPinDataCommand = GPIO_Pin_9; static const uint16_t kPinDataCommand = GPIO_Pin_9;
u8g2_t u8g2_; static uint8_t* default_buf;
uint8_t* default_buf; static uint8_t second_buf[1024];
uint8_t second_buf[1024]; static uint8_t* output_buf;
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();
}
uint8_t u8x8_stm32_gpio_and_delay(U8X8_UNUSED u8x8_t* u8x8, uint8_t u8x8_stm32_gpio_and_delay(U8X8_UNUSED u8x8_t* u8x8,
U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, 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; 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() void Display::InitGLib()
{ {
u8g2_Setup_sh1106_128x64_noname_f(&u8g2_, U8G2_R0, u8x8_byte_4wire_hw_spi, u8x8_stm32_gpio_and_delay); u8g2_.begin();
output_buf = default_buf = u8g2_.tile_buf_ptr;
u8g2_InitDisplay(&u8g2_);
u8g2_SetContrast(&u8g2_, 255);
u8g2_SetPowerSave(&u8g2_, 0);
Random::Seed(42);
} }
void Display::Flush() void Display::Flush()
{ {
uint8_t* cache = u8g2_.tile_buf_ptr; uint8_t* cache = u8g2_.getU8g2()->tile_buf_ptr;
u8g2_.tile_buf_ptr = output_buf; u8g2_.getU8g2()->tile_buf_ptr = output_buf;
u8g2_SendBuffer(&u8g2_); u8g2_.sendBuffer();
u8g2_.tile_buf_ptr = cache; u8g2_.getU8g2()->tile_buf_ptr = cache;
} }
void Display::Swap() void Display::Swap()
{ {
output_buf = u8g2_.tile_buf_ptr; output_buf = u8g2_.getU8g2()->tile_buf_ptr;
if (output_buf == default_buf) if (output_buf == default_buf)
u8g2_.tile_buf_ptr = second_buf; u8g2_.getU8g2()->tile_buf_ptr = second_buf;
else else
u8g2_.tile_buf_ptr = default_buf; u8g2_.getU8g2()->tile_buf_ptr = default_buf;
} }

View file

@ -3,7 +3,7 @@
#include "stmlib/stmlib.h" #include "stmlib/stmlib.h"
#include <stm32f37x_conf.h> #include <stm32f37x_conf.h>
#include <u8g2.h> #include <U8g2lib.h>
#define DISPLAY_WIDTH 128 #define DISPLAY_WIDTH 128
#define DISPLAY_HEIGHT 64 #define DISPLAY_HEIGHT 64
@ -14,7 +14,7 @@ public:
~Display() {} ~Display() {}
void Init(); void Init();
u8g2_t* u8g2(); U8G2* u8g2();
void Flush(); void Flush();
void Swap(); void Swap();

View file

@ -4,7 +4,7 @@
const int kMenuItemHeight = 12; 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->width = width;
this->height = height; 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 maxVisibleItems = height / kMenuItemHeight;
uint8_t itemsToRender = std::min(maxVisibleItems, uint8_t(this->itemCount - currentScrollStart)); 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++) { for (uint8_t i = 0; i < itemsToRender; i++) {
bool selected = this->selectedItem == (i + this->currentScrollStart); bool selected = this->selectedItem == (i + this->currentScrollStart);
bool editing = this->currentEditingItem == (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]; AbstractMenuItem* item = this->items[i + this->currentScrollStart];
u8g2_SetDrawColor(u8g2_, selected ? 1 : 0); u8g2_->setDrawColor(selected ? 1 : 0);
if (editing) { if (editing) {
u8g2_DrawFrame(u8g2_, xStart, yPosition, width, kMenuItemHeight); u8g2_->drawFrame(xStart, yPosition, width, kMenuItemHeight);
} else if (selected) { } else if (selected) {
u8g2_DrawBox(u8g2_, xStart, yPosition, width, kMenuItemHeight); u8g2_->drawBox(xStart, yPosition, width, kMenuItemHeight);
} }
u8g2_SetDrawColor(u8g2_, editing || !selected ? 1 : 0); u8g2_->setDrawColor(editing || !selected ? 1 : 0);
u8g2_DrawStr(u8g2_, xStart + 2, yPosition + kMenuItemHeight - 3, item->get_label()); u8g2_->drawStr(xStart + 2, yPosition + kMenuItemHeight - 3, item->get_label());
uint8_t valueStringWidth = u8g2_GetStrWidth(u8g2_, item->get_string_representation()); uint8_t valueStringWidth = u8g2_->getStrWidth(item->get_string_representation());
u8g2_DrawStr(u8g2_, xStart + width - valueStringWidth - 2, yPosition + kMenuItemHeight - 3, item->get_string_representation()); u8g2_->drawStr(xStart + width - valueStringWidth - 2, yPosition + kMenuItemHeight - 3, item->get_string_representation());
} }
} }

View file

@ -2,7 +2,7 @@
#include "menu_items.h" #include "menu_items.h"
#include <u8g2.h> #include <U8g2lib.h>
#define MAXIMUM_MENU_ITEM_COUNT 16 #define MAXIMUM_MENU_ITEM_COUNT 16
@ -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);
}; };

View file

@ -49,7 +49,7 @@ void UI::Poll()
void UI::Draw() void UI::Draw()
{ {
u8g2_ClearBuffer(display.u8g2()); display.u8g2()->clearBuffer();
mainMenu.render(display.u8g2(), 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT); mainMenu.render(display.u8g2(), 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT);

View file

@ -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++) { for (int i = 0; i < PART_COUNT; i++) {
u8g2_SetFontMode(u8g2, 1); u8g2->setFontMode(1);
u8g2_SetDrawColor(u8g2, 1); u8g2->setDrawColor(1);
if (this->selectedPart == i) { if (this->selectedPart == i) {
if (this->activePartMenu == 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 { } 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_DrawFrame(u8g2, x + i * (width / PART_COUNT), y, (width / PART_COUNT) - 1, kHeaderHeight); u8g2->drawFrame(x + i * (width / PART_COUNT), y, (width / PART_COUNT) - 1, kHeaderHeight);
} }
} else { } 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->setDrawColor(2);
u8g2_DrawStr(u8g2, x + i * (width / PART_COUNT) + 5, y + 9, kPartNames[i]); u8g2->drawStr(x + i * (width / PART_COUNT) + 5, y + 9, kPartNames[i]);
u8g2_SetDrawColor(u8g2, 1); 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); this->partMenus[this->selectedPart].render(u8g2, x, y + kHeaderHeight + 3, width, height - kHeaderHeight - 3);
} }

View file

@ -2,7 +2,7 @@
#include "../config.h" #include "../config.h"
#include "part_menu.h" #include "part_menu.h"
#include <u8g2.h> #include <U8g2lib.h>
class MainMenu { class MainMenu {
public: public:
@ -11,7 +11,7 @@ class MainMenu {
void up(); void up();
void down(); 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() MainMenu()
: activePartMenu(0) : activePartMenu(0)

View file

@ -54,7 +54,7 @@ bool PartMenu::enter()
return false; 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); menu.render(u8g2, x, y, width, height);
} }

View file

@ -2,7 +2,7 @@
#include "../menu/menu.h" #include "../menu/menu.h"
#include "../menu/menu_items.h" #include "../menu/menu_items.h"
#include <u8g2.h> #include <U8g2lib.h>
class PartMenu { class PartMenu {
public: public:
@ -13,7 +13,7 @@ class PartMenu {
void up(); void up();
void down(); 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: private:
Menu menu; Menu menu;

2
stmlib

@ -1 +1 @@
Subproject commit 0f1dc19f23d34b6f282558794bc089a147015d4f Subproject commit 99e1ad5fb7b241e13ec780c00ef1c3fd0200fdac