From 8c0ad0007a08346018d679890be4d855a9b9e67f Mon Sep 17 00:00:00 2001 From: Jan-Henrik Bruhn Date: Thu, 19 Sep 2019 17:55:48 +0200 Subject: [PATCH] Implement double buffering for screen, only do transmission in interrupt handler --- midi2cv/drivers/display.cc | 22 ++++++++++++++++++++++ midi2cv/drivers/display.h | 16 +++++++++------- midi2cv/midi2cv.cc | 4 ++-- midi2cv/ui.cc | 7 ++++++- midi2cv/ui.h | 1 + 5 files changed, 40 insertions(+), 10 deletions(-) diff --git a/midi2cv/drivers/display.cc b/midi2cv/drivers/display.cc index 43ed223..4bf80bb 100644 --- a/midi2cv/drivers/display.cc +++ b/midi2cv/drivers/display.cc @@ -12,6 +12,9 @@ const uint16_t kPinReset = GPIO_Pin_0; 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() { @@ -115,8 +118,27 @@ uint8_t u8x8_byte_4wire_hw_spi(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, 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_, 127); u8g2_SetPowerSave(&u8g2_, 0); + 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; +} diff --git a/midi2cv/drivers/display.h b/midi2cv/drivers/display.h index dfce649..9ecee24 100644 --- a/midi2cv/drivers/display.h +++ b/midi2cv/drivers/display.h @@ -1,21 +1,23 @@ #ifndef MIDI2CV_DRIVERS_DISPLAY_H #define MIDI2CV_DRIVERS_DISPLAY_H -#include #include "stmlib/stmlib.h" +#include #include - class Display { - public: - Display() { } +public: + Display() {} ~Display() {} void Init(); - u8g2_t* u8g2(); - private: + u8g2_t* u8g2(); + void Flush(); + void Swap(); + +private: DISALLOW_COPY_AND_ASSIGN(Display); - void InitGLib(); + void InitGLib(); }; extern Display display; #endif diff --git a/midi2cv/midi2cv.cc b/midi2cv/midi2cv.cc index 7622ef9..df2ef34 100644 --- a/midi2cv/midi2cv.cc +++ b/midi2cv/midi2cv.cc @@ -60,7 +60,7 @@ void TIM2_IRQHandler(void) static uint8_t count = 0; count++; if (count % 192 == 0) { - ui.Update(); + ui.Flush(); count = 0; } } @@ -112,6 +112,6 @@ int main(void) { Init(); while (1) { - //ui.Update(); + ui.Update(); } } diff --git a/midi2cv/ui.cc b/midi2cv/ui.cc index 78bb293..8643b15 100644 --- a/midi2cv/ui.cc +++ b/midi2cv/ui.cc @@ -27,5 +27,10 @@ void UI::Update() dy = -1; if (y <= 7) dy = 1; - u8g2_SendBuffer(u8g2_); + display.Swap(); +} + +void UI::Flush() +{ + display.Flush(); } diff --git a/midi2cv/ui.h b/midi2cv/ui.h index dcb8bdd..351cd5d 100644 --- a/midi2cv/ui.h +++ b/midi2cv/ui.h @@ -9,6 +9,7 @@ public: ~UI() {} void Update(); + void Flush(); private: DISALLOW_COPY_AND_ASSIGN(UI);