go语言没有官方的GUI。

其实怎么说呢,这一块儿的需求也不怎么迫切。毕竟每个语言都有自己擅长的领域。

在电脑上开发界面还是C#或QT或java的swing来的快。没必要非用go吧,更多的是在电脑上基于web的BS架构的模式。

在嵌入式上呢?笔者考察了好多GUI,目前没有发现太满意的。

如QT,LittlevGL,Guilite,awtk,Nuklear等。

这些GUI怎么说呢?要么太庞大,要么依赖一大堆,要么环境就要倒弄很久。有的需要花不少精力去投入学习成本。

但是对我们的嵌入式应用来说,界面不多,有的界面很简单啊,不需要花太多精力在界面上面。用这些就显得划不来了。

有最基础的画点,画线,画圆,图片展示就够了。

UCGUI这个GUI,搞嵌入式的基本都知道吧,比较常见,开源,且使用简单。单片机上都能用。

因此,我把UCGUI和汉字字库做了个go语言的封装。

使用起来也很简单。想要啥界面,自己画呗,反正又不多,界面也不花哨。

有人说都自己实现太花时间了吧,比如选用其他的如ZLG的awtk,或者QT,或者littlegl,或者GTK等。但是我要说,不花这时间花那时间,其实最终花的时间都差不多,甚至还没有自己画的来的快。但若界面很复杂花哨,那还是考虑其他GUI吧,自己画太难了。

简单快速的实现功能就是最好。你倒腾那些GUI就不花时间了吗。除非实在是实现不了或得不偿失了,可以考虑。

ucGUI也有诸如BUTTON,窗口之类的界面控件。但是结合我们的使用情况来说,那些几乎是摆设没有用到。

还是用的显示汉字,图片,点,线的功能多。

我对ucGUI的调用,使用go做了个封装。然后使用起来很简单,有多简单?

例如:

func main() {
	fmt.Println("Hello Go")
	GUI_Init()
	GUI_SetBkColor(0x954816)
	GUI_Clear()
	GUI_SetFont(&C.GUI_FontHZ20x20)

	GUI_DispStringAt("PRICE", 60, 156)

	GUI_DispStringAt("票价", 60, 120)

	name := ""
	fmt.Println("over!press any key to continue: ")
	fmt.Scanln(&name)
}

封装ucGUI的过程如下:

package drivers

/*

#cgo CFLAGS: -Iinclude/header

#cgo LDFLAGS: -Llib -lucgui -lm
#include <stdlib.h>
#include "GUI.h"
//extern const GUI_FONT GUI_FontHZ12;
extern const GUI_FONT GUI_FontHZ20x20;
extern const GUI_FONT GUI_FontHZ32x32;
extern const GUI_FONT GUI_FontHZ76x76;

extern void GUIcache2fb(void);
extern int setGUIcache(unsigned char sta);
extern unsigned char getGUIcache(void);
*/
import "C"
import "unsafe"
import (
	"github.com/axgle/mahonia"
)

var (
	//GUI_FontHZ12    C.GUI_FONT
	GUI_FontHZ20x20 *C.GUI_FONT
	GUI_FontHZ32x32 *C.GUI_FONT
	GUI_FontHZ76x76 *C.GUI_FONT

	GUI_FONT_8_ASCII *C.GUI_FONT

	GUI_FONT_8_1        *C.GUI_FONT
	GUI_FONT_10S_ASCII  *C.GUI_FONT
	GUI_FONT_10S_1      *C.GUI_FONT
	GUI_FONT_10_ASCII   *C.GUI_FONT
	GUI_FONT_10_1       *C.GUI_FONT
	GUI_FONT_13_ASCII   *C.GUI_FONT
	GUI_FONT_13_1       *C.GUI_FONT
	GUI_FONT_13B_ASCII  *C.GUI_FONT
	GUI_FONT_13B_1      *C.GUI_FONT
	GUI_FONT_13H_ASCII  *C.GUI_FONT
	GUI_FONT_13H_1      *C.GUI_FONT
	GUI_FONT_13HB_ASCII *C.GUI_FONT
	GUI_FONT_13HB_1     *C.GUI_FONT
	GUI_FONT_16_ASCII   *C.GUI_FONT
	GUI_FONT_16_1       *C.GUI_FONT
	GUI_FONT_16_HK      *C.GUI_FONT
	GUI_FONT_16_1HK     *C.GUI_FONT
	GUI_FONT_16B_ASCII  *C.GUI_FONT
	GUI_FONT_16B_1      *C.GUI_FONT
	// GUI_FONT_20_ASCII   *C.GUI_FONT
	// GUI_FONT_20_1       *C.GUI_FONT
	// GUI_FONT_20B_ASCII  *C.GUI_FONT
	// GUI_FONT_20B_1      *C.GUI_FONT
	GUI_FONT_24_ASCII  *C.GUI_FONT
	GUI_FONT_24_1      *C.GUI_FONT
	GUI_FONT_24B_ASCII *C.GUI_FONT
	GUI_FONT_24B_1     *C.GUI_FONT
	GUI_FONT_32_ASCII  *C.GUI_FONT
	GUI_FONT_32_1      *C.GUI_FONT
	GUI_FONT_32B_ASCII *C.GUI_FONT
	GUI_FONT_32B_1     *C.GUI_FONT

	GUI_FontD48 *C.GUI_FONT

	Rect C.GUI_RECT
)

const (
	//================
	GUI_TEXTMODE_NORMAL C.int = C.GUI_TEXTMODE_NORMAL
	GUI_TEXTMODE_XOR    C.int = C.GUI_TEXTMODE_XOR
	GUI_TEXTMODE_TRANS  C.int = C.GUI_TEXTMODE_TRANS
	GUI_TEXTMODE_REV    C.int = C.GUI_TEXTMODE_REV
	GUI_TM_NORMAL       C.int = C.GUI_TM_NORMAL
	GUI_TM_XOR          C.int = C.GUI_TM_XOR
	GUI_TM_TRANS        C.int = C.GUI_TM_TRANS
	GUI_TM_REV          C.int = C.GUI_TM_REV

	//===================
	/* Text alignment flags, horizontal */
	GUI_TA_HORIZONTAL C.int = C.GUI_TA_HORIZONTAL
	GUI_TA_LEFT       C.int = C.GUI_TA_LEFT
	GUI_TA_RIGHT      C.int = C.GUI_TA_RIGHT
	GUI_TA_CENTER     C.int = C.GUI_TA_CENTER
	GUI_TA_HCENTER    C.int = C.GUI_TA_CENTER /* easier to remember :-)  */

	/* Text alignment flags, vertical */
	GUI_TA_VERTICAL C.int = C.GUI_TA_VERTICAL
	GUI_TA_TOP      C.int = C.GUI_TA_TOP
	GUI_TA_BOTTOM   C.int = C.GUI_TA_BOTTOM
	GUI_TA_BASELINE C.int = C.GUI_TA_BASELINE
	GUI_TA_VCENTER  C.int = C.GUI_TA_VCENTER

	//===================

	GUI_BLUE         C.uint = C.GUI_BLUE
	GUI_GREEN        C.uint = C.GUI_GREEN
	GUI_RED          C.uint = C.GUI_RED
	GUI_CYAN         C.uint = C.GUI_CYAN
	GUI_MAGENTA      C.uint = C.GUI_MAGENTA
	GUI_YELLOW       C.uint = C.GUI_YELLOW
	GUI_LIGHTBLUE    C.uint = C.GUI_LIGHTBLUE
	GUI_LIGHTGREEN   C.uint = C.GUI_LIGHTGREEN
	GUI_LIGHTRED     C.uint = C.GUI_LIGHTRED
	GUI_LIGHTCYAN    C.uint = C.GUI_LIGHTCYAN
	GUI_LIGHTMAGENTA C.uint = C.GUI_LIGHTMAGENTA
	GUI_LIGHTYELLOW  C.uint = C.GUI_LIGHTYELLOW
	GUI_DARKBLUE     C.uint = C.GUI_DARKBLUE
	GUI_DARKGREEN    C.uint = C.GUI_DARKGREEN
	GUI_DARKRED      C.uint = C.GUI_DARKRED
	GUI_DARKCYAN     C.uint = C.GUI_DARKCYAN
	GUI_DARKMAGENTA  C.uint = C.GUI_DARKMAGENTA
	GUI_DARKYELLOW   C.uint = C.GUI_DARKYELLOW
	GUI_WHITE        C.uint = C.GUI_WHITE
	GUI_LIGHTGRAY    C.uint = C.GUI_LIGHTGRAY
	GUI_GRAY         C.uint = C.GUI_GRAY
	GUI_DARKGRAY     C.uint = C.GUI_DARKGRAY
	GUI_BLACK        C.uint = C.GUI_BLACK
	GUI_BROWN        C.uint = C.GUI_BROWN
	GUI_ORANGE       C.uint = C.GUI_ORANGE
	GUI_TRANSPARENT  C.uint = C.GUI_TRANSPARENT

	TOP_BAR_COLOR C.uint = 0x501f08

	GUI_IdleColor C.uint = 0x954816
)

func init() {
	//GUI_FontHZ12 = C.GUI_FontHZ12
	GUI_FontHZ20x20 = &C.GUI_FontHZ20x20
	GUI_FontHZ32x32 = &C.GUI_FontHZ32x32
	GUI_FontHZ76x76 = &C.GUI_FontHZ76x76

	GUI_FONT_8_1 = &C.GUI_Font8_1
	GUI_FONT_10S_ASCII = &C.GUI_Font10S_ASCII

	GUI_FONT_10S_1 = &C.GUI_Font10S_1
	GUI_FONT_10_ASCII = &C.GUI_Font10_ASCII
	GUI_FONT_10_1 = &C.GUI_Font10_1
	GUI_FONT_13_ASCII = &C.GUI_Font13_ASCII
	GUI_FONT_13_1 = &C.GUI_Font13_1
	GUI_FONT_13B_ASCII = &C.GUI_Font13B_ASCII
	GUI_FONT_13B_1 = &C.GUI_Font13B_1
	GUI_FONT_13H_ASCII = &C.GUI_Font13H_ASCII
	GUI_FONT_13H_1 = &C.GUI_Font13H_1
	GUI_FONT_13HB_ASCII = &C.GUI_Font13HB_ASCII
	GUI_FONT_13HB_1 = &C.GUI_Font13HB_1
	GUI_FONT_16_ASCII = &C.GUI_Font16_ASCII
	GUI_FONT_16_1 = &C.GUI_Font16_1
	GUI_FONT_16_HK = &C.GUI_Font16_HK
	GUI_FONT_16_1HK = &C.GUI_Font16_1HK
	GUI_FONT_16B_ASCII = &C.GUI_Font16B_ASCII
	GUI_FONT_16B_1 = &C.GUI_Font16B_1
	//GUI_FONT_20_ASCII   = &C.GUI_Font20_ASCII
	//GUI_FONT_20_1       = &C.GUI_Font20_1
	//GUI_FONT_20B_ASCII  = &C.GUI_Font20B_ASCII
	//GUI_FONT_20B_1      = &C.GUI_Font20B_1
	GUI_FONT_24_ASCII = &C.GUI_Font24_ASCII
	GUI_FONT_24_1 = &C.GUI_Font24_1
	GUI_FONT_24B_ASCII = &C.GUI_Font24B_ASCII
	GUI_FONT_24B_1 = &C.GUI_Font24B_1
	GUI_FONT_32_ASCII = &C.GUI_Font32_ASCII
	GUI_FONT_32_1 = &C.GUI_Font32_1
	GUI_FONT_32B_ASCII = &C.GUI_Font32B_ASCII
	GUI_FONT_32B_1 = &C.GUI_Font32B_1

	GUI_FontD48 = &C.GUI_FontD48

}

func GUI_Init() {
	C.GUI_Init()
}
func GUI_Clear() {
	C.GUI_Clear()
}

func GUI_SetColor(val C.uint) {
	C.GUI_SetColor(C.ulong(val))
}

func GUI_SetBkColor(val C.uint) {
	C.GUI_SetBkColor(C.ulong(val))
}

func GUI_SetFont(val *C.struct_GUI_FONT) {
	C.GUI_SetFont(val)
}

//注:go中的编码为utf-8,GUI显示汉字使用的GB2312
func GUI_DispStringAt(str string, x, y int) {

	enc := mahonia.NewEncoder("gbk")

	gbk := enc.ConvertString(str)
	cs := C.CString(gbk)
	defer C.free(unsafe.Pointer(cs))
	C.GUI_DispStringAt(cs, C.int(x), C.int(y))

}

func GUI_DispStringHCenterAt(str string, x, y int) {
	enc := mahonia.NewEncoder("gbk")

	gbk := enc.ConvertString(str)
	cs := C.CString(gbk)
	defer C.free(unsafe.Pointer(cs))
	C.GUI_DispStringHCenterAt(cs, C.int(x), C.int(y))
}

func GUI_FillRect(x0, y0, x1, y1 int) {
	C.GUI_FillRect(C.int(x0), C.int(y0), C.int(x1), C.int(y1))
}

func SetRectAera(x0, y0, x1, y1 int) C.GUI_RECT {
	var rClient C.GUI_RECT

	rClient.x0 = C.short(x0)
	rClient.y0 = C.short(y0)
	rClient.x1 = C.short(x1)
	rClient.y1 = C.short(y1)

	return rClient
}

func GUI_DispStringInRect(str string, rect *C.GUI_RECT, align C.int) {

	enc := mahonia.NewEncoder("gbk")

	gbk := enc.ConvertString(str)
	cs := C.CString(gbk)
	defer C.free(unsafe.Pointer(cs))
	C.GUI_DispStringInRect(cs, rect, align)

}
func GUI_SetTextMode(mode C.int) {
	C.GUI_SetTextMode(mode)
}

func GUIcache2fb() {
	C.GUIcache2fb()
}

func SetGUIcache(sta int) {
	C.setGUIcache(C.uchar(sta))
}

func GetGUIcache() int {
	return int(C.getGUIcache())
}

/*
func main() {
	fmt.Println("Hello Go")
	GUI_Init()
	GUI_SetBkColor(0x954816)
	GUI_Clear()
	GUI_SetFont(&C.GUI_FontHZ20x20)

	GUI_DispStringAt("PRICE", 60, 156)

	GUI_DispStringAt("票价", 60, 120)

	name := ""
	fmt.Println("over!press any key to continue: ")
	fmt.Scanln(&name)
}
*/

最后再说下,如何使用呢?其他人要用的话,肯定跑不起来,因为根本找不到ucgui.so这个库文件。

那么这个库文件哪来的呢?

根据自己的平台和需求,交叉编译ucgui的源码吧,编译为ucgui.so。缺少的字库也自己打包进去即可。

 

 

 

Logo

更多推荐