上次已经开启了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 C2 a" e% F. O1 _! @
6 H7 |9 z, s, t( j: A% s+ E8 T) z
然后就学着官方文档的样子去验证下SDK是否安装成功
; j1 P |+ G. r
0 |- n' }' I1 L
以上就完成了编译环境,至于说开发环境,一般还得有个IDE或者其他编辑器加调试工具。这里打算做的小程序使用gedit就行。 首先拷贝官方给的demo程序,基于GTK得hello world。 - #include <gtk/gtk.h>
( F4 [! s( I8 H3 R
( Y' O5 A: `' i# n. |- static void
9 Q, Q% b2 b) Y - print_hello (GtkWidget *widget,2 i U% f( U( S; _& ?
- gpointer data)
3 q/ ~2 z5 u8 s. g! i6 A) d - {
$ ~& [) d4 d# B! z6 P0 f+ z0 l - g_print ("Hello World\n");' \0 ]* e3 H" O* f$ e; y# _
- }
1 ?( r1 a- J5 r7 n) v: y - / B9 O* E% E/ q( G
- static void# @" B* i5 ~; t# J
- activate (GtkApplication *app,
3 @+ Z/ T+ \1 q. R - gpointer user_data)
3 R( S2 R, }0 G7 j - {
; A' T1 P: e, R6 k3 Q4 o$ d - GtkWidget *window;* ?! T9 `) S) v. \
- GtkWidget *button;. q* y/ y' R4 N5 L& T# W
- GtkWidget *button_box;7 t$ k, u0 f9 _. T5 R) a
! l2 k$ V1 c0 J8 x- i7 \- window = gtk_application_window_new (app);( h" C7 e/ i& z* a
- gtk_window_set_title (GTK_WINDOW (window), "Window");% p, W1 E! {; W! Q/ x: F$ v
- gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);' i! Y. x/ U1 X3 T
/ \5 V7 H* |9 B' ~- button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
4 T, Z% m+ [, K! Y - gtk_container_add (GTK_CONTAINER (window), button_box);7 Z6 Q3 A% `% L$ k/ p9 Z7 B& f8 \
- 9 m0 i5 H2 G, y* G' m
- button = gtk_button_new_with_label ("Hello World");& v2 X8 B+ B/ h3 ^) Y0 F
- g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);2 X# }& I0 Y5 f* a3 V
- g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
( G) O7 o9 U# U - gtk_container_add (GTK_CONTAINER (button_box), button);1 d7 G$ L- l4 H7 v
- + j/ J2 G6 g* K; h0 z' m
- gtk_widget_show_all (window);
$ _1 I/ E! s% M' `' m - }5 G5 E, ^$ R$ N; {! V9 U9 ]
" U+ C0 ~" n, [" t, X/ y- int
+ G5 Q; _$ d# ? - main (int argc,
4 h4 K+ U& s v: x - char **argv)( ^) L% w. `- [3 m
- {$ v/ \ g9 b! B' l
- GtkApplication *app;
! x* \2 z0 Y' a - int status;
' Y4 m4 S5 q; n8 {- Z: U- B - 5 x. l( @8 U) O
- app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
# T% m4 p0 p9 k" C, L - g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
7 o7 ]9 B- z/ z - status = g_application_run (G_APPLICATION (app), argc, argv);
3 T/ o7 P* y% {/ U - g_object_unref (app);9 [% p3 }. }1 p/ N- D! v- m
- 2 x5 Q# d6 O( t/ }! C) z' E
- return status;% Y: i7 X5 R; X% g
- }5 x/ I2 U) V+ I: \3 G# c, x
复制代码 然后也复制一个makefile文件,其实单个文件,直接使用gcc命令也可以得。
. u. G+ l! i3 v! V4 q3 S- PROG = gtk_hello_world
4 d$ j$ j, _+ S/ R" J - SRCS = gtk_hello_world.c
+ O! `% P- n8 H$ R0 \ - 8 K( j3 V& p- n2 W7 W* V
- CLEANFILES = $(PROG)3 S8 s, N" ^3 `6 A: }6 n; Q: L3 g9 E
5 P {" I5 I# i+ W- # Add / change option in CFLAGS and LDFLAGS
$ `4 o2 T9 B; S( e. w - CFLAGS += -Wall $(shell pkg-config --cflags gtk+-3.0)
# r! m% ^& v$ v - LDFLAGS += $(shell pkg-config --libs gtk+-3.0)
" O# B. S6 u( [/ |- u
. ~" a6 z" b5 l! O, [! `$ p/ D- all: $(PROG)# w+ M: x: Q& U; z$ V
, n; T4 m3 z t; F& c0 W/ W) ^: }- $(PROG): $(SRCS)
2 p7 z% W% `3 X - $(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)% z1 k! W9 S0 G! |& M. {$ l
- , G5 f- S9 z4 L" f( {" \; s% _
- clean:
* F3 z6 I- r3 T9 H, N0 S - rm -f $(CLEANFILES) $(patsubst %.c,%.o, $(SRCS))
9 M2 k0 L' T3 L8 i/ b$ r* W. ?
复制代码 然后make,scp到开发板执行程序,就可以看到一个hello world窗口出现在之前得桌面上了。
: y0 q, J8 q& [ Z( b 接下来改程序,说实话没用过gtk,不过看了下helloworld得程序可以看懂,所以也百度了一下,就基本做出来了。7 V/ k. K1 H, B
# N( f( z( V% K) \2 W- F
( K: s- t0 q7 ? L- #include <gtk/gtk.h>) g. e, F! ~. W: e
- #include <stdio.h>- M( W z, z" }. |# C3 t; ?
- #include <string.h>
' f+ w' G- c, c - 2 m- }. Q' ^ d; z# B
- char buf[50] = "";4 _: Z# L4 b |# ?# N
- : E# J( W4 `2 \6 B" B: R
- void calc(GtkButton *button, gpointer data)
$ a) s# a' u* `9 i2 A - {/ ]# Q( y% S. f( L! I
- const char *text = gtk_button_get_label(button);
* I0 X) C, R; F$ l5 z+ ^ - 2 L( c9 h& W: M" c
- //退个操作- e* s; k6 Z. `4 U
- if (0 == strcmp(text, "c"))
. Z& X: L8 v0 I" N - {
; y# t6 s, p" D, H0 i* f! m4 R - buf[strlen(buf) - 1] = 0;# O1 @' ]8 @) O+ |6 { D3 K
- }else{
. e( x3 J( L. W - $ h+ n1 V4 K2 x
- int a = 0, b = 0;( z* O% h: s) j
- char c;
7 S/ `5 x5 k3 h; ? - strcat(buf,text);
" A5 s8 S7 G6 Q# J - + h8 v; J4 V' l% g0 F0 `
- if (0 == strcmp("=", text)) {
4 C( o- r1 _1 \6 N8 r - * r0 ^: E9 y5 Y; @5 _
- sscanf(buf, "%d%c%d", &a, &c, &b);
9 i: D. V7 S! u0 l) w
% j" I) r& z u6 b- if ('+' == c) {
6 E, ^+ C1 H! W% m# I - sprintf(buf,"%d", a+b);
' n* V& w9 w, `5 |0 H5 [- X' b - }
w% \1 o0 e2 D( D2 S, [ G" t1 o - else if ('-' == c) {- a2 O" b9 I0 e/ G* M
- sprintf(buf, "%d", a-b);
4 G* }. ?$ u3 G6 }! t$ l* C - }
0 A' \5 H& ?$ `4 R; X4 _8 ]- H4 m - else if ('*' == c) {6 \ ~, g( I" N7 w* T
- sprintf(buf, "%d", a*b);
% h/ J9 F; m/ T1 ^/ T7 ` - }: Q6 p% B$ n, C2 c) C
- else if ('/' == c) {
" n, L0 j1 W: ^ - sprintf(buf, "%d", a/b);
) ~, ?' \7 `& N p% |1 b - }. z* g: n. \, ?6 h8 p) H2 o
) v4 v4 _( H+ V0 z+ {# i- }$ q7 e; U$ p- o. }+ ^. p5 o' W
- } q8 D' H" ]9 c4 X( L" ^
- 1 @ J7 U* m1 k) e2 i, `
- gtk_entry_set_text(GTK_ENTRY(data), buf);
K9 m2 g& l) q( g& s) {/ \% D - ; _/ H+ T& M k
- return;5 f7 l1 d& ~, ^4 y3 ~
- }( g. b! c* G9 l. v# B) `* R5 a
- 6 v# l; t- u3 a" C; S) U0 `
- int main(int argc,char *argv[])
* }. h1 l( l. T% }3 Q - {
- P o; ?* d) o: `& l* H - + t- i+ [3 M9 A
- gtk_init(&argc, &argv);& M9 s) F2 J% z. F
- ( h" {4 }. `2 ~% @. S& y, v
8 v" r6 d, H# i; E- j- GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);# Q( }" O0 K$ F" K8 O) C9 L9 r
- gtk_window_set_title (GTK_WINDOW (window), "calc");
/ E, o! B# Z7 ]2 Y' m) V* ^1 g8 ] - ' M/ ~3 g v& g
- //创建一个表格容器5行4列
0 \9 W' r# Y1 w ^ - GtkWidget *table = gtk_table_new(5,4,TRUE);! v* V1 [$ e9 }2 a% C) g# V
- ! v( r! p$ j! ?, g/ D- S, h
- gtk_container_add(GTK_CONTAINER(window), table);
* n9 D+ [- J, V% y8 r0 b - 7 V! [! E1 Y3 y8 k& O
- //创建一个行编辑
% m: M- M' E1 C) O9 H* N - GtkWidget *entry = gtk_entry_new();
3 L; l/ z8 K6 V% g/ N4 ^
$ {9 o; p" y/ z- gtk_entry_set_text(GTK_ENTRY(entry), "0");& q0 q* X: D! M2 V3 g- e
. P3 `4 I5 S$ V- gtk_editable_set_editable(GTK_EDITABLE(entry), FALSE);" j/ F$ M3 [! x
- ! b& P2 {0 |! Q) e1 j g
- //创建多个按钮
; ~3 ~7 \1 ]% J; D: A$ [7 k. E* } - GtkWidget *button0 = gtk_button_new_with_label("0");1 G/ {5 O% _! }# M( o: m
- GtkWidget *button1 = gtk_button_new_with_label("1");1 I2 P- U1 S; b6 i
- GtkWidget *button2 = gtk_button_new_with_label("2");7 }; `) z0 J* c" m$ L4 q+ N* H5 P
- GtkWidget *button3 = gtk_button_new_with_label("3");6 a6 z8 L s4 T' Y* t
- GtkWidget *button4 = gtk_button_new_with_label("4");" r; J' i: ~+ r) t/ s
- GtkWidget *button5 = gtk_button_new_with_label("5");
& k4 c8 h F8 L5 |- d* f - GtkWidget *button6 = gtk_button_new_with_label("6");
+ {+ f4 a* Q2 V! k) Y; ^% { - GtkWidget *button7 = gtk_button_new_with_label("7");" B# n. N! S+ L) [% I+ ~ w, F
- GtkWidget *button8 = gtk_button_new_with_label("8");
7 v) o l5 U/ J D1 g3 d - GtkWidget *button9 = gtk_button_new_with_label("9");1 ?' N. ^" r! k
- # W* j$ G6 x& p* f! t" p
- GtkWidget *button_add = gtk_button_new_with_label("+");3 b" q. V$ [6 P
- GtkWidget *button_minus = gtk_button_new_with_label("-");
7 c# c2 t0 L% Z; U4 r, k/ e, ~ - GtkWidget *button_mul = gtk_button_new_with_label("*");' L2 [* X. r/ K' E
- GtkWidget *button_div = gtk_button_new_with_label("/");7 \7 z% @% [/ g, q+ r/ X$ C5 V J$ i
- GtkWidget *button_equal = gtk_button_new_with_label("=");
; s- e. E9 D/ g; U/ `3 a' l% Y, v5 k - GtkWidget *button_delete = gtk_button_new_with_label("c");
: _5 f9 r& B- ~9 x$ o
$ y! f- Q1 J: T7 S( a- 2 _2 L$ F1 o6 y7 O7 |
- gtk_table_attach_defaults(GTK_TABLE(table), entry, 0, 4, 0, 1);( I9 c) O% O( [# S6 b
& D" n$ T/ c# N! |& s- gtk_table_attach_defaults(GTK_TABLE(table), button0, 0, 1, 4, 5);4 n; Y* E9 O- O! Q# t2 S
- gtk_table_attach_defaults(GTK_TABLE(table), button1, 0, 1, 3, 4);
0 J6 A( ~) V7 V X& Y1 _# K - gtk_table_attach_defaults(GTK_TABLE(table), button2, 1, 2, 3, 4);3 w9 \' i ^, ?* ^/ l5 G
- gtk_table_attach_defaults(GTK_TABLE(table), button3, 2, 3, 3, 4);; b9 N( I/ ?# Q c. a4 a7 T- L
- gtk_table_attach_defaults(GTK_TABLE(table), button4, 0, 1, 2, 3);
6 H+ E+ S: ^& J5 ^5 w" D - gtk_table_attach_defaults(GTK_TABLE(table), button5, 1, 2, 2, 3);
4 y' e- I, |& V! Q8 n - gtk_table_attach_defaults(GTK_TABLE(table), button6, 2, 3, 2, 3);4 F9 u: b" ?3 e8 Q9 r5 t0 I+ B
- gtk_table_attach_defaults(GTK_TABLE(table), button7, 0, 1, 1, 2);$ a) E' I% b: O3 \: w3 }3 L1 J
- gtk_table_attach_defaults(GTK_TABLE(table), button8, 1, 2, 1, 2);3 y; g) R5 _" t+ }
- gtk_table_attach_defaults(GTK_TABLE(table), button9, 2, 3, 1, 2);7 i8 I" y* Z9 u _! Z h$ V
- * D( o' s+ ^# l }* e8 [3 Q8 J) x
- gtk_table_attach_defaults(GTK_TABLE(table), button_add, 1, 2, 4, 5);0 J- @( X/ ]1 {1 \8 Z" l$ o8 [
- gtk_table_attach_defaults(GTK_TABLE(table), button_minus, 2, 3, 4, 5); c% d- J$ s1 v& w: U
- gtk_table_attach_defaults(GTK_TABLE(table), button_mul , 3, 4, 2, 3);9 o/ i7 v, L6 F( ^1 `
- gtk_table_attach_defaults(GTK_TABLE(table), button_div, 3, 4, 3, 4);- V) p0 m2 {2 m5 K
- gtk_table_attach_defaults(GTK_TABLE(table), button_equal, 3, 4, 4, 5);1 Q. Y$ M- [5 |* b4 _
- gtk_table_attach_defaults(GTK_TABLE(table), button_delete, 3, 4, 1, 2);
' w2 z R; p) B/ Y& }: t
! D' [, ^# x* t$ Y/ `3 Q- //注册信号函数,calc()
$ w! x# `+ y8 l6 e$ b3 W# N+ _& m - g_signal_connect(button0, "pressed", G_CALLBACK(calc), entry);3 S: M4 S7 X6 [4 `3 n! d/ D6 J0 N
- g_signal_connect(button1, "pressed", G_CALLBACK(calc), entry);
3 s! r& ?+ K4 E - g_signal_connect(button2, "pressed", G_CALLBACK(calc), entry);
7 g+ _8 i' v6 x' B - g_signal_connect(button3, "pressed", G_CALLBACK(calc), entry);
a/ @) V+ j1 A- q! o - g_signal_connect(button4, "pressed", G_CALLBACK(calc), entry);0 C/ Y; |( u7 c
- g_signal_connect(button5, "pressed", G_CALLBACK(calc), entry);+ [1 I. h3 O% C l7 Z
- g_signal_connect(button6, "pressed", G_CALLBACK(calc), entry);6 h3 L1 T* Y: R. w
- g_signal_connect(button7, "pressed", G_CALLBACK(calc), entry);
$ J N ?2 y' J' u/ X6 K6 b0 C - g_signal_connect(button8, "pressed", G_CALLBACK(calc), entry);
( X& u2 m8 `# h& y- [' O - g_signal_connect(button9, "pressed", G_CALLBACK(calc), entry);, T F+ O* p( n; \0 l: O; G
- 8 p% N! k& w6 Z3 E T6 n6 H0 X7 l- S
- g_signal_connect(button_add, "pressed", G_CALLBACK(calc), entry);
U8 |' R- q, s4 F* D - g_signal_connect(button_mul, "pressed", G_CALLBACK(calc), entry);
( i- A: n- r' d - g_signal_connect(button_div, "pressed", G_CALLBACK(calc), entry);& A, s) U4 a. e
- g_signal_connect(button_minus, "pressed", G_CALLBACK(calc), entry);% Q4 ^% z/ W/ w& f' u% P
- g_signal_connect(button_equal, "pressed", G_CALLBACK(calc), entry);# d4 y' X; v# W# @
- g_signal_connect(button_delete, "pressed", G_CALLBACK(calc), entry);
0 n: ]% p- [. M) i4 S" o
: x1 s) t/ z# V0 }- " @5 \2 F, r) G; L+ f
- gtk_widget_show_all(window);7 m. J) g" f* J8 ]8 [) O$ H
' v9 z7 u" D; }2 J( z- //主事件循环1 B! C0 W* R6 R
- gtk_main();* `% W, A$ F8 f# m( |) U( F9 z
- $ v; Q9 ]+ G5 {
- return 0;- C- }8 ]# H8 Y" O. _4 G
- }/ @: c, e7 R5 a; h! j1 N3 x9 O
复制代码 以上程序,编译运行后,就可以有以下得效果
1 r/ K4 g+ n7 D3 v' U) S* E o
( w" x' |* c+ x& t) h& d& Z# N 操作和UI都是比较简单得,就不演示过程了,系统得触摸驱动也是做好的,gtk只管做应用就好了。操作也算流畅,毕竟也没涉及到复杂的图表渲染。 搞到这里,发现其实不复杂得GUI程序用这套现成的框架倒也是挺方便。Linux系统下其实资源很丰富,软件开发方式也很多选择,所以不多做逗留了。这款开发板还有一大特点就是可以按照以往STM32单片机得方式进行使用,所以后面感受下1GHz的单片机开发方式。 |