Implement double buffering for screen, only do transmission in interrupt handler

This commit is contained in:
Jan-Henrik 2019-09-19 17:55:48 +02:00
parent f594aa9f43
commit 8c0ad0007a
5 changed files with 40 additions and 10 deletions

View file

@ -12,6 +12,9 @@ const uint16_t kPinReset = GPIO_Pin_0;
const uint16_t kPinDataCommand = GPIO_Pin_9; const uint16_t kPinDataCommand = GPIO_Pin_9;
u8g2_t u8g2_; u8g2_t u8g2_;
uint8_t* default_buf;
uint8_t second_buf[1024];
uint8_t* output_buf;
u8g2_t* Display::u8g2() u8g2_t* Display::u8g2()
{ {
@ -115,8 +118,27 @@ uint8_t u8x8_byte_4wire_hw_spi(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int,
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_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_InitDisplay(&u8g2_);
u8g2_SetContrast(&u8g2_, 127); u8g2_SetContrast(&u8g2_, 127);
u8g2_SetPowerSave(&u8g2_, 0); u8g2_SetPowerSave(&u8g2_, 0);
Random::Seed(42); Random::Seed(42);
} }
void Display::Flush()
{
uint8_t* cache = u8g2_.tile_buf_ptr;
u8g2_.tile_buf_ptr = output_buf;
u8g2_SendBuffer(&u8g2_);
u8g2_.tile_buf_ptr = cache;
}
void Display::Swap()
{
output_buf = u8g2_.tile_buf_ptr;
if (output_buf == default_buf)
u8g2_.tile_buf_ptr = second_buf;
else
u8g2_.tile_buf_ptr = default_buf;
}

View file

@ -1,21 +1,23 @@
#ifndef MIDI2CV_DRIVERS_DISPLAY_H #ifndef MIDI2CV_DRIVERS_DISPLAY_H
#define MIDI2CV_DRIVERS_DISPLAY_H #define MIDI2CV_DRIVERS_DISPLAY_H
#include <stm32f37x_conf.h>
#include "stmlib/stmlib.h" #include "stmlib/stmlib.h"
#include <stm32f37x_conf.h>
#include <u8g2.h> #include <u8g2.h>
class Display { class Display {
public: public:
Display() { } Display() {}
~Display() {} ~Display() {}
void Init(); void Init();
u8g2_t* u8g2(); u8g2_t* u8g2();
private: void Flush();
void Swap();
private:
DISALLOW_COPY_AND_ASSIGN(Display); DISALLOW_COPY_AND_ASSIGN(Display);
void InitGLib(); void InitGLib();
}; };
extern Display display; extern Display display;
#endif #endif

View file

@ -60,7 +60,7 @@ void TIM2_IRQHandler(void)
static uint8_t count = 0; static uint8_t count = 0;
count++; count++;
if (count % 192 == 0) { if (count % 192 == 0) {
ui.Update(); ui.Flush();
count = 0; count = 0;
} }
} }
@ -112,6 +112,6 @@ int main(void)
{ {
Init(); Init();
while (1) { while (1) {
//ui.Update(); ui.Update();
} }
} }

View file

@ -27,5 +27,10 @@ void UI::Update()
dy = -1; dy = -1;
if (y <= 7) if (y <= 7)
dy = 1; dy = 1;
u8g2_SendBuffer(u8g2_); display.Swap();
}
void UI::Flush()
{
display.Flush();
} }

View file

@ -9,6 +9,7 @@ public:
~UI() {} ~UI() {}
void Update(); void Update();
void Flush();
private: private:
DISALLOW_COPY_AND_ASSIGN(UI); DISALLOW_COPY_AND_ASSIGN(UI);