你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【STM32H745I-DISCO】TouchGFX探索——2、照葫芦画瓢

[复制链接]
网络孤客 发布时间:2025-3-1 12:34

一、生成代码

在TouchGFX Designer按F4可以生成代码

目录.png

从代码目录中可以看出,生成的目录已经直接支持多个常用平台,如:IAR,Keil,stm32cubeide等。我用的是stm32cubeide。

为方便以下讲解先把需要的源码位置先标示出来

源码.png

二、照葫芦画瓢,自建example

重建一个基于STM32H745I-DISCO空白工程,按照example,布局按键和显示,各控件的命名与原来一直。只做了些微调和增加一个复位按钮。

界面.png

设置按键单击事件

按钮.jpg

生成代码后,会在MainViewBase.hpp中建一个MainViewBase类,类中定义了按键的虚函数(上图:Action中设置)。

定义.png

在MainViewBase.cpp中设置了回调

callback.png

具体实现:

首先在MainView.hpp中创建MainView类,继承自MainViewBase类

子类.png

然后在MainView.cpp中编写各按键具体操作函数和刷新显示函数


void MainView::increaseValue()
{
    count = (count++ > UPPER_LIMIT) ? UPPER_LIMIT : count;
    updateGFXElements();
}

void MainView::decreaseValue()
{
    count = (count-- <= LOWER_LIMIT) ? LOWER_LIMIT : count;
    updateGFXElements();
}

void MainView::resetValue()
{
        count = 0;
        updateGFXElements();
}

void MainView::updateGFXElements()
{
    //Counter text area GFX uptade.
    Unicode::snprintf(countTxtBuffer, 3, "%d", count);
    //Button GFX update and touchable.
    if (count < UPPER_LIMIT)
    {
        buttonUp.setBitmaps(Bitmap(BITMAP_UP_BTN_ID), Bitmap(BITMAP_UP_BTN_PRESSED_ID));
        buttonUp.setTouchable(true);
    }
    else
    {
        buttonUp.setBitmaps(Bitmap(BITMAP_UP_BTN_DISABLED_ID), Bitmap(BITMAP_UP_BTN_DISABLED_ID));
        buttonUp.setTouchable(false);
    }
    if (count > LOWER_LIMIT)
    {
        buttonDown.setBitmaps(Bitmap(BITMAP_DOWN_BTN_ID), Bitmap(BITMAP_DOWN_BTN_PRESSED_ID));
        buttonDown.setTouchable(true);
    }
    else
    {
        buttonDown.setBitmaps(Bitmap(BITMAP_DOWN_BTN_DISABLED_ID), Bitmap(BITMAP_DOWN_BTN_DISABLED_ID));
        buttonDown.setTouchable(false);
    }
    // Invalidate all GFX area, which will result in it being redrawn in next tick.
    countTxt.invalidate();
    buttonUp.invalidate();
    buttonDown.invalidate();
}
钩.png
效果.gif
效果.gif
钩.png
赞 收藏 评论1 发布时间:2025-3-1 12:34

举报

1个回答
网络孤客 回答时间:2025-3-1 12:59:06
如何与显示关联起来4 D9 W# O7 V( @! P; R* f. Z

0 `0 [1 u4 q) D1 b5 a  `: |: F( B' Y$ [* q2 O) j0 i

- }( c: x5 G4 V/ p在把钩打上,就会在MainViewBase.hpp的MainViewBase类内,建立一个buffer。4 A) r, E, N% V9 P+ H0 j' }% j

, Y6 Z# u) d/ x5 @1 v```
0 U0 o" f3 M( c. _/ |, D; p- Z1 z    /*
' M2 h: J' n6 J$ q+ v! S' Z+ b     * Wildcard Buffers
3 g- p7 b' d; U, A  a! K     */# l/ F$ L# x1 o# q5 J- c8 _
    static const uint16_t COUNTTXT_SIZE = 3;
" T/ i" C% ^4 ~* l    touchgfx::Unicode::UnicodeChar countTxtBuffer[COUNTTXT_SIZE];6 {' p$ f) X8 R, Y# x2 R$ g3 q
```
4 h4 Y) T$ Z, N+ g& N
. d# e5 X5 {* a! L, r" B6 v7 I) N在MainView.cpp的刷新显示函数里,往这个buffer里填入count值就行了, L! K  F/ S2 N* n
' q* L; m, Y5 D3 q2 f% Z6 y
```5 l# }, k; H, V- S$ B% g0 L" b' U
void MainView::updateGFXElements()$ {4 i  R, Z* H2 Y
{
; S6 H# Z! A) C    //Counter text area GFX uptade.( ~4 c1 e8 V+ j+ F, O- k/ \% o5 W
    Unicode::snprintf(countTxtBuffer, 3, "%d", count);" b- ]! k7 {2 Z. W, J
    //Button GFX update and touchable.! @; |/ I# y& D: y/ C+ n: S* r
```
9 a0 A0 J% l3 V* t! e$ `7 O% _+ \' D3 W7 g& v; j: P0 _
三、效果/ r7 x$ q; A4 q  c
! T1 {% g% K! ?1 e8 u

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版