Last active
April 25, 2019 22:13
-
-
Save postspectacular/61f17333c17b0206a73e4591cd5ce59b to your computer and use it in GitHub Desktop.
Simple double buffer manager for STM32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static Screen *screen; | |
int main(void) { | |
HAL_Init(); | |
SystemClock_Config(); | |
screen = ct_screen_init(); | |
while (1) { | |
ct_screen_flip_buffers(screen); | |
// put normal drawing code here... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LCD Double Buffer manager for STM32 | |
// @author toxi | |
#include <stdlib.h> | |
#include "screen.h" | |
Screen* ct_screen_init() { | |
BSP_LCD_Init(); | |
Screen *screen = (Screen*) malloc(sizeof(Screen)); | |
screen->width = BSP_LCD_GetXSize(); | |
screen->height = BSP_LCD_GetYSize(); | |
screen->addr[0] = LCD_FB_START_ADDRESS; | |
screen->addr[1] = LCD_FB_START_ADDRESS + screen->width * screen->height * 4; | |
screen->front = 1; | |
BSP_LCD_LayerDefaultInit(0, screen->addr[0]); | |
BSP_LCD_LayerDefaultInit(1, screen->addr[1]); | |
BSP_LCD_SetLayerVisible(0, DISABLE); | |
BSP_LCD_SetLayerVisible(1, ENABLE); | |
BSP_LCD_SelectLayer(0); | |
return screen; | |
} | |
void ct_screen_flip_buffers(Screen *screen) { | |
// wait for VSYNC | |
while (!(LTDC->CDSR & LTDC_CDSR_VSYNCS)); | |
BSP_LCD_SetLayerVisible(screen->front, DISABLE); | |
screen->front ^= 1; | |
BSP_LCD_SetLayerVisible(screen->front, ENABLE); | |
BSP_LCD_SelectLayer(ct_screen_backbuffer_id(screen)); | |
} | |
uint32_t* ct_screen_backbuffer_ptr(Screen *screen) { | |
return (uint32_t*)(screen->addr[ct_screen_backbuffer_id(screen)]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LCD Double Buffer manager for STM32 | |
// @author toxi | |
#pragma once | |
#include "stm32746g_discovery_lcd.h" | |
typedef struct { | |
uint32_t addr[2]; | |
uint32_t width; | |
uint32_t height; | |
uint32_t front; | |
} Screen; | |
Screen* ct_screen_init(); | |
void ct_screen_flip_buffers(Screen *screen); | |
uint32_t* ct_screen_backbuffer_ptr(Screen *screen); | |
inline uint32_t ct_screen_backbuffer_id(Screen *screen) { | |
return 1 - screen->front; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment