上次已经开启了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/,,这个路径就可以,直接继续安装。
/ r+ t+ Y1 t3 B. g1 J9 S4 M; N
9 A) k8 P% e% v% x: Z) O$ k0 f2 v 然后就学着官方文档的样子去验证下SDK是否安装成功$ ^1 X! T( e" e) S1 X
8 o1 d4 S: I' d1 h- E0 U. E 以上就完成了编译环境,至于说开发环境,一般还得有个IDE或者其他编辑器加调试工具。这里打算做的小程序使用gedit就行。 首先拷贝官方给的demo程序,基于GTK得hello world。 - #include <gtk/gtk.h>/ ]3 w8 Y4 a/ k1 ]2 f- q( |6 }
2 W) T+ J. u L/ W, ~- static void) y/ Q9 W+ J5 B. k+ o5 r
- print_hello (GtkWidget *widget,* U( Q5 q; {- s; M+ f0 }
- gpointer data)- [" q" ~% |, Z6 P1 T# {! ^. ^, Y! X
- { ?0 f( ?9 H) j, S S6 \; m+ B
- g_print ("Hello World\n");5 {; N+ b* `/ Y' C9 [
- }
' ^( l2 f- ]- Y1 ^* J
& N- f, O& u. N$ T- static void
# i; p% F* _: {7 ]/ R3 l' l3 D9 L3 M - activate (GtkApplication *app,( k' ?" e4 N* U) V' `
- gpointer user_data)
5 Y4 \& ^& a; L& n% y* r - {
( J0 w2 n& b$ X1 ? - GtkWidget *window;: L' ~4 K5 [3 v( t
- GtkWidget *button;
% p7 N$ W* C9 ^5 J( f - GtkWidget *button_box;
5 a! Y! d" E4 ^+ o/ }. P - 0 D' M. Y# m% q5 a( R7 b: M/ ]
- window = gtk_application_window_new (app);* L0 y% B' t/ l
- gtk_window_set_title (GTK_WINDOW (window), "Window");
2 f$ T( f6 p1 x) x5 @' f' t1 {; B3 m - gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
+ a# B% Y: r7 C/ |* E! k. E+ F% P - 9 n5 \" y Z& ?4 Z4 a# D! W% M
- button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);4 z2 _3 W1 l1 K5 K5 ]
- gtk_container_add (GTK_CONTAINER (window), button_box);
0 ^; _2 E/ R3 h% t0 O
# o( M' D. l! G- button = gtk_button_new_with_label ("Hello World");
" u% L# K- U) r4 R. G! a% x% i - g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);' q) B P) W. n$ L% v
- g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);3 D! }% P5 y' |0 R! r/ x' }7 N
- gtk_container_add (GTK_CONTAINER (button_box), button);' O7 O; l, `: k- i2 e9 ?
- # f/ i; U6 h! J- B, _4 h8 S
- gtk_widget_show_all (window);! _" w" i6 B; a G- ]% W
- }
/ g* X) y. m* j - 6 {$ K) m8 M# S4 a. B, }! D5 Y
- int! m4 J% B p" e2 v2 {8 @& f
- main (int argc,
* w7 A& [3 n2 K: E - char **argv)
5 V7 d3 p# T" ^# C; A$ ~$ _ - {
- D0 ?) Z* ^; f4 d0 S$ H4 O( S- F - GtkApplication *app;
% A8 P* }. | s5 x) m: @ - int status;( ?1 M" |/ b% Y
5 \' h0 j; {' q; j0 G r- app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);- Y# L) n: D( h& O! y
- g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
4 L" M3 t" ?; W# x - status = g_application_run (G_APPLICATION (app), argc, argv); O% a: A# v* E2 p# J
- g_object_unref (app);$ t6 d) g. d$ q
- ' N# b- T$ L& @) a" F, B7 V7 `
- return status;
; @* m/ b# X* `1 l - }% r6 Z) \; P% r6 \0 E- H$ h
复制代码 然后也复制一个makefile文件,其实单个文件,直接使用gcc命令也可以得。
) p" T7 h+ H: r% }- f2 O9 n- PROG = gtk_hello_world
, }$ |- |5 [# N% n7 V# H! ] - SRCS = gtk_hello_world.c
5 j4 ?/ U- J n W7 K7 @$ S$ A - 2 y0 ]! K5 M, K
- CLEANFILES = $(PROG): {2 w0 w/ h- o3 w X5 c
- " S) Z; o6 H4 `; X ?# q$ h, I
- # Add / change option in CFLAGS and LDFLAGS
/ a# K1 s1 G% m$ e/ z9 l - CFLAGS += -Wall $(shell pkg-config --cflags gtk+-3.0)
" D u+ j4 @; {4 B - LDFLAGS += $(shell pkg-config --libs gtk+-3.0)' U" `, q+ G+ W4 N# R, g
- * y1 k$ ]! ^/ @6 w2 H; X
- all: $(PROG)
, D* r; {7 B* D
7 J6 k5 t5 S0 H6 O/ [" A* ?- $(PROG): $(SRCS)
8 r6 M4 X, |% B$ K& i - $(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
. h6 l7 D. q& v# P: ^1 [- b
" X5 R" G9 K6 F- clean:. q v* I2 K8 V/ ?: ]7 _
- rm -f $(CLEANFILES) $(patsubst %.c,%.o, $(SRCS))
& ?5 P1 O9 w: a7 h4 B
复制代码 然后make,scp到开发板执行程序,就可以看到一个hello world窗口出现在之前得桌面上了。
2 L, `5 }' i6 _* `4 A 接下来改程序,说实话没用过gtk,不过看了下helloworld得程序可以看懂,所以也百度了一下,就基本做出来了。7 \; R( m" T( A
; e8 G& O, O% ?$ E/ J4 J; ^5 l
& x9 W0 m9 H$ B, V0 X4 ]7 q- #include <gtk/gtk.h>
. A/ d# X: ?& ?/ \& e1 l2 u8 s8 u - #include <stdio.h>
* G/ @% T' J3 ~/ D. i; L3 X - #include <string.h>
+ w; j+ D0 d6 p - 8 y' z7 J. T) {$ s
- char buf[50] = "";
# p- A2 Q4 L0 w$ q! S1 k6 C - 3 L0 Z1 v; d7 y9 Q
- void calc(GtkButton *button, gpointer data)& g# w' o7 I$ J B& Q
- {
$ C* Y9 L+ ]5 e - const char *text = gtk_button_get_label(button);
& V. O9 ~. U) Z0 {( c9 A - ) {2 g$ X4 J+ f4 ^, K. I
- //退个操作6 z+ O$ R) v6 b8 B& j' h S* s; g
- if (0 == strcmp(text, "c"))
/ W' o* ]2 E' N K5 K - {
8 N5 K* g5 V( } |8 u2 Z1 a& c - buf[strlen(buf) - 1] = 0;
" d3 h: M- _2 m: g - }else{( y/ l( Q9 W2 d3 R
- P- E. w* H* s4 Y- int a = 0, b = 0;% T6 c v- O0 X }5 c3 W) u9 i
- char c;# s& i8 Q2 \& N, O& B7 x
- strcat(buf,text);, e/ O4 j! I9 s5 s
; c3 w& s: w. j- L/ E3 |! [- if (0 == strcmp("=", text)) {
7 d$ l" t) }% O. k6 } - - L# [% N( N' g- I/ M7 Z
- sscanf(buf, "%d%c%d", &a, &c, &b);
( M& u/ L6 j7 o3 C1 x
- X6 w+ n+ n' O* x. i1 T- if ('+' == c) {
1 B' x5 |% p# `6 M - sprintf(buf,"%d", a+b);
, s' S4 f: n/ F ?9 y( X - }2 m! Q$ M% Z: \/ j) J( B0 G
- else if ('-' == c) {; G7 R3 M l: F+ y- k6 p: D# Q
- sprintf(buf, "%d", a-b);9 g7 `1 v& A+ Z {
- }
% C6 N( B* m( {( N# W+ m; ? - else if ('*' == c) {. U' X7 {2 F% V
- sprintf(buf, "%d", a*b);
) V2 u& I% t) S# a, g; w" I - }7 x, b6 k( Z4 [5 ^8 m6 \- {7 r
- else if ('/' == c) {
$ {; A3 X. ~' \& h - sprintf(buf, "%d", a/b);& G$ O0 J$ A2 i
- }8 I% ^4 D5 s9 ^1 l& V6 k, y) W
- : L8 N' N' I$ I; S3 w9 B' Y
- }2 j! A' t. D! Q2 J5 i
- }6 x7 J$ X0 Q- V4 b
- % X7 i( ~2 l$ E/ B' u5 [9 A
- gtk_entry_set_text(GTK_ENTRY(data), buf);
1 K3 ]7 Z" f b3 R& X
0 G7 a6 i4 [3 n! t0 v+ P* T- return;# k1 I1 M c* U7 T0 L4 n: N& `
- }" w0 o/ Y, m9 p8 @
- & M0 {3 ?- w/ U8 j
- int main(int argc,char *argv[])
6 W7 j( b9 [; S- v: S - {. n9 x: C. c1 u' \( v
% I' s) |/ h2 i2 E d- gtk_init(&argc, &argv);
: E- `: {2 J( K# \# Q! ^! D - ! F4 L4 g8 T8 U
( S$ C0 Q9 E5 O' a& B- GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);$ Z8 Q. v: ]4 T& \8 T! n
- gtk_window_set_title (GTK_WINDOW (window), "calc");
! a1 ~; l, f' t7 k - 4 x, ?8 K$ R B* R
- //创建一个表格容器5行4列
4 N/ X. [1 {1 b8 S - GtkWidget *table = gtk_table_new(5,4,TRUE);* A3 P4 t9 u- S7 M( B
! ^. W6 X- t& b2 p9 O- gtk_container_add(GTK_CONTAINER(window), table);
0 X: T' m2 G T/ ^ - $ A# K7 [7 A- \
- //创建一个行编辑+ I5 I' |4 v1 U: W7 b
- GtkWidget *entry = gtk_entry_new();
& n0 y8 e2 y1 x$ s1 I7 m - ; N: n, r' S/ E: i
- gtk_entry_set_text(GTK_ENTRY(entry), "0");
) Q- C5 r! w: B, U - : G* p3 W( K' Q% ]
- gtk_editable_set_editable(GTK_EDITABLE(entry), FALSE);" C1 s# f0 O1 r+ l+ f/ [. w
, ~/ Y3 s& R* J6 O# V- //创建多个按钮
m* X: v9 a% b( _5 F' g - GtkWidget *button0 = gtk_button_new_with_label("0");# {& X" E) |) F' W
- GtkWidget *button1 = gtk_button_new_with_label("1");( P( e( t# L5 d' {5 P( E( f
- GtkWidget *button2 = gtk_button_new_with_label("2");
F/ n0 D% Y% A9 `" u; ] - GtkWidget *button3 = gtk_button_new_with_label("3"); r$ P. o2 o0 r' T/ O- H4 ?6 D: _0 h
- GtkWidget *button4 = gtk_button_new_with_label("4");3 q' i2 [( _/ V: p6 {
- GtkWidget *button5 = gtk_button_new_with_label("5");
0 y6 d2 ^; I+ V7 K - GtkWidget *button6 = gtk_button_new_with_label("6");
2 ^& f. Y1 P* x, `* T( f) ` - GtkWidget *button7 = gtk_button_new_with_label("7");- i. h% j+ E. y8 b7 p
- GtkWidget *button8 = gtk_button_new_with_label("8");
7 ]. c6 y' ~: j& l - GtkWidget *button9 = gtk_button_new_with_label("9");4 w% E, b e; y' L" b x( O* ~
- + @( O+ Q$ ]' G/ H
- GtkWidget *button_add = gtk_button_new_with_label("+");
1 M2 E. c' {6 w. B$ Q - GtkWidget *button_minus = gtk_button_new_with_label("-");
# j" d' Y. I1 G r4 S8 [7 Z - GtkWidget *button_mul = gtk_button_new_with_label("*");; `6 _' s! j" h/ K& Y" r9 u
- GtkWidget *button_div = gtk_button_new_with_label("/");: B* a' l/ i0 f- [" d/ X; j
- GtkWidget *button_equal = gtk_button_new_with_label("=");
: N2 U& z e! V1 f, k0 }5 D - GtkWidget *button_delete = gtk_button_new_with_label("c");
2 b$ z h+ E5 |* r4 X9 {9 x - 7 ?5 B V- P2 o% O, d0 d8 }
, e* Q b& q& \$ G3 ^4 i) Z2 Z- gtk_table_attach_defaults(GTK_TABLE(table), entry, 0, 4, 0, 1);
5 @& `. O0 M z& ~: M" X - 7 P; `/ N) X% w6 ]: Q& N
- gtk_table_attach_defaults(GTK_TABLE(table), button0, 0, 1, 4, 5);
/ u* \: b! r! h6 j2 C4 O' Q; F" z p, P - gtk_table_attach_defaults(GTK_TABLE(table), button1, 0, 1, 3, 4);) g p: E5 e( s
- gtk_table_attach_defaults(GTK_TABLE(table), button2, 1, 2, 3, 4);
5 ^* c2 d. \6 R& v' |; w - gtk_table_attach_defaults(GTK_TABLE(table), button3, 2, 3, 3, 4);( R! }3 }/ B- y3 x% C
- gtk_table_attach_defaults(GTK_TABLE(table), button4, 0, 1, 2, 3);
( Q% N, F' y4 M6 I! V$ O. } - gtk_table_attach_defaults(GTK_TABLE(table), button5, 1, 2, 2, 3);
% Y/ v% @2 ]* b& v - gtk_table_attach_defaults(GTK_TABLE(table), button6, 2, 3, 2, 3);
' t; p% n& l8 r+ a6 o8 w - gtk_table_attach_defaults(GTK_TABLE(table), button7, 0, 1, 1, 2);) @3 P1 J7 e8 I1 D8 e% x: H/ J
- gtk_table_attach_defaults(GTK_TABLE(table), button8, 1, 2, 1, 2);
( Y5 q+ t( b! ?+ u9 S# Z( |- x9 K, B - gtk_table_attach_defaults(GTK_TABLE(table), button9, 2, 3, 1, 2);3 T7 j! @) c8 M4 j. R% V
- % c" G2 V' ^4 |
- gtk_table_attach_defaults(GTK_TABLE(table), button_add, 1, 2, 4, 5);
S2 ^' p3 G* W' \6 f' r - gtk_table_attach_defaults(GTK_TABLE(table), button_minus, 2, 3, 4, 5);' g" _% a9 P9 C6 `8 E
- gtk_table_attach_defaults(GTK_TABLE(table), button_mul , 3, 4, 2, 3);9 ]' s) o: Q* t; k& k8 r8 m: d! J
- gtk_table_attach_defaults(GTK_TABLE(table), button_div, 3, 4, 3, 4);$ B& E6 d3 r: P: z0 a( z
- gtk_table_attach_defaults(GTK_TABLE(table), button_equal, 3, 4, 4, 5);
/ [9 ]! v f- f0 z& i: [ - gtk_table_attach_defaults(GTK_TABLE(table), button_delete, 3, 4, 1, 2);
4 A/ i3 g% ?! O9 b9 o: @& X# W - % H3 M6 d+ \3 W
- //注册信号函数,calc()
; h2 ?& r9 j- w# l. q - g_signal_connect(button0, "pressed", G_CALLBACK(calc), entry);
, V, n$ U& w2 _( E @ - g_signal_connect(button1, "pressed", G_CALLBACK(calc), entry);4 @0 F3 ]3 U& B1 c( [
- g_signal_connect(button2, "pressed", G_CALLBACK(calc), entry);# s4 t [/ Q) m$ W+ \% O
- g_signal_connect(button3, "pressed", G_CALLBACK(calc), entry);
3 }7 @; b' {+ n' ? - g_signal_connect(button4, "pressed", G_CALLBACK(calc), entry);; `1 A& Q* A7 }/ h9 p& K) |
- g_signal_connect(button5, "pressed", G_CALLBACK(calc), entry); x$ [3 ^( [$ S: y
- g_signal_connect(button6, "pressed", G_CALLBACK(calc), entry);
9 |- I M: O- ]6 h# e$ W+ E - g_signal_connect(button7, "pressed", G_CALLBACK(calc), entry);* e* a+ `9 D# Y9 m2 A& G
- g_signal_connect(button8, "pressed", G_CALLBACK(calc), entry);
9 U- J9 x( ?5 D+ |, z: y - g_signal_connect(button9, "pressed", G_CALLBACK(calc), entry);
. ]$ ]2 r4 ~) Y- x3 y
2 Y- |- w$ i' K0 G- g_signal_connect(button_add, "pressed", G_CALLBACK(calc), entry);
& l& R) b; ~ ^/ |- [' t d - g_signal_connect(button_mul, "pressed", G_CALLBACK(calc), entry);
O1 F/ v; Z! M2 G8 c* w6 x - g_signal_connect(button_div, "pressed", G_CALLBACK(calc), entry);1 l$ x; o2 I8 x2 z
- g_signal_connect(button_minus, "pressed", G_CALLBACK(calc), entry);/ h$ B m% Q/ b3 `! M
- g_signal_connect(button_equal, "pressed", G_CALLBACK(calc), entry);+ @9 ]) N# ^/ s. q' e! R
- g_signal_connect(button_delete, "pressed", G_CALLBACK(calc), entry);
, H; Z T n C5 t8 e - s, s4 {$ D4 X& ^) _3 H6 i# e
) G( E6 H: h3 D1 m( g- gtk_widget_show_all(window);
2 g6 z4 h2 ~4 } - 6 _4 U- W! y2 @' B1 R8 _. \
- //主事件循环
( }; G$ `) o7 R& W7 d - gtk_main();
a0 h/ e* L% F - 1 C, U7 U2 @5 Z* N, s# H
- return 0;& ]* T8 V, l% t
- }. [! a; }+ ?( i; X. W1 E: O! W+ X
复制代码 以上程序,编译运行后,就可以有以下得效果
! m8 j& _! b( v% }" R* E
0 _. r2 s9 `$ Z+ ?8 R" j 操作和UI都是比较简单得,就不演示过程了,系统得触摸驱动也是做好的,gtk只管做应用就好了。操作也算流畅,毕竟也没涉及到复杂的图表渲染。 搞到这里,发现其实不复杂得GUI程序用这套现成的框架倒也是挺方便。Linux系统下其实资源很丰富,软件开发方式也很多选择,所以不多做逗留了。这款开发板还有一大特点就是可以按照以往STM32单片机得方式进行使用,所以后面感受下1GHz的单片机开发方式。 |