diff --git a/rt-thread-version/rt-thread-smart/_sidebar.md b/rt-thread-version/rt-thread-smart/_sidebar.md index 64bb27cb48d3698572fedd3349c7975a703332ad..b237ed69aad321f079e274379653d234426e971f 100644 --- a/rt-thread-version/rt-thread-smart/_sidebar.md +++ b/rt-thread-version/rt-thread-smart/_sidebar.md @@ -3,5 +3,15 @@ - RT-Thread Smart版本 - [入门指南](/rt-thread-version/rt-thread-smart/rt-smart-quickstart/rt-smart-quickstart.md) + - [架构说明](/rt-thread-version/rt-thread-smart/architecture/architecture.md) + - [进程概述](/rt-thread-version/rt-thread-smart/rt-smart-lwp/rt-smart-lwp.md) + + - 应用开发 + + - [开箱体验](/rt-thread-version/rt-thread-smart/application-note/quick_start/quick_start.md) + - [使用 VS Code 开发 GUI 应用](/rt-thread-version/rt-thread-smart/application-note/sdl2/sdl2.md) + - [基于 FFmpeg + SDL2 实现视频播放](/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/sdl2_ffmpeg.md) + + diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/1.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/1.png new file mode 100644 index 0000000000000000000000000000000000000000..7b6ed2c171bb707d78d34bb131ce541ab4b07d2c Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/1.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/10.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/10.png new file mode 100644 index 0000000000000000000000000000000000000000..4aa6156fed6f248988dc2565f24774eaa5064f46 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/10.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/11.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/11.png new file mode 100644 index 0000000000000000000000000000000000000000..e1159d2ceb174d8fe5ff420423036c14ab755775 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/11.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/124.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/124.png new file mode 100644 index 0000000000000000000000000000000000000000..36cf7577efceed5d995d45a550d80a296b14f453 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/124.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/132.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/132.png new file mode 100644 index 0000000000000000000000000000000000000000..c139114e3e810ccda21bfd36e15360999623ddd9 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/132.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/14.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/14.png new file mode 100644 index 0000000000000000000000000000000000000000..62fe91c762c2bc212cf8355c07d0285e472c43c6 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/14.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/15.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/15.png new file mode 100644 index 0000000000000000000000000000000000000000..59c7617856881bc6a48f38687a6960e277cb8ce5 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/15.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/16.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/16.png new file mode 100644 index 0000000000000000000000000000000000000000..c6556e01ff947563a8f0287e6a02f339d0f2efdf Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/16.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/17.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/17.png new file mode 100644 index 0000000000000000000000000000000000000000..5486480ab3593b657f9b3fa8fb537fe18b01f303 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/17.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/18.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/18.png new file mode 100644 index 0000000000000000000000000000000000000000..6fa6fefcdfb239e559413af5c736efa095e326f2 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/18.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/19.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/19.png new file mode 100644 index 0000000000000000000000000000000000000000..13c6df71076f1d72077d7e78f434656b7bc5a4ef Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/19.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/2.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/2.png new file mode 100644 index 0000000000000000000000000000000000000000..2303b9888af5c3c053624cb0195ab98576ea06e1 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/2.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/20.gif b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/20.gif new file mode 100644 index 0000000000000000000000000000000000000000..faaf1f9f71dc8037d3fdc327091220597b671038 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/20.gif differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/21.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/21.png new file mode 100644 index 0000000000000000000000000000000000000000..72b66d0bd7bc476ba531f1a95c814adee153a818 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/21.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/22.gif b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/22.gif new file mode 100644 index 0000000000000000000000000000000000000000..3f6580f1d46b757094257c1a6d12281216a7ed95 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/22.gif differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/3a.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/3a.png new file mode 100644 index 0000000000000000000000000000000000000000..fcc86e76532b2f06f5de757ab15007fe4b0705e2 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/3a.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/4.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/4.png new file mode 100644 index 0000000000000000000000000000000000000000..ba7f6f224f1e21da8cca119a1dc31a06ca3a351e Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/4.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/5.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/5.png new file mode 100644 index 0000000000000000000000000000000000000000..61e5db091954068422f5d14282e3a28720c280eb Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/5.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/6.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/6.png new file mode 100644 index 0000000000000000000000000000000000000000..70054f552ea47f1d59cf73d23aa5286eb498847a Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/6.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/7.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/7.png new file mode 100644 index 0000000000000000000000000000000000000000..16f93a1d84e7db24383ae3856369b96ebf8c4ccf Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/7.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/8.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/8.png new file mode 100644 index 0000000000000000000000000000000000000000..47a46437602a26a13c4154c25a170d7a41eae76c Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/8.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/9.png b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/9.png new file mode 100644 index 0000000000000000000000000000000000000000..a5c10b53f7e45388a18665de97502b4f52d9461a Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/quick_start/figures/9.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/quick_start/quick_start.md b/rt-thread-version/rt-thread-smart/application-note/quick_start/quick_start.md new file mode 100644 index 0000000000000000000000000000000000000000..8bbdbbafd854bb1c41dbc0afd3e7f54a39a5385b --- /dev/null +++ b/rt-thread-version/rt-thread-smart/application-note/quick_start/quick_start.md @@ -0,0 +1,369 @@ +# 上手体验以及 vscode 插件使用 + +## 开箱 + +包装内自带了两根 Type-C 线和一根网线。开发板整体接口还是很丰富的:SDIO、USB、ETH、LCD、Camera: + +![1](figures/1.png) + +![1](figures/2.png) + +## 上电 + +拿到新开发板的第一件事,当然是插电,开机,打印 `hello, world`! + +1. 插入 Type-C 电源和网线,打开终端,波特率设置为 115200 + + ![0.png](figures/3a.png) + +2. 上电调试信息: + + ```shell + U-Boot 2017.03-g002b758ac9-dirty (Nov 24 2021 - 20:08:02 +0800) + + CPU: Freescale i.MX6ULL rev1.1 696 MHz (running at 396 MHz) + CPU: Industrial temperature grade (-40C to 105C) at 38C + Reset cause: WDOG + Model: Freescale i.MX6 ULL 14x14 EVK Board + Board: MX6ULL 14x14 EVK + DRAM: 512 MiB + MMC: FSL_SDHC: 0, FSL_SDHC: 1 + *** Warning - bad CRC, using default environment + + Display: TFT7016 (1024x600) + Video: 1024x600x24 + In: serial + Out: serial + Err: serial + switch to partitions #0, OK + mmc1(part 0) is current device + Net: eth1: ethernet@020b4000, eth0: ethernet@02188000 [PRIME] + Normal Boot + Hit any key to stop autoboot: 0 + reading /kernel/rtthread.bin + 1149312 bytes read in 73 ms (15 MiB/s) + ## Starting application at 0x80001000 ... + + \ | / + - RT - Thread Smart Operating System + / | \ 5.0.0 build Dec 15 2021 + 2006 - 2020 Copyright by rt-thread team + lwIP-2.1.2 initialized! + [15] E/drv.enet: emac device init success + + [20] I/I2C: I2C bus [i2c3] registered + [24] I/I2C: I2C bus [i2c4] registered + [83] E/drv.enet: + PHY Link down, please check the cable connection and link partner setting. + + [94] I/SDIO: SD card capacity 31166976 KB. + [156] I/SDIO: emmc card capacity 3817472 KB. + found part[0], begin: 2098176, size: 500.0MB + found part[1], begin: 526386176, size: 1.0GB + found part[2], begin: 1600128000, size: 10.0MB + found part[3], begin: 1610613760, size: 200.0MB + [266] I/touch: rt_touch init success + [270] I/gt911: touch device gt911 init success + [275] I/sal.skt: Socket Abstraction Layer initialize success. + emmc file system initialization done! + Dir / sd0 mount failed! + [1751] E/[RW007]: The wifi Stage 1 status 0 0 0 1 + [1758] I/WLAN.dev: wlan init success + [1763] I/WLAN.lwip: eth device init ok name:w0 + [1768] I/WLAN.dev: wlan init success + [1773] I/WLAN.lwip: eth device init ok name:w1 + + rw007 sn: [rw00700000000fc584a2c5001] + rw007 ver: [RW007_2.1.0-a7a0d089-57] + + [5247] D/drv.enet: enet1 link up + + [5789] W/WLAN.mgnt: F:rt_wlan_connect L:1091 not find ap! ssid:rt-thread + hello rt-smart! + /sd/wifi.sh wi-fi configuration file not exist in sd card! + [5807] I/mcf.link.tcp: TCPServer Waiting for client on port 5555... + + [5814] I/mcf.link: MCF link port(3) register successfully. + msh />[5850] I/mcf.trans: MCF(V0.1.0) initialized successfully. + [5855] I/urpc: uRPC(V0.1.0) initialize successfully. + [5861] I/mcf.trans: MCF protocol started successfully. + + +3. 在终端输入命令 `help` 查看帮助信息 + + ```shell + msh />help + RT-Thread shell commands: + reboot - reboot system + cpu_usage - display cpu usage + dbg - dbg + memcheck - check memory data + memtrace - dump memory trace information + gic_dump - show gic status + list_page - show page info + list_fd - list file descriptor + lsof - list open files + mount - mount + unmount - unmount the mountpoint + hello - say hello world + clear - clear the terminal screen + version - show RT-Thread version information + list_thread - list thread + list_sem - list semaphore in system + list_event - list event in system + list_mutex - list mutex in system + list_mailbox - list mail box in system + list_msgqueue - list message queue in system + list_memheap - list memory heap in system + list_mempool - list memory pool in system + list_timer - list timer in system + list_device - list device in system + list - list all symbol in system + help - RT-Thread shell help. + ps - List threads in the system. + free - Show the memory usage in the system. + ls - List information about the FILEs. + cp - Copy SOURCE to DEST. + mv - Rename SOURCE to DEST. + cat - Concatenate FILE(s) + rm - Remove(unlink) the FILE(s). + cd - Change the shell working directory. + pwd - Print the name of the current working directory. + mkdir - Create the DIRECTORY. + mkfs - format disk with file system + df - disk free + echo - echo string to file + set - set or show environment variable + list_channel - list IPC channel information + list_process - list process + kill - send a signal to a process + killall - kill processes by name + list_shm - show share memory info + sys_log - sys_log 1(enable)/0(disable) + ifconfig - list the information of all network interfaces + ping - ping network host + dns - list and set the information of dns + netstat - list the information of TCP / IP + pwm_enable - pwm_enable pwm1 1 + pwm_disable - pwm_disable pwm1 1 + pwm_set - pwm_set pwm1 1 100 50 + date - get date and time or set [year month day hour min sec] + wifi - wifi command. + + ``` + +4. 执行用户 app。用户 app 存放在目录 **/root/bin** 目录下 + + ```shell + msh /root/bin>pong.elf & + msh /root/bin> + Pong: wait on the IPC channel: 3 + + msh /root/bin>ping + msh /root/bin>ping.elf + msh /root/bin> + Ping: send count = 0 + Pong: receive count = 0 + Pong: reply count = 0 + Ping: receive the reply 0 + + Ping: send count = 1 + Pong: receive count = 1 + Pong: reply count = 1 + Ping: receive the reply 1 + + Ping: send count = 2 + Pong: receive count = 2 + Pong: reply count = 2 + Ping: receive the reply 2 + + Ping: send count = 3 + Pong: receive count = 3 + Pong: reply count = 3 + Ping: receive the reply 3 + + Ping: send count = 4 + Pong: receive count = 4 + Pong: reply count = 4 + Ping: receive the reply 4 + + Ping: send count = 5 + Pong: receive count = 5 + Pong: reply count = 5 + Ping: receive the reply 5 + + Ping: send count = 6 + Pong: receive count = 6 + Pong: reply count = 6 + Ping: receive the reply 6 + + Ping: send count = 7 + Pong: receive count = 7 + Pong: reply count = 7 + Ping: receive the reply 7 + ``` + +## 下载源码 + +ART-Pi Smart 的最新源码仓库地址:[ART-Pi-smart: ART-Pi smart开发板的SDK仓库 (gitee.com)](https://gitee.com/rtthread/ART-Pi-smart) + +使用 git 工具下载源码:`git clone https://gitee.com/rtthread/ART-Pi-smart.git ` + +## vscode 插件体验 + +RT-Thread 官方提供了 vscode 插件用来开发用户 app 代码。下面分享一下 ART-Pi Smart Vscode 插件的使用体验。 + +### 安装插件 + +打开 vscode,在扩展中搜索 RT-Thread Smart + +![00.png](figures/4.png) + +### 创建工程 + +- 使用快捷键 `ctrl + shift + p`, 选择创建 RT-Thread Smart 工程 + + ![01.png](figures/5.png) + +- 选择 RT-Thread Smart SDK 的根目录,回车 + + ![02.png](figures/6.png) + + gitee 下载下来的源码目录结构如下 + + ![03.png](figures/7.png) + +- 新建一个 `hello`工程,回车 + + ![04.png](figures/8.png) + +- 选择编译工具,目前 windows 下只有 scons。如果开发环境中没有安装 scons,需要使用命令 `pip install scons ` 安装 scons 工具。 + + ![05.png](figures/9.png) + +- 通过上面的步骤,一个用户 app 示例就创建完成了 + + ![06.png](figures/10.png) + +### 下载用户态代码 + +vscode 支持下载用户代码到 Smart 开发板。 + +- 在终端输入命令 `ifconfig` 获取开发板 IP 地址。这里的 MAC 在代码中是写死的,不知道为什么要使用这种方法。如果你的局域网中插入两块 Smart 开发板,可能会有问题,因为这两块开发板的 IP 地址是一样的。 + + ```shell + msh />ifconfig + network interface device: e1 (Default) + MTU: 1500 + MAC: a8 5e 45 91 92 93 + FLAGS: UP LINK_UP INTERNET_UP DHCP_ENABLE ETHARP BROADCAST IGMP + ip address: 192.168.110.34 + gw address: 192.168.110.1 + net mask : 255.255.255.0 + ipv6 link-local: FE80::AA5E:45FF:FE91:9293 VALID + ipv6[1] address: 0.0.0.0 INVALID + ipv6[2] address: 0.0.0.0 INVALID + dns server #0: 211.136.150.66 + dns server #1: 211.136.112.50 + + network interface device: w0 + MTU: 1500 + MAC: fc 58 4a 2c 50 01 + FLAGS: UP LINK_DOWN INTERNET_DOWN DHCP_ENABLE ETHARP BROADCAST IGMP + ip address: 0.0.0.0 + gw address: 0.0.0.0 + net mask : 0.0.0.0 + ipv6 link-local: FE80::FE58:4AFF:FE2C:5001 VALID + ipv6[1] address: 0.0.0.0 INVALID + ipv6[2] address: 0.0.0.0 INVALID + dns server #0: 211.136.150.66 + dns server #1: 211.136.112.50 + + network interface device: w1 + MTU: 1500 + MAC: fc 58 4a 2c 50 00 + FLAGS: UP LINK_DOWN INTERNET_DOWN DHCP_ENABLE ETHARP BROADCAST IGMP + ip address: 0.0.0.0 + gw address: 0.0.0.0 + net mask : 0.0.0.0 + ipv6 link-local: FE80::FE58:4AFF:FE2C:5000 VALID + ipv6[1] address: 0.0.0.0 INVALID + ipv6[2] address: 0.0.0.0 INVALID + dns server #0: 211.136.150.66 + dns server #1: 211.136.112.50 + ``` + +- 编译代码 + + ![08.png](figures/11.png) + +- 设置 IP 地址,使用快捷键 `ctrl + shift + p` 选择打开 RT-Thread Smart 设置 + + ![09.png](figures/124.png) + + 填入开发板 IP 地址,保存设置并退出 + + ![010.png](figures/132.png) + +- 下载代码 + + ![011.png](figures/14.png) + + 当终端显示以下界面时,表示代码下载成功 + + ![012.png](figures/15.png) + +- 运行可执行文件 + + ![013.png](figures/16.png) + +### 调试用户态代码 + +- 使用快捷键 `F5` 进入调试模式,可以在代码处设置断点 + + ![014.png](figures/17.png) + +- 可以使用快捷键 `F11` 进行单步调试 + +### UDB 工具 + +在下载代码和调试代码时,在终端发现 vscode smart 插件使用的是一个叫 **udb** 的工具。udb 工具位于 **ART-Pi-smart\tools\udb-tools**。RT-Thread 提供了windows 和 linux 两个版本的 udb 工具。 + +![015.png](figures/18.png) + +windows 下目录结构: + +- server.log :udb 的运行日志 +- udb.exe :可执行文件 +- udb.ini :tcp 配置文件,保存了 smart 开发板的 ip 地址 + +#### udb 命令 + +在该目录下打开 windows 终端,输入命令 `udb.exe --help ` 查看 udb 命令。 + +![016.png](figures/19.png) + +#### udb devices + +查看当前 udb 链路设备 + +```bash +PS D:\repo\gitee\ART-Pi-smart\tools\udb-tools\windows> .\udb.exe devices +List of devices attached +serial mtu version state +192.168.110.34:5555 1024 2.0.0 active +``` + +#### udb push + +推送本地文件到远端设备 + +![7.gif](figures/20.gif) + +![018.png](figures/21.png) + +#### udb pull + +拉取远端文件到本地 + +![8.gif](figures/22.gif) \ No newline at end of file diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/1.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/1.png new file mode 100644 index 0000000000000000000000000000000000000000..94981701a46fb50b29a5902394878aaa1f2aa3c9 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/1.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/10.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/10.png new file mode 100644 index 0000000000000000000000000000000000000000..650af0bbc4e036eb0662ca526fd0d5aa1e3be02d Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/10.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/12.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/12.png new file mode 100644 index 0000000000000000000000000000000000000000..bcede9dcbc25639be3ff0028a2d03a54dbb62063 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/12.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/13.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/13.png new file mode 100644 index 0000000000000000000000000000000000000000..32280f35f063475e4384419e13c4f7745177974f Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/13.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/14.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/14.png new file mode 100644 index 0000000000000000000000000000000000000000..04ec0d2af956789eeb07a06aa141ec1a3adf3a11 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/14.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/15.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/15.png new file mode 100644 index 0000000000000000000000000000000000000000..fe17ebe8cea0166ffaf547ccc125d3a9d34d726c Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/15.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/16.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/16.png new file mode 100644 index 0000000000000000000000000000000000000000..b6dd419f5ffd167a63a745aa9599305dc1211e7c Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/16.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/17.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/17.png new file mode 100644 index 0000000000000000000000000000000000000000..2c14a35c1fecc679e160d108333a60e9b60cecbe Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/17.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/18.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/18.png new file mode 100644 index 0000000000000000000000000000000000000000..d7decb5f81425ceafb77f6a0c9f1896a5e7c0b17 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/18.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/19.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/19.png new file mode 100644 index 0000000000000000000000000000000000000000..54fea38a2ebe27ef11345f643c4568b7703fabce Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/19.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/2.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/2.png new file mode 100644 index 0000000000000000000000000000000000000000..0ffe4a1606f7117252ef18a38b6614db41520e2f Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/2.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/20.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/20.png new file mode 100644 index 0000000000000000000000000000000000000000..457b56cc2d9e2303304f21330fe80ad0f5e6cf34 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/20.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/21.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/21.png new file mode 100644 index 0000000000000000000000000000000000000000..fe5980c4889c594e6af74b5e31a1eab403a1a0d0 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/21.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/22.jpg b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/22.jpg new file mode 100644 index 0000000000000000000000000000000000000000..3d628058b8bc8f5ce5ca411d38096e407ac4a72a Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/22.jpg differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/23.jpg b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/23.jpg new file mode 100644 index 0000000000000000000000000000000000000000..ab31d6554d83e8ca12df2e52a6b28b4a4b501515 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/23.jpg differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/3.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/3.png new file mode 100644 index 0000000000000000000000000000000000000000..b15cdddecb87ef78c2de3db2038afa1b4f1f3c18 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/3.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/4.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/4.png new file mode 100644 index 0000000000000000000000000000000000000000..d5429ddb6034914acc29b14390a87ba418f61730 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/4.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/5.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/5.png new file mode 100644 index 0000000000000000000000000000000000000000..149770e75f1cb6c4bce1b129c689fc61c16f8acd Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/5.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/6.jpg b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/6.jpg new file mode 100644 index 0000000000000000000000000000000000000000..e707cd3c4292ed05899ab62d3cd4654684542e90 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/6.jpg differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/7.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/7.png new file mode 100644 index 0000000000000000000000000000000000000000..8081ccbd38e9deb1acbe6c86ba199dc76ed93e15 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/7.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/8.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/8.png new file mode 100644 index 0000000000000000000000000000000000000000..205e74821da85b1ab41c47804c8b89bae9ef1ced Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/8.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/9.png b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/9.png new file mode 100644 index 0000000000000000000000000000000000000000..3dea882696dbfa2a69dd08e7e97ff6b7c0a57756 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2/figures/9.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2/sdl2.md b/rt-thread-version/rt-thread-smart/application-note/sdl2/sdl2.md new file mode 100644 index 0000000000000000000000000000000000000000..437f026e0a2523093866ca966fd5453dfbabe3b1 --- /dev/null +++ b/rt-thread-version/rt-thread-smart/application-note/sdl2/sdl2.md @@ -0,0 +1,385 @@ +# 使用 VS Code 开发 GUI 应用 + +## RT-Thread Smart 简介 + +RT-Thread Smart(简称 rt-smart)是基于 RT-Thread 操作系统衍生的新分支,面向带 MMU,中高端应用的芯片,例如 ARM Cortex-A 系列芯片,MIPS 芯片,带 MMU 的 RISC-V 芯片等。rt-smart 在 RT-Thread 操作系统的基础上启用独立、完整的进程方式,同时以混合微内核模式执行。 + +## SDL 简介 + +SDL(Simple DirectMedia Layer)是一个跨平台开发库库,使用 C 语言写成。旨在通过 OpenGL 和 Direct3D 提供对音频、键鼠、鼠标、操作杆和图形硬件的底层访问。视频回放软件、模拟器和流行游戏(包括 Value 的获奖目录和许多 Humble Bundle 游戏)都使用了它。 + +![freeciv.png](figures/1.png) + +SDL 支持 Windows、Mac OS X、Linux、iOS 和 Android。 + +SDL 2.0 是在 zlib 许可下发布的,这个许可允许用户可以在任何软件中自由的使用 SDL。 + +## RT-Thread Smart for VS Code 插件简介 + +为了使用户能够在各种平台上方便、快捷的开发 rt-smart 用户程序,RT-Thread 推出了这样一款便利且小巧的插件。 + +支持的功能包括: + +- 创建 RT-Thread Smart 工程 + +- 支持 CMake、Make、SCons 等构建方式 + +- 支持清理工程 + +- 支持一键下载、调试 + +- 支持在不同的平台,例如 Linux、Windows 下进行开发 + +支持的命令包括: + +- RT-Thread Smart:Create Project: 创建 RT-Thread Smart 工程 + +- RT-Thread Smart:Settings:打开 RT-Thread Smart 工程设置 + +## 搭建 ART-Pi Smart 环境 + +### 硬件环境搭建 + +- 串口连接:下方的 USB Type-C 接口,既是用作电源供电,同时也是 USB 转 UART 接口,主要用于系统的控制台命令行交互,包括控制台输入和输出 + + | **波特率** | **数据位** | **停止位** | + | ---------- | ---------- | ---------- | + | 115200 | 8 | 1 | + +- 网络接口:通过路由器和网线(或者通过板载的 RW007 WiFi 模块),将开发板和测试电脑连接在同一个局域网内 + + ![image.png](figures/2.png) + +- 显示接口:开发板未上电之前,先将 4.3 寸 LCD 显示器的 40 Pin FPC 排线连接到 ART-Pi Smart 开发板背面的 LCD 硬件插槽 + + ![image1.png](figures/3.png) + +### 软件环境搭建 + +#### 下载 ART-Pi Smart SDK + +[ART-Pi-Smart SDK 仓库](https://gitee.com/rtthread/ART-Pi-smart) 是 RT-Thread 团队对 ART-Pi Smart 开发板所作的支持包,用于学习和评估 RT-Thread Smart 微内核操作系统,让用户可以更简单方便地开发自己的应用程序。 + +ART-Pi smart 开发板的 SDK 仓库,主要包括 RT-Thread Smart 的源码,BSP 驱动,应用程序 demo 等。 + +SDK 下载方法:通过 Git 下载 ART-Pi Smart SDK 包: + +```shell +git clone https://gitee.com/rtthread/ART-Pi-smart.git +``` + +#### 下载工具链 + +- rt-smart 采用的工具链为:arm-linux-musleabi 工具链 + +- 需要用户自行通过下面网址下载 Linux 版本或 Windows 版本的工具链 + +- 链接: https://pan.baidu.com/s/1p7PRhV3dTGIb7hxv34YWYw 提取码: ndxq + +- 将下载的工具链,解压到指定的路径 tools/gnu_gcc 下面 + + ![4.png](figures/4.png) + +#### Linux 开发环境准备 + +输入下面的命令,配置 Linux 开发环境: + +```shell +$ sudo apt update +$ sudo apt install git bzip2 wget patch +$ sudo apt install gcc +$ sudo apt install g++ +$ sudo apt install libncurses-dev +$ sudo apt install libpng-dev +$ sudo apt install libncurses5 +$ sudo apt install automake +$ sudo apt install make +``` + +#### 编译 SDL + +在创建 SDL 应用程序前,需要将 SDL 编译为静态库文件。 + +- 在 SDK 目录下打开终端,配置环境变量: + + ```shell + liukang@liukang-virtual-machine:~/repo/ART-Pi-smart$ ls + documents kernel LICENSE README.md smart-env.bat smart-env.sh tools userapps + liukang@liukang-virtual-machine:~/repo/ART-Pi-smart$ ./smart-env.sh + ``` + +- 在 userapps/gnu-apps/sdl 目录下,执行 build_sdl.sh 文件: + + ```shell + liukang@liukang-virtual-machine:~/repo/ART-Pi-smart$ cd userapps/gnu-apps/sdl/ + liukang@liukang-virtual-machine:~/repo/ART-Pi-smart/userapps/gnu-apps/sdl$ ./build_sdl.sh + ``` + +- 上一步执行成功后,会在 userapps/sdk/lib 目录下生成 libSDL2.a 库文件;将该库文件复制到 userapps/sdk/rt-thread/lib 目录下: + + ```shell + liukang@liukang-virtual-machine:~/repo/ART-Pi-smart/userapps/gnu-apps/sdl$ cp ../../sdk/lib/libSDL2.a ../../sdk/rt-thread/lib/ + ``` + +- 完整流程如下图所示: + + ![5.png](figures/5.png) + +#### 下载 VS Code 插件 + +在使用RT-Thread Smart插件时,首先是需要在VS Code中安装它。目前VS Code RT-Thread Smart插件已经上传到VS Code市场,所以安装非常方便,可以直接在VS Code 的扩展市场中**搜索** **RT-Thread Smart**,点击安装即可: + +![ff9749d43bfe7a789d170a4524a5010f.jpg](figures/6.jpg) + +### 创建 SDL Demo + +这里将以 Linux 环境下使用 VS Code 开发 rt-smart SDL 应用程序为例来体验 VS Code 的 RT-Thread Smart 插件扩展。 + +- 在 VS Code 上使用 **Ctrlt+Shift+P** 快捷键,输入 RT-Thread 关键字,选择 “创建 RT-Thread Smart 工程”: + + ![6.png](figures/7.png) + +- 输入 ART-Pi Smart的 SDK 根目录: + + ![7.png](figures/8.png) + +- 输入工程名称: + + ![8.png](figures/9.png) + +- 选择构建方式,这里以 Make 为例: + + ![9.png](figures/10.png) + +- 修改 Makefile 文件;需要在 Makefile 文件中,链接 SDL 静态库: + + ```makefile + # rt-thread 路径 + RT_DIR = $(UROOT_DIR)/sdk/rt-thread + INC_DIR =$(UROOT_DIR)/sdk/rt-thread/include + LIB_DIR = ${UROOT_DIR}/sdk/rt-thread/lib + # sdl 路径 + SDL_DIR = ${UROOT_DIR}/sdk/include/sdl + + # 配置编译参数 + CFLAGS = -march=armv7-a -marm -msoft-float -D__RTTHREAD__ -Wall -O0 -g -gdwarf-2 -n --static + + # 加入头文件搜索路径 + CFLAGS += -I. -I$(UROOT_DIR) -I$(PROJECT_DIR) -I$(RT_DIR)/components/dfs -I$(RT_DIR)/components/drivers -I$(RT_DIR)/components/finsh -I$(RT_DIR)/components/net -I$(RT_DIR)/components/net/netdev -I$(RT_DIR)/components/net/arpa -I${INC_DIR} -I${INC_DIR}/libc -I${INC_DIR}/sys -I${SDL_DIR} + + # 加入链接文件 + LDFLAGS = -march=armv7-a -marm -msoft-float -T ${UROOT_DIR}/linker_scripts/arm/cortex-a/link.lds + + # 加入库文件 + LDFLAGS += -L$(LIB_DIR) -Wl,--whole-archive -lrtthread -Wl,--no-whole-archive -n --static -Wl,--start-group -lc -lgcc -lrtthread -lSDL2 -Wl,--end-group + ``` + +- 查询开发板的 IP 地址: + + ![12.png](figures/12.png) + +- 设置开发板的 IP 地址;在 VS Code 插件中,使用快捷键 **Ctrlt+Shift+P**,选择 “打开 RT-Thread Smart 设置”,输入开发板的 IP 地址: + + ![13.png](figures/13.png) + +#### 画线 + +- 在新建工程 main.c 文件中,输入以下代码: + + ```c + #include + #include + + #define POINTS_COUNT 4 + + extern Uint32 rtt_screen_width; + extern Uint32 rtt_screen_heigth; + + static SDL_Point points[POINTS_COUNT] = { + {320, 200}, + {300, 240}, + {340, 240}, + {320, 200} + }; + + SDL_Rect rect = {0, 0, 320, 240}; + + int main(void) + { + //The window we'll be rendering to + SDL_Window* window = NULL; + + //The surface contained by the window + SDL_Renderer *renderer; + + //use dummy video driver + SDL_setenv("SDL_VIDEODRIVER","rtt",1); + //Initialize SDL + if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) + { + printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() ); + return -1; + } + + window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, rtt_screen_width, rtt_screen_heigth, SDL_WINDOW_SHOWN ); + if( window == NULL ) + { + printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); + return -1; + } + + renderer = SDL_CreateRenderer(window, -1, 0); + + SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); + SDL_RenderDrawLine(renderer, 0, 0,320, 200); + + //SDL_RenderDrawLine(renderer, 320, 0, 320, 480); + SDL_RenderDrawLines(renderer, points, POINTS_COUNT); + + SDL_RenderPresent(renderer); + SDL_Delay( 2000 ); + + //destory renderer + if (renderer) { + SDL_DestroyRenderer(renderer); + } + //Destroy window + SDL_DestroyWindow( window ); + + //Quit SDL subsystems + SDL_Quit(); + + return 0; + } + ``` + +- 编译工程: + + ![10.png](figures/14.png) + +- 下载代码 + + ![11.png](figures/15.png) + +- 运行代码,在调试终端输入命令:`./hello.elf &` 执行用户 APP: + + ![14.png](figures/16.png) + +- LCD 显示效果如下: + + ![9gz7UH0wNyyF3g3MjFiVMg.png](figures/17.png) + +- 代码调试;在 VS Code 插件中,使用快捷键 F5 进入 Debug 模式: + + ![15.png](figures/18.png) + +#### 画方框 + + ![ohWbRoeiyOmCoV5ymOF3qg.png](figures/19.png) + +#### 背景填充 + + ![xjNK2dFoGWidXzeSysb1FQ.png](figures/20.png) + +#### 图片显示 + +- 先将图片上传到 ART-Pi Smart 文件目录下;在 VS Code 终端中手动输入命令 `/home/liukang/repo/ART-Pi-smart/tools/udb-tools/linux/udb push /home/liukang/repo/ART-Pi-smart/userapps/gnu-apps/sdl/test.bmp /root/bin/test.bmp` 上传图片到 ART-Pi Smart 开发板: + + ![16.png](figures/21.png) + +- 图片显示代码: + + ```c + #include + #include + + #define POINTS_COUNT 4 + + extern Uint32 rtt_screen_width; + extern Uint32 rtt_screen_heigth; + + SDL_Rect rect = {0, 0, 320, 240}; + + int main(void) + { + //The window we'll be rendering to + SDL_Window* window = NULL; + + //The surface contained by the window + SDL_Surface* screenSurface = NULL; + SDL_Renderer *renderer = NULL; + SDL_Texture *texture = NULL; + + //use dummy video driver + SDL_setenv("SDL_VIDEODRIVER","rtt",1); + //Initialize SDL + if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) + { + printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() ); + return -1; + } + + window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, rtt_screen_width, rtt_screen_heigth, SDL_WINDOW_SHOWN ); + if( window == NULL ) + { + printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); + return -1; + } + renderer = SDL_CreateRenderer(window, -1, 0); + + screenSurface = SDL_LoadBMP("test.bmp"); + if(!screenSurface) + { + printf("SDL_LoadBMP failed\n"); + goto end; + } + texture = SDL_CreateTextureFromSurface(renderer, screenSurface); + SDL_SetRenderTarget(renderer,texture); + SDL_RenderCopy(renderer,texture,NULL,NULL); + + SDL_RenderPresent(renderer); + SDL_Delay( 2000 ); + end: + //destory renderer + if (renderer) { + SDL_DestroyRenderer(renderer); + } + //Destroy window + SDL_DestroyWindow( window ); + + //Quit SDL subsystems + SDL_Quit(); + + return 0; + } + ``` + +- 编译、下载、执行 APP,LCD 显示如下: + + ![微信图片_20220106173533.jpg](figures/22.jpg) + +以上完整 Demo 代码存放在 **ART-Pi-smart/userapps/gnu-apps/sdl/SDL2-2.0.14/test/testrtt.c** 文件中。 + +## 结尾 + +本文档只是非常简单的在 VS Code 插件上演示了 SDL 的部分基础功能,感兴趣的小伙伴们可以在 RT-Thread Smart 上开发一些更好玩的 Demo:触摸、游戏、音乐播放、视频播放、智能家居等。最后,如果我的文章对你有所帮助,请收藏、点赞、关注! + +![123.jpg](figures/23.jpg) + +## RT-Smart 交流平台 + +如果遇到什么问题需要协助,或有什么建议和意见需要反馈的,可以加入 ART-Pi Smart 官方交流 QQ 群或者登录论坛的 rt-smart 板块发帖反馈。 + +- ART-Pi Smart : + + https://art-pi.gitee.io/smart-website/ + +- ART-Pi Smart SDK : + + https://gitee.com/rtthread/ART-Pi-smart + +- **ART-Pi Smart 官方交流 QQ 群** :622828244 + +- rt-smart 论坛板块(发帖反馈): + + https://club.rt-thread.org/ask/tag/1339.html diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/1.png b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/1.png new file mode 100644 index 0000000000000000000000000000000000000000..a20dbbdb7597563924f55eb5806ee9d1288abc6a Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/1.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/10.png b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/10.png new file mode 100644 index 0000000000000000000000000000000000000000..6e2018f285f54a346f5d342d8f2f3eb7a2e042d3 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/10.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/11.png b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/11.png new file mode 100644 index 0000000000000000000000000000000000000000..88bc92326a489284cb4d05fb7aa796563d493bde Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/11.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/2.png b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/2.png new file mode 100644 index 0000000000000000000000000000000000000000..30645e097179606077ba56f9b7d59d35a7481e9b Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/2.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/3.png b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/3.png new file mode 100644 index 0000000000000000000000000000000000000000..42d5adc5a80caab618105fc8efdd5ef43cb8d362 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/3.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/4.png b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/4.png new file mode 100644 index 0000000000000000000000000000000000000000..e897cef20b902c8d185b2c204028c340f5e3cb57 Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/4.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/5.png b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/5.png new file mode 100644 index 0000000000000000000000000000000000000000..9441feaa2434e104535c6d0fcb3f283717b9c0ec Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/5.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/6.png b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/6.png new file mode 100644 index 0000000000000000000000000000000000000000..470685bc7d5bb3ccad8c676dfcfbb0ed8acd733d Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/6.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/7.png b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/7.png new file mode 100644 index 0000000000000000000000000000000000000000..c741a1e431f24f43ad21ed7ec87fe831c09af16a Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/7.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/8.png b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/8.png new file mode 100644 index 0000000000000000000000000000000000000000..31d71ca57b36174eca4195fdf1aea21d05d0f8bc Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/8.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/9.png b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/9.png new file mode 100644 index 0000000000000000000000000000000000000000..feec6e80c27b1767292b5b61f1b1666efecc03db Binary files /dev/null and b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/figures/9.png differ diff --git a/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/sdl2_ffmpeg.md b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/sdl2_ffmpeg.md new file mode 100644 index 0000000000000000000000000000000000000000..047b6fe7ad21e22988b946e266172381143b8ac4 --- /dev/null +++ b/rt-thread-version/rt-thread-smart/application-note/sdl2_ffmpeg/sdl2_ffmpeg.md @@ -0,0 +1,464 @@ +# 基于 FFmpeg + SDL2 实现视频播放 + +基于文档《使用 VS Code 开发 GUI 应用》,使用 FFmpeg+SDL2 在 ART-Pi Smart 平台上实现视频播放功能;由于 ART-Pi Smart 没有音频模块,所以没有实现音频的解码播放。 + +## X264 + +### 简介 + +**X264** 是由 VideoLAN 开发的一个免费开源软件库和命令行实用程序,用于将视频流编码为 H.264 / MPEG-4 AVC 格式,根据GNU通用公共许可证的条款发布的。 + +FFmpeg 是一个编解码库,功能丰富,其自带 H.264 解码功能,但是要实现 H.264 编码需要集成 X264 将其作为编码器。 + +### 下载 + +```shell +git clone https://code.videolan.org/videolan/x264.git +``` + +源码目录: + +![Snipaste_2022-01-17_10-16-33.png](figures/1.png) + +### 交叉编译 + +1. 在 x264 文件夹同级目录下创建 **build_x264.sh** 文件 + +2. build_x264.sh 文件内容如下,注意:RTT_EXEC_PATH 和 ROOTDIR 修改为自己本地路径: + + ```sh + # Get initial variables + export RTT_EXEC_PATH=/home/liukang/repo/ART-Pi-smart/tools/gnu_gcc/arm-linux-musleabi_for_x86_64-pc-linux-gnu/bin + export PATH=$PATH:$RTT_EXEC_PATH:$RTT_EXEC_PATH/../arm-linux-musleabi/bin + + export CROSS_COMPILE="arm-linux-musleabi" + + if [ "$1" == "debug" ]; then + export CFLAGS="-march=armv7-a -marm -msoft-float -D__RTTHREAD__ -O0 -g -gdwarf-2 -Wall -n --static" + else + export CFLAGS="-march=armv7-a -marm -msoft-float -D__RTTHREAD__ -O2 -Wall -n --static" + fi + + export AR=${CROSS_COMPILE}-ar + export AS=${CROSS_COMPILE}-as + export LD=${CROSS_COMPILE}-ld + export RANLIB=${CROSS_COMPILE}-ranlib + export CC=${CROSS_COMPILE}-gcc + export CXX=${CROSS_COMPILE}-g++ + export NM=${CROSS_COMPILE}-nm + + ROOTDIR="/home/liukang/repo/ART-Pi-smart/userapps" + + APP_NAME="x264" + + APP_DIR=${APP_NAME} + LIB_DIR=${ROOTDIR}/sdk/lib + INC_DIR=${ROOTDIR}/sdk/include + + RT_DIR=${ROOTDIR}/sdk/rt-thread + RT_INC=" -I. -Iinclude -I${ROOTDIR} -I${RT_DIR}/include -I${RT_DIR}/components/dfs -I${RT_DIR}/components/drivers -I${RT_DIR}/components/finsh -I${RT_DIR}/components/net -I${INC_DIR}/sdl -DHAVE_CCONFIG_H" + RT_INC+=" -I${ROOTDIR}/../kernel/bsp/imx6ull-artpi-smart/drivers/" + + export CPPFLAGS=${RT_INC} + export LDFLAGS="-L${LIB_DIR} " + + export LIBS="-T ${ROOTDIR}/linker_scripts/arm/cortex-a/link.lds -march=armv7-a -marm -msoft-float -L${RT_DIR}/lib -Wl,--whole-archive -lrtthread -Wl,--no-whole-archive -n -static -Wl,--start-group -lc -lgcc -lrtthread -Wl,--end-group" + + # default build + function builddef() { + cd ${APP_DIR} + ./configure \ + --prefix=/home/liukang/repo/x264lib \ + --host=${CROSS_COMPILE} \ + --disable-asm \ + --enable-static + make clean + if [ "$1" == "verbose" ]; then + make V=1 + else + make + fi + make install + } + + builddef $1 + + ``` + +3. 运行 **build_x264.sh** 文件,生成静态库: + + ![Snipaste_2022-01-17_11-37-07.png](figures/2.png) + +4. 上面步骤成功后,在 x264lib 文件夹下,会生成 x264 的静态库文件和头文件: + + 静态库文件: + + ![Snipaste_2022-01-17_11-37-42.png](figures/3.png) + + 头文件: + + ![Snipaste_2022-01-17_11-38-01.png](figures/4.png) + +## FFmpeg + +### 简介 + +FFmpeg 是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用 LGP L或 GPL 许可证。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库 libavcodec,为了保证高可移植性和编解码质量,libavcodec 里很多 code 都是从头开发的。 + +FFmpeg 在 Linux平台下开发,但它同样也可以在其它操作系统环境中编译运行,包括 Windows、Mac OS X 等。这个项目最早由 Fabrice Bellard 发起,2004 年至 2015 年间由 Michael Niedermayer 主要负责维护。下面介绍如何将 FFmpeg 移植到 ART-Pi Smart 平台上,实现视频的解码功能。 + +### 下载 + +打开 FFmpeg 官网,下载源码: + +![Snipaste_2022-01-17_10-14-19.png](figures/5.png) + +### 交叉编译 + +1. 解压 tar.bz2 文件: + + ``` + tar -jxvf ffmpeg-snapshot.tar.bz2 + ``` + +2. 在 ffmpeg 文件夹同级目录下创建 **build_ffmpeg.sh** 文件 + + ```sh + # Get initial variables + ROOTDIR="/home/liukang/repo/ART-Pi-smart/userapps" + + APP_NAME="ffmpeg" + + APP_DIR=${APP_NAME} + LIB_DIR=${ROOTDIR}/sdk/lib + INC_DIR=${ROOTDIR}/sdk/include + + RT_DIR=${ROOTDIR}/sdk/rt-thread + RT_INC=" -I. -Iinclude -I${ROOTDIR} -I${RT_DIR}/include -I${RT_DIR}/components/dfs -I${RT_DIR}/components/drivers -I${RT_DIR}/components/finsh -I${RT_DIR}/components/net -I${INC_DIR}/sdl -DHAVE_CCONFIG_H" + RT_INC+=" -I${ROOTDIR}/../kernel/bsp/imx6ull-artpi-smart/drivers/" + + export CPPFLAGS=${RT_INC} + export LDFLAGS="-L${LIB_DIR} " + + export LIBS="-T ${ROOTDIR}/linker_scripts/arm/cortex-a/link.lds -march=armv7-a -marm -msoft-float -L${RT_DIR}/lib -Wl,--whole-archive -lrtthread -Wl,--no-whole-archive -n -static -Wl,--start-group -lc -lgcc -lrtthread -Wl,--end-group" + + export RTT_EXEC_PATH=/home/liukang/repo/ART-Pi-smart/tools/gnu_gcc/arm-linux-musleabi_for_x86_64-pc-linux-gnu/bin + export PATH=$PATH:$RTT_EXEC_PATH:$RTT_EXEC_PATH/../arm-linux-musleabi/bin + + export CROSS_COMPILE="arm-linux-musleabi" + + if [ "$1" == "debug" ]; then + export CFLAGS="-march=armv7-a -marm -msoft-float -D__RTTHREAD__ -O0 -g -gdwarf-2 -Wall -n --static" + else + export CFLAGS="-march=armv7-a -marm -msoft-float -D__RTTHREAD__ -O2 -Wall -n --static" + fi + + # default build + function builddef() { + cd ${APP_DIR} + ./configure \ + --cross-prefix=${CROSS_COMPILE} --enable-cross-compile --target-os=linux \ + --cc=${CROSS_COMPILE}-gcc \ + --ar=${CROSS_COMPILE}-ar \ + --ranlib=${CROSS_COMPILE}-ranlib \ + --arch=arm --prefix=/home/liukang/repo/ffmpeg/ffmpeg_lib \ + --pkg-config-flags="--static" \ + --enable-gpl --enable-nonfree --disable-ffplay --enable-swscale --enable-pthreads --disable-armv5te --disable-armv6 --disable-armv6t2 --disable-x86asm --disable-stripping \ + --enable-libx264 --extra-cflags=-I/home/liukang/repo/x264lib/include --extra-ldflags=-L/home/liukang/repo/x264lib/lib --extra-libs=-ldl + make clean + if [ "$1" == "verbose" ]; then + make V=1 + else + make + fi + make install + } + + builddef $1 + + ``` + +3. 运行 **build_ffmpeg.sh** 文件 + + ![Snipaste_2022-01-17_10-13-04.png](figures/6.png) + +4. 上面步骤成功后,在 ffmpeg_lib 文件夹下,会生成 ffmpeg 的静态库文件和头文件: + + Lib 库: + + ![Snipaste_2022-01-17_10-11-43.png](figures/7.png) + + 头文件: + + ![Snipaste_2022-01-17_10-11-55.png](figures/8.png) + +## 视频播放 Demo + +1. 使用 VS Code 生成 makefile 工程 + +2. 将上面生成的静态库文件放在 Smart SDK 目录下 + + ![Snipaste_2022-01-17_10-11-08.png](figures/9.png) + +3. 修改 makefile 文件,添加静态库 + + ``` + #程序版本号 + VERSION = 1.0.0 + + CROSS_COMPILE = arm-linux-musleabi- + CC = $(CROSS_COMPILE)gcc + CXX = $(CROSS_COMPILE)g++ + + + # project 根路径 + PROJECT_DIR := $(shell pwd) + + # userapps 根路径 + UROOT_DIR = $(PROJECT_DIR)/../.. + + # rt-thread 路径 + RT_DIR = $(UROOT_DIR)/sdk/rt-thread + INC_DIR =$(UROOT_DIR)/sdk/rt-thread/include + LIB_DIR = ${UROOT_DIR}/sdk/rt-thread/lib + # sdl 路径 + SDL_DIR = ${UROOT_DIR}/sdk/include/sdl + # ffmpeg + FFMPEG_DIR = ${UROOT_DIR}/sdk/include/ffmpeg + + #x264 + X264_DIR = ${UROOT_DIR}/sdk/include/x264 + + # 配置编译参数 + CFLAGS = -march=armv7-a -marm -msoft-float -D__RTTHREAD__ -Wall -O0 -g -gdwarf-2 -n --static + + # 加入头文件搜索路径 + CFLAGS += -I. -I$(UROOT_DIR) -I$(PROJECT_DIR) -I$(RT_DIR)/components/dfs -I$(RT_DIR)/components/drivers -I$(RT_DIR)/components/finsh -I$(RT_DIR)/components/net \ + -I$(RT_DIR)/components/net/netdev -I$(RT_DIR)/components/net/arpa -I${INC_DIR} -I${INC_DIR}/libc -I${INC_DIR}/sys -I${SDL_DIR} -I${FFMPEG_DIR} \ + -I${FFMPEG_DIR}/libavcodec -I${FFMPEG_DIR}/libavdevice -I${FFMPEG_DIR}/libavfilter -I${FFMPEG_DIR}/libavformat \ + -I${FFMPEG_DIR}/libavutil -I${FFMPEG_DIR}/libpostproc -I${FFMPEG_DIR}/libswresample -I${FFMPEG_DIR}/libswscale -I${X264_DIR} + + # 加入链接文件 + LDFLAGS = -march=armv7-a -marm -msoft-float -T ${UROOT_DIR}/linker_scripts/arm/cortex-a/link.lds + + # 加入库文件 + LDFLAGS += -L$(LIB_DIR) -Wl,--whole-archive -Os -lrtthread -lSDL2 -lavcodec -lavdevice -lavfilter -lavformat -lavutil -lpostproc -lswresample -lswscale -lx264 -Wl,--no-whole-archive -n --static -Wl,--start-group -lc -lgcc -lrtthread -lSDL2 -lavcodec -lavdevice -lavfilter -lavformat -lavutil -lpostproc -lswresample -lswscale -lx264 -Wl,--end-group + + default: + $(CC) $(CFLAGS) -c main.c -o main.o + $(CC) $(LDFLAGS) main.o -o hello.elf + + clean: + @rm *.o *.elf + + .PHONY: default clean + + ``` + +4. 编译 + + ![Snipaste_2022-01-17_10-10-13.png](figures/10.png) + +5. 通过 SD Card 启动 elf 文件,将生成的 hello.elf 文件和视频文件放到 SD 卡中,插入到 ART-Pi Smart: + + ![Snipaste_2022-01-17_10-15-04.png](figures/11.png) + +## 完整代码 + +```c +#include +#include +#include +#include +#include + +extern Uint32 rtt_screen_width; +extern Uint32 rtt_screen_heigth; + +int main (int argc, char *argv[]) +{ + int ret = -1; + AVFormatContext *pFormatCtx = NULL; + int videoStream; + AVCodecParameters *pCodecParameters = NULL; + AVCodecContext *pCodecCtx = NULL; + AVCodec *pCodec = NULL; + AVFrame *pFrame = NULL; + AVPacket packet; + + SDL_Rect rect; + SDL_Window *win = NULL; + SDL_Renderer *renderer = NULL; + SDL_Texture *texture = NULL; + + if(( argc != 2 )) + { + printf("error input arguments!\n"); + return(1); + } + + // 默认窗口大小 + int w_width = rtt_screen_width; + int w_height = rtt_screen_heigth; + + // use dummy video driver + SDL_setenv("SDL_VIDEODRIVER","rtt",1); + //Initialize SDL + if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) + { + printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); + return -1; + } + + // 打开输入文件 + if (avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) != 0) + { + printf("Couldn't open video file!: %s\n", argv[1]); + goto __exit; + } + + // 找到视频流 + videoStream = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0); + if (videoStream == -1) + { + printf("Din't find a video stream!\n"); + goto __exit;// Didn't find a video stream + } + + // 流参数 + pCodecParameters = pFormatCtx->streams[videoStream]->codecpar; + + // 获取解码器 + pCodec = avcodec_find_decoder(pCodecParameters->codec_id); + if (pCodec == NULL) + { + printf("Unsupported codec!\n"); + goto __exit; // Codec not found + } + + // 初始化一个编解码上下文 + pCodecCtx = avcodec_alloc_context3(pCodec); + if (avcodec_parameters_to_context(pCodecCtx, pCodecParameters) != 0) + { + printf("Couldn't copy codec context\n"); + goto __exit;// Error copying codec context + } + + // 打开解码器 + if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) + { + printf("Failed to open decoder!\n"); + goto __exit; // Could not open codec + } + + // Allocate video frame + pFrame = av_frame_alloc(); + + w_width = pCodecCtx->width; + w_height = pCodecCtx->height; + + // 创建窗口 + win = SDL_CreateWindow("Media Player", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + w_width, w_height, + SDL_WINDOW_SHOWN ); + if (!win) + { + printf("Failed to create window by SDL\n"); + goto __exit; + } + + // 创建渲染器 + renderer = SDL_CreateRenderer(win, -1, 0); + if (!renderer) + { + printf("Failed to create Renderer by SDL\n"); + goto __exit; + } + + // 创建纹理 + texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_IYUV, + SDL_TEXTUREACCESS_STREAMING, + w_width, + w_height); + + + // 读取数据 + while (av_read_frame(pFormatCtx, &packet) >= 0) + { + if (packet.stream_index == videoStream) + { + // 解码 + avcodec_send_packet(pCodecCtx, &packet); + while (avcodec_receive_frame(pCodecCtx, pFrame) == 0) + { + SDL_UpdateYUVTexture(texture, NULL, + pFrame->data[0], pFrame->linesize[0], + pFrame->data[1], pFrame->linesize[1], + pFrame->data[2], pFrame->linesize[2]); + + // set size of Window + rect.x = 0; + rect.y = 0; + rect.w = pCodecCtx->width; + rect.h = pCodecCtx->height; + + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, texture, NULL, &rect); + SDL_RenderPresent(renderer); + } + } + + av_packet_unref(&packet); + } + +__exit: + + if (pFrame) + { + av_frame_free(&pFrame); + } + + if (pCodecCtx) + { + avcodec_close(pCodecCtx); + } + + if (pCodecParameters) + { + avcodec_parameters_free(&pCodecParameters); + } + + if (pFormatCtx) + { + avformat_close_input(&pFormatCtx); + } + + if (win) + { + SDL_DestroyWindow(win); + } + + if (renderer) + { + SDL_DestroyRenderer(renderer); + } + + if (texture) + { + SDL_DestroyTexture(texture); + } + + SDL_Quit(); + + return ret; +} + +``` + +## 实机演示 + +[https://github.com/liukangcc/ART-Pi-Smart/blob/main/figures/8.gif](https://github.com/liukangcc/ART-Pi-Smart/blob/main/figures/8.gif) + +该仓库放置了编译好的 FFmpeg 和 X264 库文件:[https://github.com/liukangcc/ART-Pi-Smart](https://github.com/liukangcc/ART-Pi-Smart) \ No newline at end of file