上次已经开启了Linux系统,并且有GUI桌面,虽然不是比较熟悉的Qt环境,但也是可以做个小应用感受下stm32mp135系列产品的特点。 首先要搭建开发环境,毫无疑问安装个SDK就行了,官方WiKi页面啥都有。 基于Ubuntu18系统,补充安装所需要的库和小工具 更新软件列表 安装python3 - sudo apt-get install gawk wget git diffstat unzip texinfo gcc-multilib chrpath socat cpio python3 python3-pip python3-pexpect
复制代码 其他工具- sudo apt-get install libssl-dev libgmp-dev libmpc-dev lz4 zstd
复制代码 编译器等组件 - sudo apt-get install build-essential libncurses-dev libyaml-dev libssl-dev
复制代码 默认使用python3 - sudo apt install python-is-python3
复制代码 其他有用的工具 - sudo apt-get install coreutils bsdmainutils sed curl bc lrzsz corkscrew cvs subversion mercurial nfs-common nfs-kernel-server libarchive-zip-perl dos2unix texi2html libxml2-utils
复制代码 执行完以上内容,就可以下载和安装SDK软件包了。从以下地址获取最新的SDK https://www.st.com/en/embedded-software/stm32mp1dev.html#get-software 我还是在windows下面下载的,然后拷贝到了虚拟机使用。使用tar xvf 解压该压缩包。chmod +x 赋予可执行权限。然后运行安装就行了,默认的目录是/opt/st/stm32mp1/,,这个路径就可以,直接继续安装。0 S1 g0 s( M- Y* J5 U% b" C/ a
; c' j+ S$ ~9 _$ P# m- Z% |- P# O5 U4 o/ @ 然后就学着官方文档的样子去验证下SDK是否安装成功
2 P0 B! h! h: k! T8 K1 m* G6 {; L
, }! q5 O# R) [4 S3 d) o/ v5 m! z2 V 以上就完成了编译环境,至于说开发环境,一般还得有个IDE或者其他编辑器加调试工具。这里打算做的小程序使用gedit就行。 首先拷贝官方给的demo程序,基于GTK得hello world。 - #include <gtk/gtk.h>) n( b- N( H, y
- 6 G! E0 l7 |& V& B6 C- e3 \
- static void
( {! [: l* ?* m; l" y; i2 z- E - print_hello (GtkWidget *widget,1 E' x# h, R4 u9 j+ g
- gpointer data)# U) R: x+ V+ \# M; U
- {* `& a$ ]4 y2 ^
- g_print ("Hello World\n");1 _ ]$ a% _4 r
- }$ X1 P1 D3 A) {* F, ^
- 0 l0 M: S K5 R0 H* W9 ?9 p% T
- static void
) p( p' N* u7 `+ G6 A+ O1 o( q - activate (GtkApplication *app,& q0 D4 n+ }. Z
- gpointer user_data)$ L5 |/ A$ Q Q) _9 c9 i7 E
- {$ ]* ]3 e- Q: ], b( e
- GtkWidget *window;
* e# S( Q Q5 @ j; U" Z" H# S - GtkWidget *button;. F4 E9 |# I' N* r1 H7 {
- GtkWidget *button_box;/ @0 t6 t: ^+ m* B' |# `0 d, ~
- 8 f& [# L8 R6 m' I2 t
- window = gtk_application_window_new (app);
7 i+ J, e+ E [8 D' R5 @( |9 e - gtk_window_set_title (GTK_WINDOW (window), "Window");
' T1 G% d, u S9 Z# G - gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);6 v. U @; r e) D6 x! G0 _
- 8 ?& F( A" k- u, w) I: y- ^
- button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);$ q- U% Y4 u& p
- gtk_container_add (GTK_CONTAINER (window), button_box);
& G8 x$ \" ^9 c
/ S. r3 d9 {' {" G F8 V3 ^- button = gtk_button_new_with_label ("Hello World");
X2 g" U. u+ `& C+ c- { - g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);% E, l; b7 s7 m' }, x( \/ ^# V: F0 g
- g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
( ]/ G# F- Y3 _ - gtk_container_add (GTK_CONTAINER (button_box), button);
) G) U5 m( d9 q( E; p - + X. v. [/ n6 w! H& J
- gtk_widget_show_all (window);
0 w$ C2 q$ \; O; C - }
$ z! w' M, p8 @+ w, M1 K! Q0 ?5 I! Q
; T Z0 m( `- ?3 X! @+ y- int! e9 E$ i0 G* i- I
- main (int argc,# A5 {( Z6 o9 d5 w; T. n5 ]
- char **argv)* {) |6 ?! ]* A! q9 ^2 M s
- {
. h1 b/ ], w2 g) J F1 E( h - GtkApplication *app;- I! J! M* Y3 r+ I
- int status;
1 S, n6 H1 m" N5 H
; a0 e+ K! a* s4 P- [9 C, c- app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
2 k1 A$ M! O% {9 ^ - g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);# W9 r p3 J* C6 J# Q
- status = g_application_run (G_APPLICATION (app), argc, argv);0 ~* _0 g- I$ [5 U$ H2 X
- g_object_unref (app);$ `& m, _3 V* V; v7 n3 c
- + W4 p4 O! h; s
- return status;
) _5 W2 E- t( S - }
$ @2 ?9 ~1 R6 l/ J$ u/ Z
复制代码 然后也复制一个makefile文件,其实单个文件,直接使用gcc命令也可以得。
}8 @7 j/ K/ Z* B1 f4 a6 s- PROG = gtk_hello_world
; L$ C8 {2 h+ E7 \2 h, r/ p - SRCS = gtk_hello_world.c
' ~+ @. \5 w1 q2 F
1 x' h9 L1 S- V: P( R n! `- CLEANFILES = $(PROG)
2 x* V0 n2 K B, S7 z% {
. k- ?+ I6 Y7 u' i5 ^- # Add / change option in CFLAGS and LDFLAGS
& O/ \" Z" \; p3 _ - CFLAGS += -Wall $(shell pkg-config --cflags gtk+-3.0)
; I3 t( q, B) T* @ - LDFLAGS += $(shell pkg-config --libs gtk+-3.0); A+ x. D& l; E, U" S1 s) x O# l
- 3 Z4 C# s& a7 O) ^
- all: $(PROG)7 ^3 n. J- X! ?" c# y
- % U! T7 w0 ^4 K
- $(PROG): $(SRCS)
* E* I# h# Z1 u9 h - $(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
5 K, M o5 _ J; J9 ^; o
F' F4 N- N @4 n# \ z- clean:" n2 u6 l7 K1 N- K) h+ `, `
- rm -f $(CLEANFILES) $(patsubst %.c,%.o, $(SRCS))! h' I. i W, n" Q. {% p
复制代码 然后make,scp到开发板执行程序,就可以看到一个hello world窗口出现在之前得桌面上了。
& J `& ^8 k& l 接下来改程序,说实话没用过gtk,不过看了下helloworld得程序可以看懂,所以也百度了一下,就基本做出来了。
8 f( ]# p' X( g, `) y1 V$ }: K; J) @0 V0 \7 N6 S
' G% J2 Z# d% n, v
- #include <gtk/gtk.h>
# X: w- m/ C8 ], I( `' a) @ - #include <stdio.h>
D% e& u2 H, Y$ _+ {) }9 u( c7 L - #include <string.h>7 T2 y& D- @/ c& ]) z+ L/ B" a& w
- : U; [* o, j* O
- char buf[50] = "";" Z$ ~2 U1 Z7 d: o2 M+ e) Z
- 9 a0 H" H8 O' T2 B7 v8 ^
- void calc(GtkButton *button, gpointer data)- {' Q, I% h! t# J
- {
* {) q" s" v5 R- b! E - const char *text = gtk_button_get_label(button);$ h1 |, T2 I0 k3 F& D o
5 A1 J' v8 V/ t I- //退个操作! z P+ R8 f0 M+ X
- if (0 == strcmp(text, "c"))
# n% f1 {7 z! l2 c0 K% e6 S F - {
& ]7 ^+ p* b4 P" c) r+ C6 v8 ? - buf[strlen(buf) - 1] = 0;
+ u+ ^2 I0 B8 n% \0 D - }else{5 v; E& M4 z" c
- ' A/ N! R" D% U ?
- int a = 0, b = 0;
& L2 C2 a/ i ?# N c% C - char c;
0 }4 B% ~1 C X' }$ ^6 P; e9 [! O0 o% [ - strcat(buf,text);
/ F( o9 Y6 H/ a; B
8 n7 J6 l: D0 V/ X8 v% @- if (0 == strcmp("=", text)) {6 V/ N5 J1 ^, b" g7 j
1 k: I3 x# R5 G+ e' d, I- sscanf(buf, "%d%c%d", &a, &c, &b);
* l3 L, w# q$ E/ \ - 3 E/ |2 w# b. J7 c, C+ }
- if ('+' == c) {+ D6 L0 L2 Y. v; f4 f0 r
- sprintf(buf,"%d", a+b);8 O" Q+ l8 S' r& X; z( p3 k
- }$ c* y( _3 v( d& ^. n/ e* D
- else if ('-' == c) {, J0 o, O+ d2 H- h: D3 _8 ^
- sprintf(buf, "%d", a-b);. ?% c+ ^ f/ D( Q0 `
- }
7 s9 P5 }& K: A6 [5 ^ - else if ('*' == c) {1 s: H+ k: T7 P4 g/ y
- sprintf(buf, "%d", a*b);+ {0 G' G6 O% ^) u
- }& L3 W! \4 U5 ^+ S4 m
- else if ('/' == c) {
6 N: D' Q5 L; H! @ - sprintf(buf, "%d", a/b);& {9 L6 K% U- {1 H! F; a- X
- }
0 V' Y! a/ f4 r4 `. v
2 a: h: H; f2 e1 j) _- }" \& g$ f) y, g4 o
- }
" x8 f5 ~" ~3 L - ; v, V8 A9 o8 A
- gtk_entry_set_text(GTK_ENTRY(data), buf);% s& ^* L2 S8 G8 k' z4 a& w
9 P" C1 W! r [- return;5 L: t/ N% U( d" t$ u- O
- }
4 T) k0 C% U4 |6 m( D - _) E/ q5 S9 o1 L6 @' C
- int main(int argc,char *argv[])7 R1 e0 M8 O4 O T! t9 k. B
- {
5 U* `& ?4 @# Z9 r& q - $ y7 B B. d C; {8 S9 K
- gtk_init(&argc, &argv);' R9 s9 q' P; Q' n! j" W
- 8 h& O# T( ?6 ~. W" T
- 5 B6 A: C6 ^3 [/ C8 v0 z% Q" O5 N
- GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);7 J) v- V3 e: F
- gtk_window_set_title (GTK_WINDOW (window), "calc");+ K; c9 [2 P! n% k3 N
% S+ h0 P$ S$ `! A1 P- //创建一个表格容器5行4列
) o+ |3 F/ D- f4 K8 n( o3 w - GtkWidget *table = gtk_table_new(5,4,TRUE);4 {7 c9 m0 i) V) X
- / r& d0 M# }) O/ k# }' H
- gtk_container_add(GTK_CONTAINER(window), table);
8 H0 a* d. r- i0 Q - 8 C8 x6 ]$ D8 p- p- T, |# V
- //创建一个行编辑& r7 l4 I- ^! k$ w
- GtkWidget *entry = gtk_entry_new();# @+ z9 a9 c+ t* x9 q' Y; ?
- 8 l! Q2 U! ~4 {% O' B: ^
- gtk_entry_set_text(GTK_ENTRY(entry), "0");% c% v; v0 n8 i0 `
/ p6 T) N7 a# m+ l. D: R- gtk_editable_set_editable(GTK_EDITABLE(entry), FALSE);* z1 I5 l5 F1 {1 q9 j/ i' \
+ j% t/ W& o7 U; @0 I8 c4 J- //创建多个按钮/ F6 N3 }, ~1 n. o* i$ y
- GtkWidget *button0 = gtk_button_new_with_label("0");
7 } C+ D3 M! F6 t* d - GtkWidget *button1 = gtk_button_new_with_label("1");% G4 {, P% t& D, l
- GtkWidget *button2 = gtk_button_new_with_label("2");
, g# w, j9 B1 e. Q - GtkWidget *button3 = gtk_button_new_with_label("3");
, a+ R2 _+ t% U4 \% q$ t - GtkWidget *button4 = gtk_button_new_with_label("4");! h( H+ u7 a) j& W
- GtkWidget *button5 = gtk_button_new_with_label("5");+ r4 L- ~. e1 I
- GtkWidget *button6 = gtk_button_new_with_label("6");
8 i, s" ?/ B7 _$ L - GtkWidget *button7 = gtk_button_new_with_label("7");- B4 }+ [+ H4 A: S& N4 c
- GtkWidget *button8 = gtk_button_new_with_label("8");
0 K. @0 F3 P9 t, l1 f0 U% J* l - GtkWidget *button9 = gtk_button_new_with_label("9");% A' B0 Q9 Y G/ j. F- u$ R
; I& B0 g4 i+ M$ v& l! S" }4 \ B# F- GtkWidget *button_add = gtk_button_new_with_label("+");
" \4 F5 t' Q$ i - GtkWidget *button_minus = gtk_button_new_with_label("-");
, f) e8 r' H2 V$ o" v - GtkWidget *button_mul = gtk_button_new_with_label("*");# B! D6 e4 a0 u2 _/ T
- GtkWidget *button_div = gtk_button_new_with_label("/");0 E7 m" E- O/ A: E& ~
- GtkWidget *button_equal = gtk_button_new_with_label("=");0 \, a2 ?" V0 Z/ l
- GtkWidget *button_delete = gtk_button_new_with_label("c");$ _6 p& y# L: o; j
4 N$ H1 a5 o; k$ R* C
7 _! a4 h( K$ G8 v v$ Q- gtk_table_attach_defaults(GTK_TABLE(table), entry, 0, 4, 0, 1);
& w" F1 x; M b4 A. v. l' L - - Y% x7 X. f6 V
- gtk_table_attach_defaults(GTK_TABLE(table), button0, 0, 1, 4, 5);6 `/ ^' p* M6 C" {6 y/ J7 ~
- gtk_table_attach_defaults(GTK_TABLE(table), button1, 0, 1, 3, 4);# m1 y: V( M7 w+ y
- gtk_table_attach_defaults(GTK_TABLE(table), button2, 1, 2, 3, 4);
8 _) s& h4 d" V7 f- v - gtk_table_attach_defaults(GTK_TABLE(table), button3, 2, 3, 3, 4);
' ?7 r$ |( b, @$ A - gtk_table_attach_defaults(GTK_TABLE(table), button4, 0, 1, 2, 3);
/ g" B4 B6 S& I! C" M' V7 B$ j - gtk_table_attach_defaults(GTK_TABLE(table), button5, 1, 2, 2, 3);0 W4 `. O' X3 T
- gtk_table_attach_defaults(GTK_TABLE(table), button6, 2, 3, 2, 3);
! j4 T9 b2 Y# E y# |+ a - gtk_table_attach_defaults(GTK_TABLE(table), button7, 0, 1, 1, 2);
3 t2 u7 V; j: a. M/ K - gtk_table_attach_defaults(GTK_TABLE(table), button8, 1, 2, 1, 2);
1 }0 n9 J2 u0 y" S5 J - gtk_table_attach_defaults(GTK_TABLE(table), button9, 2, 3, 1, 2); N. \# |* S) G N2 j
- 8 \7 `' t2 E3 {9 }8 O0 Y) a! k3 `
- gtk_table_attach_defaults(GTK_TABLE(table), button_add, 1, 2, 4, 5);8 k' l/ u! W4 a- H4 W- Y, \
- gtk_table_attach_defaults(GTK_TABLE(table), button_minus, 2, 3, 4, 5);
/ j2 L- }# A- [ - gtk_table_attach_defaults(GTK_TABLE(table), button_mul , 3, 4, 2, 3);
7 H2 @" D9 b$ Z5 A% l5 z8 c5 Y - gtk_table_attach_defaults(GTK_TABLE(table), button_div, 3, 4, 3, 4);- Y4 P% l0 Q- F) M/ t0 v( b
- gtk_table_attach_defaults(GTK_TABLE(table), button_equal, 3, 4, 4, 5);
+ j; Y. G# |" z) p7 x9 b - gtk_table_attach_defaults(GTK_TABLE(table), button_delete, 3, 4, 1, 2);
0 H/ u" C. @" i x3 w! h {
; u" S8 v" Q- h0 ]) n) X8 P- //注册信号函数,calc()# @ w4 X: L7 t
- g_signal_connect(button0, "pressed", G_CALLBACK(calc), entry);+ x- @- Z. B+ N+ d+ c+ k0 T
- g_signal_connect(button1, "pressed", G_CALLBACK(calc), entry);* \( a1 Y( H; b) ^4 i% Y
- g_signal_connect(button2, "pressed", G_CALLBACK(calc), entry);
0 J0 F$ V& g/ u! N" ~( j: A# p7 }* E - g_signal_connect(button3, "pressed", G_CALLBACK(calc), entry);
8 a5 \* J8 Z, [$ X) r; ?0 l - g_signal_connect(button4, "pressed", G_CALLBACK(calc), entry);
3 O" r+ w- B, i - g_signal_connect(button5, "pressed", G_CALLBACK(calc), entry);
* s% Q1 [" w" G) H. }4 k, t/ ] - g_signal_connect(button6, "pressed", G_CALLBACK(calc), entry);
9 k! z7 h2 O* Q; J1 B( Z - g_signal_connect(button7, "pressed", G_CALLBACK(calc), entry);5 s( G" e7 t( t% D! O" g
- g_signal_connect(button8, "pressed", G_CALLBACK(calc), entry);/ q* A9 o; ^; W- B9 K5 e
- g_signal_connect(button9, "pressed", G_CALLBACK(calc), entry); a _* v% G) C+ N
- " L8 N3 x( O5 w& V) Q
- g_signal_connect(button_add, "pressed", G_CALLBACK(calc), entry);
9 J% b/ m q6 ~# j$ M0 L3 { - g_signal_connect(button_mul, "pressed", G_CALLBACK(calc), entry);
& |1 Q: K I# ^9 q( S1 w* L - g_signal_connect(button_div, "pressed", G_CALLBACK(calc), entry);5 n6 o2 T- ~8 Q1 T# j8 A
- g_signal_connect(button_minus, "pressed", G_CALLBACK(calc), entry);& G) ?# z5 V% r6 i/ h, d
- g_signal_connect(button_equal, "pressed", G_CALLBACK(calc), entry);$ r2 S8 m4 r: V
- g_signal_connect(button_delete, "pressed", G_CALLBACK(calc), entry);
# x P7 v7 S6 q0 B$ l7 B - 3 \5 G6 V+ L0 }- }; y0 \( [$ R
2 h5 g5 O' y1 V k" d. u5 \- gtk_widget_show_all(window);5 W- R+ J3 r3 x0 s' V V; W# I2 `4 s0 L* a
! @# O( b( m9 Q' z5 n5 G0 m4 n u- //主事件循环5 ^3 L, E3 }. }4 ?# o
- gtk_main();
# r; u( o/ H+ m* J
/ C5 ]7 ~4 d# m0 |- return 0;- W/ F/ T8 W5 f
- }
% D$ n% A! b( Z
复制代码 以上程序,编译运行后,就可以有以下得效果
+ ~* N0 _: E8 X1 i" @' D. Y
- \9 v2 [2 Z! d3 ` 操作和UI都是比较简单得,就不演示过程了,系统得触摸驱动也是做好的,gtk只管做应用就好了。操作也算流畅,毕竟也没涉及到复杂的图表渲染。 搞到这里,发现其实不复杂得GUI程序用这套现成的框架倒也是挺方便。Linux系统下其实资源很丰富,软件开发方式也很多选择,所以不多做逗留了。这款开发板还有一大特点就是可以按照以往STM32单片机得方式进行使用,所以后面感受下1GHz的单片机开发方式。 |