[GUI] mini open source GUI-uGUI quick use (applicable to OLED, LCD and other display devices)

1. uGUI requirements

uGUI official website: official website
github link: source code
uGUI use preparation:
1. Download the source code: ugui.c and ugui.h, and add them to the project of your platform;
2. Prepare the basic Display driver, in which the interface function between each platform and uGUI is a pixel filling function, taking 0.96 OLED as an example, it needs to use the drawing point function (monochrome OLED), other color screens, t corresponds to the color variable color

void OLED_DrawPoint(u8 x,u8 y,u8 t)
{
    
    
	u8 i,m,n;
	i=y/8;
	m=y%8;
	n=1<<m;
	if(t){
    
    OLED_GRAM[x][i]|=n;}
	else
	{
    
    
		OLED_GRAM[x][i]=~OLED_GRAM[x][i];
		OLED_GRAM[x][i]|=n;
		OLED_GRAM[x][i]=~OLED_GRAM[x][i];
	}
}

2. Start using

2.1 Modify ugui.h

Different platforms require us to modify the corresponding integer type in ugui.h, taking the STM32HAL library as an example:
in ugui.h we modify the user-defined integer type name to

typedef uint8_t      UG_U8;//无符号字符型
typedef int8_t       UG_S8;//有符号字符型
typedef uint16_t     UG_U16;//无符号短整型
typedef int16_t      UG_S16;//有符号短整型
typedef uint32_t     UG_U32;//无符号整型
typedef int32_t      UG_S32;//有符号整型

Different platforms need to modify uint8_t, int8_t, etc., pay attention to one-to-one correspondence.

2.2 Initialization

First perform the initialization operation
1. First define a user global GUI structure

UG_GUI my_ugui1;

2. Then call the initialization function in the main function

UG_Init(&my_ugui1,OLED_SetPFun,128,64);

Among them, OLED_SetPFun is your pixel filling function (mentioned in the preparation work), 128, 64 is the pixel size

3. Call the GUI selection function

UG_SelectGUI(&my_ugui1);

At this point, the initialization operation is completed

2.3 Simple example

A simple example is given below:
platform: STM32F103ZET6
display device: OLED (drive SH1106, 12864, 1.3 inches) (SPI, SPI DMA, analog SPI are all available, but the refresh speed is different)

OLED_Refresh() is the video memory update function

void uGUI_mytest(void)
{
    
    
	UG_GUI my_ugui1;
	//初始化函数
	UG_Init(&my_ugui1,OLED_SetPFun,128,64);
	//选择GUI
	UG_SelectGUI(&my_ugui1);
	//选择字体
	UG_FontSelect(&FONT_8X8);
	//字符显示
	UG_PutChar('B',20,20,C_WHITE,C_BLACK);
	OLED_Refresh();
	HAL_Delay(500);
	OLED_Clear();
	//全屏填充函数
	UG_FillScreen(C_WHITE);
	OLED_Refresh();
	HAL_Delay(1000);
	OLED_Clear();
	//矩形填充
	UG_FillFrame(0,0,50,50,C_WHITE);
	OLED_Refresh();
	HAL_Delay(1000);
	OLED_Clear();
	//圆角矩形填充
	UG_FillRoundFrame(0,0,50,50,10,C_WHITE);
	OLED_Refresh();
	HAL_Delay(1000);
	OLED_Clear();
	//字符串测试
	UG_FontSelect(&FONT_16X26);
	UG_SetBackcolor(C_BLACK);
	UG_SetForecolor(C_WHITE);
	UG_PutString(0,0,"Hello Wlord!");
	OLED_Refresh();
	HAL_Delay(1000);
	OLED_Clear();
}

Demonstration renderings:

string display
Rounded rectangle display
character display

3. More functions

uGUI includes characters, pictures, buttons, windows and other display and function callback functions, as well as standard touch input and other input device interfaces. For details, please refer to the official documentation.
Note that the digital display function is not given in the official library:
here is a digital display function written by the author, for reference only:

UG_U32 UG_Pow(UG_U8 m,UG_U8 n)
{
    
    
	UG_U32 result=1;
	while(n--)
	{
    
    
	  result*=m;
	}
	return result;
}

void UG_PutNum(UG_S16 x,UG_S16 y,UG_S32 num, UG_U8 len,UG_COLOR fc, UG_COLOR bc)
{
    
    
  	UG_U8 t,temp;
	  UG_U8 enshow=0;	
		for(t=0;t<len;t++)
			{
    
    
				temp=(num/UG_Pow(10,len-t-1))%10;
				if(enshow==0&&t<(len-1))
				{
    
    
					if(temp==0)
					{
    
    
						UG_PutChar(' ',x+t*(gui->font.char_width+gui->font.char_h_space-1),y,fc,bc);
						continue;
					}
					else enshow=1; 
				}		
						UG_PutChar(temp+'0',x+t*(gui->font.char_width+gui->font.char_h_space-1),y,fc,bc);
				
		
		}

}

Be careful not to forget to declare in the header file ugui.h!
display effect:
insert image description here

Guess you like

Origin blog.csdn.net/qq_39587650/article/details/119491697
GUI