本帖最后由 creep 于 2018-1-13 19:14 编辑 * q2 B: z/ T" T; n: k5 S
. W' z5 r5 v( k" U7 k2 b) G5 u4 | 推荐阅读最新的Littlevgl移植(更新时间:2018-1-13):
0 ?0 j9 E9 d/ |; q( j $ [1 b5 H/ n \0 n& o. a
开源GUI LittlevGL V5.0版本移植
3 R: W3 p0 r! E) l4 P- }3 a+ r( c. E S, I7 J( z9 X
, o: b! c" P8 }- M0 `1 ?; N! J
LittlevGL 是一个开源免费的GUI,支持触摸屏操作,移植简单方便,开发者一直在不断完善更新。LittlevGL 自带了丰富的控件:窗口、按键、标签、list、图表等,还可以自定义控件;支持很多特效:透明、阴影、自动显示隐藏滚动条、界面切换动画、图标打开关闭动画、平滑的拖拽控件、分层显示、反锯齿等等。
9 E, v. G& K; m7 A3 D8 y. v, b) R2 h( C4 f& i0 b
, v( r+ T8 |3 r c A
下面是我在STM32F769-DISCO上移植的演示模式的效果图片.5 T" D. p) q& X: A+ Q
( h' ]( n6 n S
, i5 D D& U0 g" ?3 C
! W+ A$ E! @1 P7 R# A; T
+ p$ s3 x$ m6 q. [4 X+ t, b
, q0 R# n$ y5 p, c( d主要控件可以通过下面的图片有个基本的了解:( a6 e, c! q6 ?5 c
+ Q4 U& j$ N7 |" W+ b# y* @5 t5 A% [4 \' \! C
) H% }% \% F+ x3 U$ }/ |1、移植 U& R# u5 Q3 \" }9 [2 q& M' J
/ s3 ]& j! L' k# cLittlevGL 的移植非常简单,用户只需要提供systick、触摸屏、LCD显示接口即可。源码里面有3个文件用于分别添加这3个接口,具体如下:. c$ x8 I( j6 U1 J( C
! k3 v6 w$ ]5 g6 C- i5 x- x n
' W2 S l. U9 b. j4 [0 A/ ~" |' d
也就是下面几个函数:
. N; L" A& a: [' r8 v5 x# l, \% \; {$ H# ^7 A; g7 M. A
" T# K% j. P8 E& Q; C. M. i3 H& `8 S# z- hal/disp disp_fill(x1, y1, x2, y2, color) to fill area with a color
- hal/disp disp_map(x1, y1, x2, y2, &color_array) copy a color map to an area
- hal/disp disp_color_cpy(dest, src, length, opa) copy pixel, optional for GPU
- hal/indev indev_get(id, &x, &y) get the x and y coordinates from an input device (e.g. touch pad)
- hal/systick systick_get() get a system tick with 1 ms resolution
- hal/systick systick_elapse(prev_time) get the elapsed milliseconds sience prev_time
% T4 G: x" ?4 h& g2 k, F: S% e a)systick的移植直接使用的是HAL库里面提供的: Y/ J. k! ^/ l) S8 u
4 Q8 s6 O4 S4 {- /**
) ?; \4 \8 } @: D0 k3 u - * Get the elapsed milliseconds since start up$ Y* O+ S; y: \, R4 {2 Y$ o$ k
- * @return the elapsed milliseconds
3 m l# E: _, l9 |# m5 r - */8 x/ [! c7 j' u0 d
- uint32_t systick_get(void)
+ x+ k" x8 v/ b6 A* L - {
; S1 v7 _- s* Z1 {- w8 A - return HAL_GetTick();6 ~. G5 i# n8 h9 I
- }
' [& j* g! }/ j( C. \ - /**
2 ~. C5 i1 D+ m2 \% ]8 } - * Get the elapsed milliseconds science a previous time stamp3 y9 K, j- ~& V$ x& d: z; w: E! e ~
- * @param prev_tick a previous time stamp from 'systick_get'
1 a# f2 J8 V$ x4 z/ C$ C - * @return the elapsed milliseconds since 'prev_tick'8 e1 C& L5 @/ X9 P5 N {8 V
- */
# P1 ~" Z4 U- ^! I2 u/ Y: m - uint32_t systick_elaps(uint32_t prev_tick)5 {' @0 t' h+ f: }6 g+ X
- {% M7 w5 n- W) e& z4 ^9 w
- volatile uint32_t act_time = systick_get();
1 [% ]5 o, A) O. H
. ~/ V+ f! E; o; m$ g4 j- /*If there is no overflow in sys_time! s6 i( a' {; o1 |( c! v* h
- simple subtract*/
3 k" F! \ _) C; x7 M9 ? - if(act_time >= prev_tick) {7 F+ D' H4 j3 P8 c& M
- prev_tick = act_time - prev_tick;% ~: U0 c5 Z! M+ B6 c
- } else {
% I* L+ s, y" h) s( t# j. A - prev_tick = UINT32_MAX - prev_tick + 1;5 q s0 L( y6 E/ J1 {7 ?0 m8 g; R
- prev_tick += act_time;
/ a; L3 L- c1 X2 h' o - }! z1 G; ]" N' N; X2 n7 O
- return prev_tick;
+ T5 @9 M& G+ H* U' ?" v - }
复制代码 b)提供触摸屏的函数,用于初始化触摸屏和返回触摸屏扫描结果
6 ^; @& J. V4 f6 A( j$ o- /**
. F% u5 a8 @9 s$ D6 G5 T" \7 w - * Initialize your input devices here. `$ \$ _1 \) c J0 E9 [
- */% g. F4 r) U6 K9 p) k! A
- void indev_init(void)4 |) i* f! q9 M9 p7 n
- {
; S, e; W. {& ~ - BSP_TS_Init(DISP_HOR_RES, DISP_VER_RES);5 g+ B( W' q* p" D! X0 U
- }1 }3 a V# u8 A
. v: D$ k* o' V& |9 y: X- /**
" V5 u1 C* X' M4 r - * Read an input device
2 u) I/ M* [" K$ m, m" ` - * @param indev_id id of the input device to read
( f5 f2 j& g$ B' ?7 |. f4 G - * @param x put the x coordinate here
5 Z H* D9 ?- F n+ C, L2 C - * @param y put the y coordinate here4 P- w% V- Z a/ @7 ?1 [. _
- * @return true: the device is pressed, false: released- b; Z% y& F1 T1 \- k0 y( h; v# u$ A! |
- */
% ^. i' |4 |, ?3 e9 w& ^ - bool indev_get(uint8_t indev_id, int16_t * x, int16_t * y)
# o/ e8 p- o0 J" ` - {# k+ f: \+ {6 r1 g; Y7 C1 @
- if(indev_id != 0) {
X3 L) i# W* i$ | - *x = 0;$ v, _ {, B( Y) h$ X
- *y = 0;; I( ^7 n7 X D/ e7 |
- return false;
- H' T2 t( c& `" F% W/ P h; s5 M - }3 J9 s/ p% A- g( T' t% U! \
- static int16_t last_x = 0;3 S- p7 x+ I! P1 w* ~
- static int16_t last_y = 0;! E; N) Y. Q- x# ~% r# z) E) H
- bool press;
. M( A% H2 l4 x; [5 a9 [1 L - BSP_TS_GetState(&TS_State);" u9 i+ x; ]. Q! w6 ~+ a& }
- if(TS_State.touchDetected != 0) {
: M3 x) e4 ?& x - *x = TS_State.touchX[0];! w9 `3 s6 p( u4 I' B: d! @
- *y = TS_State.touchY[0]; \& W7 v1 d2 x# |' X
- press = true;, k; p$ P3 g+ \ L! n g
- } else {
2 O4 e. X3 ]# j6 i - *x = last_x;* y* P7 g% Y/ z% h( l5 R1 A
- *y = last_y;
0 x" g( u& i$ U5 ~ - press = false;
6 m: t2 J7 }8 m1 P3 _ - }
) ^/ b- G9 `8 N, C5 t - + ?( h* ?. J9 L# t# }
- return press;$ {: C. w* m9 e' l
- }
复制代码 c)LCD的移植分为几个函数,) G9 z6 M: D" t; F$ v8 x
: s1 i* U8 _ [
1)一个是用单一色填充矩形区域,这个实际调用的次数较小
8 m5 e) V9 H" R' s
8 D5 q3 A, E2 ?* V4 G) @3 J6 x- /**
+ B/ S- M- X- r8 r - * Fill a rectangular area with a color& P$ x4 n, y* l$ u
- * @param x1 left coordinate of the rectangle" E: B" q8 i4 J& u/ }
- * @param x2 right coordinate of the rectangle; l$ h, ^, V+ X' X! e& y1 P
- * @param y1 top coordinate of the rectangle% {+ E* w" p5 K4 w
- * @param y2 bottom coordinate of the rectangle
& v2 A6 `5 q; w9 I: p& B" E7 w3 y - * @param color fill color) A* i, y( ^3 N6 N
- */
: y& K8 B1 `2 F1 h' p" z9 G - void disp_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2, color_t color)% Z7 r( g# {" c
- {
. U8 v( O2 ~. [. X - /*Return if the area is out the screen*/. z1 h0 N) E) b$ O3 R- D* z- y! L5 i5 i
- if(x2 < 0) return;) d8 u3 j4 F. J- e8 P2 y
7 U( o# K/ U& G5 F2 h4 H6 g- if(y2 < 0) return;
8 x7 f# Y) }- N: T& H" t! | - : u+ @! [1 p. k' Q3 @" t6 w4 ?
- if(x1 > DISP_HOR_RES - 1) return;* ^# p+ | M+ w; F6 K4 f; @
9 z( L, D7 Z3 t- if(y1 > DISP_VER_RES - 1) return;
4 j4 t r( b S4 i p# \8 G
: P! Y' X! k* I. X1 m- /*Truncate the area to the screen*/! Q E- m( z& ^! f0 a5 k
- int32_t act_x1 = x1 < 0 ? 0 : x1;3 B# g' t m6 F* u, Z1 p
- int32_t act_y1 = y1 < 0 ? 0 : y1;
4 M4 C! i8 r# N; b W - int32_t act_x2 = x2 > DISP_HOR_RES - 1 ? DISP_HOR_RES - 1 : x2; }1 D5 k/ O0 X) h
- int32_t act_y2 = y2 > DISP_VER_RES - 1 ? DISP_VER_RES - 1 : y2;& I4 ?* o/ J9 N. ^
- , _, _! }0 e! n P
- LCD_FillRectPart(act_x1, act_y1, act_x2 - act_x1 + 1, act_y2 - act_y1 + 1, ((color.full)));, u! o4 f: u6 J9 R7 ~8 Q/ H
- }
复制代码 2)这个是用一个颜色map填充一个矩形区域,这个主要用显示时局部刷新使用,是LCD显示调用的底层接口,使用非常频繁,如果需要优先显示速度,可以从这个函数入手,使用块内存复制或者DMA2D来实现。
. `8 D8 W$ w3 I1 `: a5 O- /**" J7 @% C3 v' u
- * Put a color map to a rectangular area- g- f" ^2 J/ b- b) l% |
- * @param x1 left coordinate of the rectangle
6 ?# Q$ ~& B* J" O/ Z/ H( I - * @param x2 right coordinate of the rectangle; ~; M) F! Q- F
- * @param y1 top coordinate of the rectangle) @; {- l. N) a% j& ~0 k! M8 H
- * @param y2 bottom coordinate of the rectangle
, M& @: l: t$ F2 R, _8 o, W/ X5 _ - * @param color_p pointer to an array of colors
j" H) [2 s3 V. f( z - */
; @ I3 ~, z* d% n8 k" C - void disp_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const color_t * color_p)
{2 b8 P' c5 z; g9 V n8 J - {7 k9 ^ e% C* e" Z+ i
- /*Return if the area is out the screen*/$ h6 c% O9 ^6 M# p
- if(x2 < 0) return;
& A: x: D+ O( A - , y8 I9 R) V+ c. i* m
- if(y2 < 0) return;; j# C( i. Q5 D, K
- 1 n* |: S8 }! o7 R# M" C* h
- if(x1 > DISP_HOR_RES - 1) return; z1 P* F3 q H6 X$ |% `
- & B2 q I+ G. ` q
- if(y1 > DISP_VER_RES - 1) return;5 L2 d) o0 I4 f8 G) m' G
- . W0 H8 ?" @# g, ]2 |& u
- /*Truncate the area to the screen*/' v4 T x. l: U
- int32_t act_x1 = x1 < 0 ? 0 : x1;
3 y) ]$ w" R+ g, o5 } - int32_t act_y1 = y1 < 0 ? 0 : y1;3 [4 e0 t7 {/ C/ Z3 F
- int32_t act_x2 = x2 > DISP_HOR_RES - 1 ? DISP_HOR_RES - 1 : x2;
0 r: a1 ~, [, k: u - int32_t act_y2 = y2 > DISP_VER_RES - 1 ? DISP_VER_RES - 1 : y2;$ v1 d) u1 X- n5 _6 D2 Q9 B
- # d4 b% k: u; `5 N5 b7 j# Z# U
- ' S, d! x: ?' \( E5 z7 m! h
- uint32_t y;
! W+ J& x) O, P/ v9 _
* g8 W C+ ?* b3 [( E. D n- for(y = act_y1; y <= act_y2; y++)" Y: ]. F& p3 p7 q, E; \
- {
! y, C7 k, v( U - memcpy(&my_fb[y * DISP_HOR_RES + act_x1],
; |" n9 Q. p& F2 }0 ] - color_p,
2 `1 r$ T6 a! I* ^! Y - (act_x2 - act_x1 + 1) * sizeof(my_fb[0]));* C( U h( Y8 o1 v( ^( r5 b+ `9 B- m- L
- color_p += x2 - x1 + 1; /*Skip the parts out of the screen*/+ Z7 n) Y. H9 e
- }
6 j' p$ _/ p- S/ u: a
- e4 l+ m7 h5 N% }. c6 ^. ^- }
复制代码 3)硬件加速可选项打开后可以使用下面的函数复制加速填充复制内存
& E9 f3 Y; }& X) X- /**& Y9 b3 f3 G; F# r
- * Copy pixels to destination memory using opacity
3 |0 `; N0 E8 U9 j) Q3 Q0 ]9 j - * @param dest a memory address. Copy 'src' here.+ p3 z) G& B% l0 h0 A3 `+ h$ j, [5 N, U
- * @param src pointer to pixel map. Copy it to 'dest'.
7 G- \2 {3 u5 j - * @param length number of pixels in 'src'* E; l5 N$ ^3 l# ^2 b" v
- * @param opa opacity (0, OPA_TRANSP: transparent ... 255, OPA_COVER, fully cover)' @! t6 E5 O2 |$ \; B( M
- */: Z& w$ ^+ u. ~$ c
- void disp_color_cpy(color_t * dest, const color_t * src, uint32_t length, opa_t opa)
, k6 J, r) o+ i0 d - {5 e# O- t; }1 M4 K3 F' o. ]0 _0 z
- /*Wait for the previous operation*/) I' X$ a6 `/ G: D: j! D [
- HAL_DMA2D_PollForTransfer(&Dma2dHandle, 100);
; Z8 i) \9 h, `& B. r/ s& ^" Z# V - * n- b1 ^7 v5 j, n0 I
- Dma2dHandle.LayerCfg[1].InputAlpha = opa;% R! G) J+ J& w* a. [
- HAL_DMA2D_ConfigLayer(&Dma2dHandle, 1);# {: j5 i# b" q0 g/ N! x9 x0 T
- HAL_DMA2D_BlendingStart(&Dma2dHandle, (uint32_t) src, (uint32_t) dest, (uint32_t)dest, length, 1);
/ ^% }3 K' m* t1 e6 ~6 } - }
复制代码 移植了上面接口之后基本问题就不大了,littlevgl 目前的代码结构有点乱,分为core、hal、misc 几个部分,我是分别建立一个文件夹然后把相应的文件全部添加到里面了,官网也没有详细的移植说明文档。所有的源码如下:- J" L+ u; Q) h. Y$ A6 R, |
( z! P- E$ [$ z4 a
添加到工程中后如下整体如下:3 u4 c) T2 J$ K' T8 x7 ^, d
2 d) U' t! p& Q0 S% r# j8 C
1 ?6 O8 i! v. G, B/ H
3 m* u, {) f# s3 `8 A5 d# O
/ b6 i9 d4 ^! h$ R& l
) h/ j; \- p, o* R a将所有的文件添加到工程中后用keil编译可能会有一些错误和警告出现,但都很好解决。: {4 W' w( I$ [7 p
5 v7 p s8 [: c" Q B: ?, Y8 g$ o
2、运行效果% ^! N6 ?. U/ Z' j9 h
- G6 A; _1 I2 S9 xLittlevGL内置一个类似桌面系统的演示例子,通过打开相应的宏定义即可
9 O, O3 v) o! L+ @1 f) _3 D0 O& y4 c$ p% f3 H! w. R6 u) P1 y: o
- O, _4 n2 y7 m0 k
2 q9 _+ i" O5 k' P% {# J$ x5 {: }7 F8 K
LittlevGL有个桌面上模拟的工程,下面是论坛网友 @QianFan 在linux下运行官方例子的运行效果:
- Y& h6 R% p+ v7 f
" Q+ n6 g8 z4 D" \5 i- i, X6 O6 y! q) g' ]- u' k, ^. T# n+ f
0 P5 i9 J: T. Q& ]/ C
2 d& W" z; R6 Y1 g* M) n+ \除此之外官网还有一个在STM32F429-DISOC上移植的例子,在youtube上可以观看,因为转换为了gif可能看起来不是很流畅,实际演示效果很不错。
% v* J2 S% F# q. i- `" w9 V. J) n; n1 b6 F9 P& L' ?/ ]
) `/ L8 U4 `8 p) Q
5 S8 k, u2 {- J. L; w/ H. X
7 j. k; w |5 C下面是我在STM32F769-DISOC上面的移植,和linux下模拟运行的差不多。因为769-disco的分辨率很高,移植后我没有使用优化,所以可以看到画面切换有些闪烁。
; I0 |7 D% V' {5 w* W/ Q
. A' ^, Y( ^3 L; Z6 A8 w# I0 Q+ ]
7 s3 R8 O; q% D+ h2 i$ l/ Y! Q/ T2 p2 ^
3、关于LittlevGL
; h% _' l1 y" F4 T
& A# r' q6 F# M. ] LittlevGL里面有个简单的时间片调度系统,刷新显示和触摸处理等任务会周期的被调用处理。LCD刷新采用了局部缓存刷新,这样能避免闪烁出现。LittlevGL目前只有一个开发者在业余时间维护更新,但是开发者热情很高,可以在github上看到更新很频繁,我测试的使用的V4.2版本,开发者正在开发4.3版本并加入了不少新的功能,有建议可以在github和作者讨论互动。
9 V+ S1 y4 }/ D4 r1 A: k目前代码结构看上去有点乱,有人在也提到了这个问题,计划好像是在5.0版本引入新的代码组织结构,感兴趣的可以去github提意见,开发者很乐意沟通讨论。官网也有不少控件的使用介绍和例子参考。
6 S# _' t3 r, L/ \# _) V. I9 b9 p- e j, F+ j( v# h2 e2 Q1 z3 l2 \
" T6 L) V- ] m2 j; `5 k& p. \, x* s
- m; B' }$ w3 z
; F9 u# H; K8 T; Y9 H) c
, }6 W4 U$ L Y% P; |9 \官网和github地址如下:5 U* v1 ] F0 h$ ]; Z! n! M/ b- [
8 A$ a ~( `) P1 X2 n: Y) HLittlevGL官网:http://www.gl.littlev.hu/, o+ h. M7 t6 Y# ~
LittlevGLgithub :http://github.com/littlevgl 和 http://github.com/littlevgl/lvgl
+ J$ y- [/ L1 O* o/ [& A" R; V S4 Y$ t2 t) e/ D3 h- _; a# \$ I
下面2个视频是在PC Simulator (Linux)和F4-DICO上的移植演示可能需要科学的方式才能打开。 i' v8 u9 D& T! k& J" w% N
How to Run Littlev Graphics Library in PC Simulator (Linux):http://youtu.be/ZzLdct2ymvg
8 {5 l1 C, G/ Q, l+ o0 E; j4 rEmbedded GUI on STM32 Discovery with Littlev Free Graphics Library:http://youtu.be/DcJdK137WKM
9 [6 y X5 g6 N/ a. g7 c
0 ]2 A, x* x+ \# r) h$ {! i 和众多开源的RTOS不一样,目前GUI开源的不是很多,虽然很多商用的GUI也很炫酷,但是我们没法知道具体效果的实现细节,也没法学习到这种能力,LittlevGL目前实现的一些特性和控件我认为是开源的GUI里面水平非常高的了。但是目前LittlevGL资料不是很多,只能通过阅读源码去学习和研究。
4 x3 j6 }9 {4 r$ S% V$ i. u0 i; u
: G) L: f& r2 U; S: @
3 m/ h* @& n+ \* O测试代码:
% H9 V' n- P! a
LittlevGL-STM32F769-DISCO.rar
(4.72 MB, 下载次数: 867)
|
看到一个别人分享的开源中文教程。
http://github.com/littlevgl/proj_pc
是的,没有限制,移植吧。
谢谢楼主分享
如何模拟? 如何在linux中运行?. `$ B2 h) h: G: g$ E1 A
+ N2 f: U. o& O8 O- V5 D9 O
文档很少
文档是很少,只能通过看代码学习了,如果感兴趣的话可以一起讨论。